Jeffrey Yasskin | 1d75d3a | 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 | 431b0a7 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 11 | #include "llvm/Support/CommandLine.h" |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 12 | #include "llvm/Config/config.h" |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 15 | #include <string> |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 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(NULL, old_value) << old_value; |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 27 | #if HAVE_SETENV |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 28 | setenv(name, value, true); |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 29 | #else |
| 30 | # define SKIP_ENVIRONMENT_TESTS |
| 31 | #endif |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | ~TempEnvVar() { |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 35 | #if HAVE_SETENV |
| 36 | // Assume setenv and unsetenv come together. |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 37 | unsetenv(name); |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 38 | #endif |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | private: |
| 42 | const char *const name; |
| 43 | }; |
| 44 | |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 45 | cl::OptionCategory TestCategory("Test Options", "Description"); |
| 46 | cl::opt<int> TestOption("test-option", cl::desc("old description")); |
| 47 | TEST(CommandLineTest, ModifyExisitingOption) { |
| 48 | const char Description[] = "New description"; |
| 49 | const char ArgString[] = "new-test-option"; |
| 50 | const char ValueString[] = "Integer"; |
| 51 | |
| 52 | StringMap<cl::Option*> Map; |
| 53 | cl::getRegisteredOptions(Map); |
| 54 | |
| 55 | ASSERT_TRUE(Map.count("test-option") == 1) << |
| 56 | "Could not find option in map."; |
| 57 | |
| 58 | cl::Option *Retrieved = Map["test-option"]; |
| 59 | ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option."; |
| 60 | |
| 61 | ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) << |
| 62 | "Incorrect default option category."; |
| 63 | |
| 64 | Retrieved->setCategory(TestCategory); |
| 65 | ASSERT_EQ(&TestCategory,Retrieved->Category) << |
| 66 | "Failed to modify option's option category."; |
| 67 | |
| 68 | Retrieved->setDescription(Description); |
| 69 | ASSERT_STREQ(Retrieved->HelpStr, Description) << |
| 70 | "Changing option description failed."; |
| 71 | |
| 72 | Retrieved->setArgStr(ArgString); |
| 73 | ASSERT_STREQ(ArgString, Retrieved->ArgStr) << |
| 74 | "Failed to modify option's Argument string."; |
| 75 | |
| 76 | Retrieved->setValueStr(ValueString); |
| 77 | ASSERT_STREQ(Retrieved->ValueStr, ValueString) << |
| 78 | "Failed to modify option's Value string."; |
| 79 | |
| 80 | Retrieved->setHiddenFlag(cl::Hidden); |
| 81 | ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) << |
| 82 | "Failed to modify option's hidden flag."; |
| 83 | } |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 84 | #ifndef SKIP_ENVIRONMENT_TESTS |
| 85 | |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 86 | const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS"; |
| 87 | |
| 88 | cl::opt<std::string> EnvironmentTestOption("env-test-opt"); |
| 89 | TEST(CommandLineTest, ParseEnvironment) { |
| 90 | TempEnvVar TEV(test_env_var, "-env-test-opt=hello"); |
| 91 | EXPECT_EQ("", EnvironmentTestOption); |
| 92 | cl::ParseEnvironmentOptions("CommandLineTest", test_env_var); |
| 93 | EXPECT_EQ("hello", EnvironmentTestOption); |
| 94 | } |
| 95 | |
Alexander Kornienko | 8932fe4 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 96 | // This test used to make valgrind complain |
| 97 | // ("Conditional jump or move depends on uninitialised value(s)") |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 98 | // |
| 99 | // Warning: Do not run any tests after this one that try to gain access to |
| 100 | // registered command line options because this will likely result in a |
| 101 | // SEGFAULT. This can occur because the cl::opt in the test below is declared |
| 102 | // on the stack which will be destroyed after the test completes but the |
| 103 | // command line system will still hold a pointer to a deallocated cl::Option. |
Alexander Kornienko | 8932fe4 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 104 | TEST(CommandLineTest, ParseEnvironmentToLocalVar) { |
| 105 | // Put cl::opt on stack to check for proper initialization of fields. |
| 106 | cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local"); |
| 107 | TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local"); |
| 108 | EXPECT_EQ("", EnvironmentTestOptionLocal); |
| 109 | cl::ParseEnvironmentOptions("CommandLineTest", test_env_var); |
| 110 | EXPECT_EQ("hello-local", EnvironmentTestOptionLocal); |
| 111 | } |
| 112 | |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 113 | #endif // SKIP_ENVIRONMENT_TESTS |
| 114 | |
Andrew Trick | b7ad33b | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 115 | TEST(CommandLineTest, UseOptionCategory) { |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 116 | cl::opt<int> TestOption2("test-option", cl::cat(TestCategory)); |
Andrew Trick | b7ad33b | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 117 | |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 118 | ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option " |
Andrew Trick | b7ad33b | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 119 | "Category."; |
| 120 | } |
| 121 | |
Reid Kleckner | 431b0a7 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 122 | class StrDupSaver : public cl::StringSaver { |
| 123 | const char *SaveString(const char *Str) LLVM_OVERRIDE { |
| 124 | return strdup(Str); |
| 125 | } |
| 126 | }; |
| 127 | |
Rui Ueyama | 264e92d | 2013-07-30 19:03:20 +0000 | [diff] [blame] | 128 | typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver, |
| 129 | SmallVectorImpl<const char *> &NewArgv); |
| 130 | |
| 131 | |
| 132 | void testCommandLineTokenizer(ParserFunction *parse, const char *Input, |
| 133 | const char *const Output[], size_t OutputSize) { |
| 134 | SmallVector<const char *, 0> Actual; |
| 135 | StrDupSaver Saver; |
| 136 | parse(Input, Saver, Actual); |
| 137 | EXPECT_EQ(OutputSize, Actual.size()); |
| 138 | for (unsigned I = 0, E = Actual.size(); I != E; ++I) { |
| 139 | if (I < OutputSize) |
| 140 | EXPECT_STREQ(Output[I], Actual[I]); |
| 141 | free(const_cast<char *>(Actual[I])); |
| 142 | } |
| 143 | } |
| 144 | |
Reid Kleckner | 431b0a7 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 145 | TEST(CommandLineTest, TokenizeGNUCommandLine) { |
| 146 | const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' " |
| 147 | "foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\""; |
| 148 | const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar", |
| 149 | "foobarbaz", "C:\\src\\foo.cpp", |
| 150 | "C:\\src\\foo.cpp" }; |
Rui Ueyama | 264e92d | 2013-07-30 19:03:20 +0000 | [diff] [blame] | 151 | testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output, |
| 152 | array_lengthof(Output)); |
| 153 | } |
| 154 | |
| 155 | TEST(CommandLineTest, TokenizeWindowsCommandLine) { |
| 156 | const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr " |
| 157 | "\"st \\\"u\" \\v"; |
| 158 | const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k", |
| 159 | "lmn", "o", "pqr", "st \"u", "\\v" }; |
| 160 | testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output, |
| 161 | array_lengthof(Output)); |
Reid Kleckner | 431b0a7 | 2013-07-18 16:52:05 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 164 | } // anonymous namespace |