[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / unittests / Support / CommandLineTest.cpp
blob1a1c8a4404e32b0e9850ae00402d379208d1aec3
1 //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/CommandLine.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Triple.h"
13 #include "llvm/Config/config.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/InitLLVM.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Program.h"
19 #include "llvm/Support/StringSaver.h"
20 #include "gtest/gtest.h"
21 #include <fstream>
22 #include <stdlib.h>
23 #include <string>
25 using namespace llvm;
27 namespace {
29 class TempEnvVar {
30 public:
31 TempEnvVar(const char *name, const char *value)
32 : name(name) {
33 const char *old_value = getenv(name);
34 EXPECT_EQ(nullptr, old_value) << old_value;
35 #if HAVE_SETENV
36 setenv(name, value, true);
37 #else
38 # define SKIP_ENVIRONMENT_TESTS
39 #endif
42 ~TempEnvVar() {
43 #if HAVE_SETENV
44 // Assume setenv and unsetenv come together.
45 unsetenv(name);
46 #else
47 (void)name; // Suppress -Wunused-private-field.
48 #endif
51 private:
52 const char *const name;
55 template <typename T, typename Base = cl::opt<T>>
56 class StackOption : public Base {
57 public:
58 template <class... Ts>
59 explicit StackOption(Ts &&... Ms) : Base(std::forward<Ts>(Ms)...) {}
61 ~StackOption() override { this->removeArgument(); }
63 template <class DT> StackOption<T> &operator=(const DT &V) {
64 this->setValue(V);
65 return *this;
69 class StackSubCommand : public cl::SubCommand {
70 public:
71 StackSubCommand(StringRef Name,
72 StringRef Description = StringRef())
73 : SubCommand(Name, Description) {}
75 StackSubCommand() : SubCommand() {}
77 ~StackSubCommand() { unregisterSubCommand(); }
81 cl::OptionCategory TestCategory("Test Options", "Description");
82 TEST(CommandLineTest, ModifyExisitingOption) {
83 StackOption<int> TestOption("test-option", cl::desc("old description"));
85 static const char Description[] = "New description";
86 static const char ArgString[] = "new-test-option";
87 static const char ValueString[] = "Integer";
89 StringMap<cl::Option *> &Map =
90 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
92 ASSERT_TRUE(Map.count("test-option") == 1) <<
93 "Could not find option in map.";
95 cl::Option *Retrieved = Map["test-option"];
96 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
98 ASSERT_NE(Retrieved->Categories.end(),
99 find_if(Retrieved->Categories,
100 [&](const llvm::cl::OptionCategory *Cat) {
101 return Cat == &cl::GeneralCategory;
103 << "Incorrect default option category.";
105 Retrieved->addCategory(TestCategory);
106 ASSERT_NE(Retrieved->Categories.end(),
107 find_if(Retrieved->Categories,
108 [&](const llvm::cl::OptionCategory *Cat) {
109 return Cat == &TestCategory;
111 << "Failed to modify option's option category.";
113 Retrieved->setDescription(Description);
114 ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
115 << "Changing option description failed.";
117 Retrieved->setArgStr(ArgString);
118 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
119 << "Failed to modify option's Argument string.";
121 Retrieved->setValueStr(ValueString);
122 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
123 << "Failed to modify option's Value string.";
125 Retrieved->setHiddenFlag(cl::Hidden);
126 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
127 "Failed to modify option's hidden flag.";
129 #ifndef SKIP_ENVIRONMENT_TESTS
131 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
133 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
134 TEST(CommandLineTest, ParseEnvironment) {
135 TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
136 EXPECT_EQ("", EnvironmentTestOption);
137 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
138 EXPECT_EQ("hello", EnvironmentTestOption);
141 // This test used to make valgrind complain
142 // ("Conditional jump or move depends on uninitialised value(s)")
144 // Warning: Do not run any tests after this one that try to gain access to
145 // registered command line options because this will likely result in a
146 // SEGFAULT. This can occur because the cl::opt in the test below is declared
147 // on the stack which will be destroyed after the test completes but the
148 // command line system will still hold a pointer to a deallocated cl::Option.
149 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
150 // Put cl::opt on stack to check for proper initialization of fields.
151 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
152 TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
153 EXPECT_EQ("", EnvironmentTestOptionLocal);
154 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
155 EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
158 #endif // SKIP_ENVIRONMENT_TESTS
160 TEST(CommandLineTest, UseOptionCategory) {
161 StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
163 ASSERT_NE(TestOption2.Categories.end(),
164 find_if(TestOption2.Categories,
165 [&](const llvm::cl::OptionCategory *Cat) {
166 return Cat == &TestCategory;
168 << "Failed to assign Option Category.";
171 TEST(CommandLineTest, UseMultipleCategories) {
172 StackOption<int> TestOption2("test-option2", cl::cat(TestCategory),
173 cl::cat(cl::GeneralCategory),
174 cl::cat(cl::GeneralCategory));
176 // Make sure cl::GeneralCategory wasn't added twice.
177 ASSERT_EQ(TestOption2.Categories.size(), 2U);
179 ASSERT_NE(TestOption2.Categories.end(),
180 find_if(TestOption2.Categories,
181 [&](const llvm::cl::OptionCategory *Cat) {
182 return Cat == &TestCategory;
184 << "Failed to assign Option Category.";
185 ASSERT_NE(TestOption2.Categories.end(),
186 find_if(TestOption2.Categories,
187 [&](const llvm::cl::OptionCategory *Cat) {
188 return Cat == &cl::GeneralCategory;
190 << "Failed to assign General Category.";
192 cl::OptionCategory AnotherCategory("Additional test Options", "Description");
193 StackOption<int> TestOption("test-option", cl::cat(TestCategory),
194 cl::cat(AnotherCategory));
195 ASSERT_EQ(TestOption.Categories.end(),
196 find_if(TestOption.Categories,
197 [&](const llvm::cl::OptionCategory *Cat) {
198 return Cat == &cl::GeneralCategory;
200 << "Failed to remove General Category.";
201 ASSERT_NE(TestOption.Categories.end(),
202 find_if(TestOption.Categories,
203 [&](const llvm::cl::OptionCategory *Cat) {
204 return Cat == &TestCategory;
206 << "Failed to assign Option Category.";
207 ASSERT_NE(TestOption.Categories.end(),
208 find_if(TestOption.Categories,
209 [&](const llvm::cl::OptionCategory *Cat) {
210 return Cat == &AnotherCategory;
212 << "Failed to assign Another Category.";
215 typedef void ParserFunction(StringRef Source, StringSaver &Saver,
216 SmallVectorImpl<const char *> &NewArgv,
217 bool MarkEOLs);
219 void testCommandLineTokenizer(ParserFunction *parse, StringRef Input,
220 const char *const Output[], size_t OutputSize) {
221 SmallVector<const char *, 0> Actual;
222 BumpPtrAllocator A;
223 StringSaver Saver(A);
224 parse(Input, Saver, Actual, /*MarkEOLs=*/false);
225 EXPECT_EQ(OutputSize, Actual.size());
226 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
227 if (I < OutputSize) {
228 EXPECT_STREQ(Output[I], Actual[I]);
233 TEST(CommandLineTest, TokenizeGNUCommandLine) {
234 const char Input[] =
235 "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
236 "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
237 const char *const Output[] = {
238 "foo bar", "foo bar", "foo bar", "foo\\bar",
239 "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"};
240 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
241 array_lengthof(Output));
244 TEST(CommandLineTest, TokenizeWindowsCommandLine1) {
245 const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
246 "\"st \\\"u\" \\v";
247 const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
248 "lmn", "o", "pqr", "st \"u", "\\v" };
249 testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
250 array_lengthof(Output));
253 TEST(CommandLineTest, TokenizeWindowsCommandLine2) {
254 const char Input[] = "clang -c -DFOO=\"\"\"ABC\"\"\" x.cpp";
255 const char *const Output[] = { "clang", "-c", "-DFOO=\"ABC\"", "x.cpp"};
256 testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
257 array_lengthof(Output));
260 TEST(CommandLineTest, TokenizeConfigFile1) {
261 const char *Input = "\\";
262 const char *const Output[] = { "\\" };
263 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
264 array_lengthof(Output));
267 TEST(CommandLineTest, TokenizeConfigFile2) {
268 const char *Input = "\\abc";
269 const char *const Output[] = { "abc" };
270 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
271 array_lengthof(Output));
274 TEST(CommandLineTest, TokenizeConfigFile3) {
275 const char *Input = "abc\\";
276 const char *const Output[] = { "abc\\" };
277 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
278 array_lengthof(Output));
281 TEST(CommandLineTest, TokenizeConfigFile4) {
282 const char *Input = "abc\\\n123";
283 const char *const Output[] = { "abc123" };
284 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
285 array_lengthof(Output));
288 TEST(CommandLineTest, TokenizeConfigFile5) {
289 const char *Input = "abc\\\r\n123";
290 const char *const Output[] = { "abc123" };
291 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
292 array_lengthof(Output));
295 TEST(CommandLineTest, TokenizeConfigFile6) {
296 const char *Input = "abc\\\n";
297 const char *const Output[] = { "abc" };
298 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
299 array_lengthof(Output));
302 TEST(CommandLineTest, TokenizeConfigFile7) {
303 const char *Input = "abc\\\r\n";
304 const char *const Output[] = { "abc" };
305 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
306 array_lengthof(Output));
309 TEST(CommandLineTest, TokenizeConfigFile8) {
310 SmallVector<const char *, 0> Actual;
311 BumpPtrAllocator A;
312 StringSaver Saver(A);
313 cl::tokenizeConfigFile("\\\n", Saver, Actual, /*MarkEOLs=*/false);
314 EXPECT_TRUE(Actual.empty());
317 TEST(CommandLineTest, TokenizeConfigFile9) {
318 SmallVector<const char *, 0> Actual;
319 BumpPtrAllocator A;
320 StringSaver Saver(A);
321 cl::tokenizeConfigFile("\\\r\n", Saver, Actual, /*MarkEOLs=*/false);
322 EXPECT_TRUE(Actual.empty());
325 TEST(CommandLineTest, TokenizeConfigFile10) {
326 const char *Input = "\\\nabc";
327 const char *const Output[] = { "abc" };
328 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
329 array_lengthof(Output));
332 TEST(CommandLineTest, TokenizeConfigFile11) {
333 const char *Input = "\\\r\nabc";
334 const char *const Output[] = { "abc" };
335 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
336 array_lengthof(Output));
339 TEST(CommandLineTest, AliasesWithArguments) {
340 static const size_t ARGC = 3;
341 const char *const Inputs[][ARGC] = {
342 { "-tool", "-actual=x", "-extra" },
343 { "-tool", "-actual", "x" },
344 { "-tool", "-alias=x", "-extra" },
345 { "-tool", "-alias", "x" }
348 for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
349 StackOption<std::string> Actual("actual");
350 StackOption<bool> Extra("extra");
351 StackOption<std::string> Input(cl::Positional);
353 cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
355 cl::ParseCommandLineOptions(ARGC, Inputs[i]);
356 EXPECT_EQ("x", Actual);
357 EXPECT_EQ(0, Input.getNumOccurrences());
359 Alias.removeArgument();
363 void testAliasRequired(int argc, const char *const *argv) {
364 StackOption<std::string> Option("option", cl::Required);
365 cl::alias Alias("o", llvm::cl::aliasopt(Option));
367 cl::ParseCommandLineOptions(argc, argv);
368 EXPECT_EQ("x", Option);
369 EXPECT_EQ(1, Option.getNumOccurrences());
371 Alias.removeArgument();
374 TEST(CommandLineTest, AliasRequired) {
375 const char *opts1[] = { "-tool", "-option=x" };
376 const char *opts2[] = { "-tool", "-o", "x" };
377 testAliasRequired(array_lengthof(opts1), opts1);
378 testAliasRequired(array_lengthof(opts2), opts2);
381 TEST(CommandLineTest, HideUnrelatedOptions) {
382 StackOption<int> TestOption1("hide-option-1");
383 StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
385 cl::HideUnrelatedOptions(TestCategory);
387 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
388 << "Failed to hide extra option.";
389 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
390 << "Hid extra option that should be visable.";
392 StringMap<cl::Option *> &Map =
393 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
394 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
395 << "Hid default option that should be visable.";
398 cl::OptionCategory TestCategory2("Test Options set 2", "Description");
400 TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
401 StackOption<int> TestOption1("multi-hide-option-1");
402 StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory));
403 StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2));
405 const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
406 &TestCategory2};
408 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
410 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
411 << "Failed to hide extra option.";
412 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
413 << "Hid extra option that should be visable.";
414 ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
415 << "Hid extra option that should be visable.";
417 StringMap<cl::Option *> &Map =
418 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
419 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
420 << "Hid default option that should be visable.";
423 TEST(CommandLineTest, SetValueInSubcategories) {
424 cl::ResetCommandLineParser();
426 StackSubCommand SC1("sc1", "First subcommand");
427 StackSubCommand SC2("sc2", "Second subcommand");
429 StackOption<bool> TopLevelOpt("top-level", cl::init(false));
430 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
431 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
433 EXPECT_FALSE(TopLevelOpt);
434 EXPECT_FALSE(SC1Opt);
435 EXPECT_FALSE(SC2Opt);
436 const char *args[] = {"prog", "-top-level"};
437 EXPECT_TRUE(
438 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
439 EXPECT_TRUE(TopLevelOpt);
440 EXPECT_FALSE(SC1Opt);
441 EXPECT_FALSE(SC2Opt);
443 TopLevelOpt = false;
445 cl::ResetAllOptionOccurrences();
446 EXPECT_FALSE(TopLevelOpt);
447 EXPECT_FALSE(SC1Opt);
448 EXPECT_FALSE(SC2Opt);
449 const char *args2[] = {"prog", "sc1", "-sc1"};
450 EXPECT_TRUE(
451 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
452 EXPECT_FALSE(TopLevelOpt);
453 EXPECT_TRUE(SC1Opt);
454 EXPECT_FALSE(SC2Opt);
456 SC1Opt = false;
458 cl::ResetAllOptionOccurrences();
459 EXPECT_FALSE(TopLevelOpt);
460 EXPECT_FALSE(SC1Opt);
461 EXPECT_FALSE(SC2Opt);
462 const char *args3[] = {"prog", "sc2", "-sc2"};
463 EXPECT_TRUE(
464 cl::ParseCommandLineOptions(3, args3, StringRef(), &llvm::nulls()));
465 EXPECT_FALSE(TopLevelOpt);
466 EXPECT_FALSE(SC1Opt);
467 EXPECT_TRUE(SC2Opt);
470 TEST(CommandLineTest, LookupFailsInWrongSubCommand) {
471 cl::ResetCommandLineParser();
473 StackSubCommand SC1("sc1", "First subcommand");
474 StackSubCommand SC2("sc2", "Second subcommand");
476 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
477 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
479 std::string Errs;
480 raw_string_ostream OS(Errs);
482 const char *args[] = {"prog", "sc1", "-sc2"};
483 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
484 OS.flush();
485 EXPECT_FALSE(Errs.empty());
488 TEST(CommandLineTest, AddToAllSubCommands) {
489 cl::ResetCommandLineParser();
491 StackSubCommand SC1("sc1", "First subcommand");
492 StackOption<bool> AllOpt("everywhere", cl::sub(*cl::AllSubCommands),
493 cl::init(false));
494 StackSubCommand SC2("sc2", "Second subcommand");
496 const char *args[] = {"prog", "-everywhere"};
497 const char *args2[] = {"prog", "sc1", "-everywhere"};
498 const char *args3[] = {"prog", "sc2", "-everywhere"};
500 std::string Errs;
501 raw_string_ostream OS(Errs);
503 EXPECT_FALSE(AllOpt);
504 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS));
505 EXPECT_TRUE(AllOpt);
507 AllOpt = false;
509 cl::ResetAllOptionOccurrences();
510 EXPECT_FALSE(AllOpt);
511 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS));
512 EXPECT_TRUE(AllOpt);
514 AllOpt = false;
516 cl::ResetAllOptionOccurrences();
517 EXPECT_FALSE(AllOpt);
518 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS));
519 EXPECT_TRUE(AllOpt);
521 // Since all parsing succeeded, the error message should be empty.
522 OS.flush();
523 EXPECT_TRUE(Errs.empty());
526 TEST(CommandLineTest, ReparseCommandLineOptions) {
527 cl::ResetCommandLineParser();
529 StackOption<bool> TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand),
530 cl::init(false));
532 const char *args[] = {"prog", "-top-level"};
534 EXPECT_FALSE(TopLevelOpt);
535 EXPECT_TRUE(
536 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
537 EXPECT_TRUE(TopLevelOpt);
539 TopLevelOpt = false;
541 cl::ResetAllOptionOccurrences();
542 EXPECT_FALSE(TopLevelOpt);
543 EXPECT_TRUE(
544 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
545 EXPECT_TRUE(TopLevelOpt);
548 TEST(CommandLineTest, RemoveFromRegularSubCommand) {
549 cl::ResetCommandLineParser();
551 StackSubCommand SC("sc", "Subcommand");
552 StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false));
553 StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false));
555 const char *args[] = {"prog", "sc", "-remove-option"};
557 std::string Errs;
558 raw_string_ostream OS(Errs);
560 EXPECT_FALSE(RemoveOption);
561 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
562 EXPECT_TRUE(RemoveOption);
563 OS.flush();
564 EXPECT_TRUE(Errs.empty());
566 RemoveOption.removeArgument();
568 cl::ResetAllOptionOccurrences();
569 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
570 OS.flush();
571 EXPECT_FALSE(Errs.empty());
574 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
575 cl::ResetCommandLineParser();
577 StackOption<bool> TopLevelRemove(
578 "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
579 StackOption<bool> TopLevelKeep(
580 "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
582 const char *args[] = {"prog", "-top-level-remove"};
584 EXPECT_FALSE(TopLevelRemove);
585 EXPECT_TRUE(
586 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
587 EXPECT_TRUE(TopLevelRemove);
589 TopLevelRemove.removeArgument();
591 cl::ResetAllOptionOccurrences();
592 EXPECT_FALSE(
593 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
596 TEST(CommandLineTest, RemoveFromAllSubCommands) {
597 cl::ResetCommandLineParser();
599 StackSubCommand SC1("sc1", "First Subcommand");
600 StackSubCommand SC2("sc2", "Second Subcommand");
601 StackOption<bool> RemoveOption("remove-option", cl::sub(*cl::AllSubCommands),
602 cl::init(false));
603 StackOption<bool> KeepOption("keep-option", cl::sub(*cl::AllSubCommands),
604 cl::init(false));
606 const char *args0[] = {"prog", "-remove-option"};
607 const char *args1[] = {"prog", "sc1", "-remove-option"};
608 const char *args2[] = {"prog", "sc2", "-remove-option"};
610 // It should work for all subcommands including the top-level.
611 EXPECT_FALSE(RemoveOption);
612 EXPECT_TRUE(
613 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
614 EXPECT_TRUE(RemoveOption);
616 RemoveOption = false;
618 cl::ResetAllOptionOccurrences();
619 EXPECT_FALSE(RemoveOption);
620 EXPECT_TRUE(
621 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
622 EXPECT_TRUE(RemoveOption);
624 RemoveOption = false;
626 cl::ResetAllOptionOccurrences();
627 EXPECT_FALSE(RemoveOption);
628 EXPECT_TRUE(
629 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
630 EXPECT_TRUE(RemoveOption);
632 RemoveOption.removeArgument();
634 // It should not work for any subcommands including the top-level.
635 cl::ResetAllOptionOccurrences();
636 EXPECT_FALSE(
637 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
638 cl::ResetAllOptionOccurrences();
639 EXPECT_FALSE(
640 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
641 cl::ResetAllOptionOccurrences();
642 EXPECT_FALSE(
643 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
646 TEST(CommandLineTest, GetRegisteredSubcommands) {
647 cl::ResetCommandLineParser();
649 StackSubCommand SC1("sc1", "First Subcommand");
650 StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false));
651 StackSubCommand SC2("sc2", "Second subcommand");
652 StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
654 const char *args0[] = {"prog", "sc1"};
655 const char *args1[] = {"prog", "sc2"};
657 EXPECT_TRUE(
658 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
659 EXPECT_FALSE(Opt1);
660 EXPECT_FALSE(Opt2);
661 for (auto *S : cl::getRegisteredSubcommands()) {
662 if (*S) {
663 EXPECT_EQ("sc1", S->getName());
667 cl::ResetAllOptionOccurrences();
668 EXPECT_TRUE(
669 cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls()));
670 EXPECT_FALSE(Opt1);
671 EXPECT_FALSE(Opt2);
672 for (auto *S : cl::getRegisteredSubcommands()) {
673 if (*S) {
674 EXPECT_EQ("sc2", S->getName());
679 TEST(CommandLineTest, DefaultOptions) {
680 cl::ResetCommandLineParser();
682 StackOption<std::string> Bar("bar", cl::sub(*cl::AllSubCommands),
683 cl::DefaultOption);
684 StackOption<std::string, cl::alias> Bar_Alias(
685 "b", cl::desc("Alias for -bar"), cl::aliasopt(Bar), cl::DefaultOption);
687 StackOption<bool> Foo("foo", cl::init(false), cl::sub(*cl::AllSubCommands),
688 cl::DefaultOption);
689 StackOption<bool, cl::alias> Foo_Alias("f", cl::desc("Alias for -foo"),
690 cl::aliasopt(Foo), cl::DefaultOption);
692 StackSubCommand SC1("sc1", "First Subcommand");
693 // Override "-b" and change type in sc1 SubCommand.
694 StackOption<bool> SC1_B("b", cl::sub(SC1), cl::init(false));
695 StackSubCommand SC2("sc2", "Second subcommand");
696 // Override "-foo" and change type in sc2 SubCommand. Note that this does not
697 // affect "-f" alias, which continues to work correctly.
698 StackOption<std::string> SC2_Foo("foo", cl::sub(SC2));
700 const char *args0[] = {"prog", "-b", "args0 bar string", "-f"};
701 EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args0) / sizeof(char *), args0,
702 StringRef(), &llvm::nulls()));
703 EXPECT_TRUE(Bar == "args0 bar string");
704 EXPECT_TRUE(Foo);
705 EXPECT_FALSE(SC1_B);
706 EXPECT_TRUE(SC2_Foo.empty());
708 cl::ResetAllOptionOccurrences();
710 const char *args1[] = {"prog", "sc1", "-b", "-bar", "args1 bar string", "-f"};
711 EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args1) / sizeof(char *), args1,
712 StringRef(), &llvm::nulls()));
713 EXPECT_TRUE(Bar == "args1 bar string");
714 EXPECT_TRUE(Foo);
715 EXPECT_TRUE(SC1_B);
716 EXPECT_TRUE(SC2_Foo.empty());
717 for (auto *S : cl::getRegisteredSubcommands()) {
718 if (*S) {
719 EXPECT_EQ("sc1", S->getName());
723 cl::ResetAllOptionOccurrences();
725 const char *args2[] = {"prog", "sc2", "-b", "args2 bar string",
726 "-f", "-foo", "foo string"};
727 EXPECT_TRUE(cl::ParseCommandLineOptions(sizeof(args2) / sizeof(char *), args2,
728 StringRef(), &llvm::nulls()));
729 EXPECT_TRUE(Bar == "args2 bar string");
730 EXPECT_TRUE(Foo);
731 EXPECT_FALSE(SC1_B);
732 EXPECT_TRUE(SC2_Foo == "foo string");
733 for (auto *S : cl::getRegisteredSubcommands()) {
734 if (*S) {
735 EXPECT_EQ("sc2", S->getName());
738 cl::ResetCommandLineParser();
741 TEST(CommandLineTest, ArgumentLimit) {
742 std::string args(32 * 4096, 'a');
743 EXPECT_FALSE(llvm::sys::commandLineFitsWithinSystemLimits("cl", args.data()));
746 TEST(CommandLineTest, ResponseFileWindows) {
747 if (!Triple(sys::getProcessTriple()).isOSWindows())
748 return;
750 StackOption<std::string, cl::list<std::string>> InputFilenames(
751 cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
752 StackOption<bool> TopLevelOpt("top-level", cl::init(false));
754 // Create response file.
755 int FileDescriptor;
756 SmallString<64> TempPath;
757 std::error_code EC =
758 llvm::sys::fs::createTemporaryFile("resp-", ".txt", FileDescriptor, TempPath);
759 EXPECT_TRUE(!EC);
761 std::ofstream RspFile(TempPath.c_str());
762 EXPECT_TRUE(RspFile.is_open());
763 RspFile << "-top-level\npath\\dir\\file1\npath/dir/file2";
764 RspFile.close();
766 llvm::SmallString<128> RspOpt;
767 RspOpt.append(1, '@');
768 RspOpt.append(TempPath.c_str());
769 const char *args[] = {"prog", RspOpt.c_str()};
770 EXPECT_FALSE(TopLevelOpt);
771 EXPECT_TRUE(
772 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
773 EXPECT_TRUE(TopLevelOpt);
774 EXPECT_TRUE(InputFilenames[0] == "path\\dir\\file1");
775 EXPECT_TRUE(InputFilenames[1] == "path/dir/file2");
777 llvm::sys::fs::remove(TempPath.c_str());
780 TEST(CommandLineTest, ResponseFiles) {
781 llvm::SmallString<128> TestDir;
782 std::error_code EC =
783 llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
784 EXPECT_TRUE(!EC);
786 // Create included response file of first level.
787 llvm::SmallString<128> IncludedFileName;
788 llvm::sys::path::append(IncludedFileName, TestDir, "resp1");
789 std::ofstream IncludedFile(IncludedFileName.c_str());
790 EXPECT_TRUE(IncludedFile.is_open());
791 IncludedFile << "-option_1 -option_2\n"
792 "@incdir/resp2\n"
793 "-option_3=abcd\n"
794 "@incdir/resp3\n"
795 "-option_4=efjk\n";
796 IncludedFile.close();
798 // Directory for included file.
799 llvm::SmallString<128> IncDir;
800 llvm::sys::path::append(IncDir, TestDir, "incdir");
801 EC = llvm::sys::fs::create_directory(IncDir);
802 EXPECT_TRUE(!EC);
804 // Create included response file of second level.
805 llvm::SmallString<128> IncludedFileName2;
806 llvm::sys::path::append(IncludedFileName2, IncDir, "resp2");
807 std::ofstream IncludedFile2(IncludedFileName2.c_str());
808 EXPECT_TRUE(IncludedFile2.is_open());
809 IncludedFile2 << "-option_21 -option_22\n";
810 IncludedFile2 << "-option_23=abcd\n";
811 IncludedFile2.close();
813 // Create second included response file of second level.
814 llvm::SmallString<128> IncludedFileName3;
815 llvm::sys::path::append(IncludedFileName3, IncDir, "resp3");
816 std::ofstream IncludedFile3(IncludedFileName3.c_str());
817 EXPECT_TRUE(IncludedFile3.is_open());
818 IncludedFile3 << "-option_31 -option_32\n";
819 IncludedFile3 << "-option_33=abcd\n";
820 IncludedFile3.close();
822 // Prepare 'file' with reference to response file.
823 SmallString<128> IncRef;
824 IncRef.append(1, '@');
825 IncRef.append(IncludedFileName.c_str());
826 llvm::SmallVector<const char *, 4> Argv =
827 { "test/test", "-flag_1", IncRef.c_str(), "-flag_2" };
829 // Expand response files.
830 llvm::BumpPtrAllocator A;
831 llvm::StringSaver Saver(A);
832 bool Res = llvm::cl::ExpandResponseFiles(
833 Saver, llvm::cl::TokenizeGNUCommandLine, Argv, false, true);
834 EXPECT_TRUE(Res);
835 EXPECT_EQ(Argv.size(), 13U);
836 EXPECT_STREQ(Argv[0], "test/test");
837 EXPECT_STREQ(Argv[1], "-flag_1");
838 EXPECT_STREQ(Argv[2], "-option_1");
839 EXPECT_STREQ(Argv[3], "-option_2");
840 EXPECT_STREQ(Argv[4], "-option_21");
841 EXPECT_STREQ(Argv[5], "-option_22");
842 EXPECT_STREQ(Argv[6], "-option_23=abcd");
843 EXPECT_STREQ(Argv[7], "-option_3=abcd");
844 EXPECT_STREQ(Argv[8], "-option_31");
845 EXPECT_STREQ(Argv[9], "-option_32");
846 EXPECT_STREQ(Argv[10], "-option_33=abcd");
847 EXPECT_STREQ(Argv[11], "-option_4=efjk");
848 EXPECT_STREQ(Argv[12], "-flag_2");
850 llvm::sys::fs::remove(IncludedFileName3);
851 llvm::sys::fs::remove(IncludedFileName2);
852 llvm::sys::fs::remove(IncDir);
853 llvm::sys::fs::remove(IncludedFileName);
854 llvm::sys::fs::remove(TestDir);
857 TEST(CommandLineTest, RecursiveResponseFiles) {
858 SmallString<128> TestDir;
859 std::error_code EC = sys::fs::createUniqueDirectory("unittest", TestDir);
860 EXPECT_TRUE(!EC);
862 SmallString<128> SelfFilePath;
863 sys::path::append(SelfFilePath, TestDir, "self.rsp");
864 std::string SelfFileRef = std::string("@") + SelfFilePath.c_str();
866 SmallString<128> NestedFilePath;
867 sys::path::append(NestedFilePath, TestDir, "nested.rsp");
868 std::string NestedFileRef = std::string("@") + NestedFilePath.c_str();
870 SmallString<128> FlagFilePath;
871 sys::path::append(FlagFilePath, TestDir, "flag.rsp");
872 std::string FlagFileRef = std::string("@") + FlagFilePath.c_str();
874 std::ofstream SelfFile(SelfFilePath.str());
875 EXPECT_TRUE(SelfFile.is_open());
876 SelfFile << "-option_1\n";
877 SelfFile << FlagFileRef << "\n";
878 SelfFile << NestedFileRef << "\n";
879 SelfFile << SelfFileRef << "\n";
880 SelfFile.close();
882 std::ofstream NestedFile(NestedFilePath.str());
883 EXPECT_TRUE(NestedFile.is_open());
884 NestedFile << "-option_2\n";
885 NestedFile << FlagFileRef << "\n";
886 NestedFile << SelfFileRef << "\n";
887 NestedFile << NestedFileRef << "\n";
888 NestedFile.close();
890 std::ofstream FlagFile(FlagFilePath.str());
891 EXPECT_TRUE(FlagFile.is_open());
892 FlagFile << "-option_x\n";
893 FlagFile.close();
895 // Ensure:
896 // Recursive expansion terminates
897 // Recursive files never expand
898 // Non-recursive repeats are allowed
899 SmallVector<const char *, 4> Argv = {"test/test", SelfFileRef.c_str(),
900 "-option_3"};
901 BumpPtrAllocator A;
902 StringSaver Saver(A);
903 #ifdef _WIN32
904 cl::TokenizerCallback Tokenizer = cl::TokenizeWindowsCommandLine;
905 #else
906 cl::TokenizerCallback Tokenizer = cl::TokenizeGNUCommandLine;
907 #endif
908 bool Res = cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false);
909 EXPECT_FALSE(Res);
911 EXPECT_EQ(Argv.size(), 9U);
912 EXPECT_STREQ(Argv[0], "test/test");
913 EXPECT_STREQ(Argv[1], "-option_1");
914 EXPECT_STREQ(Argv[2], "-option_x");
915 EXPECT_STREQ(Argv[3], "-option_2");
916 EXPECT_STREQ(Argv[4], "-option_x");
917 EXPECT_STREQ(Argv[5], SelfFileRef.c_str());
918 EXPECT_STREQ(Argv[6], NestedFileRef.c_str());
919 EXPECT_STREQ(Argv[7], SelfFileRef.c_str());
920 EXPECT_STREQ(Argv[8], "-option_3");
923 TEST(CommandLineTest, ResponseFilesAtArguments) {
924 SmallString<128> TestDir;
925 std::error_code EC = sys::fs::createUniqueDirectory("unittest", TestDir);
926 EXPECT_TRUE(!EC);
928 SmallString<128> ResponseFilePath;
929 sys::path::append(ResponseFilePath, TestDir, "test.rsp");
931 std::ofstream ResponseFile(ResponseFilePath.c_str());
932 EXPECT_TRUE(ResponseFile.is_open());
933 ResponseFile << "-foo" << "\n";
934 ResponseFile << "-bar" << "\n";
935 ResponseFile.close();
937 // Ensure we expand rsp files after lots of non-rsp arguments starting with @.
938 constexpr size_t NON_RSP_AT_ARGS = 64;
939 SmallVector<const char *, 4> Argv = {"test/test"};
940 Argv.append(NON_RSP_AT_ARGS, "@non_rsp_at_arg");
941 std::string ResponseFileRef = std::string("@") + ResponseFilePath.c_str();
942 Argv.push_back(ResponseFileRef.c_str());
944 BumpPtrAllocator A;
945 StringSaver Saver(A);
946 bool Res = cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Argv,
947 false, false);
948 EXPECT_FALSE(Res);
950 // ASSERT instead of EXPECT to prevent potential out-of-bounds access.
951 ASSERT_EQ(Argv.size(), 1 + NON_RSP_AT_ARGS + 2);
952 size_t i = 0;
953 EXPECT_STREQ(Argv[i++], "test/test");
954 for (; i < 1 + NON_RSP_AT_ARGS; ++i)
955 EXPECT_STREQ(Argv[i], "@non_rsp_at_arg");
956 EXPECT_STREQ(Argv[i++], "-foo");
957 EXPECT_STREQ(Argv[i++], "-bar");
960 TEST(CommandLineTest, SetDefautValue) {
961 cl::ResetCommandLineParser();
963 StackOption<std::string> Opt1("opt1", cl::init("true"));
964 StackOption<bool> Opt2("opt2", cl::init(true));
965 cl::alias Alias("alias", llvm::cl::aliasopt(Opt2));
966 StackOption<int> Opt3("opt3", cl::init(3));
968 const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"};
970 EXPECT_TRUE(
971 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
973 EXPECT_TRUE(Opt1 == "false");
974 EXPECT_TRUE(Opt2);
975 EXPECT_TRUE(Opt3 == 3);
977 Opt2 = false;
978 Opt3 = 1;
980 cl::ResetAllOptionOccurrences();
982 for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
983 cl::Option *O = OM.second;
984 if (O->ArgStr == "opt2") {
985 continue;
987 O->setDefault();
990 EXPECT_TRUE(Opt1 == "true");
991 EXPECT_TRUE(Opt2);
992 EXPECT_TRUE(Opt3 == 3);
993 Alias.removeArgument();
996 TEST(CommandLineTest, ReadConfigFile) {
997 llvm::SmallVector<const char *, 1> Argv;
999 llvm::SmallString<128> TestDir;
1000 std::error_code EC =
1001 llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
1002 EXPECT_TRUE(!EC);
1004 llvm::SmallString<128> TestCfg;
1005 llvm::sys::path::append(TestCfg, TestDir, "foo");
1006 std::ofstream ConfigFile(TestCfg.c_str());
1007 EXPECT_TRUE(ConfigFile.is_open());
1008 ConfigFile << "# Comment\n"
1009 "-option_1\n"
1010 "@subconfig\n"
1011 "-option_3=abcd\n"
1012 "-option_4=\\\n"
1013 "cdef\n";
1014 ConfigFile.close();
1016 llvm::SmallString<128> TestCfg2;
1017 llvm::sys::path::append(TestCfg2, TestDir, "subconfig");
1018 std::ofstream ConfigFile2(TestCfg2.c_str());
1019 EXPECT_TRUE(ConfigFile2.is_open());
1020 ConfigFile2 << "-option_2\n"
1021 "\n"
1022 " # comment\n";
1023 ConfigFile2.close();
1025 // Make sure the current directory is not the directory where config files
1026 // resides. In this case the code that expands response files will not find
1027 // 'subconfig' unless it resolves nested inclusions relative to the including
1028 // file.
1029 llvm::SmallString<128> CurrDir;
1030 EC = llvm::sys::fs::current_path(CurrDir);
1031 EXPECT_TRUE(!EC);
1032 EXPECT_TRUE(StringRef(CurrDir) != StringRef(TestDir));
1034 llvm::BumpPtrAllocator A;
1035 llvm::StringSaver Saver(A);
1036 bool Result = llvm::cl::readConfigFile(TestCfg, Saver, Argv);
1038 EXPECT_TRUE(Result);
1039 EXPECT_EQ(Argv.size(), 4U);
1040 EXPECT_STREQ(Argv[0], "-option_1");
1041 EXPECT_STREQ(Argv[1], "-option_2");
1042 EXPECT_STREQ(Argv[2], "-option_3=abcd");
1043 EXPECT_STREQ(Argv[3], "-option_4=cdef");
1045 llvm::sys::fs::remove(TestCfg2);
1046 llvm::sys::fs::remove(TestCfg);
1047 llvm::sys::fs::remove(TestDir);
1050 TEST(CommandLineTest, PositionalEatArgsError) {
1051 cl::ResetCommandLineParser();
1053 StackOption<std::string, cl::list<std::string>> PosEatArgs(
1054 "positional-eat-args", cl::Positional, cl::desc("<arguments>..."),
1055 cl::ZeroOrMore, cl::PositionalEatsArgs);
1056 StackOption<std::string, cl::list<std::string>> PosEatArgs2(
1057 "positional-eat-args2", cl::Positional, cl::desc("Some strings"),
1058 cl::ZeroOrMore, cl::PositionalEatsArgs);
1060 const char *args[] = {"prog", "-positional-eat-args=XXXX"};
1061 const char *args2[] = {"prog", "-positional-eat-args=XXXX", "-foo"};
1062 const char *args3[] = {"prog", "-positional-eat-args", "-foo"};
1063 const char *args4[] = {"prog", "-positional-eat-args",
1064 "-foo", "-positional-eat-args2",
1065 "-bar", "foo"};
1067 std::string Errs;
1068 raw_string_ostream OS(Errs);
1069 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS)); OS.flush();
1070 EXPECT_FALSE(Errs.empty()); Errs.clear();
1071 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS)); OS.flush();
1072 EXPECT_FALSE(Errs.empty()); Errs.clear();
1073 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS)); OS.flush();
1074 EXPECT_TRUE(Errs.empty()); Errs.clear();
1076 cl::ResetAllOptionOccurrences();
1077 EXPECT_TRUE(cl::ParseCommandLineOptions(6, args4, StringRef(), &OS)); OS.flush();
1078 EXPECT_TRUE(PosEatArgs.size() == 1);
1079 EXPECT_TRUE(PosEatArgs2.size() == 2);
1080 EXPECT_TRUE(Errs.empty());
1083 #ifdef _WIN32
1084 TEST(CommandLineTest, GetCommandLineArguments) {
1085 int argc = __argc;
1086 char **argv = __argv;
1088 // GetCommandLineArguments is called in InitLLVM.
1089 llvm::InitLLVM X(argc, argv);
1091 EXPECT_EQ(llvm::sys::path::is_absolute(argv[0]),
1092 llvm::sys::path::is_absolute(__argv[0]));
1094 EXPECT_TRUE(llvm::sys::path::filename(argv[0])
1095 .equals_lower("supporttests.exe"))
1096 << "Filename of test executable is "
1097 << llvm::sys::path::filename(argv[0]);
1099 #endif
1101 class OutputRedirector {
1102 public:
1103 OutputRedirector(int RedirectFD)
1104 : RedirectFD(RedirectFD), OldFD(dup(RedirectFD)) {
1105 if (OldFD == -1 ||
1106 sys::fs::createTemporaryFile("unittest-redirect", "", NewFD,
1107 FilePath) ||
1108 dup2(NewFD, RedirectFD) == -1)
1109 Valid = false;
1112 ~OutputRedirector() {
1113 dup2(OldFD, RedirectFD);
1114 close(OldFD);
1115 close(NewFD);
1118 SmallVector<char, 128> FilePath;
1119 bool Valid = true;
1121 private:
1122 int RedirectFD;
1123 int OldFD;
1124 int NewFD;
1127 struct AutoDeleteFile {
1128 SmallVector<char, 128> FilePath;
1129 ~AutoDeleteFile() {
1130 if (!FilePath.empty())
1131 sys::fs::remove(std::string(FilePath.data(), FilePath.size()));
1135 class PrintOptionInfoTest : public ::testing::Test {
1136 public:
1137 // Return std::string because the output of a failing EXPECT check is
1138 // unreadable for StringRef. It also avoids any lifetime issues.
1139 template <typename... Ts> std::string runTest(Ts... OptionAttributes) {
1140 outs().flush(); // flush any output from previous tests
1141 AutoDeleteFile File;
1143 OutputRedirector Stdout(fileno(stdout));
1144 if (!Stdout.Valid)
1145 return "";
1146 File.FilePath = Stdout.FilePath;
1148 StackOption<OptionValue> TestOption(Opt, cl::desc(HelpText),
1149 OptionAttributes...);
1150 printOptionInfo(TestOption, 26);
1151 outs().flush();
1153 auto Buffer = MemoryBuffer::getFile(File.FilePath);
1154 if (!Buffer)
1155 return "";
1156 return Buffer->get()->getBuffer().str();
1159 enum class OptionValue { Val };
1160 const StringRef Opt = "some-option";
1161 const StringRef HelpText = "some help";
1163 private:
1164 // This is a workaround for cl::Option sub-classes having their
1165 // printOptionInfo functions private.
1166 void printOptionInfo(const cl::Option &O, size_t Width) {
1167 O.printOptionInfo(Width);
1171 TEST_F(PrintOptionInfoTest, PrintOptionInfoValueOptionalWithoutSentinel) {
1172 std::string Output =
1173 runTest(cl::ValueOptional,
1174 cl::values(clEnumValN(OptionValue::Val, "v1", "desc1")));
1176 // clang-format off
1177 EXPECT_EQ(Output, (" --" + Opt + "=<value> - " + HelpText + "\n"
1178 " =v1 - desc1\n")
1179 .str());
1180 // clang-format on
1183 TEST_F(PrintOptionInfoTest, PrintOptionInfoValueOptionalWithSentinel) {
1184 std::string Output = runTest(
1185 cl::ValueOptional, cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"),
1186 clEnumValN(OptionValue::Val, "", "")));
1188 // clang-format off
1189 EXPECT_EQ(Output,
1190 (" --" + Opt + " - " + HelpText + "\n"
1191 " --" + Opt + "=<value> - " + HelpText + "\n"
1192 " =v1 - desc1\n")
1193 .str());
1194 // clang-format on
1197 TEST_F(PrintOptionInfoTest, PrintOptionInfoValueOptionalWithSentinelWithHelp) {
1198 std::string Output = runTest(
1199 cl::ValueOptional, cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"),
1200 clEnumValN(OptionValue::Val, "", "desc2")));
1202 // clang-format off
1203 EXPECT_EQ(Output, (" --" + Opt + " - " + HelpText + "\n"
1204 " --" + Opt + "=<value> - " + HelpText + "\n"
1205 " =v1 - desc1\n"
1206 " =<empty> - desc2\n")
1207 .str());
1208 // clang-format on
1211 TEST_F(PrintOptionInfoTest, PrintOptionInfoValueRequiredWithEmptyValueName) {
1212 std::string Output = runTest(
1213 cl::ValueRequired, cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"),
1214 clEnumValN(OptionValue::Val, "", "")));
1216 // clang-format off
1217 EXPECT_EQ(Output, (" --" + Opt + "=<value> - " + HelpText + "\n"
1218 " =v1 - desc1\n"
1219 " =<empty>\n")
1220 .str());
1221 // clang-format on
1224 TEST_F(PrintOptionInfoTest, PrintOptionInfoEmptyValueDescription) {
1225 std::string Output = runTest(
1226 cl::ValueRequired, cl::values(clEnumValN(OptionValue::Val, "v1", "")));
1228 // clang-format off
1229 EXPECT_EQ(Output,
1230 (" --" + Opt + "=<value> - " + HelpText + "\n"
1231 " =v1\n").str());
1232 // clang-format on
1235 class GetOptionWidthTest : public ::testing::Test {
1236 public:
1237 enum class OptionValue { Val };
1239 template <typename... Ts>
1240 size_t runTest(StringRef ArgName, Ts... OptionAttributes) {
1241 StackOption<OptionValue> TestOption(ArgName, cl::desc("some help"),
1242 OptionAttributes...);
1243 return getOptionWidth(TestOption);
1246 private:
1247 // This is a workaround for cl::Option sub-classes having their
1248 // printOptionInfo
1249 // functions private.
1250 size_t getOptionWidth(const cl::Option &O) { return O.getOptionWidth(); }
1253 TEST_F(GetOptionWidthTest, GetOptionWidthArgNameLonger) {
1254 StringRef ArgName("a-long-argument-name");
1255 size_t ExpectedStrSize = (" --" + ArgName + "=<value> - ").str().size();
1256 EXPECT_EQ(
1257 runTest(ArgName, cl::values(clEnumValN(OptionValue::Val, "v", "help"))),
1258 ExpectedStrSize);
1261 TEST_F(GetOptionWidthTest, GetOptionWidthFirstOptionNameLonger) {
1262 StringRef OptName("a-long-option-name");
1263 size_t ExpectedStrSize = (" =" + OptName + " - ").str().size();
1264 EXPECT_EQ(
1265 runTest("a", cl::values(clEnumValN(OptionValue::Val, OptName, "help"),
1266 clEnumValN(OptionValue::Val, "b", "help"))),
1267 ExpectedStrSize);
1270 TEST_F(GetOptionWidthTest, GetOptionWidthSecondOptionNameLonger) {
1271 StringRef OptName("a-long-option-name");
1272 size_t ExpectedStrSize = (" =" + OptName + " - ").str().size();
1273 EXPECT_EQ(
1274 runTest("a", cl::values(clEnumValN(OptionValue::Val, "b", "help"),
1275 clEnumValN(OptionValue::Val, OptName, "help"))),
1276 ExpectedStrSize);
1279 TEST_F(GetOptionWidthTest, GetOptionWidthEmptyOptionNameLonger) {
1280 size_t ExpectedStrSize = StringRef(" =<empty> - ").size();
1281 // The length of a=<value> (including indentation) is actually the same as the
1282 // =<empty> string, so it is impossible to distinguish via testing the case
1283 // where the empty string is picked from where the option name is picked.
1284 EXPECT_EQ(runTest("a", cl::values(clEnumValN(OptionValue::Val, "b", "help"),
1285 clEnumValN(OptionValue::Val, "", "help"))),
1286 ExpectedStrSize);
1289 TEST_F(GetOptionWidthTest,
1290 GetOptionWidthValueOptionalEmptyOptionWithNoDescription) {
1291 StringRef ArgName("a");
1292 // The length of a=<value> (including indentation) is actually the same as the
1293 // =<empty> string, so it is impossible to distinguish via testing the case
1294 // where the empty string is ignored from where it is not ignored.
1295 // The dash will not actually be printed, but the space it would take up is
1296 // included to ensure a consistent column width.
1297 size_t ExpectedStrSize = (" -" + ArgName + "=<value> - ").str().size();
1298 EXPECT_EQ(runTest(ArgName, cl::ValueOptional,
1299 cl::values(clEnumValN(OptionValue::Val, "value", "help"),
1300 clEnumValN(OptionValue::Val, "", ""))),
1301 ExpectedStrSize);
1304 TEST_F(GetOptionWidthTest,
1305 GetOptionWidthValueRequiredEmptyOptionWithNoDescription) {
1306 // The length of a=<value> (including indentation) is actually the same as the
1307 // =<empty> string, so it is impossible to distinguish via testing the case
1308 // where the empty string is picked from where the option name is picked
1309 size_t ExpectedStrSize = StringRef(" =<empty> - ").size();
1310 EXPECT_EQ(runTest("a", cl::ValueRequired,
1311 cl::values(clEnumValN(OptionValue::Val, "value", "help"),
1312 clEnumValN(OptionValue::Val, "", ""))),
1313 ExpectedStrSize);
1316 TEST(CommandLineTest, PrefixOptions) {
1317 cl::ResetCommandLineParser();
1319 StackOption<std::string, cl::list<std::string>> IncludeDirs(
1320 "I", cl::Prefix, cl::desc("Declare an include directory"));
1322 // Test non-prefixed variant works with cl::Prefix options.
1323 EXPECT_TRUE(IncludeDirs.empty());
1324 const char *args[] = {"prog", "-I=/usr/include"};
1325 EXPECT_TRUE(
1326 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
1327 EXPECT_TRUE(IncludeDirs.size() == 1);
1328 EXPECT_TRUE(IncludeDirs.front().compare("/usr/include") == 0);
1330 IncludeDirs.erase(IncludeDirs.begin());
1331 cl::ResetAllOptionOccurrences();
1333 // Test non-prefixed variant works with cl::Prefix options when value is
1334 // passed in following argument.
1335 EXPECT_TRUE(IncludeDirs.empty());
1336 const char *args2[] = {"prog", "-I", "/usr/include"};
1337 EXPECT_TRUE(
1338 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
1339 EXPECT_TRUE(IncludeDirs.size() == 1);
1340 EXPECT_TRUE(IncludeDirs.front().compare("/usr/include") == 0);
1342 IncludeDirs.erase(IncludeDirs.begin());
1343 cl::ResetAllOptionOccurrences();
1345 // Test prefixed variant works with cl::Prefix options.
1346 EXPECT_TRUE(IncludeDirs.empty());
1347 const char *args3[] = {"prog", "-I/usr/include"};
1348 EXPECT_TRUE(
1349 cl::ParseCommandLineOptions(2, args3, StringRef(), &llvm::nulls()));
1350 EXPECT_TRUE(IncludeDirs.size() == 1);
1351 EXPECT_TRUE(IncludeDirs.front().compare("/usr/include") == 0);
1353 StackOption<std::string, cl::list<std::string>> MacroDefs(
1354 "D", cl::AlwaysPrefix, cl::desc("Define a macro"),
1355 cl::value_desc("MACRO[=VALUE]"));
1357 cl::ResetAllOptionOccurrences();
1359 // Test non-prefixed variant does not work with cl::AlwaysPrefix options:
1360 // equal sign is part of the value.
1361 EXPECT_TRUE(MacroDefs.empty());
1362 const char *args4[] = {"prog", "-D=HAVE_FOO"};
1363 EXPECT_TRUE(
1364 cl::ParseCommandLineOptions(2, args4, StringRef(), &llvm::nulls()));
1365 EXPECT_TRUE(MacroDefs.size() == 1);
1366 EXPECT_TRUE(MacroDefs.front().compare("=HAVE_FOO") == 0);
1368 MacroDefs.erase(MacroDefs.begin());
1369 cl::ResetAllOptionOccurrences();
1371 // Test non-prefixed variant does not allow value to be passed in following
1372 // argument with cl::AlwaysPrefix options.
1373 EXPECT_TRUE(MacroDefs.empty());
1374 const char *args5[] = {"prog", "-D", "HAVE_FOO"};
1375 EXPECT_FALSE(
1376 cl::ParseCommandLineOptions(3, args5, StringRef(), &llvm::nulls()));
1377 EXPECT_TRUE(MacroDefs.empty());
1379 cl::ResetAllOptionOccurrences();
1381 // Test prefixed variant works with cl::AlwaysPrefix options.
1382 EXPECT_TRUE(MacroDefs.empty());
1383 const char *args6[] = {"prog", "-DHAVE_FOO"};
1384 EXPECT_TRUE(
1385 cl::ParseCommandLineOptions(2, args6, StringRef(), &llvm::nulls()));
1386 EXPECT_TRUE(MacroDefs.size() == 1);
1387 EXPECT_TRUE(MacroDefs.front().compare("HAVE_FOO") == 0);
1390 TEST(CommandLineTest, GroupingWithValue) {
1391 cl::ResetCommandLineParser();
1393 StackOption<bool> OptF("f", cl::Grouping, cl::desc("Some flag"));
1394 StackOption<bool> OptB("b", cl::Grouping, cl::desc("Another flag"));
1395 StackOption<bool> OptD("d", cl::Grouping, cl::ValueDisallowed,
1396 cl::desc("ValueDisallowed option"));
1397 StackOption<std::string> OptV("v", cl::Grouping,
1398 cl::desc("ValueRequired option"));
1399 StackOption<std::string> OptO("o", cl::Grouping, cl::ValueOptional,
1400 cl::desc("ValueOptional option"));
1402 // Should be possible to use an option which requires a value
1403 // at the end of a group.
1404 const char *args1[] = {"prog", "-fv", "val1"};
1405 EXPECT_TRUE(
1406 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
1407 EXPECT_TRUE(OptF);
1408 EXPECT_STREQ("val1", OptV.c_str());
1409 OptV.clear();
1410 cl::ResetAllOptionOccurrences();
1412 // Should not crash if it is accidentally used elsewhere in the group.
1413 const char *args2[] = {"prog", "-vf", "val2"};
1414 EXPECT_FALSE(
1415 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
1416 OptV.clear();
1417 cl::ResetAllOptionOccurrences();
1419 // Should allow the "opt=value" form at the end of the group
1420 const char *args3[] = {"prog", "-fv=val3"};
1421 EXPECT_TRUE(
1422 cl::ParseCommandLineOptions(2, args3, StringRef(), &llvm::nulls()));
1423 EXPECT_TRUE(OptF);
1424 EXPECT_STREQ("val3", OptV.c_str());
1425 OptV.clear();
1426 cl::ResetAllOptionOccurrences();
1428 // Should allow assigning a value for a ValueOptional option
1429 // at the end of the group
1430 const char *args4[] = {"prog", "-fo=val4"};
1431 EXPECT_TRUE(
1432 cl::ParseCommandLineOptions(2, args4, StringRef(), &llvm::nulls()));
1433 EXPECT_TRUE(OptF);
1434 EXPECT_STREQ("val4", OptO.c_str());
1435 OptO.clear();
1436 cl::ResetAllOptionOccurrences();
1438 // Should assign an empty value if a ValueOptional option is used elsewhere
1439 // in the group.
1440 const char *args5[] = {"prog", "-fob"};
1441 EXPECT_TRUE(
1442 cl::ParseCommandLineOptions(2, args5, StringRef(), &llvm::nulls()));
1443 EXPECT_TRUE(OptF);
1444 EXPECT_EQ(1, OptO.getNumOccurrences());
1445 EXPECT_EQ(1, OptB.getNumOccurrences());
1446 EXPECT_TRUE(OptO.empty());
1447 cl::ResetAllOptionOccurrences();
1449 // Should not allow an assignment for a ValueDisallowed option.
1450 const char *args6[] = {"prog", "-fd=false"};
1451 EXPECT_FALSE(
1452 cl::ParseCommandLineOptions(2, args6, StringRef(), &llvm::nulls()));
1455 TEST(CommandLineTest, GroupingAndPrefix) {
1456 cl::ResetCommandLineParser();
1458 StackOption<bool> OptF("f", cl::Grouping, cl::desc("Some flag"));
1459 StackOption<bool> OptB("b", cl::Grouping, cl::desc("Another flag"));
1460 StackOption<std::string> OptP("p", cl::Prefix, cl::Grouping,
1461 cl::desc("Prefix and Grouping"));
1462 StackOption<std::string> OptA("a", cl::AlwaysPrefix, cl::Grouping,
1463 cl::desc("AlwaysPrefix and Grouping"));
1465 // Should be possible to use a cl::Prefix option without grouping.
1466 const char *args1[] = {"prog", "-pval1"};
1467 EXPECT_TRUE(
1468 cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls()));
1469 EXPECT_STREQ("val1", OptP.c_str());
1470 OptP.clear();
1471 cl::ResetAllOptionOccurrences();
1473 // Should be possible to pass a value in a separate argument.
1474 const char *args2[] = {"prog", "-p", "val2"};
1475 EXPECT_TRUE(
1476 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
1477 EXPECT_STREQ("val2", OptP.c_str());
1478 OptP.clear();
1479 cl::ResetAllOptionOccurrences();
1481 // The "-opt=value" form should work, too.
1482 const char *args3[] = {"prog", "-p=val3"};
1483 EXPECT_TRUE(
1484 cl::ParseCommandLineOptions(2, args3, StringRef(), &llvm::nulls()));
1485 EXPECT_STREQ("val3", OptP.c_str());
1486 OptP.clear();
1487 cl::ResetAllOptionOccurrences();
1489 // All three previous cases should work the same way if an option with both
1490 // cl::Prefix and cl::Grouping modifiers is used at the end of a group.
1491 const char *args4[] = {"prog", "-fpval4"};
1492 EXPECT_TRUE(
1493 cl::ParseCommandLineOptions(2, args4, StringRef(), &llvm::nulls()));
1494 EXPECT_TRUE(OptF);
1495 EXPECT_STREQ("val4", OptP.c_str());
1496 OptP.clear();
1497 cl::ResetAllOptionOccurrences();
1499 const char *args5[] = {"prog", "-fp", "val5"};
1500 EXPECT_TRUE(
1501 cl::ParseCommandLineOptions(3, args5, StringRef(), &llvm::nulls()));
1502 EXPECT_TRUE(OptF);
1503 EXPECT_STREQ("val5", OptP.c_str());
1504 OptP.clear();
1505 cl::ResetAllOptionOccurrences();
1507 const char *args6[] = {"prog", "-fp=val6"};
1508 EXPECT_TRUE(
1509 cl::ParseCommandLineOptions(2, args6, StringRef(), &llvm::nulls()));
1510 EXPECT_TRUE(OptF);
1511 EXPECT_STREQ("val6", OptP.c_str());
1512 OptP.clear();
1513 cl::ResetAllOptionOccurrences();
1515 // Should assign a value even if the part after a cl::Prefix option is equal
1516 // to the name of another option.
1517 const char *args7[] = {"prog", "-fpb"};
1518 EXPECT_TRUE(
1519 cl::ParseCommandLineOptions(2, args7, StringRef(), &llvm::nulls()));
1520 EXPECT_TRUE(OptF);
1521 EXPECT_STREQ("b", OptP.c_str());
1522 EXPECT_FALSE(OptB);
1523 OptP.clear();
1524 cl::ResetAllOptionOccurrences();
1526 // Should be possible to use a cl::AlwaysPrefix option without grouping.
1527 const char *args8[] = {"prog", "-aval8"};
1528 EXPECT_TRUE(
1529 cl::ParseCommandLineOptions(2, args8, StringRef(), &llvm::nulls()));
1530 EXPECT_STREQ("val8", OptA.c_str());
1531 OptA.clear();
1532 cl::ResetAllOptionOccurrences();
1534 // Should not be possible to pass a value in a separate argument.
1535 const char *args9[] = {"prog", "-a", "val9"};
1536 EXPECT_FALSE(
1537 cl::ParseCommandLineOptions(3, args9, StringRef(), &llvm::nulls()));
1538 cl::ResetAllOptionOccurrences();
1540 // With the "-opt=value" form, the "=" symbol should be preserved.
1541 const char *args10[] = {"prog", "-a=val10"};
1542 EXPECT_TRUE(
1543 cl::ParseCommandLineOptions(2, args10, StringRef(), &llvm::nulls()));
1544 EXPECT_STREQ("=val10", OptA.c_str());
1545 OptA.clear();
1546 cl::ResetAllOptionOccurrences();
1548 // All three previous cases should work the same way if an option with both
1549 // cl::AlwaysPrefix and cl::Grouping modifiers is used at the end of a group.
1550 const char *args11[] = {"prog", "-faval11"};
1551 EXPECT_TRUE(
1552 cl::ParseCommandLineOptions(2, args11, StringRef(), &llvm::nulls()));
1553 EXPECT_TRUE(OptF);
1554 EXPECT_STREQ("val11", OptA.c_str());
1555 OptA.clear();
1556 cl::ResetAllOptionOccurrences();
1558 const char *args12[] = {"prog", "-fa", "val12"};
1559 EXPECT_FALSE(
1560 cl::ParseCommandLineOptions(3, args12, StringRef(), &llvm::nulls()));
1561 cl::ResetAllOptionOccurrences();
1563 const char *args13[] = {"prog", "-fa=val13"};
1564 EXPECT_TRUE(
1565 cl::ParseCommandLineOptions(2, args13, StringRef(), &llvm::nulls()));
1566 EXPECT_TRUE(OptF);
1567 EXPECT_STREQ("=val13", OptA.c_str());
1568 OptA.clear();
1569 cl::ResetAllOptionOccurrences();
1571 // Should assign a value even if the part after a cl::AlwaysPrefix option
1572 // is equal to the name of another option.
1573 const char *args14[] = {"prog", "-fab"};
1574 EXPECT_TRUE(
1575 cl::ParseCommandLineOptions(2, args14, StringRef(), &llvm::nulls()));
1576 EXPECT_TRUE(OptF);
1577 EXPECT_STREQ("b", OptA.c_str());
1578 EXPECT_FALSE(OptB);
1579 OptA.clear();
1580 cl::ResetAllOptionOccurrences();
1583 TEST(CommandLineTest, LongOptions) {
1584 cl::ResetCommandLineParser();
1586 StackOption<bool> OptA("a", cl::desc("Some flag"));
1587 StackOption<bool> OptBLong("long-flag", cl::desc("Some long flag"));
1588 StackOption<bool, cl::alias> OptB("b", cl::desc("Alias to --long-flag"),
1589 cl::aliasopt(OptBLong));
1590 StackOption<std::string> OptAB("ab", cl::desc("Another long option"));
1592 std::string Errs;
1593 raw_string_ostream OS(Errs);
1595 const char *args1[] = {"prog", "-a", "-ab", "val1"};
1596 const char *args2[] = {"prog", "-a", "--ab", "val1"};
1597 const char *args3[] = {"prog", "-ab", "--ab", "val1"};
1600 // The following tests treat `-` and `--` the same, and always match the
1601 // longest string.
1604 EXPECT_TRUE(
1605 cl::ParseCommandLineOptions(4, args1, StringRef(), &OS)); OS.flush();
1606 EXPECT_TRUE(OptA);
1607 EXPECT_FALSE(OptBLong);
1608 EXPECT_STREQ("val1", OptAB.c_str());
1609 EXPECT_TRUE(Errs.empty()); Errs.clear();
1610 cl::ResetAllOptionOccurrences();
1612 EXPECT_TRUE(
1613 cl::ParseCommandLineOptions(4, args2, StringRef(), &OS)); OS.flush();
1614 EXPECT_TRUE(OptA);
1615 EXPECT_FALSE(OptBLong);
1616 EXPECT_STREQ("val1", OptAB.c_str());
1617 EXPECT_TRUE(Errs.empty()); Errs.clear();
1618 cl::ResetAllOptionOccurrences();
1620 // Fails because `-ab` and `--ab` are treated the same and appear more than
1621 // once. Also, `val1` is unexpected.
1622 EXPECT_FALSE(
1623 cl::ParseCommandLineOptions(4, args3, StringRef(), &OS)); OS.flush();
1624 outs()<< Errs << "\n";
1625 EXPECT_FALSE(Errs.empty()); Errs.clear();
1626 cl::ResetAllOptionOccurrences();
1629 // The following tests treat `-` and `--` differently, with `-` for short, and
1630 // `--` for long options.
1633 // Fails because `-ab` is treated as `-a -b`, so `-a` is seen twice, and
1634 // `val1` is unexpected.
1635 EXPECT_FALSE(cl::ParseCommandLineOptions(4, args1, StringRef(),
1636 &OS, nullptr, true)); OS.flush();
1637 EXPECT_FALSE(Errs.empty()); Errs.clear();
1638 cl::ResetAllOptionOccurrences();
1640 // Works because `-a` is treated differently than `--ab`.
1641 EXPECT_TRUE(cl::ParseCommandLineOptions(4, args2, StringRef(),
1642 &OS, nullptr, true)); OS.flush();
1643 EXPECT_TRUE(Errs.empty()); Errs.clear();
1644 cl::ResetAllOptionOccurrences();
1646 // Works because `-ab` is treated as `-a -b`, and `--ab` is a long option.
1647 EXPECT_TRUE(cl::ParseCommandLineOptions(4, args3, StringRef(),
1648 &OS, nullptr, true));
1649 EXPECT_TRUE(OptA);
1650 EXPECT_TRUE(OptBLong);
1651 EXPECT_STREQ("val1", OptAB.c_str());
1652 OS.flush();
1653 EXPECT_TRUE(Errs.empty()); Errs.clear();
1654 cl::ResetAllOptionOccurrences();
1656 } // anonymous namespace