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