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.
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.
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.
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.
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.And here is how I check if there is comment node in AST.
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.