kevinaboos/LibToolingExample

Does not work in out of tree build

Opened this issue · 3 comments

The build should not be performed as you instruct, where you svn co to llvm, cd llvm; make.

With such a configuration (with a build directory and a llvm source directory), the makefile breaks.

I have worked on this some more, and the good news is that not many changes are needed to make it work.

I can't test the build the way you describe (in-tree build), but for an out-of-tree build one simply needs to add this example into the makefile found in the clang/tools directory.

Here's a patch of the changes to get this working on latest clang. There were a few small api changes and stuff. maybe I will make a pull request.

diff --git a/Example.cpp b/Example.cpp
index de10c6b..d95e918 100644
--- a/Example.cpp
+++ b/Example.cpp
@@ -104,8 +104,8 @@ public:

 class ExampleFrontendAction : public ASTFrontendAction {
 public:
-    virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef file) {
-        return new ExampleASTConsumer(&CI); // pass CI pointer to ASTConsumer
+    virtual unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, StringRef file) {
+        return make_unique<ExampleASTConsumer>(&CI); // pass CI pointer to ASTConsumer
     }
 };

@@ -113,12 +113,12 @@ public:

 int main(int argc, const char **argv) {
     // parse the command-line args passed to your code
-    CommonOptionsParser op(argc, argv);
+    CommonOptionsParser op(argc, argv, llvm::cl::GeneralCategory);
     // create a new Clang Tool instance (a LibTooling environment)
     ClangTool Tool(op.getCompilations(), op.getSourcePathList());

     // run the Clang Tool, creating a new FrontendAction (explained below)
-    int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>());
+    int result = Tool.run(newFrontendActionFactory<ExampleFrontendAction>().get());

     errs() << "\nFound " << numFunctions << " functions.\n\n";
     // print out the rewritten source code ("rewriter" is a global var.)
diff --git a/Makefile b/Makefile
index f7c7cc2..7c7c99e 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CLANG_LEVEL := ../..

-TOOLNAME = example  #the name of your tool's executable
+TOOLNAME = kevinaboos_example  #the name of your tool's executable

 SOURCES := Example.cpp  #the Clang source files you want to compile

@@ -10,7 +10,7 @@ LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader support mc option

 USEDLIBS = clangFrontend.a clangSerialization.a clangDriver.a \
            clangTooling.a clangParse.a clangSema.a \
-           clangAnalysis.a clangRewriteFrontend.a clangRewriteCore.a \
+           clangAnalysis.a clangRewriteFrontend.a clangRewrite.a \
           clangEdit.a clangAST.a clangLex.a clangBasic.a

 include $(CLANG_LEVEL)/Makefile

thanks @unphased - your diff was helpful, please do submit a PR! :)

never mind - noticed the PR from https://github.com/showgood/LibToolingExample (Kevin, please merge it or @unphased's one!) :)