[cfe-dev] How to use AST Matchers without using ninja?

Han Wang wanghan02 at gmail.com
Thu Jul 9 01:58:04 PDT 2015


Hi Douglas,

Thanks very much for your answer! It works find for the simple foo.cpp with
these libraries. But when I try to expend it a little bit using
RefactoringTool to match the if statement, it seems to need more libraries.

#include <stdio.h>
#include <string>
#include <vector>
#include <system_error>
#include <type_traits>
#include "clang/AST/AST.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/raw_ostream.h"


using namespace llvm;
using clang::tooling::newFrontendActionFactory;
using clang::tooling::Replacement;
using clang::tooling::CompilationDatabase;
using namespace clang;
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;

static llvm::cl::OptionCategory ToolingSampleCategory("IfStmt");

class IfStmtHandler : public MatchFinder::MatchCallback {
public:
  IfStmtHandler(Replacements *Replace) : Replace(Replace) {}

  virtual void run(const MatchFinder::MatchResult &Result) {
    // The matched 'if' statement was bound to 'ifStmt'.
    if (const IfStmt *IfS =
Result.Nodes.getNodeAs<clang::IfStmt>("ifStmt")) {
      IfS->dump();
    }
  }
private:
  Replacements *Replace;
};


int main(int argc, const char **argv) {
  printf("Removing all ColIt<>...\n");
  CommonOptionsParser op(argc, argv, ToolingSampleCategory);
  std::vector<std::string> sources({
    "/Users/hwang/dev/code/tcaddin/Chart/ChartSE.cpp"});
  RefactoringTool Tool(op.getCompilations(), sources);
  // Set up AST matcher callbacks.
  IfStmtHandler HandlerForIf(&Tool.getReplacements());

  MatchFinder Finder;
  Finder.addMatcher(ifStmt().bind("ifStmt"), &HandlerForIf);
  return 0;
}

error messages:

Undefined symbols for architecture x86_64:
  "clang::tooling::RefactoringTool::getReplacements()", referenced from:
      _main in foo-06331f.o

"clang::tooling::RefactoringTool::RefactoringTool(clang::tooling::CompilationDatabase
const&, llvm::ArrayRef<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > >,
std::__1::shared_ptr<clang::PCHContainerOperations>)", referenced from:
      _main in foo-06331f.o
  "clang::tooling::CommonOptionsParser::CommonOptionsParser(int&, char
const**, llvm::cl::OptionCategory&, char const*)", referenced from:
      _main in foo-06331f.o
  "clang::tooling::ClangTool::~ClangTool()", referenced from:
      clang::tooling::RefactoringTool::~RefactoringTool() in foo-06331f.o
  "vtable for clang::RawPCHContainerOperations", referenced from:
      clang::RawPCHContainerOperations::RawPCHContainerOperations() in
foo-06331f.o
  NOTE: a missing vtable usually means the first non-inline virtual member
function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)

On Wed, Jul 8, 2015 at 7:45 PM, Douglas Katzman <dougk at google.com> wrote:

> Sounds like you're not linking against the necessary libraries, and you
> didn't add "-fno-rtti" to the cc command.
>
> Try compiling this minimal 'foo.cpp' program:
>
> #include "clang/ASTMatchers/ASTMatchFinder.h"
> #include "clang/ASTMatchers/ASTMatchers.h"
> int main() { return 0; }
>
> % g++ -fno-rtti -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS
> -D__STDC_LIMIT_MACROS -std=c++11 -g -Iinclude -Itools/clang/include
> -I../src/include -I../src/tools/clang/include -c foo.cpp
>
> % g++ -o foo foo.o \
>   ../build/lib/libclangAST.a \
>   ../build/lib/libclangASTMatchers.a \
>   ../build/lib/libclangLex.a ../build/lib/libclangBasic.a \
>   ../build/lib/libLLVMSupport.a \
>   -lpthread -ldl -lncurses
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150709/91f32fac/attachment.html>


More information about the cfe-dev mailing list