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/ADT/SmallString.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Config/config.h"
13 #include "llvm/Support/CommandLine.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 TEST(CommandLineTest, TokenizeGNUCommandLine) {
189   const char Input[] =
190       "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
191       "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
192   const char *const Output[] = {
193       "foo bar",     "foo bar",   "foo bar",          "foo\\bar",
194       "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"};
195   testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
196                            array_lengthof(Output));
197 }
198 
199 TEST(CommandLineTest, TokenizeWindowsCommandLine) {
200   const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
201                       "\"st \\\"u\" \\v";
202   const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
203                                  "lmn", "o", "pqr", "st \"u", "\\v" };
204   testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
205                            array_lengthof(Output));
206 }
207 
208 TEST(CommandLineTest, AliasesWithArguments) {
209   static const size_t ARGC = 3;
210   const char *const Inputs[][ARGC] = {
211     { "-tool", "-actual=x", "-extra" },
212     { "-tool", "-actual", "x" },
213     { "-tool", "-alias=x", "-extra" },
214     { "-tool", "-alias", "x" }
215   };
216 
217   for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
218     StackOption<std::string> Actual("actual");
219     StackOption<bool> Extra("extra");
220     StackOption<std::string> Input(cl::Positional);
221 
222     cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
223 
224     cl::ParseCommandLineOptions(ARGC, Inputs[i]);
225     EXPECT_EQ("x", Actual);
226     EXPECT_EQ(0, Input.getNumOccurrences());
227 
228     Alias.removeArgument();
229   }
230 }
231 
232 void testAliasRequired(int argc, const char *const *argv) {
233   StackOption<std::string> Option("option", cl::Required);
234   cl::alias Alias("o", llvm::cl::aliasopt(Option));
235 
236   cl::ParseCommandLineOptions(argc, argv);
237   EXPECT_EQ("x", Option);
238   EXPECT_EQ(1, Option.getNumOccurrences());
239 
240   Alias.removeArgument();
241 }
242 
243 TEST(CommandLineTest, AliasRequired) {
244   const char *opts1[] = { "-tool", "-option=x" };
245   const char *opts2[] = { "-tool", "-o", "x" };
246   testAliasRequired(array_lengthof(opts1), opts1);
247   testAliasRequired(array_lengthof(opts2), opts2);
248 }
249 
250 TEST(CommandLineTest, HideUnrelatedOptions) {
251   StackOption<int> TestOption1("hide-option-1");
252   StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
253 
254   cl::HideUnrelatedOptions(TestCategory);
255 
256   ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
257       << "Failed to hide extra option.";
258   ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
259       << "Hid extra option that should be visable.";
260 
261   StringMap<cl::Option *> &Map =
262       cl::getRegisteredOptions(*cl::TopLevelSubCommand);
263   ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
264       << "Hid default option that should be visable.";
265 }
266 
267 cl::OptionCategory TestCategory2("Test Options set 2", "Description");
268 
269 TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
270   StackOption<int> TestOption1("multi-hide-option-1");
271   StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory));
272   StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2));
273 
274   const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
275                                                    &TestCategory2};
276 
277   cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
278 
279   ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
280       << "Failed to hide extra option.";
281   ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
282       << "Hid extra option that should be visable.";
283   ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
284       << "Hid extra option that should be visable.";
285 
286   StringMap<cl::Option *> &Map =
287       cl::getRegisteredOptions(*cl::TopLevelSubCommand);
288   ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
289       << "Hid default option that should be visable.";
290 }
291 
292 TEST(CommandLineTest, SetValueInSubcategories) {
293   cl::ResetCommandLineParser();
294 
295   StackSubCommand SC1("sc1", "First subcommand");
296   StackSubCommand SC2("sc2", "Second subcommand");
297 
298   StackOption<bool> TopLevelOpt("top-level", cl::init(false));
299   StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
300   StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
301 
302   EXPECT_FALSE(TopLevelOpt);
303   EXPECT_FALSE(SC1Opt);
304   EXPECT_FALSE(SC2Opt);
305   const char *args[] = {"prog", "-top-level"};
306   EXPECT_TRUE(
307       cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
308   EXPECT_TRUE(TopLevelOpt);
309   EXPECT_FALSE(SC1Opt);
310   EXPECT_FALSE(SC2Opt);
311 
312   TopLevelOpt = false;
313 
314   cl::ResetAllOptionOccurrences();
315   EXPECT_FALSE(TopLevelOpt);
316   EXPECT_FALSE(SC1Opt);
317   EXPECT_FALSE(SC2Opt);
318   const char *args2[] = {"prog", "sc1", "-sc1"};
319   EXPECT_TRUE(
320       cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
321   EXPECT_FALSE(TopLevelOpt);
322   EXPECT_TRUE(SC1Opt);
323   EXPECT_FALSE(SC2Opt);
324 
325   SC1Opt = false;
326 
327   cl::ResetAllOptionOccurrences();
328   EXPECT_FALSE(TopLevelOpt);
329   EXPECT_FALSE(SC1Opt);
330   EXPECT_FALSE(SC2Opt);
331   const char *args3[] = {"prog", "sc2", "-sc2"};
332   EXPECT_TRUE(
333       cl::ParseCommandLineOptions(3, args3, StringRef(), &llvm::nulls()));
334   EXPECT_FALSE(TopLevelOpt);
335   EXPECT_FALSE(SC1Opt);
336   EXPECT_TRUE(SC2Opt);
337 }
338 
339 TEST(CommandLineTest, LookupFailsInWrongSubCommand) {
340   cl::ResetCommandLineParser();
341 
342   StackSubCommand SC1("sc1", "First subcommand");
343   StackSubCommand SC2("sc2", "Second subcommand");
344 
345   StackOption<bool> SC1Opt("sc1", cl::sub(SC1), cl::init(false));
346   StackOption<bool> SC2Opt("sc2", cl::sub(SC2), cl::init(false));
347 
348   std::string Errs;
349   raw_string_ostream OS(Errs);
350 
351   const char *args[] = {"prog", "sc1", "-sc2"};
352   EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
353   OS.flush();
354   EXPECT_FALSE(Errs.empty());
355 }
356 
357 TEST(CommandLineTest, AddToAllSubCommands) {
358   cl::ResetCommandLineParser();
359 
360   StackSubCommand SC1("sc1", "First subcommand");
361   StackOption<bool> AllOpt("everywhere", cl::sub(*cl::AllSubCommands),
362                            cl::init(false));
363   StackSubCommand SC2("sc2", "Second subcommand");
364 
365   const char *args[] = {"prog", "-everywhere"};
366   const char *args2[] = {"prog", "sc1", "-everywhere"};
367   const char *args3[] = {"prog", "sc2", "-everywhere"};
368 
369   std::string Errs;
370   raw_string_ostream OS(Errs);
371 
372   EXPECT_FALSE(AllOpt);
373   EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), &OS));
374   EXPECT_TRUE(AllOpt);
375 
376   AllOpt = false;
377 
378   cl::ResetAllOptionOccurrences();
379   EXPECT_FALSE(AllOpt);
380   EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), &OS));
381   EXPECT_TRUE(AllOpt);
382 
383   AllOpt = false;
384 
385   cl::ResetAllOptionOccurrences();
386   EXPECT_FALSE(AllOpt);
387   EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), &OS));
388   EXPECT_TRUE(AllOpt);
389 
390   // Since all parsing succeeded, the error message should be empty.
391   OS.flush();
392   EXPECT_TRUE(Errs.empty());
393 }
394 
395 TEST(CommandLineTest, ReparseCommandLineOptions) {
396   cl::ResetCommandLineParser();
397 
398   StackOption<bool> TopLevelOpt("top-level", cl::sub(*cl::TopLevelSubCommand),
399                                 cl::init(false));
400 
401   const char *args[] = {"prog", "-top-level"};
402 
403   EXPECT_FALSE(TopLevelOpt);
404   EXPECT_TRUE(
405       cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
406   EXPECT_TRUE(TopLevelOpt);
407 
408   TopLevelOpt = false;
409 
410   cl::ResetAllOptionOccurrences();
411   EXPECT_FALSE(TopLevelOpt);
412   EXPECT_TRUE(
413       cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
414   EXPECT_TRUE(TopLevelOpt);
415 }
416 
417 TEST(CommandLineTest, RemoveFromRegularSubCommand) {
418   cl::ResetCommandLineParser();
419 
420   StackSubCommand SC("sc", "Subcommand");
421   StackOption<bool> RemoveOption("remove-option", cl::sub(SC), cl::init(false));
422   StackOption<bool> KeepOption("keep-option", cl::sub(SC), cl::init(false));
423 
424   const char *args[] = {"prog", "sc", "-remove-option"};
425 
426   std::string Errs;
427   raw_string_ostream OS(Errs);
428 
429   EXPECT_FALSE(RemoveOption);
430   EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
431   EXPECT_TRUE(RemoveOption);
432   OS.flush();
433   EXPECT_TRUE(Errs.empty());
434 
435   RemoveOption.removeArgument();
436 
437   cl::ResetAllOptionOccurrences();
438   EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), &OS));
439   OS.flush();
440   EXPECT_FALSE(Errs.empty());
441 }
442 
443 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
444   cl::ResetCommandLineParser();
445 
446   StackOption<bool> TopLevelRemove(
447       "top-level-remove", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
448   StackOption<bool> TopLevelKeep(
449       "top-level-keep", cl::sub(*cl::TopLevelSubCommand), cl::init(false));
450 
451   const char *args[] = {"prog", "-top-level-remove"};
452 
453   EXPECT_FALSE(TopLevelRemove);
454   EXPECT_TRUE(
455       cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
456   EXPECT_TRUE(TopLevelRemove);
457 
458   TopLevelRemove.removeArgument();
459 
460   cl::ResetAllOptionOccurrences();
461   EXPECT_FALSE(
462       cl::ParseCommandLineOptions(2, args, StringRef(), &llvm::nulls()));
463 }
464 
465 TEST(CommandLineTest, RemoveFromAllSubCommands) {
466   cl::ResetCommandLineParser();
467 
468   StackSubCommand SC1("sc1", "First Subcommand");
469   StackSubCommand SC2("sc2", "Second Subcommand");
470   StackOption<bool> RemoveOption("remove-option", cl::sub(*cl::AllSubCommands),
471                                  cl::init(false));
472   StackOption<bool> KeepOption("keep-option", cl::sub(*cl::AllSubCommands),
473                                cl::init(false));
474 
475   const char *args0[] = {"prog", "-remove-option"};
476   const char *args1[] = {"prog", "sc1", "-remove-option"};
477   const char *args2[] = {"prog", "sc2", "-remove-option"};
478 
479   // It should work for all subcommands including the top-level.
480   EXPECT_FALSE(RemoveOption);
481   EXPECT_TRUE(
482       cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
483   EXPECT_TRUE(RemoveOption);
484 
485   RemoveOption = false;
486 
487   cl::ResetAllOptionOccurrences();
488   EXPECT_FALSE(RemoveOption);
489   EXPECT_TRUE(
490       cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
491   EXPECT_TRUE(RemoveOption);
492 
493   RemoveOption = false;
494 
495   cl::ResetAllOptionOccurrences();
496   EXPECT_FALSE(RemoveOption);
497   EXPECT_TRUE(
498       cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
499   EXPECT_TRUE(RemoveOption);
500 
501   RemoveOption.removeArgument();
502 
503   // It should not work for any subcommands including the top-level.
504   cl::ResetAllOptionOccurrences();
505   EXPECT_FALSE(
506       cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
507   cl::ResetAllOptionOccurrences();
508   EXPECT_FALSE(
509       cl::ParseCommandLineOptions(3, args1, StringRef(), &llvm::nulls()));
510   cl::ResetAllOptionOccurrences();
511   EXPECT_FALSE(
512       cl::ParseCommandLineOptions(3, args2, StringRef(), &llvm::nulls()));
513 }
514 
515 TEST(CommandLineTest, GetRegisteredSubcommands) {
516   cl::ResetCommandLineParser();
517 
518   StackSubCommand SC1("sc1", "First Subcommand");
519   StackOption<bool> Opt1("opt1", cl::sub(SC1), cl::init(false));
520   StackSubCommand SC2("sc2", "Second subcommand");
521   StackOption<bool> Opt2("opt2", cl::sub(SC2), cl::init(false));
522 
523   const char *args0[] = {"prog", "sc1"};
524   const char *args1[] = {"prog", "sc2"};
525 
526   EXPECT_TRUE(
527       cl::ParseCommandLineOptions(2, args0, StringRef(), &llvm::nulls()));
528   EXPECT_FALSE(Opt1);
529   EXPECT_FALSE(Opt2);
530   for (auto *S : cl::getRegisteredSubcommands()) {
531     if (*S)
532       EXPECT_EQ("sc1", S->getName());
533   }
534 
535   cl::ResetAllOptionOccurrences();
536   EXPECT_TRUE(
537       cl::ParseCommandLineOptions(2, args1, StringRef(), &llvm::nulls()));
538   EXPECT_FALSE(Opt1);
539   EXPECT_FALSE(Opt2);
540   for (auto *S : cl::getRegisteredSubcommands()) {
541     if (*S)
542       EXPECT_EQ("sc2", S->getName());
543   }
544 }
545 
546 TEST(CommandLineTest, ResponseFiles) {
547   llvm::SmallString<128> TestDir;
548   std::error_code EC =
549     llvm::sys::fs::createUniqueDirectory("unittest", TestDir);
550   EXPECT_TRUE(!EC);
551 
552   // Create included response file of first level.
553   llvm::SmallString<128> IncludedFileName;
554   llvm::sys::path::append(IncludedFileName, TestDir, "resp1");
555   std::ofstream IncludedFile(IncludedFileName.c_str());
556   EXPECT_TRUE(IncludedFile.is_open());
557   IncludedFile << "-option_1 -option_2\n"
558                   "@incdir/resp2\n"
559                   "-option_3=abcd\n";
560   IncludedFile.close();
561 
562   // Directory for included file.
563   llvm::SmallString<128> IncDir;
564   llvm::sys::path::append(IncDir, TestDir, "incdir");
565   EC = llvm::sys::fs::create_directory(IncDir);
566   EXPECT_TRUE(!EC);
567 
568   // Create included response file of second level.
569   llvm::SmallString<128> IncludedFileName2;
570   llvm::sys::path::append(IncludedFileName2, IncDir, "resp2");
571   std::ofstream IncludedFile2(IncludedFileName2.c_str());
572   EXPECT_TRUE(IncludedFile2.is_open());
573   IncludedFile2 << "-option_21 -option_22\n";
574   IncludedFile2 << "-option_23=abcd\n";
575   IncludedFile2.close();
576 
577   // Prepare 'file' with reference to response file.
578   SmallString<128> IncRef;
579   IncRef.append(1, '@');
580   IncRef.append(IncludedFileName.c_str());
581   llvm::SmallVector<const char *, 4> Argv =
582                           { "test/test", "-flag_1", IncRef.c_str(), "-flag_2" };
583 
584   // Expand response files.
585   llvm::BumpPtrAllocator A;
586   llvm::StringSaver Saver(A);
587   bool Res = llvm::cl::ExpandResponseFiles(
588                     Saver, llvm::cl::TokenizeGNUCommandLine, Argv, false, true);
589   EXPECT_TRUE(Res);
590   EXPECT_EQ(Argv.size(), 9U);
591   EXPECT_STREQ(Argv[0], "test/test");
592   EXPECT_STREQ(Argv[1], "-flag_1");
593   EXPECT_STREQ(Argv[2], "-option_1");
594   EXPECT_STREQ(Argv[3], "-option_2");
595   EXPECT_STREQ(Argv[4], "-option_21");
596   EXPECT_STREQ(Argv[5], "-option_22");
597   EXPECT_STREQ(Argv[6], "-option_23=abcd");
598   EXPECT_STREQ(Argv[7], "-option_3=abcd");
599   EXPECT_STREQ(Argv[8], "-flag_2");
600 
601   llvm::sys::fs::remove(IncludedFileName2);
602   llvm::sys::fs::remove(IncDir);
603   llvm::sys::fs::remove(IncludedFileName);
604   llvm::sys::fs::remove(TestDir);
605 }
606 
607 }  // anonymous namespace
608