【问题标题】:How to print a line on Terminal Using Clang Libtooling?如何使用 Clang Libtooling 在终端上打印一行?
【发布时间】:2015-07-07 11:37:44
【问题描述】:

我对 CLang 和 Libtooling 比较陌生。我想在终端上显示源代码中的一行。我在 RecursiveASTVisitor 中有一个 *VisitFunctionDecl(FunctionDecl func)。对于每个函数,我得到 SourceRange 并从中得到 SourceLocation。但我不明白如何显示。我有事要做

【问题讨论】:

  • 我需要 SourceManager,但我不知道如何访问/声明它。
  • 您没有ASTContext 指针吗?它可以为您提供SourceManager 参考。或者给你一个FullSourceLoc 实例,你可以直接使用它来获取源代码行(如果我正确阅读了参考资料)。也许你应该阅读例如this RecursiveASTVisitor tutorial?'
  • 非常感谢。正如你所说,我使用 ASTContext 来获取 FllSourceLoc 并使用它得到了我正在寻找的东西。

标签: c++ clang libtooling


【解决方案1】:

您需要 FullSourceLoc:

    FullSourceLoc functionDeclFullLocation = Context->getFullLoc(func.getLocStart());
    if (functionDeclFullLocation.isValid())
        llvm::outs() << "Found FunctionDecl at "
        << functionDeclFullLocation.getManager().getFilename(functionDeclFullLocation) << ":"
        << functionDeclFullLocation.getSpellingLineNumber() << ":"
        << functionDeclFullLocation.getSpellingColumnNumber() << "\n";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    相关资源
    最近更新 更多