1 //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/Program.h"
18 #include "llvm/Support/StringSaver.h"
19 #include "gtest/gtest.h"
30 TempEnvVar(const char *name, const char *value)
32 const char *old_value = getenv(name);
33 EXPECT_EQ(nullptr, old_value) << old_value;
35 setenv(name, value, true);
37 # define SKIP_ENVIRONMENT_TESTS
43 // Assume setenv and unsetenv come together.
46 (void)name; // Suppress -Wunused-private-field.
51 const char *const name;
54 template <typename T, typename Base = cl::opt<T>>
55 class StackOption : public Base {
57 template <class... Ts>
58 explicit StackOption(Ts &&... Ms) : Base(std::forward<Ts>(Ms)...) {}
60 ~StackOption() override { this->removeArgument(); }
62 template <class DT> StackOption<T> &operator=(const DT &V) {
68 class StackSubCommand : public cl::SubCommand {
70 StackSubCommand(StringRef Name,
71 StringRef Description = StringRef())
72 : SubCommand(Name, Description) {}
74 StackSubCommand() : SubCommand() {}
76 ~StackSubCommand() { unregisterSubCommand(); }
80 cl::OptionCategory TestCategory("Test Options", "Description");
81 TEST(CommandLineTest, ModifyExisitingOption) {
82 StackOption<int> TestOption("test-option", cl::desc("old description"));
84 static const char Description[] = "New description";
85 static const char ArgString[] = "new-test-option";
86 static const char ValueString[] = "Integer";
88 StringMap<cl::Option *> &Map =
89 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
91 ASSERT_TRUE(Map.count("test-option") == 1) <<
92 "Could not find option in map.";
94 cl::Option *Retrieved = Map["test-option"];
95 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
97 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
98 "Incorrect default option category.";
100 Retrieved->setCategory(TestCategory);
101 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
102 "Failed to modify option's option category.";
104 Retrieved->setDescription(Description);
105 ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
106 << "Changing option description failed.";
108 Retrieved->setArgStr(ArgString);
109 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
110 << "Failed to modify option's Argument string.";
112 Retrieved->setValueStr(ValueString);
113 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
114 << "Failed to modify option's Value string.";
116 Retrieved->setHiddenFlag(cl::Hidden);
117 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
118 "Failed to modify option's hidden flag.";
120 #ifndef SKIP_ENVIRONMENT_TESTS
122 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
124 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
125 TEST(CommandLineTest, ParseEnvironment) {
126 TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
127 EXPECT_EQ("", EnvironmentTestOption);
128 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
129 EXPECT_EQ("hello", EnvironmentTestOption);
132 // This test used to make valgrind complain
133 // ("Conditional jump or move depends on uninitialised value(s)")
135 // Warning: Do not run any tests after this one that try to gain access to
136 // registered command line options because this will likely result in a
137 // SEGFAULT. This can occur because the cl::opt in the test below is declared
138 // on the stack which will be destroyed after the test completes but the
139 // command line system will still hold a pointer to a deallocated cl::Option.
140 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
141 // Put cl::opt on stack to check for proper initialization of fields.
142 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
143 TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
144 EXPECT_EQ("", EnvironmentTestOptionLocal);
145 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
146 EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
149 #endif // SKIP_ENVIRONMENT_TESTS
151 TEST(CommandLineTest, UseOptionCategory) {
152 StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
154 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
158 typedef void ParserFunction(StringRef Source, StringSaver &Saver,
159 SmallVectorImpl<const char *> &NewArgv,
162 void testCommandLineTokenizer(ParserFunction *parse, StringRef Input,
163 const char *const Output[], size_t OutputSize) {
164 SmallVector<const char *, 0> Actual;
166 StringSaver Saver(A);
167 parse(Input, Saver, Actual, /*MarkEOLs=*/false);
168 EXPECT_EQ(OutputSize, Actual.size());
169 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
170 if (I < OutputSize) {
171 EXPECT_STREQ(Output[I], Actual[I]);
176 TEST(CommandLineTest, TokenizeGNUCommandLine) {
178 "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
179 "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
180 const char *const Output[] = {
181 "foo bar", "foo bar", "foo bar", "foo\\bar",
182 "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"};
183 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
184 array_lengthof(Output));
187 TEST(CommandLineTest, TokenizeWindowsCommandLine) {
188 const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
190 const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
191 "lmn", "o", "pqr", "st \"u", "\\v" };
192 testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
193 array_lengthof(Output));
196 TEST(CommandLineTest, TokenizeConfigFile1) {
197 const char *Input = "\\";
198 const char *const Output[] = { "\\" };
199 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
200 array_lengthof(Output));
203 TEST(CommandLineTest, TokenizeConfigFile2) {
204 const char *Input = "\\abc";
205 const char *const Output[] = { "abc" };
206 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
207 array_lengthof(Output));
210 TEST(CommandLineTest, TokenizeConfigFile3) {
211 const char *Input = "abc\\";
212 const char *const Output[] = { "abc\\" };
213 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
214 array_lengthof(Output));
217 TEST(CommandLineTest, TokenizeConfigFile4) {
218 const char *Input = "abc\\\n123";
219 const char *const Output[] = { "abc123" };
220 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
221 array_lengthof(Output));
224 TEST(CommandLineTest, TokenizeConfigFile5) {
225 const char *Input = "abc\\\r\n123";
226 const char *const Output[] = { "abc123" };
227 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
228 array_lengthof(Output));
231 TEST(CommandLineTest, TokenizeConfigFile6) {
232 const char *Input = "abc\\\n";
233 const char *const Output[] = { "abc" };
234 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
235 array_lengthof(Output));
238 TEST(CommandLineTest, TokenizeConfigFile7) {
239 const char *Input = "abc\\\r\n";
240 const char *const Output[] = { "abc" };
241 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
242 array_lengthof(Output));
245 TEST(CommandLineTest, TokenizeConfigFile8) {
246 SmallVector<const char *, 0> Actual;
248 StringSaver Saver(A);
249 cl::tokenizeConfigFile("\\\n", Saver, Actual, /*MarkEOLs=*/false);
250 EXPECT_TRUE(Actual.empty());
253 TEST(CommandLineTest, TokenizeConfigFile9) {
254 SmallVector<const char *, 0> Actual;
256 StringSaver Saver(A);
257 cl::tokenizeConfigFile("\\\r\n", Saver, Actual, /*MarkEOLs=*/false);
258 EXPECT_TRUE(Actual.empty());
261 TEST(CommandLineTest, TokenizeConfigFile10) {
262 const char *Input = "\\\nabc";
263 const char *const Output[] = { "abc" };
264 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
265 array_lengthof(Output));
268 TEST(CommandLineTest, TokenizeConfigFile11) {
269 const char *Input = "\\\r\nabc";
270 const char *const Output[] = { "abc" };
271 testCommandLineTokenizer(cl::tokenizeConfigFile, Input, Output,
272 array_lengthof(Output));
275 TEST(CommandLineTest, AliasesWithArguments) {
276 static const size_t ARGC = 3;
277 const char *const Inputs[][ARGC] = {
278 { "-tool", "-actual=x", "-extra" },
279 { "-tool", "-actual", "x" },
280 { "-tool", "-alias=x", "-extra" },
281 { "-tool", "-alias", "x" }
284 for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
285 StackOption<std::string> Actual("actual");
286 StackOption<bool> Extra("extra");
287 StackOption<std::string> Input(cl::Positional);
289 cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
291 cl::ParseCommandLineOptions(ARGC, Inputs[i]);
292 EXPECT_EQ("x", Actual);
293 EXPECT_EQ(0, Input.getNumOccurrences());
295 Alias.removeArgument();
299 void testAliasRequired(int argc, const char *const *argv) {
300 StackOption<std::string> Option("option", cl::Required);
301 cl::alias Alias("o", llvm::cl::aliasopt(Option));
303 cl::ParseCommandLineOptions(argc, argv);
304 EXPECT_EQ("x", Option);
305 EXPECT_EQ(1, Option.getNumOccurrences());
307 Alias.removeArgument();
310 TEST(CommandLineTest, AliasRequired) {
311 const char *opts1[] = { "-tool", "-option=x" };
312 const char *opts2[] = { "-tool", "-o", "x" };
313 testAliasRequired(array_lengthof(opts1), opts1);
314 testAliasRequired(array_lengthof(opts2), opts2);
317 TEST(CommandLineTest, HideUnrelatedOptions) {
318 StackOption<int> TestOption1("hide-option-1");
319 StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
321 cl::HideUnrelatedOptions(TestCategory);
323 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
324 << "Failed to hide extra option.";
325 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
326 << "Hid extra option that should be visable.";
328 StringMap<cl::Option *> &Map =
329 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
330 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
331 << "Hid default option that should be visable.";
334 cl::OptionCategory TestCategory2("Test Options set 2", "Description");
336 TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
337 StackOption<int> TestOption1("multi-hide-option-1");
338 StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory));
339 StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2));
341 const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
344 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
346 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
347 << "Failed to hide extra option.";
348 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
349 << "Hid extra option that should be visable.";
350 ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
351 << "Hid extra option that should be visable.";
353 StringMap<cl::Option *> &Map =
354 cl::getRegisteredOptions(*cl::TopLevelSubCommand);
355 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
356 << "Hid default option that should be visable.";
359 TEST(CommandLineTest, SetValueInSubcategories) {
360 cl::ResetCommandLineParser();
362 StackSubCommand SC1("sc1", "First subcommand");
363 StackSubCommand SC2("sc2", "Second subcommand");
365 StackOption<bool> TopLevelOpt("top-level", cl::init(false));
366 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
367 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
369 EXPECT_FALSE(TopLevelOpt);
370 EXPECT_FALSE(SC1Opt);
371 EXPECT_FALSE(SC2Opt);
372 const char *args[] = {"prog", "-top-level"};
374 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
375 EXPECT_TRUE(TopLevelOpt);
376 EXPECT_FALSE(SC1Opt);
377 EXPECT_FALSE(SC2Opt);
381 cl::ResetAllOptionOccurrences();
382 EXPECT_FALSE(TopLevelOpt);
383 EXPECT_FALSE(SC1Opt);
384 EXPECT_FALSE(SC2Opt);
385 const char *args2[] = {"prog", "sc1", "-sc1"};
387 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
388 EXPECT_FALSE(TopLevelOpt);
390 EXPECT_FALSE(SC2Opt);
394 cl::ResetAllOptionOccurrences();
395 EXPECT_FALSE(TopLevelOpt);
396 EXPECT_FALSE(SC1Opt);
397 EXPECT_FALSE(SC2Opt);
398 const char *args3[] = {"prog", "sc2", "-sc2"};
400 cl::ParseCommandLineOptions(3, args3, StringRef(), &llvm::nulls()));
401 EXPECT_FALSE(TopLevelOpt);
402 EXPECT_FALSE(SC1Opt);
406 TEST(CommandLineTest, LookupFailsInWrongSubCommand) {
407 cl::ResetCommandLineParser();
409 StackSubCommand SC1("sc1", "First subcommand");
410 StackSubCommand SC2("sc2", "Second subcommand");
412 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
413 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
416 raw_string_ostream OS(Errs);
418 const char *args[] = {"prog", "sc1", "-sc2"};
419 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
421 EXPECT_FALSE(Errs.empty());
424 TEST(CommandLineTest, AddToAllSubCommands) {
425 cl::ResetCommandLineParser();
427 StackSubCommand SC1("sc1", "First subcommand");
428 StackOption<bool> AllOpt("everywhere", cl::sub(*cl::AllSubCommands),
430 StackSubCommand SC2("sc2", "Second subcommand");
432 const char *args[] = {"prog", "-everywhere"};
433 const char *args2[] = {"prog", "sc1", "-everywhere"};
434 const char *args3[] = {"prog", "sc2", "-everywhere"};
437 raw_string_ostream OS(Errs);
439 EXPECT_FALSE(AllOpt);
440 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS));
445 cl::ResetAllOptionOccurrences();
446 EXPECT_FALSE(AllOpt);
447 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS));
452 cl::ResetAllOptionOccurrences();
453 EXPECT_FALSE(AllOpt);
454 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS));
457 // Since all parsing succeeded, the error message should be empty.
459 EXPECT_TRUE(Errs.empty());
462 TEST(CommandLineTest, ReparseCommandLineOptions) {
463 cl::ResetCommandLineParser();
465 StackOption<bool> TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand),
468 const char *args[] = {"prog", "-top-level"};
470 EXPECT_FALSE(TopLevelOpt);
472 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
473 EXPECT_TRUE(TopLevelOpt);
477 cl::ResetAllOptionOccurrences();
478 EXPECT_FALSE(TopLevelOpt);
480 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
481 EXPECT_TRUE(TopLevelOpt);
484 TEST(CommandLineTest, RemoveFromRegularSubCommand) {
485 cl::ResetCommandLineParser();
487 StackSubCommand SC("sc", "Subcommand");
488 StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false));
489 StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false));
491 const char *args[] = {"prog", "sc", "-remove-option"};
494 raw_string_ostream OS(Errs);
496 EXPECT_FALSE(RemoveOption);
497 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
498 EXPECT_TRUE(RemoveOption);
500 EXPECT_TRUE(Errs.empty());
502 RemoveOption.removeArgument();
504 cl::ResetAllOptionOccurrences();
505 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
507 EXPECT_FALSE(Errs.empty());
510 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
511 cl::ResetCommandLineParser();
513 StackOption<bool> TopLevelRemove(
514 "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
515 StackOption<bool> TopLevelKeep(
516 "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
518 const char *args[] = {"prog", "-top-level-remove"};
520 EXPECT_FALSE(TopLevelRemove);
522 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
523 EXPECT_TRUE(TopLevelRemove);
525 TopLevelRemove.removeArgument();
527 cl::ResetAllOptionOccurrences();
529 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
532 TEST(CommandLineTest, RemoveFromAllSubCommands) {
533 cl::ResetCommandLineParser();
535 StackSubCommand SC1("sc1", "First Subcommand");
536 StackSubCommand SC2("sc2", "Second Subcommand");
537 StackOption<bool> RemoveOption("remove-option", cl::sub(*cl::AllSubCommands),
539 StackOption<bool> KeepOption("keep-option", cl::sub(*cl::AllSubCommands),
542 const char *args0[] = {"prog", "-remove-option"};
543 const char *args1[] = {"prog", "sc1", "-remove-option"};
544 const char *args2[] = {"prog", "sc2", "-remove-option"};
546 // It should work for all subcommands including the top-level.
547 EXPECT_FALSE(RemoveOption);
549 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
550 EXPECT_TRUE(RemoveOption);
552 RemoveOption = false;
554 cl::ResetAllOptionOccurrences();
555 EXPECT_FALSE(RemoveOption);
557 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
558 EXPECT_TRUE(RemoveOption);
560 RemoveOption = false;
562 cl::ResetAllOptionOccurrences();
563 EXPECT_FALSE(RemoveOption);
565 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
566 EXPECT_TRUE(RemoveOption);
568 RemoveOption.removeArgument();
570 // It should not work for any subcommands including the top-level.
571 cl::ResetAllOptionOccurrences();
573 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
574 cl::ResetAllOptionOccurrences();
576 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
577 cl::ResetAllOptionOccurrences();
579 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
582 TEST(CommandLineTest, GetRegisteredSubcommands) {
583 cl::ResetCommandLineParser();
585 StackSubCommand SC1("sc1", "First Subcommand");
586 StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false));
587 StackSubCommand SC2("sc2", "Second subcommand");
588 StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
590 const char *args0[] = {"prog", "sc1"};
591 const char *args1[] = {"prog", "sc2"};
594 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
597 for (auto *S : cl::getRegisteredSubcommands()) {
599 EXPECT_EQ("sc1", S->getName());
603 cl::ResetAllOptionOccurrences();
605 cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls()));
608 for (auto *S : cl::getRegisteredSubcommands()) {
610 EXPECT_EQ("sc2", S->getName());
615 TEST(CommandLineTest, ArgumentLimit) {
616 std::string args(32 * 4096, 'a');
617 EXPECT_FALSE(llvm::sys::commandLineFitsWithinSystemLimits("cl", args.data()));
620 TEST(CommandLineTest, ResponseFileWindows) {
621 if (!Triple(sys::getProcessTriple()).isOSWindows())
624 StackOption<std::string, cl::list<std::string>> InputFilenames(
625 cl::Positional, cl::desc("<input files>"), cl::ZeroOrMore);
626 StackOption<bool> TopLevelOpt("top-level", cl::init(false));
628 // Create response file.
630 SmallString<64> TempPath;
632 llvm::sys::fs::createTemporaryFile("resp-", ".txt", FileDescriptor, TempPath);
635 std::ofstream RspFile(TempPath.c_str());
636 EXPECT_TRUE(RspFile.is_open());
637 RspFile << "-top-level\npath\\dir\\file1\npath/dir/file2";
640 llvm::SmallString<128> RspOpt;
641 RspOpt.append(1, '@');
642 RspOpt.append(TempPath.c_str());
643 const char *args[] = {"prog", RspOpt.c_str()};
644 EXPECT_FALSE(TopLevelOpt);
646 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
647 EXPECT_TRUE(TopLevelOpt);
648 EXPECT_TRUE(InputFilenames[0] == "path\\dir\\file1");
649 EXPECT_TRUE(InputFilenames[1] == "path/dir/file2");
651 llvm::sys::fs::remove(TempPath.c_str());
654 TEST(CommandLineTest, ResponseFiles) {
655 llvm::SmallString<128> TestDir;
657 llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
660 // Create included response file of first level.
661 llvm::SmallString<128> IncludedFileName;
662 llvm::sys::path::append(IncludedFileName, TestDir, "resp1");
663 std::ofstream IncludedFile(IncludedFileName.c_str());
664 EXPECT_TRUE(IncludedFile.is_open());
665 IncludedFile << "-option_1 -option_2\n"
668 IncludedFile.close();
670 // Directory for included file.
671 llvm::SmallString<128> IncDir;
672 llvm::sys::path::append(IncDir, TestDir, "incdir");
673 EC = llvm::sys::fs::create_directory(IncDir);
676 // Create included response file of second level.
677 llvm::SmallString<128> IncludedFileName2;
678 llvm::sys::path::append(IncludedFileName2, IncDir, "resp2");
679 std::ofstream IncludedFile2(IncludedFileName2.c_str());
680 EXPECT_TRUE(IncludedFile2.is_open());
681 IncludedFile2 << "-option_21 -option_22\n";
682 IncludedFile2 << "-option_23=abcd\n";
683 IncludedFile2.close();
685 // Prepare 'file' with reference to response file.
686 SmallString<128> IncRef;
687 IncRef.append(1, '@');
688 IncRef.append(IncludedFileName.c_str());
689 llvm::SmallVector<const char *, 4> Argv =
690 { "test/test", "-flag_1", IncRef.c_str(), "-flag_2" };
692 // Expand response files.
693 llvm::BumpPtrAllocator A;
694 llvm::StringSaver Saver(A);
695 bool Res = llvm::cl::ExpandResponseFiles(
696 Saver, llvm::cl::TokenizeGNUCommandLine, Argv, false, true);
698 EXPECT_EQ(Argv.size(), 9U);
699 EXPECT_STREQ(Argv[0], "test/test");
700 EXPECT_STREQ(Argv[1], "-flag_1");
701 EXPECT_STREQ(Argv[2], "-option_1");
702 EXPECT_STREQ(Argv[3], "-option_2");
703 EXPECT_STREQ(Argv[4], "-option_21");
704 EXPECT_STREQ(Argv[5], "-option_22");
705 EXPECT_STREQ(Argv[6], "-option_23=abcd");
706 EXPECT_STREQ(Argv[7], "-option_3=abcd");
707 EXPECT_STREQ(Argv[8], "-flag_2");
709 llvm::sys::fs::remove(IncludedFileName2);
710 llvm::sys::fs::remove(IncDir);
711 llvm::sys::fs::remove(IncludedFileName);
712 llvm::sys::fs::remove(TestDir);
715 TEST(CommandLineTest, SetDefautValue) {
716 cl::ResetCommandLineParser();
718 StackOption<std::string> Opt1("opt1", cl::init("true"));
719 StackOption<bool> Opt2("opt2", cl::init(true));
720 cl::alias Alias("alias", llvm::cl::aliasopt(Opt2));
721 StackOption<int> Opt3("opt3", cl::init(3));
723 const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"};
726 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
728 EXPECT_TRUE(Opt1 == "false");
730 EXPECT_TRUE(Opt3 == 3);
735 cl::ResetAllOptionOccurrences();
737 for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) {
738 cl::Option *O = OM.second;
739 if (O->ArgStr == "opt2") {
745 EXPECT_TRUE(Opt1 == "true");
747 EXPECT_TRUE(Opt3 == 3);
748 Alias.removeArgument();
751 TEST(CommandLineTest, ReadConfigFile) {
752 llvm::SmallVector<const char *, 1> Argv;
754 llvm::SmallString<128> TestDir;
756 llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
759 llvm::SmallString<128> TestCfg;
760 llvm::sys::path::append(TestCfg, TestDir, "foo");
761 std::ofstream ConfigFile(TestCfg.c_str());
762 EXPECT_TRUE(ConfigFile.is_open());
763 ConfigFile << "# Comment\n"
771 llvm::SmallString<128> TestCfg2;
772 llvm::sys::path::append(TestCfg2, TestDir, "subconfig");
773 std::ofstream ConfigFile2(TestCfg2.c_str());
774 EXPECT_TRUE(ConfigFile2.is_open());
775 ConfigFile2 << "-option_2\n"
780 // Make sure the current directory is not the directory where config files
781 // resides. In this case the code that expands response files will not find
782 // 'subconfig' unless it resolves nested inclusions relative to the including
784 llvm::SmallString<128> CurrDir;
785 EC = llvm::sys::fs::current_path(CurrDir);
787 EXPECT_TRUE(StringRef(CurrDir) != StringRef(TestDir));
789 llvm::BumpPtrAllocator A;
790 llvm::StringSaver Saver(A);
791 bool Result = llvm::cl::readConfigFile(TestCfg, Saver, Argv);
794 EXPECT_EQ(Argv.size(), 4U);
795 EXPECT_STREQ(Argv[0], "-option_1");
796 EXPECT_STREQ(Argv[1], "-option_2");
797 EXPECT_STREQ(Argv[2], "-option_3=abcd");
798 EXPECT_STREQ(Argv[3], "-option_4=cdef");
800 llvm::sys::fs::remove(TestCfg2);
801 llvm::sys::fs::remove(TestCfg);
802 llvm::sys::fs::remove(TestDir);
805 TEST(CommandLineTest, PositionalEatArgsError) {
806 StackOption<std::string, cl::list<std::string>> PosEatArgs(
807 "positional-eat-args", cl::Positional, cl::desc("<arguments>..."),
808 cl::ZeroOrMore, cl::PositionalEatsArgs);
810 const char *args[] = {"prog", "-positional-eat-args=XXXX"};
811 const char *args2[] = {"prog", "-positional-eat-args=XXXX", "-foo"};
812 const char *args3[] = {"prog", "-positional-eat-args", "-foo"};
815 raw_string_ostream OS(Errs);
816 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS)); OS.flush();
817 EXPECT_FALSE(Errs.empty()); Errs.clear();
818 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS)); OS.flush();
819 EXPECT_FALSE(Errs.empty()); Errs.clear();
820 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS)); OS.flush();
821 EXPECT_TRUE(Errs.empty());
824 } // anonymous namespace