a132eadad0f2611cf884065165aa626f0dd253f3
[oota-llvm.git] / unittests / Support / CommandLineTest.cpp
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(NULL, 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 #endif
39   }
40
41  private:
42   const char *const name;
43 };
44
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 }
84 #ifndef SKIP_ENVIRONMENT_TESTS
85
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
96 // This test used to make valgrind complain
97 // ("Conditional jump or move depends on uninitialised value(s)")
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.
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
113 #endif  // SKIP_ENVIRONMENT_TESTS
114
115 TEST(CommandLineTest, UseOptionCategory) {
116   cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
117
118   ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
119                                                   "Category.";
120 }
121
122 class StrDupSaver : public cl::StringSaver {
123   const char *SaveString(const char *Str) LLVM_OVERRIDE {
124     return strdup(Str);
125   }
126 };
127
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
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));
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));
162 }
163
164 }  // anonymous namespace