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