[CommandLine] Keep option default value unset if no cl::init() is used
authorYevgeny Rouban <yrouban@azul.com>
Fri, 11 Mar 2022 07:21:45 +0000 (14:21 +0700)
committerYevgeny Rouban <yrouban@azul.com>
Fri, 11 Mar 2022 07:24:25 +0000 (14:24 +0700)
commitc5f34d169244db3f321d75d036902751ec03fe42
treea6d277501d0ae12f3e75e36206ce8f5512536d82
parentf2ac513812e197758e29354c190004eb7bc2f8b6
[CommandLine] Keep option default value unset if no cl::init() is used

Current declaration of cl::opt is incoherent between class and non-class
specializations of the opt_storage template. There is an inconsistency
in the initialization of the Default field: for inClass instances
the default constructor is used - it sets the Optional Default field to
None; though for non-inClass instances the Default field is set to
the type's default value. For non-inClass instances it is impossible
to know if the option is defined with cl::init() initializer or not:

cl::opt<int> i1("option-i1");
cl::opt<int> i2("option-i2", cl::init(0));
cl::opt<std::string> s1("option-s1");
cl::opt<std::string> s2("option-s2", cl::init(""));

assert(s1.Default.hasValue() != s2.Default.hasValue()); // Ok
assert(i1.Default.hasValue() != i2.Default.hasValue()); // Fails

This patch changes constructor of the non-class specializations to keep
the Default field unset (that is None) rather than initialize it with
DataType().

Reviewed By: lattner
Differential Revision: https://reviews.llvm.org/D114645
llvm/include/llvm/Support/CommandLine.h
llvm/unittests/Support/CommandLineTest.cpp