Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to make preprocessor retain comment with clangTool? #192

Open
posutsai opened this issue Mar 20, 2020 · 1 comment
Open
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:tooling LibTooling question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@posutsai
Copy link

I am trying to use clang AST matcher to parse comments from input source code but the comments inside ASTContext still remains empty. Here is a simple example of my input code.

// input.c
int main() {
  // [my_tag] tag_A, tag_B
  return 0;
}

After my program process the input, it will output tag_A, tag_B. I attempt to follow the clang users manual and add -Wdocumentation, -fparse-all-comments even -E -CC but none of these make AST retain comments after preprocessing. I attach my compile_commands.json as follows.

[
  {
    "directory": "/home/my/project/target/directory",
    "arguments": ["/usr/local/bin/clang", "-c", "-std=c++14", "-Qunused-arguments", "-m64", "-fparse-all-comments", "-I/usr/include", "-I/usr/local/lib/clang/10.0.0/include", "-o", "build/.objs/input/linux/x86_64/release/target/target.cpp.o", "target/target.cpp"],
    "file": "target/target.cpp"
  }
]

And here is how I check if there is comment node in AST.

class MyPrinter : public MatchFinder::MatchCallback {
  public:
    virtual void run(const MatchFinder::MatchResult &Result) {
      ASTContext *Context = Result.Context;
      SourceManager& sm = Context->getSourceManager();
      if (!Context->Comments.empty())
        llvm::outs() << "There is no parsed comment\n";
    }
};

int main(int argc, const char **argv) {
  // CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
  std::string err;
  std::unique_ptr<CompilationDatabase> cd = CompilationDatabase::autoDetectFromSource("/home/my/project/target/directory/compile_commands.json", err);

  ClangTool Tool(*cd, cd->getAllFiles());

  MyPrinter Printer;
  MatchFinder Finder;

  StatementMatcher functionMatcher =
    callExpr(callee(functionDecl(hasName("pthread_mutex_lock")))).bind("functions");

  Finder.addMatcher(functionMatcher, &Printer);

  return Tool.run(newFrontendActionFactory(&Finder).get());
}

I modify the tutorial in the LLVM documentation to the code above. Besides adding comment parsing flags, I also try to mimic how ClangTool runs FrontendAction. However, codes become more and more out of control. I still wonder if there is any way to explicitly modify the PreprocessorOutputOptions.ShowComments in the CompilerInstance or CompilerInvocation.
In a word, I would like to know how to make ASTContext retain the comment information after preprocessing.
Thank you.

@RKSimon RKSimon added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:tooling LibTooling labels Apr 12, 2022
@llvmbot
Copy link
Member

llvmbot commented Apr 12, 2022

@llvm/issue-subscribers-clang-driver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang:tooling LibTooling question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

3 participants