首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获得#的源位置,包括使用clang libtooling?

如何获得#的源位置,包括使用clang libtooling?
EN

Stack Overflow用户
提问于 2017-04-17 09:09:29
回答 2查看 1.1K关注 0票数 1

是否有任何方法可以通过其clang::SourceLocationclang::FileEntry或其他方法获取文件中的每个#includeclang::FileID

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-18 18:12:59

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

SourceManager.GetIncludedLoc(fileid)

票数 2
EN

Stack Overflow用户

发布于 2017-05-19 19:31:03

谢谢你的回答,你说得对

我已经自己发现了(在clang3.8中,它被称为getIncludeLoc),但忘了在这里写。我用这个找到位置,毕竟#包括我可以把自己的位置。这是我为此写的函数(当然不是最好的方法),希望它能帮助某人

代码语言:javascript
复制
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);
}

还有一些关于包含的信息可以通过

代码语言:javascript
复制
Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, StringRef &Buffer)

代码语言:javascript
复制
Lexer::ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines = 0)    
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43448448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档