walkerWx / Numa

Clang based numerical analysis
5 stars 2 forks source link

How to pass command line options to libtooling? #1

Open walkerWx opened 9 years ago

walkerWx commented 9 years ago

I have encountered the problem that while using clang to parse a c++ file,clang can not know where the header files are.As usual we can give clang the ' -I ' options to tell clang where to find the header files in the command line.

While you have written your own clang tools,after you build the executable binary file,for example a.out, you can add the '-extra-arg' option to pass command line options to you tool as follow: ./a.out -extra-arg=-I/Your/header/file/location [your original arguments here]

amithks25 commented 4 years ago

I have carried out some work on libtooling. I have created one tool called loop-covert which acts like a syntax-checker.

I have one file compile_commands.json file.

I have built the clang from source code using ninja and cmake as mentioned in the below link.

http://clang.llvm.org/docs/LibASTMatchersTutorial.html

This is my compile_commands.json file

{
"directory": "/home/Desktop/clang-llvm/llvm-project/buildubuntu/bin",
"command": "/home/Desktop/clang-llvm/clang/bin/clang -o man.o -c /home/Desktop/clang-llvm/llvm-project/buildubuntu/bin/test1.cpp",
"file": "/home/Desktop/clang-llvm/llvm-project/buildubuntu/bin/test1.cpp"
}
]

This is my loop-convert file.

#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"

#include "llvm/Support/CommandLine.h"

using namespace clang::tooling;
using namespace llvm;

static llvm::cl::OptionCategory MyToolCategory("my-tool options");

static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);

static cl::extrahelp MoreHelp("\nMore help text...\n");

int main(int argc, const char **argv) { 

    std::string errormsg = "";
  std::unique_ptr<CompilationDatabase> cd = ((CompilationDatabase::autoDetectFromDirectory (argv[2],errormsg)));
  ClangTool Tool(*cd ,cd->getAllFiles());
    int result=Tool.run(&(*newFrontendActionFactory<clang::SyntaxOnlyAction>())); 

return result; 
}

test1.cpp is a simple c++ program file.

I invoke above files in command line like below

bin\loop-convert -p bin\compile_commands.json

according to this the man.o an object file should be created , but it is not ..