llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.86k stars 11.91k forks source link

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

Open posutsai opened 4 years ago

posutsai commented 4 years ago

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.

llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-driver