Parsing All Comments

Hi,

I am trying to parse the comment attached to declarations. However, it seems that clang only parses documentation comments, but not line comments or block comments. Is there a way do enable the parser to parse all the comments?

I am using LibTooling and new to it, and cannot find a way to this. Your help is greatly appreciated.

Thank you.

Hi,

I am trying to parse the comment attached to declarations. However, it
seems that clang only parses documentation comments, but not line comments
or block comments.

Can you give examples of what you're hoping to parse, and how you might
access it?

Is there a way do enable the parser to parse all the comments?

I am using LibTooling and new to it, and cannot find a way to this. Your
help is greatly appreciated.

Thank you.

You might look at CommentOptions::ParseAllComments in case that's related
to what you're trying to do.

-- James

Thank you for your reply. The comment is very simple. Each function definition has a preceding comment, listing all the global variables used in the function definition. I need to parse this comment to get the variable list.

Yes, I actually found another old post mentioning CommentOptions::ParseAllComments. However I am new to LLVM/Clang, and I found out it was just a structure defined in CommentOptions.h if I am not wrong.

The following shows the stub of my LitTooling code. Any advice will be appreciated. Thank you.

class InstrumentAction : public ASTFrontendAction {
public:
virtual std::unique_ptr CreateASTConsumer(CompilerInstance &CI,
StringRef InFile){
errs() << "Processing " << InFile << “\n”;
return std::unique_ptr(
new Instrument(InFile, CI.getASTContext(), InstrumentProb));
}
};

int main(int argc, const char **argv) {
InstrumentProb.setCategory(MyToolCategory);
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory().get());
}

CommentOptions is indeed just a class, and ParseAllComments a field in it.
It's normally set by this code from lib/Frontend/CompilerInvocation.cpp:

static void ParseCommentArgs(CommentOptions &Opts, ArgList &Args) {
  Opts.BlockCommandNames =
Args.getAllArgValues(OPT_fcomment_block_commands);
  Opts.ParseAllComments = Args.hasArg(OPT_fparse_all_comments);
}

i.e., you can set it using a command line argument -fparse-all-comments if
that's how you're configuring your tool.

-- James

This is great. I can simply use the command line option. Thank you so much.