2017-04-17 59 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

相关问题