llvm::cl::opt and enums

I was adding a new option for our backend, and because of the nature of the option I wanted to use an ‘enum’, something like:

enum Direction {






cl:opt myOpt(“option-switch”, cl::init(up), cl::desc(“what it does”), cl::Hidden);

This bit is fine, but then I wanted to change it on the command-line to ‘clang’ and tried:

clang … -mllvm -option-switch=3 …

but that doesn’t work, and I get something like “invalid option ‘3’”. If I change the type to ‘int’ it works fine. Is it valid to use an ‘enum’ in this way? And if so, what is the correct way of specifying a value to the option if I do use an ‘enum’ for the type?



Try something similar to this:



Thanks Renato,

Yes, checking it early as a string would work. Originally I had thought of making the option be a string, but since I need to check it for about 50% of our MachineInstr's, using StringSwitch didn't make sense for performance. But pre-handling the option like this should work.

All the best,


I have to follow up to this, and say thanks very much to you Renato.

Your recommended solution not only works perfectly, it is also very elegant and I can use meaningful words for the options instead of having to remember the enumerate values. It is also far safer, because it rejects invalid values.

Thanks again,


Great! Glad to have helped. :slight_smile: