[clang-tools-extra] r317333 - Use ToolExecutor framework in the sample tool-template.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 3 09:03:57 PDT 2017


Author: ioeric
Date: Fri Nov  3 09:03:56 2017
New Revision: 317333

URL: http://llvm.org/viewvc/llvm-project?rev=317333&view=rev
Log:
Use ToolExecutor framework in the sample tool-template.

Modified:
    clang-tools-extra/trunk/tool-template/CMakeLists.txt
    clang-tools-extra/trunk/tool-template/ToolTemplate.cpp

Modified: clang-tools-extra/trunk/tool-template/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/tool-template/CMakeLists.txt?rev=317333&r1=317332&r2=317333&view=diff
==============================================================================
--- clang-tools-extra/trunk/tool-template/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/tool-template/CMakeLists.txt Fri Nov  3 09:03:56 2017
@@ -12,4 +12,5 @@ target_link_libraries(tool-template
   clangBasic
   clangFrontend
   clangTooling
+  clangToolingRefactor
   )

Modified: clang-tools-extra/trunk/tool-template/ToolTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/tool-template/ToolTemplate.cpp?rev=317333&r1=317332&r2=317333&view=diff
==============================================================================
--- clang-tools-extra/trunk/tool-template/ToolTemplate.cpp (original)
+++ clang-tools-extra/trunk/tool-template/ToolTemplate.cpp Fri Nov  3 09:03:56 2017
@@ -40,7 +40,9 @@
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Tooling/CommonOptionsParser.h"
+#include "clang/Tooling/Execution.h"
 #include "clang/Tooling/Refactoring.h"
+#include "clang/Tooling/Refactoring/AtomicChange.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -54,19 +56,31 @@ using namespace llvm;
 namespace {
 class ToolTemplateCallback : public MatchFinder::MatchCallback {
 public:
-  ToolTemplateCallback(std::map<std::string, Replacements> *Replace)
-      : Replace(Replace) {}
+  ToolTemplateCallback(ExecutionContext &Context) : Context(Context) {}
 
   void run(const MatchFinder::MatchResult &Result) override {
     // TODO: This routine will get called for each thing that the matchers
     // find.
     // At this point, you can examine the match, and do whatever you want,
     // including replacing the matched text with other text
-    (void)Replace; // This to prevent an "unused member variable" warning;
+    auto *D = Result.Nodes.getNodeAs<NamedDecl>("decl");
+    assert(D);
+    // Use AtomicChange to get a key.
+    if (D->getLocStart().isValid()) {
+      AtomicChange Change(*Result.SourceManager, D->getLocStart());
+      Context.reportResult(Change.getKey(), D->getQualifiedNameAsString());
+    }
+  }
+
+  void onStartOfTranslationUnit() override {
+    Context.reportResult("START", "Start of TU.");
+  }
+  void onEndOfTranslationUnit() override {
+    Context.reportResult("END", "End of TU.");
   }
 
 private:
-  std::map<std::string, Replacements> *Replace;
+  ExecutionContext &Context;
 };
 } // end anonymous namespace
 
@@ -76,15 +90,33 @@ static cl::OptionCategory ToolTemplateCa
 
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
-  CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory);
-  RefactoringTool Tool(OptionsParser.getCompilations(),
-                       OptionsParser.getSourcePathList());
+
+  auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
+      argc, argv, ToolTemplateCategory);
+
+  if (!Executor) {
+    llvm::errs() << llvm::toString(Executor.takeError()) << "\n";
+    return 1;
+  }
+
   ast_matchers::MatchFinder Finder;
-  ToolTemplateCallback Callback(&Tool.getReplacements());
+  ToolTemplateCallback Callback(*Executor->get()->getExecutionContext());
 
   // TODO: Put your matchers here.
   // Use Finder.addMatcher(...) to define the patterns in the AST that you
   // want to match against. You are not limited to just one matcher!
-
-  return Tool.run(newFrontendActionFactory(&Finder).get());
+  //
+  // This is a sample matcher:
+  Finder.addMatcher(
+      namedDecl(cxxRecordDecl(), isExpansionInMainFile()).bind("decl"),
+      &Callback);
+
+  auto Err = Executor->get()->execute(newFrontendActionFactory(&Finder));
+  if (Err) {
+    llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+  }
+  Executor->get()->getToolResults()->forEachResult(
+      [](llvm::StringRef key, llvm::StringRef value) {
+        llvm::errs() << "----" << key.str() << "\n" << value.str() << "\n";
+      });
 }




More information about the cfe-commits mailing list