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