Skip to content

Commit 70921d4

Browse files
committed
Revert r358337: "[CommandLineParser] Add DefaultOption flag"
The change causes test failures under asan. Reverting to unbreak our integrate. llvm-svn: 358414
1 parent bb6cd82 commit 70921d4

File tree

7 files changed

+17
-142
lines changed

7 files changed

+17
-142
lines changed

clang/lib/Tooling/CommonOptionsParser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ std::vector<CompileCommand> ArgumentsAdjustingCompilations::adjustCommands(
8383
llvm::Error CommonOptionsParser::init(
8484
int &argc, const char **argv, cl::OptionCategory &Category,
8585
llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) {
86+
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden,
87+
cl::sub(*cl::AllSubCommands));
8688

8789
static cl::opt<std::string> BuildPath("p", cl::desc("Build path"),
8890
cl::Optional, cl::cat(Category),

llvm/docs/CommandLine.rst

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ this:
128128
USAGE: compiler [options]
129129

130130
OPTIONS:
131-
-h - Alias for -help
132131
-help - display available options (-help-hidden for more)
133132
-o <filename> - Specify output filename
134133

@@ -195,7 +194,6 @@ declarations above, the ``-help`` option synopsis is now extended to:
195194
USAGE: compiler [options] <input file>
196195

197196
OPTIONS:
198-
-h - Alias for -help
199197
-help - display available options (-help-hidden for more)
200198
-o <filename> - Specify output filename
201199

@@ -1253,14 +1251,6 @@ specify boolean properties that modify the option.
12531251
with ``cl::CommaSeparated``, this modifier only makes sense with a `cl::list`_
12541252
option.
12551253

1256-
.. _cl::DefaultOption:
1257-
1258-
* The **cl::DefaultOption** modifier is used to specify that the option is a
1259-
default that can be overridden by application specific parsers. For example,
1260-
the ``-help`` alias, ``-h``, is registered this way, so it can be overridden
1261-
by applications that need to use the ``-h`` option for another purpose,
1262-
either as a regular option or an alias for another option.
1263-
12641254
.. _response files:
12651255

12661256
Response files

llvm/include/llvm/Support/CommandLine.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ enum MiscFlags { // Miscellaneous flags to adjust argument
175175
// If this is enabled, multiple letter options are allowed to bunch together
176176
// with only a single hyphen for the whole group. This allows emulation
177177
// of the behavior that ls uses for example: ls -la === ls -l -a
178-
Grouping = 0x08,
179-
180-
// Default option
181-
DefaultOption = 0x10
178+
Grouping = 0x08
182179
};
183180

184181
//===----------------------------------------------------------------------===//
@@ -273,7 +270,7 @@ class Option {
273270
unsigned Value : 2;
274271
unsigned HiddenFlag : 2; // enum OptionHidden
275272
unsigned Formatting : 2; // enum FormattingFlags
276-
unsigned Misc : 5;
273+
unsigned Misc : 4;
277274
unsigned Position = 0; // Position of last occurrence of the option
278275
unsigned AdditionalVals = 0; // Greater than 0 for multi-valued option.
279276

@@ -309,7 +306,6 @@ class Option {
309306
bool hasArgStr() const { return !ArgStr.empty(); }
310307
bool isPositional() const { return getFormattingFlag() == cl::Positional; }
311308
bool isSink() const { return getMiscFlags() & cl::Sink; }
312-
bool isDefaultOption() const { return getMiscFlags() & cl::DefaultOption; }
313309

314310
bool isConsumeAfter() const {
315311
return getNumOccurrencesFlag() == cl::ConsumeAfter;
@@ -386,7 +382,7 @@ class Option {
386382
}
387383

388384
inline int getNumOccurrences() const { return NumOccurrences; }
389-
void reset();
385+
inline void reset() { NumOccurrences = 0; }
390386
};
391387

392388
//===----------------------------------------------------------------------===//
@@ -1736,10 +1732,7 @@ class alias : public Option {
17361732
error("cl::alias must have argument name specified!");
17371733
if (!AliasFor)
17381734
error("cl::alias must have an cl::aliasopt(option) specified!");
1739-
if (!Subs.empty())
1740-
error("cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!");
17411735
Subs = AliasFor->Subs;
1742-
Category = AliasFor->Category;
17431736
addArgument();
17441737
}
17451738

llvm/lib/Support/CommandLine.cpp

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ class CommandLineParser {
9898
// This collects additional help to be printed.
9999
std::vector<StringRef> MoreHelp;
100100

101-
// This collects Options added with the cl::DefaultOption flag. Since they can
102-
// be overridden, they are not added to the appropriate SubCommands until
103-
// ParseCommandLineOptions actually runs.
104-
SmallVector<Option*, 4> DefaultOptions;
105-
106101
// This collects the different option categories that have been registered.
107102
SmallPtrSet<OptionCategory *, 16> RegisteredOptionCategories;
108103

@@ -151,11 +146,6 @@ class CommandLineParser {
151146
void addOption(Option *O, SubCommand *SC) {
152147
bool HadErrors = false;
153148
if (O->hasArgStr()) {
154-
// If it's a DefaultOption, check to make sure it isn't already there.
155-
if (O->isDefaultOption() &&
156-
SC->OptionsMap.find(O->ArgStr) != SC->OptionsMap.end())
157-
return;
158-
159149
// Add argument to the argument map!
160150
if (!SC->OptionsMap.insert(std::make_pair(O->ArgStr, O)).second) {
161151
errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
@@ -195,12 +185,7 @@ class CommandLineParser {
195185
}
196186
}
197187

198-
void addOption(Option *O, bool ProcessDefaultOption = false) {
199-
if (!ProcessDefaultOption && O->isDefaultOption()) {
200-
DefaultOptions.push_back(O);
201-
return;
202-
}
203-
188+
void addOption(Option *O) {
204189
if (O->Subs.empty()) {
205190
addOption(O, &*TopLevelSubCommand);
206191
} else {
@@ -216,12 +201,8 @@ class CommandLineParser {
216201
OptionNames.push_back(O->ArgStr);
217202

218203
SubCommand &Sub = *SC;
219-
auto End = Sub.OptionsMap.end();
220-
for (auto Name : OptionNames) {
221-
auto I = Sub.OptionsMap.find(Name);
222-
if (I != End && I->getValue() == O)
223-
Sub.OptionsMap.erase(I);
224-
}
204+
for (auto Name : OptionNames)
205+
Sub.OptionsMap.erase(Name);
225206

226207
if (O->getFormattingFlag() == cl::Positional)
227208
for (auto Opt = Sub.PositionalOpts.begin();
@@ -285,13 +266,8 @@ class CommandLineParser {
285266
if (O->Subs.empty())
286267
updateArgStr(O, NewName, &*TopLevelSubCommand);
287268
else {
288-
if (O->isInAllSubCommands()) {
289-
for (auto SC : RegisteredSubCommands)
290-
updateArgStr(O, NewName, SC);
291-
} else {
292-
for (auto SC : O->Subs)
293-
updateArgStr(O, NewName, SC);
294-
}
269+
for (auto SC : O->Subs)
270+
updateArgStr(O, NewName, SC);
295271
}
296272
}
297273

@@ -390,13 +366,6 @@ void Option::setArgStr(StringRef S) {
390366
ArgStr = S;
391367
}
392368

393-
void Option::reset() {
394-
NumOccurrences = 0;
395-
setDefault();
396-
if (isDefaultOption())
397-
removeArgument();
398-
}
399-
400369
// Initialise the general option category.
401370
OptionCategory llvm::cl::GeneralCategory("General options");
402371

@@ -1198,10 +1167,6 @@ bool CommandLineParser::ParseCommandLineOptions(int argc,
11981167
auto &SinkOpts = ChosenSubCommand->SinkOpts;
11991168
auto &OptionsMap = ChosenSubCommand->OptionsMap;
12001169

1201-
for (auto O: DefaultOptions) {
1202-
addOption(O, true);
1203-
}
1204-
12051170
if (ConsumeAfterOpt) {
12061171
assert(PositionalOpts.size() > 0 &&
12071172
"Cannot specify cl::ConsumeAfter without a positional argument!");
@@ -2181,9 +2146,6 @@ static cl::opt<HelpPrinterWrapper, true, parser<bool>>
21812146
cl::location(WrappedNormalPrinter), cl::ValueDisallowed,
21822147
cl::cat(GenericCategory), cl::sub(*AllSubCommands));
21832148

2184-
static cl::alias HOpA("h", cl::desc("Alias for -help"), cl::aliasopt(HOp),
2185-
cl::DefaultOption);
2186-
21872149
static cl::opt<HelpPrinterWrapper, true, parser<bool>>
21882150
HHOp("help-hidden", cl::desc("Display all available options"),
21892151
cl::location(WrappedHiddenPrinter), cl::Hidden, cl::ValueDisallowed,

llvm/test/Support/check-default-options.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

llvm/tools/llvm-opt-report/OptReport.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
using namespace llvm;
3737
using namespace llvm::yaml;
3838

39+
static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
40+
3941
// Mark all our options with this category, everything else (except for -version
4042
// and -help) will be hidden.
4143
static cl::OptionCategory
@@ -438,6 +440,11 @@ int main(int argc, const char **argv) {
438440
"A tool to generate an optimization report from YAML optimization"
439441
" record files.\n");
440442

443+
if (Help) {
444+
cl::PrintHelpMessage();
445+
return 0;
446+
}
447+
441448
LocationInfoTy LocationInfo;
442449
if (!readLocationInfo(LocationInfo))
443450
return 1;

llvm/unittests/Support/CommandLineTest.cpp

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -620,67 +620,6 @@ TEST(CommandLineTest, GetRegisteredSubcommands) {
620620
}
621621
}
622622

623-
TEST(CommandLineTest, DefaultOptions) {
624-
cl::ResetCommandLineParser();
625-
626-
StackOption<std::string> Bar("bar", cl::sub(*cl::AllSubCommands),
627-
cl::DefaultOption);
628-
StackOption<std::string, cl::alias> Bar_Alias(
629-
"b", cl::desc("Alias for -bar"), cl::aliasopt(Bar), cl::DefaultOption);
630-
631-
StackOption<bool> Foo("foo", cl::init(false), cl::sub(*cl::AllSubCommands),
632-
cl::DefaultOption);
633-
StackOption<bool, cl::alias> Foo_Alias("f", cl::desc("Alias for -foo"),
634-
cl::aliasopt(Foo), cl::DefaultOption);
635-
636-
StackSubCommand SC1("sc1", "First Subcommand");
637-
// Override "-b" and change type in sc1 SubCommand.
638-
StackOption<bool> SC1_B("b", cl::sub(SC1), cl::init(false));
639-
StackSubCommand SC2("sc2", "Second subcommand");
640-
// Override "-foo" and change type in sc2 SubCommand. Note that this does not
641-
// affect "-f" alias, which continues to work correctly.
642-
StackOption<std::string> SC2_Foo("foo", cl::sub(SC2));
643-
644-
const char *args0[] = {"prog", "-b", "args0 bar string", "-f"};
645-
EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args0) / sizeof(char *), args0,
646-
StringRef(), &llvm::nulls()));
647-
EXPECT_TRUE(Bar == "args0 bar string");
648-
EXPECT_TRUE(Foo);
649-
EXPECT_FALSE(SC1_B);
650-
EXPECT_TRUE(SC2_Foo.empty());
651-
652-
cl::ResetAllOptionOccurrences();
653-
654-
const char *args1[] = {"prog", "sc1", "-b", "-bar", "args1 bar string", "-f"};
655-
EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args1) / sizeof(char *), args1,
656-
StringRef(), &llvm::nulls()));
657-
EXPECT_TRUE(Bar == "args1 bar string");
658-
EXPECT_TRUE(Foo);
659-
EXPECT_TRUE(SC1_B);
660-
EXPECT_TRUE(SC2_Foo.empty());
661-
for (auto *S : cl::getRegisteredSubcommands()) {
662-
if (*S) {
663-
EXPECT_EQ("sc1", S->getName());
664-
}
665-
}
666-
667-
cl::ResetAllOptionOccurrences();
668-
669-
const char *args2[] = {"prog", "sc2", "-b", "args2 bar string",
670-
"-f", "-foo", "foo string"};
671-
EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args2) / sizeof(char *), args2,
672-
StringRef(), &llvm::nulls()));
673-
EXPECT_TRUE(Bar == "args2 bar string");
674-
EXPECT_TRUE(Foo);
675-
EXPECT_FALSE(SC1_B);
676-
EXPECT_TRUE(SC2_Foo == "foo string");
677-
for (auto *S : cl::getRegisteredSubcommands()) {
678-
if (*S) {
679-
EXPECT_EQ("sc2", S->getName());
680-
}
681-
}
682-
}
683-
684623
TEST(CommandLineTest, ArgumentLimit) {
685624
std::string args(32 * 4096, 'a');
686625
EXPECT_FALSE(llvm::sys::commandLineFitsWithinSystemLimits("cl", args.data()));

0 commit comments

Comments
 (0)