1 //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Config/config.h"
12 #include "llvm/Support/CommandLine.h"
13 #include "gtest/gtest.h"
23 TempEnvVar(const char *name, const char *value)
25 const char *old_value = getenv(name);
26 EXPECT_EQ(NULL, old_value) << old_value;
28 setenv(name, value, true);
30 # define SKIP_ENVIRONMENT_TESTS
36 // Assume setenv and unsetenv come together.
42 const char *const name;
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";
52 StringMap<cl::Option*> Map;
53 cl::getRegisteredOptions(Map);
55 ASSERT_TRUE(Map.count("test-option") == 1) <<
56 "Could not find option in map.";
58 cl::Option *Retrieved = Map["test-option"];
59 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
61 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
62 "Incorrect default option category.";
64 Retrieved->setCategory(TestCategory);
65 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
66 "Failed to modify option's option category.";
68 Retrieved->setDescription(Description);
69 ASSERT_STREQ(Retrieved->HelpStr, Description) <<
70 "Changing option description failed.";
72 Retrieved->setArgStr(ArgString);
73 ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
74 "Failed to modify option's Argument string.";
76 Retrieved->setValueStr(ValueString);
77 ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
78 "Failed to modify option's Value string.";
80 Retrieved->setHiddenFlag(cl::Hidden);
81 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
82 "Failed to modify option's hidden flag.";
84 #ifndef SKIP_ENVIRONMENT_TESTS
86 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
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);
96 // This test used to make valgrind complain
97 // ("Conditional jump or move depends on uninitialised value(s)")
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.
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);
113 #endif // SKIP_ENVIRONMENT_TESTS
115 TEST(CommandLineTest, UseOptionCategory) {
116 cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
118 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
122 class StrDupSaver : public cl::StringSaver {
123 const char *SaveString(const char *Str) LLVM_OVERRIDE {
128 typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver,
129 SmallVectorImpl<const char *> &NewArgv);
132 void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
133 const char *const Output[], size_t OutputSize) {
134 SmallVector<const char *, 0> Actual;
136 parse(Input, Saver, Actual);
137 EXPECT_EQ(OutputSize, Actual.size());
138 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
140 EXPECT_STREQ(Output[I], Actual[I]);
141 free(const_cast<char *>(Actual[I]));
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" };
151 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
152 array_lengthof(Output));
155 TEST(CommandLineTest, TokenizeWindowsCommandLine) {
156 const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
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));
164 } // anonymous namespace