Add flags to clang and llvm


I want to add a flag to clang and I want this flag to be passed to llvm... I'm looking in the Driver directory,
but I'm a bit lost !

I have written an optimisation pass in llvm and I want to be able to call it with my own flag (not the -O) like that for example:

clang input.c -o output -b{1,2,3,4}

Is there some documentation or some example somewhere ? I didn't find any in the doc :frowning:



this has helped me adding a custom flag/command line option:

There are a few steps involved. I've just started working on LLVM/Clang myself, so please correct me if I'm missing/misinterpreting an important step...

1) Add a definition of your option

Let's say you want to add an option to clang that should be used in cc1 mode only. Look for "" and add your definition similar to the ones that are already there. There are various examples for boolean options ("Flag<["-"]"), options with one argument ("Separate<["-"]") and options with a list of arguments ("Joined<["-"]").

After this step, you should see your option using the command line:

clang -cc1 -help

2) Add the option to one of the "...Options" classes

If you've grouped your option definition under, e.g., "Diagnostic Options", "DiagnosticOptions.h" is the place to go. If the argument of your option is either a boolean, an integer, or an enum, you can simply declare the option in "DiagnosticOptions.def". If you need to process a, e.g., string parameter, create a new public member variable in "DiagnosticOptions.h".

3) Parsing the option

Go to "CompilerInvocation.cpp" and look for the method that handles your type of option, e.g., "clang::ParseDiagnosticArgs" for a diagnostics option. Getting the value from the command line should be straight-forward given the existing code. Your option can be accessed using the automatically generated* type "OPT_<your-option-name>". You read the option/parameter from the command line and save it to the DiagnosticOptions object.

*I'm not sure when these are generated, you may have to build the project before this step... I use Xcode for Clang development and the type was available as soon as I added it in step 2.

4) Using the option

...this is where I've encountered problems since I get "unused argument" warnings... it seems as if you need to forward the option to specific tools along the tool chain in "Tools.cpp". I've added a few lines to the "Clang::ConstructJob" method, pushing my option and its argument to CmdArgs just like it's done with all the other options. However, I haven't figured out how to get a hold of the DiagnosticOptions object from a more or less arbitrary point of my code, and why I see "unused parameter" warnings. Strangely enough, the warning pops up in the log before a message that I add to the log when parsing the option (step 3)... any help would be greatly appreciated.

I hope I could help with the first steps.


Hi Julien,

In order to tweak the 'unroll-threshold' parameter from LLVM's
LoopUnrollPass (lib/Transforms/Scalar/LoopUnrollPass.cpp) here are the
options I pass to clang :

-mllvm -unroll-threshold=4



Thanks for the answer, but that is obsolutely not what I want to do :wink:



Thanks... I'll try that, it's a good hint !


If you use FrontendOptions instead of the DiagnosticsOptions, you won't have the problems I encountered at stage 4), by the way.