是否有任何方法可以通过其clang::SourceLocation或clang::FileEntry或其他方法获取文件中的每个#include的clang::FileID?
发布于 2017-05-18 18:12:59
如何使用源管理器的GetIncludedLoc函数,该函数以fileid为参数。
SourceManager.GetIncludedLoc(fileid)
发布于 2017-05-19 19:31:03
谢谢你的回答,你说得对
我已经自己发现了(在clang3.8中,它被称为getIncludeLoc),但忘了在这里写。我用这个找到位置,毕竟#包括我可以把自己的位置。这是我为此写的函数(当然不是最好的方法),希望它能帮助某人
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) https://stackoverflow.com/questions/43448448
复制相似问题