blob: e1d1fa59e58d3e30c4ba2dfcfe9586ffe2597913 [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"
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000013#include "gtest/gtest.h"
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000014#include <stdlib.h>
Chandler Carruth130cec22012-12-04 10:23:08 +000015#include <string>
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000016
17using namespace llvm;
18
19namespace {
20
21class 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;
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000027#if HAVE_SETENV
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000028 setenv(name, value, true);
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000029#else
30# define SKIP_ENVIRONMENT_TESTS
31#endif
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000032 }
33
34 ~TempEnvVar() {
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000035#if HAVE_SETENV
36 // Assume setenv and unsetenv come together.
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000037 unsetenv(name);
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +000038#endif
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +000039 }
40
41 private:
42 const char *const name;
43};
44
Jordan Rosec25b0c72014-01-29 18:54:17 +000045template <typename T>
46class StackOption : public cl::opt<T> {
47 using Base = cl::opt<T>;
48public:
49 // One option...
50 template<class M0t>
51 explicit StackOption(const M0t &M0) : Base(M0) {}
52
53 // Two options...
54 template<class M0t, class M1t>
55 StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {}
56
57 // Three options...
58 template<class M0t, class M1t, class M2t>
59 StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {}
60
61 // Four options...
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) {}
65
66 ~StackOption() {
67 this->removeArgument();
68 }
69};
70
71
Andrew Trick7cb710d2013-05-06 21:56:35 +000072cl::OptionCategory TestCategory("Test Options", "Description");
73cl::opt<int> TestOption("test-option", cl::desc("old description"));
74TEST(CommandLineTest, ModifyExisitingOption) {
75 const char Description[] = "New description";
76 const char ArgString[] = "new-test-option";
77 const char ValueString[] = "Integer";
78
79 StringMap<cl::Option*> Map;
80 cl::getRegisteredOptions(Map);
81
82 ASSERT_TRUE(Map.count("test-option") == 1) <<
83 "Could not find option in map.";
84
85 cl::Option *Retrieved = Map["test-option"];
86 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
87
88 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
89 "Incorrect default option category.";
90
91 Retrieved->setCategory(TestCategory);
92 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
93 "Failed to modify option's option category.";
94
95 Retrieved->setDescription(Description);
96 ASSERT_STREQ(Retrieved->HelpStr, Description) <<
97 "Changing option description failed.";
98
99 Retrieved->setArgStr(ArgString);
100 ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
101 "Failed to modify option's Argument string.";
102
103 Retrieved->setValueStr(ValueString);
104 ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
105 "Failed to modify option's Value string.";
106
107 Retrieved->setHiddenFlag(cl::Hidden);
108 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
109 "Failed to modify option's hidden flag.";
110}
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +0000111#ifndef SKIP_ENVIRONMENT_TESTS
112
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +0000113const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
114
115cl::opt<std::string> EnvironmentTestOption("env-test-opt");
116TEST(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);
121}
122
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000123// This test used to make valgrind complain
124// ("Conditional jump or move depends on uninitialised value(s)")
Andrew Trick7cb710d2013-05-06 21:56:35 +0000125//
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.
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000131TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
132 // Put cl::opt on stack to check for proper initialization of fields.
Jordan Rosec25b0c72014-01-29 18:54:17 +0000133 StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
Alexander Kornienko8c724a72012-08-13 10:43:36 +0000134 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);
138}
139
Jeffrey Yasskin14a5cc52009-09-25 21:07:20 +0000140#endif // SKIP_ENVIRONMENT_TESTS
141
Andrew Trick0537a982013-05-06 21:56:23 +0000142TEST(CommandLineTest, UseOptionCategory) {
Jordan Rosec25b0c72014-01-29 18:54:17 +0000143 StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
Andrew Trick0537a982013-05-06 21:56:23 +0000144
Andrew Trick7cb710d2013-05-06 21:56:35 +0000145 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
Andrew Trick0537a982013-05-06 21:56:23 +0000146 "Category.";
147}
148
Reid Klecknera73c7782013-07-18 16:52:05 +0000149class StrDupSaver : public cl::StringSaver {
150 const char *SaveString(const char *Str) LLVM_OVERRIDE {
151 return strdup(Str);
152 }
153};
154
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000155typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver,
156 SmallVectorImpl<const char *> &NewArgv);
157
158
159void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
160 const char *const Output[], size_t OutputSize) {
161 SmallVector<const char *, 0> Actual;
162 StrDupSaver Saver;
163 parse(Input, Saver, Actual);
164 EXPECT_EQ(OutputSize, Actual.size());
165 for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
166 if (I < OutputSize)
167 EXPECT_STREQ(Output[I], Actual[I]);
168 free(const_cast<char *>(Actual[I]));
169 }
170}
171
Reid Klecknera73c7782013-07-18 16:52:05 +0000172TEST(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" };
Rui Ueyamaa2222b52013-07-30 19:03:20 +0000178 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
179 array_lengthof(Output));
180}
181
182TEST(CommandLineTest, TokenizeWindowsCommandLine) {
183 const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
184 "\"st \\\"u\" \\v";
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));
Reid Klecknera73c7782013-07-18 16:52:05 +0000189}
190
Jordan Rosec25b0c72014-01-29 18:54:17 +0000191TEST(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" }
198 };
199
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);
204
205 cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
206
207 cl::ParseCommandLineOptions(ARGC, Inputs[i]);
208 EXPECT_EQ("x", Actual);
209 EXPECT_EQ(0, Input.getNumOccurrences());
210
211 Alias.removeArgument();
212 }
213}
214
Jeffrey Yasskina75d6bf2009-09-24 01:14:07 +0000215} // anonymous namespace