blob: c54e1b9570f899aa0bc58ea05b1f80ad798d8dda [file] [log] [blame]
Jeffrey Yasskin1d75d3a2009-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 Kleckner431b0a72013-07-18 16:52:05 +000010#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000011#include "llvm/Support/CommandLine.h"
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000012#include "llvm/Config/config.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000013#include "gtest/gtest.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000014#include <stdlib.h>
Chandler Carruth5a88dda2012-12-04 10:23:08 +000015#include <string>
Jeffrey Yasskin1d75d3a2009-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 Yasskinb7a8d402009-09-25 21:07:20 +000027#if HAVE_SETENV
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000028 setenv(name, value, true);
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000029#else
30# define SKIP_ENVIRONMENT_TESTS
31#endif
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000032 }
33
34 ~TempEnvVar() {
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000035#if HAVE_SETENV
36 // Assume setenv and unsetenv come together.
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000037 unsetenv(name);
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000038#endif
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000039 }
40
41 private:
42 const char *const name;
43};
44
Andrew Trick61e01722013-05-06 21:56:35 +000045cl::OptionCategory TestCategory("Test Options", "Description");
46cl::opt<int> TestOption("test-option", cl::desc("old description"));
47TEST(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}
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000084#ifndef SKIP_ENVIRONMENT_TESTS
85
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000086const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
87
88cl::opt<std::string> EnvironmentTestOption("env-test-opt");
89TEST(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
Alexander Kornienko8932fe42012-08-13 10:43:36 +000096// This test used to make valgrind complain
97// ("Conditional jump or move depends on uninitialised value(s)")
Andrew Trick61e01722013-05-06 21:56:35 +000098//
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.
Alexander Kornienko8932fe42012-08-13 10:43:36 +0000104TEST(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
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +0000113#endif // SKIP_ENVIRONMENT_TESTS
114
Andrew Trickb7ad33b2013-05-06 21:56:23 +0000115TEST(CommandLineTest, UseOptionCategory) {
Andrew Trick61e01722013-05-06 21:56:35 +0000116 cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
Andrew Trickb7ad33b2013-05-06 21:56:23 +0000117
Andrew Trick61e01722013-05-06 21:56:35 +0000118 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
Andrew Trickb7ad33b2013-05-06 21:56:23 +0000119 "Category.";
120}
121
Reid Kleckner431b0a72013-07-18 16:52:05 +0000122class StrDupSaver : public cl::StringSaver {
123 const char *SaveString(const char *Str) LLVM_OVERRIDE {
124 return strdup(Str);
125 }
126};
127
Rui Ueyama264e92d2013-07-30 19:03:20 +0000128typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver,
129 SmallVectorImpl<const char *> &NewArgv);
130
131
132void 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
Reid Kleckner431b0a72013-07-18 16:52:05 +0000145TEST(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" };
Rui Ueyama264e92d2013-07-30 19:03:20 +0000151 testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
152 array_lengthof(Output));
153}
154
155TEST(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));
Reid Kleckner431b0a72013-07-18 16:52:05 +0000162}
163
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +0000164} // anonymous namespace