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 { left, right, up, down }; cl:opt<Direction> 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? Thanks, MartinO -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160831/6c0ce2fe/attachment.html>
On 31 August 2016 at 19:06, Martin J. O'Riordan via llvm-dev <llvm-dev at lists.llvm.org> wrote:> 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: https://reviews.llvm.org/D24070 cheers, --renato
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, MartinO -----Original Message----- From: Renato Golin [mailto:renato.golin at linaro.org] Sent: 31 August 2016 19:13 To: Martin J. O'Riordan <Martin.ORiordan at movidius.com> Cc: LLVM Developers <llvm-dev at lists.llvm.org> Subject: Re: [llvm-dev] llvm::cl::opt and enums On 31 August 2016 at 19:06, Martin J. O'Riordan via llvm-dev <llvm-dev at lists.llvm.org> wrote:> 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: https://reviews.llvm.org/D24070 cheers, --renato
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, MartinO -----Original Message----- From: Renato Golin [mailto:renato.golin at linaro.org] Sent: 31 August 2016 19:13 To: Martin J. O'Riordan <Martin.ORiordan at movidius.com> Cc: LLVM Developers <llvm-dev at lists.llvm.org> Subject: Re: [llvm-dev] llvm::cl::opt and enums On 31 August 2016 at 19:06, Martin J. O'Riordan via llvm-dev < <mailto:llvm-dev at lists.llvm.org> llvm-dev at lists.llvm.org> wrote:> 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: <https://reviews.llvm.org/D24070> https://reviews.llvm.org/D24070 cheers, --renato -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160901/0c206ab6/attachment.html>