blob: 760df3c018286e9903a9fb71a5f282f53f067af0 [file] [log] [blame]
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +00001//===- 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 Klecknera73c7782013-07-18 16:52:05 +000010#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000011#include "llvm/Config/config.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000012#include "llvm/Support/CommandLine.h"
Rafael Espindola454adf62015-06-13 12:49:52 +000013#include "llvm/Support/StringSaver.h"
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000014#include "gtest/gtest.h"
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000015#include <stdlib.h>
Chandler Carruth130cec22012-12-04 10:23:08 +000016#include <string>
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000017
18using namespace llvm;
19
20namespace {
21
22class TempEnvVar {
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000023 public:
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000024 TempEnvVar(const char *name, const char *value)
25 : name(name) {
26 const char *old_value = getenv(name);
Craig Topper66f09ad2014-06-08 22:29:17 +000027 EXPECT_EQ(nullptr, old_value) << old_value;
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000028#if HAVE_SETENV
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000029 setenv(name, value, true);
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000030#else
31# define SKIP_ENVIRONMENT_TESTS
32#endif
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000033 }
34
35 ~TempEnvVar() {
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000036#if HAVE_SETENV
37 // Assume setenv and unsetenv come together.
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000038 unsetenv(name);
Reid Kleckner542a4542015-02-26 21:08:21 +000039#else
40 (void)name; // Suppress -Wunused-private-field.
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000041#endif
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000042 }
43
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000044 private:
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000045 const char *const name;
46};
47
Jordan Rosec25b0c72014-01-29 18:54:17 +000048template <typename T>
49class StackOption : public cl::opt<T> {
Jordan Rosed2ad22a2014-01-29 19:14:23 +000050 typedef cl::opt<T> Base;
Jordan Rosec25b0c72014-01-29 18:54:17 +000051public:
52 // One option...
53 template<class M0t>
54 explicit StackOption(const M0t &M0) : Base(M0) {}
55
56 // Two options...
57 template<class M0t, class M1t>
58 StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {}
59
60 // Three options...
61 template<class M0t, class M1t, class M2t>
62 StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {}
63
64 // Four options...
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) {}
68
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000069 ~StackOption() override { this->removeArgument(); }
Jordan Rosec25b0c72014-01-29 18:54:17 +000070};
71
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000072
Andrew Trick7cb710d2013-05-06 21:56:35 +000073cl::OptionCategory TestCategory("Test Options", "Description");
Andrew Trick7cb710d2013-05-06 21:56:35 +000074TEST(CommandLineTest, ModifyExisitingOption) {
Chris Bienemand1d94302015-01-28 19:00:25 +000075 StackOption<int> TestOption("test-option", cl::desc("old description"));
76
Andrew Trick7cb710d2013-05-06 21:56:35 +000077 const char Description[] = "New description";
78 const char ArgString[] = "new-test-option";
79 const char ValueString[] = "Integer";
80
Chris Bienemand1d94302015-01-28 19:00:25 +000081 StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
Andrew Trick7cb710d2013-05-06 21:56:35 +000082
83 ASSERT_TRUE(Map.count("test-option") == 1) <<
84 "Could not find option in map.";
85
86 cl::Option *Retrieved = Map["test-option"];
87 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
88
89 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
90 "Incorrect default option category.";
91
92 Retrieved->setCategory(TestCategory);
93 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
94 "Failed to modify option's option category.";
95
96 Retrieved->setDescription(Description);
David Blaikieff43d692015-11-17 19:00:52 +000097 ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
98 << "Changing option description failed.";
Andrew Trick7cb710d2013-05-06 21:56:35 +000099
100 Retrieved->setArgStr(ArgString);
David Blaikieff43d692015-11-17 19:00:52 +0000101 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
102 << "Failed to modify option's Argument string.";
Andrew Trick7cb710d2013-05-06 21:56:35 +0000103
104 Retrieved->setValueStr(ValueString);
David Blaikieff43d692015-11-17 19:00:52 +0000105 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
106 << "Failed to modify option's Value string.";
Andrew Trick7cb710d2013-05-06 21:56:35 +0000107
108 Retrieved->setHiddenFlag(cl::Hidden);
109 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
110 "Failed to modify option's hidden flag.";
111}
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +0000112#ifndef SKIP_ENVIRONMENT_TESTS
113
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +0000114const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
115
116cl::opt<std::string> EnvironmentTestOption("env-test-opt");
117TEST(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);
122}
123
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000124// This test used to make valgrind complain
125// ("Conditional jump or move depends on uninitialised value(s)")
Andrew Trick7cb710d2013-05-06 21:56:35 +0000126//
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.
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000132TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
133 // Put cl::opt on stack to check for proper initialization of fields.
Jordan Rosec25b0c72014-01-29 18:54:17 +0000134 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000135 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);
139}
140
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +0000141#endif // SKIP_ENVIRONMENT_TESTS
142
Andrew Trick0537a982013-05-06 21:56:23 +0000143TEST(CommandLineTest, UseOptionCategory) {
Jordan Rosec25b0c72014-01-29 18:54:17 +0000144 StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
Andrew Trick0537a982013-05-06 21:56:23 +0000145
Andrew Trick7cb710d2013-05-06 21:56:35 +0000146 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
Andrew Trick0537a982013-05-06 21:56:23 +0000147 "Category.";
148}
149
Rafael Espindola454adf62015-06-13 12:49:52 +0000150typedef void ParserFunction(StringRef Source, StringSaver &Saver,
Reid Klecknere3f146d2014-08-22 19:29:17 +0000151 SmallVectorImpl<const char *> &NewArgv,
152 bool MarkEOLs);
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000153
154void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
155 const char *const Output[], size_t OutputSize) {
156 SmallVector<const char *, 0> Actual;
Rafael Espindola454adf62015-06-13 12:49:52 +0000157 BumpPtrAllocator A;
Rafael Espindolab82455d2015-08-13 01:07:02 +0000158 StringSaver Saver(A);
Reid Klecknere3f146d2014-08-22 19:29:17 +0000159 parse(Input, Saver, Actual, /*MarkEOLs=*/false);
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000160 EXPECT_EQ(OutputSize, Actual.size());
161 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
162 if (I < OutputSize)
163 EXPECT_STREQ(Output[I], Actual[I]);
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000164 }
165}
166
Reid Klecknera73c7782013-07-18 16:52:05 +0000167TEST(CommandLineTest, TokenizeGNUCommandLine) {
Nico Weberfa7f4892016-04-26 13:53:56 +0000168 const char *Input =
169 "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
170 "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
171 const char *const Output[] = {
172 "foo bar", "foo bar", "foo bar", "foo\\bar",
173 "-DFOO=bar()", "foobarbaz", "C:\\src\\foo.cpp", "C:srcfoo.cpp"};
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000174 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
175 array_lengthof(Output));
176}
177
178TEST(CommandLineTest, TokenizeWindowsCommandLine) {
179 const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
180 "\"st \\\"u\" \\v";
181 const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
182 "lmn", "o", "pqr", "st \"u", "\\v" };
183 testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
184 array_lengthof(Output));
Reid Klecknera73c7782013-07-18 16:52:05 +0000185}
186
Jordan Rosec25b0c72014-01-29 18:54:17 +0000187TEST(CommandLineTest, AliasesWithArguments) {
188 static const size_t ARGC = 3;
189 const char *const Inputs[][ARGC] = {
190 { "-tool", "-actual=x", "-extra" },
191 { "-tool", "-actual", "x" },
192 { "-tool", "-alias=x", "-extra" },
193 { "-tool", "-alias", "x" }
194 };
195
196 for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
197 StackOption<std::string> Actual("actual");
198 StackOption<bool> Extra("extra");
199 StackOption<std::string> Input(cl::Positional);
200
201 cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
202
203 cl::ParseCommandLineOptions(ARGC, Inputs[i]);
204 EXPECT_EQ("x", Actual);
205 EXPECT_EQ(0, Input.getNumOccurrences());
206
207 Alias.removeArgument();
208 }
209}
210
Justin Bogner759645e2014-07-14 20:53:57 +0000211void testAliasRequired(int argc, const char *const *argv) {
212 StackOption<std::string> Option("option", cl::Required);
213 cl::alias Alias("o", llvm::cl::aliasopt(Option));
214
215 cl::ParseCommandLineOptions(argc, argv);
216 EXPECT_EQ("x", Option);
217 EXPECT_EQ(1, Option.getNumOccurrences());
218
219 Alias.removeArgument();
220}
221
222TEST(CommandLineTest, AliasRequired) {
223 const char *opts1[] = { "-tool", "-option=x" };
224 const char *opts2[] = { "-tool", "-o", "x" };
225 testAliasRequired(array_lengthof(opts1), opts1);
226 testAliasRequired(array_lengthof(opts2), opts2);
227}
228
Chris Bieneman9e13af72015-01-21 22:45:52 +0000229TEST(CommandLineTest, HideUnrelatedOptions) {
Chris Bieneman68169362015-01-27 22:21:06 +0000230 StackOption<int> TestOption1("hide-option-1");
231 StackOption<int> TestOption2("hide-option-2", cl::cat(TestCategory));
Chris Bieneman9e13af72015-01-21 22:45:52 +0000232
233 cl::HideUnrelatedOptions(TestCategory);
234
235 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
236 << "Failed to hide extra option.";
237 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
238 << "Hid extra option that should be visable.";
Chris Bieneman831fc5e2015-01-26 16:56:00 +0000239
Chris Bienemand1d94302015-01-28 19:00:25 +0000240 StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
Chris Bieneman68169362015-01-27 22:21:06 +0000241 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
242 << "Hid default option that should be visable.";
243}
244
245cl::OptionCategory TestCategory2("Test Options set 2", "Description");
246
247TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
248 StackOption<int> TestOption1("multi-hide-option-1");
249 StackOption<int> TestOption2("multi-hide-option-2", cl::cat(TestCategory));
250 StackOption<int> TestOption3("multi-hide-option-3", cl::cat(TestCategory2));
251
252 const cl::OptionCategory *VisibleCategories[] = {&TestCategory,
253 &TestCategory2};
254
255 cl::HideUnrelatedOptions(makeArrayRef(VisibleCategories));
256
257 ASSERT_EQ(cl::ReallyHidden, TestOption1.getOptionHiddenFlag())
258 << "Failed to hide extra option.";
259 ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
260 << "Hid extra option that should be visable.";
261 ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
262 << "Hid extra option that should be visable.";
263
Chris Bienemand1d94302015-01-28 19:00:25 +0000264 StringMap<cl::Option *> &Map = cl::getRegisteredOptions();
Chris Bieneman831fc5e2015-01-26 16:56:00 +0000265 ASSERT_EQ(cl::NotHidden, Map["help"]->getOptionHiddenFlag())
266 << "Hid default option that should be visable.";
Chris Bieneman9e13af72015-01-21 22:45:52 +0000267}
Justin Bogner759645e2014-07-14 20:53:57 +0000268
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000269} // anonymous namespace