Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | a73c778 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 11 | #include "llvm/Config/config.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 12 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | 454adf6 | 2015-06-13 12:49:52 +0000 | [diff] [blame] | 13 | #include "llvm/Support/StringSaver.h" |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 16 | #include <string> |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 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); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 27 | EXPECT_EQ(nullptr, old_value) << old_value; |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 28 | #if HAVE_SETENV |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 29 | setenv(name, value, true); |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 30 | #else |
| 31 | # define SKIP_ENVIRONMENT_TESTS |
| 32 | #endif |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | ~TempEnvVar() { |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 36 | #if HAVE_SETENV |
| 37 | // Assume setenv and unsetenv come together. |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 38 | unsetenv(name); |
Reid Kleckner | 542a454 | 2015-02-26 21:08:21 +0000 | [diff] [blame] | 39 | #else |
| 40 | (void)name; // Suppress -Wunused-private-field. |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 41 | #endif |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | private: |
| 45 | const char *const name; |
| 46 | }; |
| 47 | |
Jordan Rose | c25b0c7 | 2014-01-29 18:54:17 +0000 | [diff] [blame] | 48 | template <typename T> |
| 49 | class StackOption : public cl::opt<T> { |
Jordan Rose | d2ad22a | 2014-01-29 19:14:23 +0000 | [diff] [blame] | 50 | typedef cl::opt<T> Base; |
Jordan Rose | c25b0c7 | 2014-01-29 18:54:17 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 69 | ~StackOption() override { this->removeArgument(); } |
Jordan Rose | c25b0c7 | 2014-01-29 18:54:17 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 73 | cl::OptionCategory TestCategory("Test Options", "Description"); |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 74 | TEST(CommandLineTest, ModifyExisitingOption) { |
Chris Bieneman | d1d9430 | 2015-01-28 19:00:25 +0000 | [diff] [blame] | 75 | StackOption<int> TestOption("test-option", cl::desc("old description")); |
| 76 | |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 77 | const char Description[] = "New description"; |
| 78 | const char ArgString[] = "new-test-option"; |
| 79 | const char ValueString[] = "Integer"; |
| 80 | |
Chris Bieneman | d1d9430 | 2015-01-28 19:00:25 +0000 | [diff] [blame] | 81 | StringMap<cl::Option *> &Map = cl::getRegisteredOptions(); |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 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); |
David Blaikie | ff43d69 | 2015-11-17 19:00:52 +0000 | [diff] [blame^] | 97 | ASSERT_STREQ(Retrieved->HelpStr.data(), Description) |
| 98 | << "Changing option description failed."; |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 99 | |
| 100 | Retrieved->setArgStr(ArgString); |
David Blaikie | ff43d69 | 2015-11-17 19:00:52 +0000 | [diff] [blame^] | 101 | ASSERT_STREQ(ArgString, Retrieved->ArgStr.data()) |
| 102 | << "Failed to modify option's Argument string."; |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 103 | |
| 104 | Retrieved->setValueStr(ValueString); |
David Blaikie | ff43d69 | 2015-11-17 19:00:52 +0000 | [diff] [blame^] | 105 | ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString) |
| 106 | << "Failed to modify option's Value string."; |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 107 | |
| 108 | Retrieved->setHiddenFlag(cl::Hidden); |
| 109 | ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) << |
| 110 | "Failed to modify option's hidden flag."; |
| 111 | } |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 112 | #ifndef SKIP_ENVIRONMENT_TESTS |
| 113 | |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 8c724a7 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 124 | // This test used to make valgrind complain |
| 125 | // ("Conditional jump or move depends on uninitialised value(s)") |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 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. |
Alexander Kornienko | 8c724a7 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 132 | TEST(CommandLineTest, ParseEnvironmentToLocalVar) { |
| 133 | // Put cl::opt on stack to check for proper initialization of fields. |
Jordan Rose | c25b0c7 | 2014-01-29 18:54:17 +0000 | [diff] [blame] | 134 | StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local"); |
Alexander Kornienko | 8c724a7 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 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 | |
Jeffrey Yasskin | 14a5cc5 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 141 | #endif // SKIP_ENVIRONMENT_TESTS |
| 142 | |
Andrew Trick | 0537a98 | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 143 | TEST(CommandLineTest, UseOptionCategory) { |
Jordan Rose | c25b0c7 | 2014-01-29 18:54:17 +0000 | [diff] [blame] | 144 | StackOption<int> TestOption2("test-option", cl::cat(TestCategory)); |
Andrew Trick | 0537a98 | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 145 | |
Andrew Trick | 7cb710d | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 146 | ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option " |
Andrew Trick | 0537a98 | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 147 | "Category."; |
| 148 | } |
| 149 | |
Rafael Espindola | 454adf6 | 2015-06-13 12:49:52 +0000 | [diff] [blame] | 150 | typedef void ParserFunction(StringRef Source, StringSaver &Saver, |
Reid Kleckner | e3f146d | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 151 | SmallVectorImpl<const char *> &NewArgv, |
| 152 | bool MarkEOLs); |
Rui Ueyama | a2222b5 | 2013-07-30 19:03:20 +0000 | [diff] [blame] | 153 | |
| 154 | void testCommandLineTokenizer(ParserFunction *parse, const char *Input, |
| 155 | const char *const Output[], size_t OutputSize) { |
| 156 | SmallVector<const char *, 0> Actual; |
Rafael Espindola | 454adf6 | 2015-06-13 12:49:52 +0000 | [diff] [blame] | 157 | BumpPtrAllocator A; |
Rafael Espindola | b82455d | 2015-08-13 01:07:02 +0000 | [diff] [blame] | 158 | StringSaver Saver(A); |
Reid Kleckner | e3f146d | 2014-08-22 19:29:17 +0000 | [diff] [blame] | 159 | parse(Input, Saver, Actual, /*MarkEOLs=*/false); |
Rui Ueyama | a2222b5 | 2013-07-30 19:03:20 +0000 | [diff] [blame] | 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]); |
Rui Ueyama | a2222b5 | 2013-07-30 19:03:20 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Reid Kleckner | a73c778 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 167 | TEST(CommandLineTest, TokenizeGNUCommandLine) { |
| 168 | const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' " |
| 169 | "foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\""; |
| 170 | const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar", |
| 171 | "foobarbaz", "C:\\src\\foo.cpp", |
| 172 | "C:\\src\\foo.cpp" }; |
Rui Ueyama | a2222b5 | 2013-07-30 19:03:20 +0000 | [diff] [blame] | 173 | testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output, |
| 174 | array_lengthof(Output)); |
| 175 | } |
| 176 | |
| 177 | TEST(CommandLineTest, TokenizeWindowsCommandLine) { |
| 178 | const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr " |
| 179 | "\"st \\\"u\" \\v"; |
| 180 | const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k", |
| 181 | "lmn", "o", "pqr", "st \"u", "\\v" }; |
| 182 | testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output, |
| 183 | array_lengthof(Output)); |
Reid Kleckner | a73c778 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jordan Rose | c25b0c7 | 2014-01-29 18:54:17 +0000 | [diff] [blame] | 186 | TEST(CommandLineTest, AliasesWithArguments) { |
| 187 | static const size_t ARGC = 3; |
| 188 | const char *const Inputs[][ARGC] = { |
| 189 | { "-tool", "-actual=x", "-extra" }, |
| 190 | { "-tool", "-actual", "x" }, |
| 191 | { "-tool", "-alias=x", "-extra" }, |
| 192 | { "-tool", "-alias", "x" } |
| 193 | }; |
| 194 | |
| 195 | for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) { |
| 196 | StackOption<std::string> Actual("actual"); |
| 197 | StackOption<bool> Extra("extra"); |
| 198 | StackOption<std::string> Input(cl::Positional); |
| 199 | |
| 200 | cl::alias Alias("alias", llvm::cl::aliasopt(Actual)); |
| 201 | |
| 202 | cl::ParseCommandLineOptions(ARGC, Inputs[i]); |
| 203 | EXPECT_EQ("x", Actual); |
| 204 | EXPECT_EQ(0, Input.getNumOccurrences()); |
| 205 | |
| 206 | Alias.removeArgument(); |
| 207 | } |
| 208 | } |
| 209 | |
Justin Bogner | 759645e | 2014-07-14 20:53:57 +0000 | [diff] [blame] | 210 | void testAliasRequired(int argc, const char *const *argv) { |
| 211 | StackOption<std::string> Option("option", cl::Required); |
| 212 | cl::alias Alias("o", llvm::cl::aliasopt(Option)); |
| 213 | |
| 214 | cl::ParseCommandLineOptions(argc, argv); |
| 215 | EXPECT_EQ("x", Option); |
| 216 | EXPECT_EQ(1, Option.getNumOccurrences()); |
| 217 | |
| 218 | Alias.removeArgument(); |
| 219 | } |
| 220 | |
| 221 | TEST(CommandLineTest, AliasRequired) { |
| 222 | const char *opts1[] = { "-tool", "-option=x" }; |
| 223 | const char *opts2[] = { "-tool", "-o", "x" }; |
| 224 | testAliasRequired(array_lengthof(opts1), opts1); |
| 225 | testAliasRequired(array_lengthof(opts2), opts2); |
| 226 | } |
| 227 | |
Chris Bieneman | 9e13af7 | 2015-01-21 22:45:52 +0000 | [diff] [blame] | 228 | TEST(CommandLineTest, HideUnrelatedOptions) { |
Chris Bieneman | 6816936 | 2015-01-27 22:21:06 +0000 | [diff] [blame] | 229 | StackOption<int> TestOption1("hide-option-1"); |
| 230 | StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory)); |
Chris Bieneman | 9e13af7 | 2015-01-21 22:45:52 +0000 | [diff] [blame] | 231 | |
| 232 | cl::HideUnrelatedOptions(TestCategory); |
| 233 | |
| 234 | ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag()) |
| 235 | << "Failed to hide extra option."; |
| 236 | ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag()) |
| 237 | << "Hid extra option that should be visable."; |
Chris Bieneman | 831fc5e | 2015-01-26 16:56:00 +0000 | [diff] [blame] | 238 | |
Chris Bieneman | d1d9430 | 2015-01-28 19:00:25 +0000 | [diff] [blame] | 239 | StringMap<cl::Option *> &Map = cl::getRegisteredOptions(); |
Chris Bieneman | 6816936 | 2015-01-27 22:21:06 +0000 | [diff] [blame] | 240 | ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) |
| 241 | << "Hid default option that should be visable."; |
| 242 | } |
| 243 | |
| 244 | cl::OptionCategory TestCategory2("Test Options set 2", "Description"); |
| 245 | |
| 246 | TEST(CommandLineTest, HideUnrelatedOptionsMulti) { |
| 247 | StackOption<int> TestOption1("multi-hide-option-1"); |
| 248 | StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory)); |
| 249 | StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2)); |
| 250 | |
| 251 | const cl::OptionCategory *VisibleCategories[] = {&TestCategory, |
| 252 | &TestCategory2}; |
| 253 | |
| 254 | cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories)); |
| 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 | ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag()) |
| 261 | << "Hid extra option that should be visable."; |
| 262 | |
Chris Bieneman | d1d9430 | 2015-01-28 19:00:25 +0000 | [diff] [blame] | 263 | StringMap<cl::Option *> &Map = cl::getRegisteredOptions(); |
Chris Bieneman | 831fc5e | 2015-01-26 16:56:00 +0000 | [diff] [blame] | 264 | ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag()) |
| 265 | << "Hid default option that should be visable."; |
Chris Bieneman | 9e13af7 | 2015-01-21 22:45:52 +0000 | [diff] [blame] | 266 | } |
Justin Bogner | 759645e | 2014-07-14 20:53:57 +0000 | [diff] [blame] | 267 | |
Jeffrey Yasskin | a75d6bf | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 268 | } // anonymous namespace |