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/STLExtras.h"
11 #include "llvm/Config/config.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "llvm/Support/StringSaver.h"
14 #include "gtest/gtest.h"
15 #include <stdlib.h>
16 #include <string>
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 class TempEnvVar {
23  public:
24   TempEnvVar(const char *name, const char *value)
25       : name(name) {
26     const char *old_value = getenv(name);
27     EXPECT_EQ(nullptr, old_value) << old_value;
28 #if HAVE_SETENV
29     setenv(name, value, true);
30 #else
31 #   define SKIP_ENVIRONMENT_TESTS
32 #endif
33   }
34 
35   ~TempEnvVar() {
36 #if HAVE_SETENV
37     // Assume setenv and unsetenv come together.
38     unsetenv(name);
39 #else
40     (void)name; // Suppress -Wunused-private-field.
41 #endif
42   }
43 
44  private:
45   const char *const name;
46 };
47 
48 template <typename T>
49 class StackOption : public cl::opt<T> {
50   typedef cl::opt<T> Base;
51 public:
52   // One option...
53   template<class M0t>
54   explicit StackOption(const M0t &M0) : Base(M0) {}
55 
56   // Two options...
57   template<class M0t, class M1t>
58   StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {}
59 
60   // Three options...
61   template<class M0t, class M1t, class M2t>
62   StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {}
63 
64   // Four options...
65   template<class M0t, class M1t, class M2t, class M3t>
66   StackOption(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
67     : Base(M0, M1, M2, M3) {}
68 
69   ~StackOption() override { this->removeArgument(); }
70 };
71 
72 
73 cl::OptionCategory TestCategory("Test Options", "Description");
74 TEST(CommandLineTest, ModifyExisitingOption) {
75   StackOption<int> TestOption("test-option", cl::desc("old description"));
76 
77   const char Description[] = "New description";
78   const char ArgString[] = "new-test-option";
79   const char ValueString[] = "Integer";
80 
81   StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
82 
83   ASSERT_TRUE(Map.count("test-option") == 1) <<
84     "Could not find option in map.";
85 
86   cl::Option *Retrieved = Map["test-option"];
87   ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
88 
89   ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
90     "Incorrect default option category.";
91 
92   Retrieved->setCategory(TestCategory);
93   ASSERT_EQ(&TestCategory,Retrieved->Category) <<
94     "Failed to modify option's option category.";
95 
96   Retrieved->setDescription(Description);
97   ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
98       << "Changing option description failed.";
99 
100   Retrieved->setArgStr(ArgString);
101   ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
102       << "Failed to modify option's Argument string.";
103 
104   Retrieved->setValueStr(ValueString);
105   ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
106       << "Failed to modify option's Value string.";
107 
108   Retrieved->setHiddenFlag(cl::Hidden);
109   ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
110     "Failed to modify option's hidden flag.";
111 }
112 #ifndef SKIP_ENVIRONMENT_TESTS
113 
114 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
115 
116 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
117 TEST(CommandLineTest, ParseEnvironment) {
118   TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
119   EXPECT_EQ("", EnvironmentTestOption);
120   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
121   EXPECT_EQ("hello", EnvironmentTestOption);
122 }
123 
124 // This test used to make valgrind complain
125 // ("Conditional jump or move depends on uninitialised value(s)")
126 //
127 // Warning: Do not run any tests after this one that try to gain access to
128 // registered command line options because this will likely result in a
129 // SEGFAULT. This can occur because the cl::opt in the test below is declared
130 // on the stack which will be destroyed after the test completes but the
131 // command line system will still hold a pointer to a deallocated cl::Option.
132 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
133   // Put cl::opt on stack to check for proper initialization of fields.
134   StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
135   TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
136   EXPECT_EQ("", EnvironmentTestOptionLocal);
137   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
138   EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
139 }
140 
141 #endif  // SKIP_ENVIRONMENT_TESTS
142 
143 TEST(CommandLineTest, UseOptionCategory) {
144   StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
145 
146   ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
147                                                   "Category.";
148 }
149 
150 typedef void ParserFunction(StringRef Source, StringSaver &Saver,
151                             SmallVectorImpl<const char *> &NewArgv,
152                             bool MarkEOLs);
153 
154 void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
155                               const char *const Output[], size_t OutputSize) {
156   SmallVector<const char *, 0> Actual;
157   BumpPtrAllocator A;
158   StringSaver Saver(A);
159   parse(Input, Saver, Actual, /*MarkEOLs=*/false);
160   EXPECT_EQ(OutputSize, Actual.size());
161   for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
162     if (I < OutputSize)
163       EXPECT_STREQ(Output[I], Actual[I]);
164   }
165 }
166 
167 TEST(CommandLineTest, TokenizeGNUCommandLine) {
168   const char *Input =
169       "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
170       "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
171   const char *const Output[] = {
172       "foo bar",     "foo bar",   "foo bar",          "foo\\bar",
173       "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"};
174   testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
175                            array_lengthof(Output));
176 }
177 
178 TEST(CommandLineTest, TokenizeWindowsCommandLine) {
179   const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
180                       "\"st \\\"u\" \\v";
181   const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
182                                  "lmn", "o", "pqr", "st \"u", "\\v" };
183   testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
184                            array_lengthof(Output));
185 }
186 
187 TEST(CommandLineTest, AliasesWithArguments) {
188   static const size_t ARGC = 3;
189   const char *const Inputs[][ARGC] = {
190     { "-tool", "-actual=x", "-extra" },
191     { "-tool", "-actual", "x" },
192     { "-tool", "-alias=x", "-extra" },
193     { "-tool", "-alias", "x" }
194   };
195 
196   for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
197     StackOption<std::string> Actual("actual");
198     StackOption<bool> Extra("extra");
199     StackOption<std::string> Input(cl::Positional);
200 
201     cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
202 
203     cl::ParseCommandLineOptions(ARGC, Inputs[i]);
204     EXPECT_EQ("x", Actual);
205     EXPECT_EQ(0, Input.getNumOccurrences());
206 
207     Alias.removeArgument();
208   }
209 }
210 
211 void testAliasRequired(int argc, const char *const *argv) {
212   StackOption<std::string> Option("option", cl::Required);
213   cl::alias Alias("o", llvm::cl::aliasopt(Option));
214 
215   cl::ParseCommandLineOptions(argc, argv);
216   EXPECT_EQ("x", Option);
217   EXPECT_EQ(1, Option.getNumOccurrences());
218 
219   Alias.removeArgument();
220 }
221 
222 TEST(CommandLineTest, AliasRequired) {
223   const char *opts1[] = { "-tool", "-option=x" };
224   const char *opts2[] = { "-tool", "-o", "x" };
225   testAliasRequired(array_lengthof(opts1), opts1);
226   testAliasRequired(array_lengthof(opts2), opts2);
227 }
228 
229 TEST(CommandLineTest, HideUnrelatedOptions) {
230   StackOption<int> TestOption1("hide-option-1");
231   StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
232 
233   cl::HideUnrelatedOptions(TestCategory);
234 
235   ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
236       << "Failed to hide extra option.";
237   ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
238       << "Hid extra option that should be visable.";
239 
240   StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
241   ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
242       << "Hid default option that should be visable.";
243 }
244 
245 cl::OptionCategory TestCategory2("Test Options set 2", "Description");
246 
247 TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
248   StackOption<int> TestOption1("multi-hide-option-1");
249   StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory));
250   StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2));
251 
252   const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
253                                                    &TestCategory2};
254 
255   cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
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   ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
262       << "Hid extra option that should be visable.";
263 
264   StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
265   ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
266       << "Hid default option that should be visable.";
267 }
268 
269 }  // anonymous namespace
270