Разбор комментариев с лязгом АСТ - PullRequest
1 голос
/ 19 марта 2020

Я бы хотел проанализировать пользовательский тег с помощью clang AST. Вот простая иллюстрация ввода моего модуля компиляции.

#include <stdio.h>
int main() {
  // \my-tags tag_A, tag_B
  printf("helloworld");
  return 0;
}

Как я могу получить эти теги после \my-tags?

После прочтения clang руководства пользователя , я понимаю, что -Wdocumentation, -fparse-all-comments или даже -fcomment-block-commands могут удовлетворить мое требование. Однако, когда я добавляю один из этих флагов в свой compile_commands.json, ASTContext.Comments.empty() все равно выдает True. Я прилагаю мой compile_commands.json и мой код внешнего интерфейса AST ниже для справки.

// 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())
        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());
}

...