Skip to content

Commit fcd9fa4

Browse files
author
Yevgeny Rouban
committed
[Support] Try 2: Reset option to its default if its Default field is undefined
opt::setDefaultImpl() is changed to set the option value to the option type's default if the Default field is not set. This results in option value reset by Option::reset() or ResetAllOptionOccurrences() even if the cl::init() is not specified. Example: StackOption<std::string> Str("str"); // No cl::init(). Str = "some value"; cl::ResetAllOptionOccurrences(); EXPECT_EQ("", Str); // The Str is reset. Reviewed By: lattner Differential Revision: https://reviews.llvm.org/D115433
1 parent 43f208e commit fcd9fa4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

llvm/include/llvm/Support/CommandLine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,8 @@ class opt : public Option,
14361436
const OptionValue<DataType> &V = this->getDefault();
14371437
if (V.hasValue())
14381438
this->setValue(V.getValue());
1439+
else
1440+
this->setValue(T());
14391441
}
14401442

14411443
template <class T,

llvm/unittests/Support/CommandLineTest.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,8 +1934,9 @@ TEST(CommandLineTest, ConsumeAfterTwoPositionals) {
19341934
TEST(CommandLineTest, ResetAllOptionOccurrences) {
19351935
cl::ResetCommandLineParser();
19361936

1937-
// -option -enableA -enableC [sink] input [args]
1937+
// -option -str -enableA -enableC [sink] input [args]
19381938
StackOption<bool> Option("option");
1939+
StackOption<std::string> Str("str");
19391940
enum Vals { ValA, ValB, ValC };
19401941
StackOption<Vals, cl::bits<Vals>> Bits(
19411942
cl::values(clEnumValN(ValA, "enableA", "Enable A"),
@@ -1945,15 +1946,16 @@ TEST(CommandLineTest, ResetAllOptionOccurrences) {
19451946
StackOption<std::string> Input(cl::Positional);
19461947
StackOption<std::string, cl::list<std::string>> ExtraArgs(cl::ConsumeAfter);
19471948

1948-
const char *Args[] = {"prog", "-option", "-enableA", "-enableC",
1949-
"-unknown", "input", "-arg"};
1949+
const char *Args[] = {"prog", "-option", "-str=STR", "-enableA",
1950+
"-enableC", "-unknown", "input", "-arg"};
19501951

19511952
std::string Errs;
19521953
raw_string_ostream OS(Errs);
1953-
EXPECT_TRUE(cl::ParseCommandLineOptions(7, Args, StringRef(), &OS));
1954+
EXPECT_TRUE(cl::ParseCommandLineOptions(8, Args, StringRef(), &OS));
19541955
EXPECT_TRUE(OS.str().empty());
19551956

19561957
EXPECT_TRUE(Option);
1958+
EXPECT_EQ("STR", Str);
19571959
EXPECT_EQ((1u << ValA) | (1u << ValC), Bits.getBits());
19581960
EXPECT_EQ(1u, Sink.size());
19591961
EXPECT_EQ("-unknown", Sink[0]);
@@ -1963,6 +1965,7 @@ TEST(CommandLineTest, ResetAllOptionOccurrences) {
19631965

19641966
cl::ResetAllOptionOccurrences();
19651967
EXPECT_FALSE(Option);
1968+
EXPECT_EQ("", Str);
19661969
EXPECT_EQ(0u, Bits.getBits());
19671970
EXPECT_EQ(0u, Sink.size());
19681971
EXPECT_EQ(0, Input.getNumOccurrences());

0 commit comments

Comments
 (0)