I am trying to parse comment in my source code. Here is a simple example code of my input.
int main() {
foo(); // [my-tag] tag_A, tag_B
return 0;
}
I would like my tool to parse the string after [my-tag]
in comment which is “tag_A, tag_B
” out in this example. After reading the tutorial in the documentation, I mimic the matcher and attempt to access the comment node in ASTContext. However, I always get true from ASTContext.Comments.empty()
. I doubt the issue comes from the comment parsing flags. According to the clang user manual, clang suppose to retain the comments after I specifying one of these three.
-Wdocumentation
-fparse-all-comments
-fcomment-block-commands
But none of these three will make the empty()
return false
. I try to alter the preprocessing behavior by adding -E -CC
. Yet it also makes preprocessor ignore the comments. I attach my compile_commands.json and how I check if the comment node appears in the ASTContext below.
// compile_commands.json
[
{
"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"
}
]
// CommentParser.cpp
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()) // I check the comment node here.
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 even try tp setPreprocessorOutputOpts
explicitly by manually constructing CompilerInstance and CompilerInvocation. However I don’t get what I want. Btw, I even can’t see the comment appear when I use clang-check -ast-dump input.c --extra-arg=-fparse-all-comments
In a word, I wonder if there is a simple way to make ASTContext retain the comment information.