1

有没有办法通过它的或什么来获取clang::SourceLocation每个#include文件? clang::FileIDclang::FileEntry

4

2 回答 2

2

如何使用以 fileid 作为参数的源管理器的 GetIncludedLoc 函数。

SourceManager.GetIncludedLoc(fileid)

于 2017-05-18T18:12:59.327 回答
1

感谢@Hemant 的回答,你是对的

我已经自己发现了(在clang 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)    
于 2017-05-19T19:31:03.520 回答