llvm.org GIT mirror llvm / 14eebb8
Revert r358337: "[CommandLineParser] Add DefaultOption flag" The change causes test failures under asan. Reverting to unbreak our integrate. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358414 91177308-0d34-0410-b5e6-96231b3b80d8 Ilya Biryukov 4 months ago
6 changed file(s) with 15 addition(s) and 142 deletion(s). Raw diff Collapse all Expand all
127127 USAGE: compiler [options]
128128
129129 OPTIONS:
130 -h - Alias for -help
131130 -help - display available options (-help-hidden for more)
132131 -o - Specify output filename
133132
194193 USAGE: compiler [options]
195194
196195 OPTIONS:
197 -h - Alias for -help
198196 -help - display available options (-help-hidden for more)
199197 -o - Specify output filename
200198
12521250 with ``cl::CommaSeparated``, this modifier only makes sense with a `cl::list`_
12531251 option.
12541252
1255 .. _cl::DefaultOption:
1256
1257 * The **cl::DefaultOption** modifier is used to specify that the option is a
1258 default that can be overridden by application specific parsers. For example,
1259 the ``-help`` alias, ``-h``, is registered this way, so it can be overridden
1260 by applications that need to use the ``-h`` option for another purpose,
1261 either as a regular option or an alias for another option.
1262
12631253 .. _response files:
12641254
12651255 Response files
174174 // If this is enabled, multiple letter options are allowed to bunch together
175175 // with only a single hyphen for the whole group. This allows emulation
176176 // of the behavior that ls uses for example: ls -la === ls -l -a
177 Grouping = 0x08,
178
179 // Default option
180 DefaultOption = 0x10
177 Grouping = 0x08
181178 };
182179
183180 //===----------------------------------------------------------------------===//
272269 unsigned Value : 2;
273270 unsigned HiddenFlag : 2; // enum OptionHidden
274271 unsigned Formatting : 2; // enum FormattingFlags
275 unsigned Misc : 5;
272 unsigned Misc : 4;
276273 unsigned Position = 0; // Position of last occurrence of the option
277274 unsigned AdditionalVals = 0; // Greater than 0 for multi-valued option.
278275
308305 bool hasArgStr() const { return !ArgStr.empty(); }
309306 bool isPositional() const { return getFormattingFlag() == cl::Positional; }
310307 bool isSink() const { return getMiscFlags() & cl::Sink; }
311 bool isDefaultOption() const { return getMiscFlags() & cl::DefaultOption; }
312308
313309 bool isConsumeAfter() const {
314310 return getNumOccurrencesFlag() == cl::ConsumeAfter;
385381 }
386382
387383 inline int getNumOccurrences() const { return NumOccurrences; }
388 void reset();
384 inline void reset() { NumOccurrences = 0; }
389385 };
390386
391387 //===----------------------------------------------------------------------===//
17351731 error("cl::alias must have argument name specified!");
17361732 if (!AliasFor)
17371733 error("cl::alias must have an cl::aliasopt(option) specified!");
1738 if (!Subs.empty())
1739 error("cl::alias must not have cl::sub(), aliased option's cl::sub() will be used!");
17401734 Subs = AliasFor->Subs;
1741 Category = AliasFor->Category;
17421735 addArgument();
17431736 }
17441737
9797 // This collects additional help to be printed.
9898 std::vector MoreHelp;
9999
100 // This collects Options added with the cl::DefaultOption flag. Since they can
101 // be overridden, they are not added to the appropriate SubCommands until
102 // ParseCommandLineOptions actually runs.
103 SmallVector DefaultOptions;
104
105100 // This collects the different option categories that have been registered.
106101 SmallPtrSet RegisteredOptionCategories;
107102
150145 void addOption(Option *O, SubCommand *SC) {
151146 bool HadErrors = false;
152147 if (O->hasArgStr()) {
153 // If it's a DefaultOption, check to make sure it isn't already there.
154 if (O->isDefaultOption() &&
155 SC->OptionsMap.find(O->ArgStr) != SC->OptionsMap.end())
156 return;
157
158148 // Add argument to the argument map!
159149 if (!SC->OptionsMap.insert(std::make_pair(O->ArgStr, O)).second) {
160150 errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
194184 }
195185 }
196186
197 void addOption(Option *O, bool ProcessDefaultOption = false) {
198 if (!ProcessDefaultOption && O->isDefaultOption()) {
199 DefaultOptions.push_back(O);
200 return;
201 }
202
187 void addOption(Option *O) {
203188 if (O->Subs.empty()) {
204189 addOption(O, &*TopLevelSubCommand);
205190 } else {
215200 OptionNames.push_back(O->ArgStr);
216201
217202 SubCommand &Sub = *SC;
218 auto End = Sub.OptionsMap.end();
219 for (auto Name : OptionNames) {
220 auto I = Sub.OptionsMap.find(Name);
221 if (I != End && I->getValue() == O)
222 Sub.OptionsMap.erase(I);
223 }
203 for (auto Name : OptionNames)
204 Sub.OptionsMap.erase(Name);
224205
225206 if (O->getFormattingFlag() == cl::Positional)
226207 for (auto Opt = Sub.PositionalOpts.begin();
284265 if (O->Subs.empty())
285266 updateArgStr(O, NewName, &*TopLevelSubCommand);
286267 else {
287 if (O->isInAllSubCommands()) {
288 for (auto SC : RegisteredSubCommands)
289 updateArgStr(O, NewName, SC);
290 } else {
291 for (auto SC : O->Subs)
292 updateArgStr(O, NewName, SC);
293 }
268 for (auto SC : O->Subs)
269 updateArgStr(O, NewName, SC);
294270 }
295271 }
296272
387363 GlobalParser->updateArgStr(this, S);
388364 assert((S.empty() || S[0] != '-') && "Option can't start with '-");
389365 ArgStr = S;
390 }
391
392 void Option::reset() {
393 NumOccurrences = 0;
394 setDefault();
395 if (isDefaultOption())
396 removeArgument();
397366 }
398367
399368 // Initialise the general option category.
11961165 auto &PositionalOpts = ChosenSubCommand->PositionalOpts;
11971166 auto &SinkOpts = ChosenSubCommand->SinkOpts;
11981167 auto &OptionsMap = ChosenSubCommand->OptionsMap;
1199
1200 for (auto O: DefaultOptions) {
1201 addOption(O, true);
1202 }
12031168
12041169 if (ConsumeAfterOpt) {
12051170 assert(PositionalOpts.size() > 0 &&
21792144 HOp("help", cl::desc("Display available options (-help-hidden for more)"),
21802145 cl::location(WrappedNormalPrinter), cl::ValueDisallowed,
21812146 cl::cat(GenericCategory), cl::sub(*AllSubCommands));
2182
2183 static cl::alias HOpA("h", cl::desc("Alias for -help"), cl::aliasopt(HOp),
2184 cl::DefaultOption);
21852147
21862148 static cl::opt>
21872149 HHOp("help-hidden", cl::desc("Display all available options"),
+0
-18
test/Support/check-default-options.txt less more
None # RUN: llvm-objdump -help-hidden %t | FileCheck --check-prefix=CHECK-OBJDUMP %s
1 # RUN: llvm-readobj -help-hidden %t | FileCheck --check-prefix=CHECK-READOBJ %s
2 # RUN: llvm-tblgen -help-hidden %t | FileCheck --check-prefix=CHECK-TBLGEN %s
3 # RUN: llvm-opt-report -help-hidden %t | FileCheck --check-prefix=CHECK-OPT-RPT %s
4 # RUN: llvm-dwarfdump -help-hidden %t | FileCheck --check-prefix=CHECK-DWARF %s
5 # RUN: llvm-dwarfdump -h %t | FileCheck --check-prefix=CHECK-DWARF-H %s
6
7
8 # CHECK-OBJDUMP: -h - Alias for --section-headers
9 # CHECK-READOBJ: -h - Alias for --file-headers
10 # CHECK-TBLGEN: -h - Alias for -help
11 # CHECK-OPT-RPT: -h - Alias for -help
12 # CHECK-DWARF: -h - Alias for -help
13
14 # llvm-dwarfdump declares `-h` option and prints special help in that case,
15 # which is weird, but makes for a good test, i.e., shows the default `-h`
16 # wasn't used.
17 # CHECK-DWARF-H-NOT: -help-list - Display list of available options (-help-list-hidden for more)
3535 using namespace llvm;
3636 using namespace llvm::yaml;
3737
38 static cl::opt Help("h", cl::desc("Alias for -help"), cl::Hidden);
39
3840 // Mark all our options with this category, everything else (except for -version
3941 // and -help) will be hidden.
4042 static cl::OptionCategory
437439 "A tool to generate an optimization report from YAML optimization"
438440 " record files.\n");
439441
442 if (Help) {
443 cl::PrintHelpMessage();
444 return 0;
445 }
446
440447 LocationInfoTy LocationInfo;
441448 if (!readLocationInfo(LocationInfo))
442449 return 1;
612612 cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls()));
613613 EXPECT_FALSE(Opt1);
614614 EXPECT_FALSE(Opt2);
615 for (auto *S : cl::getRegisteredSubcommands()) {
616 if (*S) {
617 EXPECT_EQ("sc2", S->getName());
618 }
619 }
620 }
621
622 TEST(CommandLineTest, DefaultOptions) {
623 cl::ResetCommandLineParser();
624
625 StackOption Bar("bar", cl::sub(*cl::AllSubCommands),
626 cl::DefaultOption);
627 StackOption Bar_Alias(
628 "b", cl::desc("Alias for -bar"), cl::aliasopt(Bar), cl::DefaultOption);
629
630 StackOption Foo("foo", cl::init(false), cl::sub(*cl::AllSubCommands),
631 cl::DefaultOption);
632 StackOption Foo_Alias("f", cl::desc("Alias for -foo"),
633 cl::aliasopt(Foo), cl::DefaultOption);
634
635 StackSubCommand SC1("sc1", "First Subcommand");
636 // Override "-b" and change type in sc1 SubCommand.
637 StackOption SC1_B("b", cl::sub(SC1), cl::init(false));
638 StackSubCommand SC2("sc2", "Second subcommand");
639 // Override "-foo" and change type in sc2 SubCommand. Note that this does not
640 // affect "-f" alias, which continues to work correctly.
641 StackOption SC2_Foo("foo", cl::sub(SC2));
642
643 const char *args0[] = {"prog", "-b", "args0 bar string", "-f"};
644 EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args0) / sizeof(char *), args0,
645 StringRef(), &llvm::nulls()));
646 EXPECT_TRUE(Bar == "args0 bar string");
647 EXPECT_TRUE(Foo);
648 EXPECT_FALSE(SC1_B);
649 EXPECT_TRUE(SC2_Foo.empty());
650
651 cl::ResetAllOptionOccurrences();
652
653 const char *args1[] = {"prog", "sc1", "-b", "-bar", "args1 bar string", "-f"};
654 EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args1) / sizeof(char *), args1,
655 StringRef(), &llvm::nulls()));
656 EXPECT_TRUE(Bar == "args1 bar string");
657 EXPECT_TRUE(Foo);
658 EXPECT_TRUE(SC1_B);
659 EXPECT_TRUE(SC2_Foo.empty());
660 for (auto *S : cl::getRegisteredSubcommands()) {
661 if (*S) {
662 EXPECT_EQ("sc1", S->getName());
663 }
664 }
665
666 cl::ResetAllOptionOccurrences();
667
668 const char *args2[] = {"prog", "sc2", "-b", "args2 bar string",
669 "-f", "-foo", "foo string"};
670 EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args2) / sizeof(char *), args2,
671 StringRef(), &llvm::nulls()));
672 EXPECT_TRUE(Bar == "args2 bar string");
673 EXPECT_TRUE(Foo);
674 EXPECT_FALSE(SC1_B);
675 EXPECT_TRUE(SC2_Foo == "foo string");
676615 for (auto *S : cl::getRegisteredSubcommands()) {
677616 if (*S) {
678617 EXPECT_EQ("sc2", S->getName());