1 //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/CommandLine.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "llvm/ADT/SmallString.h" 13 #include "llvm/Config/config.h" 14 #include "llvm/Support/FileSystem.h" 15 #include "llvm/Support/Path.h" 16 #include "llvm/Support/Program.h" 17 #include "llvm/Support/StringSaver.h" 18 #include "gtest/gtest.h" 19 #include <fstream> 20 #include <stdlib.h> 21 #include <string> 22 23 using namespace llvm; 24 25 namespace { 26 27 class TempEnvVar { 28 public: 29 TempEnvVar(const char *name, const char *value) 30 : name(name) { 31 const char *old_value = getenv(name); 32 EXPECT_EQ(nullptr, old_value) << old_value; 33 #if HAVE_SETENV 34 setenv(name, value, true); 35 #else 36 # define SKIP_ENVIRONMENT_TESTS 37 #endif 38 } 39 40 ~TempEnvVar() { 41 #if HAVE_SETENV 42 // Assume setenv and unsetenv come together. 43 unsetenv(name); 44 #else 45 (void)name; // Suppress -Wunused-private-field. 46 #endif 47 } 48 49 private: 50 const char *const name; 51 }; 52 53 template <typename T> 54 class StackOption : public cl::opt<T> { 55 typedef cl::opt<T> Base; 56 public: 57 // One option... 58 template<class M0t> 59 explicit StackOption(const M0t &M0) : Base(M0) {} 60 61 // Two options... 62 template<class M0t, class M1t> 63 StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {} 64 65 // Three options... 66 template<class M0t, class M1t, class M2t> 67 StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {} 68 69 // Four options... 70 template<class M0t, class M1t, class M2t, class M3t> 71 StackOption(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3) 72 : Base(M0, M1, M2, M3) {} 73 74 ~StackOption() override { this->removeArgument(); } 75 76 template <class DT> StackOption<T> &operator=(const DT &V) { 77 this->setValue(V); 78 return *this; 79 } 80 }; 81 82 class StackSubCommand : public cl::SubCommand { 83 public: 84 StackSubCommand(StringRef Name, 85 StringRef Description = StringRef()) 86 : SubCommand(Name, Description) {} 87 88 StackSubCommand() : SubCommand() {} 89 90 ~StackSubCommand() { unregisterSubCommand(); } 91 }; 92 93 94 cl::OptionCategory TestCategory("Test Options", "Description"); 95 TEST(CommandLineTest, ModifyExisitingOption) { 96 StackOption<int> TestOption("test-option", cl::desc("old description")); 97 98 const char Description[] = "New description"; 99 const char ArgString[] = "new-test-option"; 100 const char ValueString[] = "Integer"; 101 102 StringMap<cl::Option *> &Map = 103 cl::getRegisteredOptions(*cl::TopLevelSubCommand); 104 105 ASSERT_TRUE(Map.count("test-option") == 1) << 106 "Could not find option in map."; 107 108 cl::Option *Retrieved = Map["test-option"]; 109 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option."; 110 111 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) << 112 "Incorrect default option category."; 113 114 Retrieved->setCategory(TestCategory); 115 ASSERT_EQ(&TestCategory,Retrieved->Category) << 116 "Failed to modify option's option category."; 117 118 Retrieved->setDescription(Description); 119 ASSERT_STREQ(Retrieved->HelpStr.data(), Description) 120 << "Changing option description failed."; 121 122 Retrieved->setArgStr(ArgString); 123 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data()) 124 << "Failed to modify option's Argument string."; 125 126 Retrieved->setValueStr(ValueString); 127 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString) 128 << "Failed to modify option's Value string."; 129 130 Retrieved->setHiddenFlag(cl::Hidden); 131 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) << 132 "Failed to modify option's hidden flag."; 133 } 134 #ifndef SKIP_ENVIRONMENT_TESTS 135 136 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS"; 137 138 cl::opt<std::string> EnvironmentTestOption("env-test-opt"); 139 TEST(CommandLineTest, ParseEnvironment) { 140 TempEnvVar TEV(test_env_var, "-env-test-opt=hello"); 141 EXPECT_EQ("", EnvironmentTestOption); 142 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var); 143 EXPECT_EQ("hello", EnvironmentTestOption); 144 } 145 146 // This test used to make valgrind complain 147 // ("Conditional jump or move depends on uninitialised value(s)") 148 // 149 // Warning: Do not run any tests after this one that try to gain access to 150 // registered command line options because this will likely result in a 151 // SEGFAULT. This can occur because the cl::opt in the test below is declared 152 // on the stack which will be destroyed after the test completes but the 153 // command line system will still hold a pointer to a deallocated cl::Option. 154 TEST(CommandLineTest, ParseEnvironmentToLocalVar) { 155 // Put cl::opt on stack to check for proper initialization of fields. 156 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local"); 157 TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local"); 158 EXPECT_EQ("", EnvironmentTestOptionLocal); 159 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var); 160 EXPECT_EQ("hello-local", EnvironmentTestOptionLocal); 161 } 162 163 #endif // SKIP_ENVIRONMENT_TESTS 164 165 TEST(CommandLineTest, UseOptionCategory) { 166 StackOption<int> TestOption2("test-option", cl::cat(TestCategory)); 167 168 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option " 169 "Category."; 170 } 171 172 typedef void ParserFunction(StringRef Source, StringSaver &Saver, 173 SmallVectorImpl<const char *> &NewArgv, 174 bool MarkEOLs); 175 176 void testCommandLineTokenizer(ParserFunction *parse, StringRef Input, 177 const char *const Output[], size_t OutputSize) { 178 SmallVector<const char *, 0> Actual; 179 BumpPtrAllocator A; 180 StringSaver Saver(A); 181 parse(Input, Saver, Actual, /*MarkEOLs=*/false); 182 EXPECT_EQ(OutputSize, Actual.size()); 183 for (unsigned I = 0, E = Actual.size(); I != E; ++I) { 184 if (I < OutputSize) { 185 EXPECT_STREQ(Output[I], Actual[I]); 186 } 187 } 188 } 189 190 TEST(CommandLineTest, TokenizeGNUCommandLine) { 191 const char Input[] = 192 "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) " 193 "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\""; 194 const char *const Output[] = { 195 "foo bar", "foo bar", "foo bar", "foo\\bar", 196 "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"}; 197 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output, 198 array_lengthof(Output)); 199 } 200 201 TEST(CommandLineTest, TokenizeWindowsCommandLine) { 202 const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr " 203 "\"st \\\"u\" \\v"; 204 const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k", 205 "lmn", "o", "pqr", "st \"u", "\\v" }; 206 testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output, 207 array_lengthof(Output)); 208 } 209 210 TEST(CommandLineTest, AliasesWithArguments) { 211 static const size_t ARGC = 3; 212 const char *const Inputs[][ARGC] = { 213 { "-tool", "-actual=x", "-extra" }, 214 { "-tool", "-actual", "x" }, 215 { "-tool", "-alias=x", "-extra" }, 216 { "-tool", "-alias", "x" } 217 }; 218 219 for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) { 220 StackOption<std::string> Actual("actual"); 221 StackOption<bool> Extra("extra"); 222 StackOption<std::string> Input(cl::Positional); 223 224 cl::alias Alias("alias", llvm::cl::aliasopt(Actual)); 225 226 cl::ParseCommandLineOptions(ARGC, Inputs[i]); 227 EXPECT_EQ("x", Actual); 228 EXPECT_EQ(0, Input.getNumOccurrences()); 229 230 Alias.removeArgument(); 231 } 232 } 233 234 void testAliasRequired(int argc, const char *const *argv) { 235 StackOption<std::string> Option("option", cl::Required); 236 cl::alias Alias("o", llvm::cl::aliasopt(Option)); 237 238 cl::ParseCommandLineOptions(argc, argv); 239 EXPECT_EQ("x", Option); 240 EXPECT_EQ(1, Option.getNumOccurrences()); 241 242 Alias.removeArgument(); 243 } 244 245 TEST(CommandLineTest, AliasRequired) { 246 const char *opts1[] = { "-tool", "-option=x" }; 247 const char *opts2[] = { "-tool", "-o", "x" }; 248 testAliasRequired(array_lengthof(opts1), opts1); 249 testAliasRequired(array_lengthof(opts2), opts2); 250 } 251 252 TEST(CommandLineTest, HideUnrelatedOptions) { 253 StackOption<int> TestOption1("hide-option-1"); 254 StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory)); 255 256 cl::HideUnrelatedOptions(TestCategory); 257 258 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag()) 259 << "Failed to hide extra option."; 260 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) 261 << "Hid extra option that should be visable."; 262 263 StringMap<cl::Option *> &Map = 264 cl::getRegisteredOptions(*cl::TopLevelSubCommand); 265 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) 266 << "Hid default option that should be visable."; 267 } 268 269 cl::OptionCategory TestCategory2("Test Options set 2", "Description"); 270 271 TEST(CommandLineTest, HideUnrelatedOptionsMulti) { 272 StackOption<int> TestOption1("multi-hide-option-1"); 273 StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory)); 274 StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2)); 275 276 const cl::OptionCategory *VisibleCategories[] = {&TestCategory, 277 &TestCategory2}; 278 279 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories)); 280 281 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag()) 282 << "Failed to hide extra option."; 283 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) 284 << "Hid extra option that should be visable."; 285 ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag()) 286 << "Hid extra option that should be visable."; 287 288 StringMap<cl::Option *> &Map = 289 cl::getRegisteredOptions(*cl::TopLevelSubCommand); 290 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) 291 << "Hid default option that should be visable."; 292 } 293 294 TEST(CommandLineTest, SetValueInSubcategories) { 295 cl::ResetCommandLineParser(); 296 297 StackSubCommand SC1("sc1", "First subcommand"); 298 StackSubCommand SC2("sc2", "Second subcommand"); 299 300 StackOption<bool> TopLevelOpt("top-level", cl::init(false)); 301 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false)); 302 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false)); 303 304 EXPECT_FALSE(TopLevelOpt); 305 EXPECT_FALSE(SC1Opt); 306 EXPECT_FALSE(SC2Opt); 307 const char *args[] = {"prog", "-top-level"}; 308 EXPECT_TRUE( 309 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls())); 310 EXPECT_TRUE(TopLevelOpt); 311 EXPECT_FALSE(SC1Opt); 312 EXPECT_FALSE(SC2Opt); 313 314 TopLevelOpt = false; 315 316 cl::ResetAllOptionOccurrences(); 317 EXPECT_FALSE(TopLevelOpt); 318 EXPECT_FALSE(SC1Opt); 319 EXPECT_FALSE(SC2Opt); 320 const char *args2[] = {"prog", "sc1", "-sc1"}; 321 EXPECT_TRUE( 322 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls())); 323 EXPECT_FALSE(TopLevelOpt); 324 EXPECT_TRUE(SC1Opt); 325 EXPECT_FALSE(SC2Opt); 326 327 SC1Opt = false; 328 329 cl::ResetAllOptionOccurrences(); 330 EXPECT_FALSE(TopLevelOpt); 331 EXPECT_FALSE(SC1Opt); 332 EXPECT_FALSE(SC2Opt); 333 const char *args3[] = {"prog", "sc2", "-sc2"}; 334 EXPECT_TRUE( 335 cl::ParseCommandLineOptions(3, args3, StringRef(), &llvm::nulls())); 336 EXPECT_FALSE(TopLevelOpt); 337 EXPECT_FALSE(SC1Opt); 338 EXPECT_TRUE(SC2Opt); 339 } 340 341 TEST(CommandLineTest, LookupFailsInWrongSubCommand) { 342 cl::ResetCommandLineParser(); 343 344 StackSubCommand SC1("sc1", "First subcommand"); 345 StackSubCommand SC2("sc2", "Second subcommand"); 346 347 StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false)); 348 StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false)); 349 350 std::string Errs; 351 raw_string_ostream OS(Errs); 352 353 const char *args[] = {"prog", "sc1", "-sc2"}; 354 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS)); 355 OS.flush(); 356 EXPECT_FALSE(Errs.empty()); 357 } 358 359 TEST(CommandLineTest, AddToAllSubCommands) { 360 cl::ResetCommandLineParser(); 361 362 StackSubCommand SC1("sc1", "First subcommand"); 363 StackOption<bool> AllOpt("everywhere", cl::sub(*cl::AllSubCommands), 364 cl::init(false)); 365 StackSubCommand SC2("sc2", "Second subcommand"); 366 367 const char *args[] = {"prog", "-everywhere"}; 368 const char *args2[] = {"prog", "sc1", "-everywhere"}; 369 const char *args3[] = {"prog", "sc2", "-everywhere"}; 370 371 std::string Errs; 372 raw_string_ostream OS(Errs); 373 374 EXPECT_FALSE(AllOpt); 375 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS)); 376 EXPECT_TRUE(AllOpt); 377 378 AllOpt = false; 379 380 cl::ResetAllOptionOccurrences(); 381 EXPECT_FALSE(AllOpt); 382 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS)); 383 EXPECT_TRUE(AllOpt); 384 385 AllOpt = false; 386 387 cl::ResetAllOptionOccurrences(); 388 EXPECT_FALSE(AllOpt); 389 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS)); 390 EXPECT_TRUE(AllOpt); 391 392 // Since all parsing succeeded, the error message should be empty. 393 OS.flush(); 394 EXPECT_TRUE(Errs.empty()); 395 } 396 397 TEST(CommandLineTest, ReparseCommandLineOptions) { 398 cl::ResetCommandLineParser(); 399 400 StackOption<bool> TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand), 401 cl::init(false)); 402 403 const char *args[] = {"prog", "-top-level"}; 404 405 EXPECT_FALSE(TopLevelOpt); 406 EXPECT_TRUE( 407 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls())); 408 EXPECT_TRUE(TopLevelOpt); 409 410 TopLevelOpt = false; 411 412 cl::ResetAllOptionOccurrences(); 413 EXPECT_FALSE(TopLevelOpt); 414 EXPECT_TRUE( 415 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls())); 416 EXPECT_TRUE(TopLevelOpt); 417 } 418 419 TEST(CommandLineTest, RemoveFromRegularSubCommand) { 420 cl::ResetCommandLineParser(); 421 422 StackSubCommand SC("sc", "Subcommand"); 423 StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false)); 424 StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false)); 425 426 const char *args[] = {"prog", "sc", "-remove-option"}; 427 428 std::string Errs; 429 raw_string_ostream OS(Errs); 430 431 EXPECT_FALSE(RemoveOption); 432 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS)); 433 EXPECT_TRUE(RemoveOption); 434 OS.flush(); 435 EXPECT_TRUE(Errs.empty()); 436 437 RemoveOption.removeArgument(); 438 439 cl::ResetAllOptionOccurrences(); 440 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS)); 441 OS.flush(); 442 EXPECT_FALSE(Errs.empty()); 443 } 444 445 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) { 446 cl::ResetCommandLineParser(); 447 448 StackOption<bool> TopLevelRemove( 449 "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false)); 450 StackOption<bool> TopLevelKeep( 451 "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false)); 452 453 const char *args[] = {"prog", "-top-level-remove"}; 454 455 EXPECT_FALSE(TopLevelRemove); 456 EXPECT_TRUE( 457 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls())); 458 EXPECT_TRUE(TopLevelRemove); 459 460 TopLevelRemove.removeArgument(); 461 462 cl::ResetAllOptionOccurrences(); 463 EXPECT_FALSE( 464 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls())); 465 } 466 467 TEST(CommandLineTest, RemoveFromAllSubCommands) { 468 cl::ResetCommandLineParser(); 469 470 StackSubCommand SC1("sc1", "First Subcommand"); 471 StackSubCommand SC2("sc2", "Second Subcommand"); 472 StackOption<bool> RemoveOption("remove-option", cl::sub(*cl::AllSubCommands), 473 cl::init(false)); 474 StackOption<bool> KeepOption("keep-option", cl::sub(*cl::AllSubCommands), 475 cl::init(false)); 476 477 const char *args0[] = {"prog", "-remove-option"}; 478 const char *args1[] = {"prog", "sc1", "-remove-option"}; 479 const char *args2[] = {"prog", "sc2", "-remove-option"}; 480 481 // It should work for all subcommands including the top-level. 482 EXPECT_FALSE(RemoveOption); 483 EXPECT_TRUE( 484 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls())); 485 EXPECT_TRUE(RemoveOption); 486 487 RemoveOption = false; 488 489 cl::ResetAllOptionOccurrences(); 490 EXPECT_FALSE(RemoveOption); 491 EXPECT_TRUE( 492 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls())); 493 EXPECT_TRUE(RemoveOption); 494 495 RemoveOption = false; 496 497 cl::ResetAllOptionOccurrences(); 498 EXPECT_FALSE(RemoveOption); 499 EXPECT_TRUE( 500 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls())); 501 EXPECT_TRUE(RemoveOption); 502 503 RemoveOption.removeArgument(); 504 505 // It should not work for any subcommands including the top-level. 506 cl::ResetAllOptionOccurrences(); 507 EXPECT_FALSE( 508 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls())); 509 cl::ResetAllOptionOccurrences(); 510 EXPECT_FALSE( 511 cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls())); 512 cl::ResetAllOptionOccurrences(); 513 EXPECT_FALSE( 514 cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls())); 515 } 516 517 TEST(CommandLineTest, GetRegisteredSubcommands) { 518 cl::ResetCommandLineParser(); 519 520 StackSubCommand SC1("sc1", "First Subcommand"); 521 StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false)); 522 StackSubCommand SC2("sc2", "Second subcommand"); 523 StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false)); 524 525 const char *args0[] = {"prog", "sc1"}; 526 const char *args1[] = {"prog", "sc2"}; 527 528 EXPECT_TRUE( 529 cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls())); 530 EXPECT_FALSE(Opt1); 531 EXPECT_FALSE(Opt2); 532 for (auto *S : cl::getRegisteredSubcommands()) { 533 if (*S) { 534 EXPECT_EQ("sc1", S->getName()); 535 } 536 } 537 538 cl::ResetAllOptionOccurrences(); 539 EXPECT_TRUE( 540 cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls())); 541 EXPECT_FALSE(Opt1); 542 EXPECT_FALSE(Opt2); 543 for (auto *S : cl::getRegisteredSubcommands()) { 544 if (*S) { 545 EXPECT_EQ("sc2", S->getName()); 546 } 547 } 548 } 549 550 TEST(CommandLineTest, ArgumentLimit) { 551 std::string args(32 * 4096, 'a'); 552 EXPECT_FALSE(llvm::sys::commandLineFitsWithinSystemLimits("cl", args.data())); 553 } 554 555 TEST(CommandLineTest, ResponseFiles) { 556 llvm::SmallString<128> TestDir; 557 std::error_code EC = 558 llvm::sys::fs::createUniqueDirectory("unittest", TestDir); 559 EXPECT_TRUE(!EC); 560 561 // Create included response file of first level. 562 llvm::SmallString<128> IncludedFileName; 563 llvm::sys::path::append(IncludedFileName, TestDir, "resp1"); 564 std::ofstream IncludedFile(IncludedFileName.c_str()); 565 EXPECT_TRUE(IncludedFile.is_open()); 566 IncludedFile << "-option_1 -option_2\n" 567 "@incdir/resp2\n" 568 "-option_3=abcd\n"; 569 IncludedFile.close(); 570 571 // Directory for included file. 572 llvm::SmallString<128> IncDir; 573 llvm::sys::path::append(IncDir, TestDir, "incdir"); 574 EC = llvm::sys::fs::create_directory(IncDir); 575 EXPECT_TRUE(!EC); 576 577 // Create included response file of second level. 578 llvm::SmallString<128> IncludedFileName2; 579 llvm::sys::path::append(IncludedFileName2, IncDir, "resp2"); 580 std::ofstream IncludedFile2(IncludedFileName2.c_str()); 581 EXPECT_TRUE(IncludedFile2.is_open()); 582 IncludedFile2 << "-option_21 -option_22\n"; 583 IncludedFile2 << "-option_23=abcd\n"; 584 IncludedFile2.close(); 585 586 // Prepare 'file' with reference to response file. 587 SmallString<128> IncRef; 588 IncRef.append(1, '@'); 589 IncRef.append(IncludedFileName.c_str()); 590 llvm::SmallVector<const char *, 4> Argv = 591 { "test/test", "-flag_1", IncRef.c_str(), "-flag_2" }; 592 593 // Expand response files. 594 llvm::BumpPtrAllocator A; 595 llvm::StringSaver Saver(A); 596 bool Res = llvm::cl::ExpandResponseFiles( 597 Saver, llvm::cl::TokenizeGNUCommandLine, Argv, false, true); 598 EXPECT_TRUE(Res); 599 EXPECT_EQ(Argv.size(), 9U); 600 EXPECT_STREQ(Argv[0], "test/test"); 601 EXPECT_STREQ(Argv[1], "-flag_1"); 602 EXPECT_STREQ(Argv[2], "-option_1"); 603 EXPECT_STREQ(Argv[3], "-option_2"); 604 EXPECT_STREQ(Argv[4], "-option_21"); 605 EXPECT_STREQ(Argv[5], "-option_22"); 606 EXPECT_STREQ(Argv[6], "-option_23=abcd"); 607 EXPECT_STREQ(Argv[7], "-option_3=abcd"); 608 EXPECT_STREQ(Argv[8], "-flag_2"); 609 610 llvm::sys::fs::remove(IncludedFileName2); 611 llvm::sys::fs::remove(IncDir); 612 llvm::sys::fs::remove(IncludedFileName); 613 llvm::sys::fs::remove(TestDir); 614 } 615 616 TEST(CommandLineTest, SetDefautValue) { 617 cl::ResetCommandLineParser(); 618 619 StackOption<std::string> Opt1("opt1", cl::init("true")); 620 StackOption<bool> Opt2("opt2", cl::init(true)); 621 cl::alias Alias("alias", llvm::cl::aliasopt(Opt2)); 622 StackOption<int> Opt3("opt3", cl::init(3)); 623 624 const char *args[] = {"prog", "-opt1=false", "-opt2", "-opt3"}; 625 626 EXPECT_TRUE( 627 cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls())); 628 629 EXPECT_TRUE(Opt1 == "false"); 630 EXPECT_TRUE(Opt2); 631 EXPECT_TRUE(Opt3 == 3); 632 633 Opt2 = false; 634 Opt3 = 1; 635 636 cl::ResetAllOptionOccurrences(); 637 638 for (auto &OM : cl::getRegisteredOptions(*cl::TopLevelSubCommand)) { 639 cl::Option *O = OM.second; 640 if (O->ArgStr == "opt2") { 641 continue; 642 } 643 O->setDefault(); 644 } 645 646 EXPECT_TRUE(Opt1 == "true"); 647 EXPECT_TRUE(Opt2); 648 EXPECT_TRUE(Opt3 == 3); 649 } 650 651 } // anonymous namespace 652