2017-04-17 61 views

回答

2

如何使用以fileid爲參數的源代碼管理器的GetIncludedLoc函數呢?

SourceManager.GetIncludedLoc(FILEID)

0

感謝的@Hemant的回答,你說得對

我已經發現,通過我自己(在鐺3.8它被稱爲getIncludeLoc) 卻忘了寫在這裏。 我用它來找到所有#includes之後我可以放置自己的位置。 這裏的功能(可以肯定不是最好的方式),我寫了這個,希望它可以幫助別人

SourceLocation getIncludeLocation(FileID fileID, SourceManager &sm, unsigned carriages) { 
     return SourceLocation(); 
    set<unsigned> lines; 
    if (fileID.isInvalid()) 
    for (auto it = sm.fileinfo_begin(); it != sm.fileinfo_end(); it++) { 
     SourceLocation includeLoc = sm.getIncludeLoc(sm.translateFile(it->first)); 
     if (includeLoc.isValid() && sm.isInFileID(includeLoc, fileID)) { 
      lines.insert(sm.getSpellingLineNumber(includeLoc)); 
     } 
    } 
    unsigned pos(0); 
    if (!lines.empty()) { 
     bool first = true; 
     for (unsigned line :lines) { 
      if (first) 
       first = false; 
      else if ((line - pos) > carriages) 
       break; 
      pos = line; 
      //cout << "Include line:" << pos << endl; 
     } 
     //cout << console_hline('-') << endl; 
    } 
    cout << sm.getFileEntryForID(fileID)->getName() << endl; 
    return sm.translateFileLineCol(sm.getFileEntryForID(fileID), ++pos, 1); 
} 

也有一些相關信息包括可以通過

Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer) 

Lexer::ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines = 0)  
可以得到
+0

謝謝,@Yuriy瞭解更多信息。 – Hemant

相關問題