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(nullptr, 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
;
46 class StackOption
: public cl::opt
<T
> {
47 typedef cl::opt
<T
> Base
;
51 explicit StackOption(const M0t
&M0
) : Base(M0
) {}
54 template<class M0t
, class M1t
>
55 StackOption(const M0t
&M0
, const M1t
&M1
) : Base(M0
, M1
) {}
58 template<class M0t
, class M1t
, class M2t
>
59 StackOption(const M0t
&M0
, const M1t
&M1
, const M2t
&M2
) : Base(M0
, M1
, M2
) {}
62 template<class M0t
, class M1t
, class M2t
, class M3t
>
63 StackOption(const M0t
&M0
, const M1t
&M1
, const M2t
&M2
, const M3t
&M3
)
64 : Base(M0
, M1
, M2
, M3
) {}
67 this->removeArgument();
72 cl::OptionCategory
TestCategory("Test Options", "Description");
73 cl::opt
<int> TestOption("test-option", cl::desc("old description"));
74 TEST(CommandLineTest
, ModifyExisitingOption
) {
75 const char Description
[] = "New description";
76 const char ArgString
[] = "new-test-option";
77 const char ValueString
[] = "Integer";
79 StringMap
<cl::Option
*> Map
;
80 cl::getRegisteredOptions(Map
);
82 ASSERT_TRUE(Map
.count("test-option") == 1) <<
83 "Could not find option in map.";
85 cl::Option
*Retrieved
= Map
["test-option"];
86 ASSERT_EQ(&TestOption
, Retrieved
) << "Retrieved wrong option.";
88 ASSERT_EQ(&cl::GeneralCategory
,Retrieved
->Category
) <<
89 "Incorrect default option category.";
91 Retrieved
->setCategory(TestCategory
);
92 ASSERT_EQ(&TestCategory
,Retrieved
->Category
) <<
93 "Failed to modify option's option category.";
95 Retrieved
->setDescription(Description
);
96 ASSERT_STREQ(Retrieved
->HelpStr
, Description
) <<
97 "Changing option description failed.";
99 Retrieved
->setArgStr(ArgString
);
100 ASSERT_STREQ(ArgString
, Retrieved
->ArgStr
) <<
101 "Failed to modify option's Argument string.";
103 Retrieved
->setValueStr(ValueString
);
104 ASSERT_STREQ(Retrieved
->ValueStr
, ValueString
) <<
105 "Failed to modify option's Value string.";
107 Retrieved
->setHiddenFlag(cl::Hidden
);
108 ASSERT_EQ(cl::Hidden
, TestOption
.getOptionHiddenFlag()) <<
109 "Failed to modify option's hidden flag.";
111 #ifndef SKIP_ENVIRONMENT_TESTS
113 const char test_env_var
[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
115 cl::opt
<std::string
> EnvironmentTestOption("env-test-opt");
116 TEST(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
);
123 // This test used to make valgrind complain
124 // ("Conditional jump or move depends on uninitialised value(s)")
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.
131 TEST(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
);
140 #endif // SKIP_ENVIRONMENT_TESTS
142 TEST(CommandLineTest
, UseOptionCategory
) {
143 StackOption
<int> TestOption2("test-option", cl::cat(TestCategory
));
145 ASSERT_EQ(&TestCategory
,TestOption2
.Category
) << "Failed to assign Option "
149 class StrDupSaver
: public cl::StringSaver
{
150 const char *SaveString(const char *Str
) override
{
155 typedef void ParserFunction(StringRef Source
, llvm::cl::StringSaver
&Saver
,
156 SmallVectorImpl
<const char *> &NewArgv
,
159 void testCommandLineTokenizer(ParserFunction
*parse
, const char *Input
,
160 const char *const Output
[], size_t OutputSize
) {
161 SmallVector
<const char *, 0> Actual
;
163 parse(Input
, Saver
, Actual
, /*MarkEOLs=*/false);
164 EXPECT_EQ(OutputSize
, Actual
.size());
165 for (unsigned I
= 0, E
= Actual
.size(); I
!= E
; ++I
) {
167 EXPECT_STREQ(Output
[I
], Actual
[I
]);
168 free(const_cast<char *>(Actual
[I
]));
172 TEST(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
));
182 TEST(CommandLineTest
, TokenizeWindowsCommandLine
) {
183 const char *Input
= "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
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
));
191 TEST(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" }
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
);
205 cl::alias
Alias("alias", llvm::cl::aliasopt(Actual
));
207 cl::ParseCommandLineOptions(ARGC
, Inputs
[i
]);
208 EXPECT_EQ("x", Actual
);
209 EXPECT_EQ(0, Input
.getNumOccurrences());
211 Alias
.removeArgument();
215 void testAliasRequired(int argc
, const char *const *argv
) {
216 StackOption
<std::string
> Option("option", cl::Required
);
217 cl::alias
Alias("o", llvm::cl::aliasopt(Option
));
219 cl::ParseCommandLineOptions(argc
, argv
);
220 EXPECT_EQ("x", Option
);
221 EXPECT_EQ(1, Option
.getNumOccurrences());
223 Alias
.removeArgument();
226 TEST(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
);
234 } // anonymous namespace