0

I am working with Clang Libtooling. I need to run my clang MyFrontendAction on certain files specified in the command line.

CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());
int i=Tool.run(newFrontendActionFactory<MyFrontendAction>().get());

I run it by:

./(driver) file1 file2 --

and it runs my tool on both file1 and file2. I just want it to run on file1 and want to do some other stuff on file2. How can I do it?

Awaid Shaheen
  • 95
  • 1
  • 9

1 Answers1

1

You can create your own (modified) argc and argv and pass those to the OptionsParser constructor.

int my_argc = argc - 1;
const char *my_argv[my_argc];
for (int i = 0; i < my_argc; ++i) my_argv[i] = argv[i];
const char *extra_file = argv[argc - 1];

Now you have my_argv that contains the command line to provide your tool and extra_file with file2. Then just proceed as usual:

CommonOptionsParser OptionsParser(my_argc, my_argv, MyToolCategory);
// etc.

If this isn't a trivial tool and you want to allow the usual kinds of arguments, you'll have to do something a little fancier. You might, for example, create a CommonOptionsParser with the original set of arguments, then read out the results of getSourcePathList(), and match the second string in your original argv in order to exclude it. Then you can create a second CommonOptionsParser for actual use in your ClangTool.