[cfe-dev] ClangTool and the usual commandline options

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Sun Dec 13 03:44:46 PST 2015


On Sun, Dec 13, 2015 at 5:03 AM Russell Wallace via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> I'm writing a program that uses ClangTool to parrse C++ code; per the
> tutorials, the main module looks as below.
>
> It works for a single file, but more complex inputs require the usual
> commandline options to set include path, Microsoft compatibility mode et
> cetera, and by default you only get a few options like -help and -version.
>

a) you use a compilation database (
http://clang.llvm.org/docs/JSONCompilationDatabase.html), which is made to
work well for multiple files with different sets of arguments
b) you call your tool with
./bin/my-tool path/to/file.cc -- <compiler args>



>
> How do you get the usual set of commandline options that clang has?
>
> // Apply a custom category to all command-line options so that they are the
> // only ones displayed.
> static cl::OptionCategory MyToolCategory("my-tool options");
>
> // CommonOptionsParser declares HelpMessage with a description of the
> common
> // command-line options related to the compilation database and input
> files.
> // It's nice to have this help message in all tools.
> static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
>
> int main(int argc, const char **argv) {
>   std::set_new_handler([]() {
>     errs() << "new: " << strerror(errno) << '\n';
>     exit(1);
>   });
>
>   sys::PrintStackTraceOnErrorSignal();
>   PrettyStackTraceProgram X(argc, argv);
>
> #ifdef _WIN32
>   // Stack overflow
>   AddVectoredExceptionHandler(0, handler);
> #endif
>
>   CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
>   ClangTool Tool(OptionsParser.getCompilations(),
>                  OptionsParser.getSourcePathList());
>   return Tool.run(newFrontendActionFactory<MainAction>().get());
> }
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151213/4be10d5e/attachment.html>


More information about the cfe-dev mailing list