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 "llvm/Support/StringSaver.h"
14 #include "gtest/gtest.h"
24 TempEnvVar(const char *name, const char *value)
26 const char *old_value = getenv(name);
27 EXPECT_EQ(nullptr, old_value) << old_value;
29 setenv(name, value, true);
31 # define SKIP_ENVIRONMENT_TESTS
37 // Assume setenv and unsetenv come together.
40 (void)name; // Suppress -Wunused-private-field.
45 const char *const name;
49 class StackOption : public cl::opt<T> {
50 typedef cl::opt<T> Base;
54 explicit StackOption(const M0t &M0) : Base(M0) {}
57 template<class M0t, class M1t>
58 StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {}
61 template<class M0t, class M1t, class M2t>
62 StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {}
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) {}
69 ~StackOption() override { this->removeArgument(); }
73 cl::OptionCategory TestCategory("Test Options", "Description");
74 TEST(CommandLineTest, ModifyExisitingOption) {
75 StackOption<int> TestOption("test-option", cl::desc("old description"));
77 const char Description[] = "New description";
78 const char ArgString[] = "new-test-option";
79 const char ValueString[] = "Integer";
81 StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
83 ASSERT_TRUE(Map.count("test-option") == 1) <<
84 "Could not find option in map.";
86 cl::Option *Retrieved = Map["test-option"];
87 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
89 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
90 "Incorrect default option category.";
92 Retrieved->setCategory(TestCategory);
93 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
94 "Failed to modify option's option category.";
96 Retrieved->setDescription(Description);
97 ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
98 << "Changing option description failed.";
100 Retrieved->setArgStr(ArgString);
101 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
102 << "Failed to modify option's Argument string.";
104 Retrieved->setValueStr(ValueString);
105 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
106 << "Failed to modify option's Value string.";
108 Retrieved->setHiddenFlag(cl::Hidden);
109 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
110 "Failed to modify option's hidden flag.";
112 #ifndef SKIP_ENVIRONMENT_TESTS
114 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
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);
124 // This test used to make valgrind complain
125 // ("Conditional jump or move depends on uninitialised value(s)")
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.
132 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
133 // Put cl::opt on stack to check for proper initialization of fields.
134 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
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);
141 #endif // SKIP_ENVIRONMENT_TESTS
143 TEST(CommandLineTest, UseOptionCategory) {
144 StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
146 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
150 typedef void ParserFunction(StringRef Source, StringSaver &Saver,
151 SmallVectorImpl<const char *> &NewArgv,
154 void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
155 const char *const Output[], size_t OutputSize) {
156 SmallVector<const char *, 0> Actual;
158 StringSaver Saver(A);
159 parse(Input, Saver, Actual, /*MarkEOLs=*/false);
160 EXPECT_EQ(OutputSize, Actual.size());
161 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
163 EXPECT_STREQ(Output[I], Actual[I]);
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" };
173 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
174 array_lengthof(Output));
177 TEST(CommandLineTest, TokenizeWindowsCommandLine) {
178 const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
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));
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" }
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);
200 cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
202 cl::ParseCommandLineOptions(ARGC, Inputs[i]);
203 EXPECT_EQ("x", Actual);
204 EXPECT_EQ(0, Input.getNumOccurrences());
206 Alias.removeArgument();
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));
214 cl::ParseCommandLineOptions(argc, argv);
215 EXPECT_EQ("x", Option);
216 EXPECT_EQ(1, Option.getNumOccurrences());
218 Alias.removeArgument();
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);
228 TEST(CommandLineTest, HideUnrelatedOptions) {
229 StackOption<int> TestOption1("hide-option-1");
230 StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
232 cl::HideUnrelatedOptions(TestCategory);
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.";
239 StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
240 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
241 << "Hid default option that should be visable.";
244 cl::OptionCategory TestCategory2("Test Options set 2", "Description");
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));
251 const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
254 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
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.";
263 StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
264 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
265 << "Hid default option that should be visable.";
268 } // anonymous namespace