1f4a2713aSLionel Sambuc //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
11f4a2713aSLionel Sambuc #include "llvm/Config/config.h"
12*0a6a1f1dSLionel Sambuc #include "llvm/Support/CommandLine.h"
13f4a2713aSLionel Sambuc #include "gtest/gtest.h"
14f4a2713aSLionel Sambuc #include <stdlib.h>
15f4a2713aSLionel Sambuc #include <string>
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc using namespace llvm;
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc namespace {
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc class TempEnvVar {
22f4a2713aSLionel Sambuc  public:
TempEnvVar(const char * name,const char * value)23f4a2713aSLionel Sambuc   TempEnvVar(const char *name, const char *value)
24f4a2713aSLionel Sambuc       : name(name) {
25f4a2713aSLionel Sambuc     const char *old_value = getenv(name);
26*0a6a1f1dSLionel Sambuc     EXPECT_EQ(nullptr, old_value) << old_value;
27f4a2713aSLionel Sambuc #if HAVE_SETENV
28f4a2713aSLionel Sambuc     setenv(name, value, true);
29f4a2713aSLionel Sambuc #else
30f4a2713aSLionel Sambuc #   define SKIP_ENVIRONMENT_TESTS
31f4a2713aSLionel Sambuc #endif
32f4a2713aSLionel Sambuc   }
33f4a2713aSLionel Sambuc 
~TempEnvVar()34f4a2713aSLionel Sambuc   ~TempEnvVar() {
35f4a2713aSLionel Sambuc #if HAVE_SETENV
36f4a2713aSLionel Sambuc     // Assume setenv and unsetenv come together.
37f4a2713aSLionel Sambuc     unsetenv(name);
38f4a2713aSLionel Sambuc #endif
39f4a2713aSLionel Sambuc   }
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc  private:
42f4a2713aSLionel Sambuc   const char *const name;
43f4a2713aSLionel Sambuc };
44f4a2713aSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc template <typename T>
46*0a6a1f1dSLionel Sambuc class StackOption : public cl::opt<T> {
47*0a6a1f1dSLionel Sambuc   typedef cl::opt<T> Base;
48*0a6a1f1dSLionel Sambuc public:
49*0a6a1f1dSLionel Sambuc   // One option...
50*0a6a1f1dSLionel Sambuc   template<class M0t>
StackOption(const M0t & M0)51*0a6a1f1dSLionel Sambuc   explicit StackOption(const M0t &M0) : Base(M0) {}
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc   // Two options...
54*0a6a1f1dSLionel Sambuc   template<class M0t, class M1t>
StackOption(const M0t & M0,const M1t & M1)55*0a6a1f1dSLionel Sambuc   StackOption(const M0t &M0, const M1t &M1) : Base(M0, M1) {}
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc   // Three options...
58*0a6a1f1dSLionel Sambuc   template<class M0t, class M1t, class M2t>
StackOption(const M0t & M0,const M1t & M1,const M2t & M2)59*0a6a1f1dSLionel Sambuc   StackOption(const M0t &M0, const M1t &M1, const M2t &M2) : Base(M0, M1, M2) {}
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc   // Four options...
62*0a6a1f1dSLionel Sambuc   template<class M0t, class M1t, class M2t, class M3t>
StackOption(const M0t & M0,const M1t & M1,const M2t & M2,const M3t & M3)63*0a6a1f1dSLionel Sambuc   StackOption(const M0t &M0, const M1t &M1, const M2t &M2, const M3t &M3)
64*0a6a1f1dSLionel Sambuc     : Base(M0, M1, M2, M3) {}
65*0a6a1f1dSLionel Sambuc 
~StackOption()66*0a6a1f1dSLionel Sambuc   ~StackOption() {
67*0a6a1f1dSLionel Sambuc     this->removeArgument();
68*0a6a1f1dSLionel Sambuc   }
69*0a6a1f1dSLionel Sambuc };
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc 
72f4a2713aSLionel Sambuc cl::OptionCategory TestCategory("Test Options", "Description");
73f4a2713aSLionel Sambuc cl::opt<int> TestOption("test-option", cl::desc("old description"));
TEST(CommandLineTest,ModifyExisitingOption)74f4a2713aSLionel Sambuc TEST(CommandLineTest, ModifyExisitingOption) {
75f4a2713aSLionel Sambuc   const char Description[] = "New description";
76f4a2713aSLionel Sambuc   const char ArgString[] = "new-test-option";
77f4a2713aSLionel Sambuc   const char ValueString[] = "Integer";
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   StringMap<cl::Option*> Map;
80f4a2713aSLionel Sambuc   cl::getRegisteredOptions(Map);
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc   ASSERT_TRUE(Map.count("test-option") == 1) <<
83f4a2713aSLionel Sambuc     "Could not find option in map.";
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   cl::Option *Retrieved = Map["test-option"];
86f4a2713aSLionel Sambuc   ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc   ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
89f4a2713aSLionel Sambuc     "Incorrect default option category.";
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc   Retrieved->setCategory(TestCategory);
92f4a2713aSLionel Sambuc   ASSERT_EQ(&TestCategory,Retrieved->Category) <<
93f4a2713aSLionel Sambuc     "Failed to modify option's option category.";
94f4a2713aSLionel Sambuc 
95f4a2713aSLionel Sambuc   Retrieved->setDescription(Description);
96f4a2713aSLionel Sambuc   ASSERT_STREQ(Retrieved->HelpStr, Description) <<
97f4a2713aSLionel Sambuc     "Changing option description failed.";
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   Retrieved->setArgStr(ArgString);
100f4a2713aSLionel Sambuc   ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
101f4a2713aSLionel Sambuc     "Failed to modify option's Argument string.";
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   Retrieved->setValueStr(ValueString);
104f4a2713aSLionel Sambuc   ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
105f4a2713aSLionel Sambuc     "Failed to modify option's Value string.";
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc   Retrieved->setHiddenFlag(cl::Hidden);
108f4a2713aSLionel Sambuc   ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
109f4a2713aSLionel Sambuc     "Failed to modify option's hidden flag.";
110f4a2713aSLionel Sambuc }
111f4a2713aSLionel Sambuc #ifndef SKIP_ENVIRONMENT_TESTS
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc cl::opt<std::string> EnvironmentTestOption("env-test-opt");
TEST(CommandLineTest,ParseEnvironment)116f4a2713aSLionel Sambuc TEST(CommandLineTest, ParseEnvironment) {
117f4a2713aSLionel Sambuc   TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
118f4a2713aSLionel Sambuc   EXPECT_EQ("", EnvironmentTestOption);
119f4a2713aSLionel Sambuc   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
120f4a2713aSLionel Sambuc   EXPECT_EQ("hello", EnvironmentTestOption);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc // This test used to make valgrind complain
124f4a2713aSLionel Sambuc // ("Conditional jump or move depends on uninitialised value(s)")
125f4a2713aSLionel Sambuc //
126f4a2713aSLionel Sambuc // Warning: Do not run any tests after this one that try to gain access to
127f4a2713aSLionel Sambuc // registered command line options because this will likely result in a
128f4a2713aSLionel Sambuc // SEGFAULT. This can occur because the cl::opt in the test below is declared
129f4a2713aSLionel Sambuc // on the stack which will be destroyed after the test completes but the
130f4a2713aSLionel Sambuc // command line system will still hold a pointer to a deallocated cl::Option.
TEST(CommandLineTest,ParseEnvironmentToLocalVar)131f4a2713aSLionel Sambuc TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
132f4a2713aSLionel Sambuc   // Put cl::opt on stack to check for proper initialization of fields.
133*0a6a1f1dSLionel Sambuc   StackOption<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
134f4a2713aSLionel Sambuc   TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
135f4a2713aSLionel Sambuc   EXPECT_EQ("", EnvironmentTestOptionLocal);
136f4a2713aSLionel Sambuc   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
137f4a2713aSLionel Sambuc   EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc #endif  // SKIP_ENVIRONMENT_TESTS
141f4a2713aSLionel Sambuc 
TEST(CommandLineTest,UseOptionCategory)142f4a2713aSLionel Sambuc TEST(CommandLineTest, UseOptionCategory) {
143*0a6a1f1dSLionel Sambuc   StackOption<int> TestOption2("test-option", cl::cat(TestCategory));
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
146f4a2713aSLionel Sambuc                                                   "Category.";
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc class StrDupSaver : public cl::StringSaver {
SaveString(const char * Str)150*0a6a1f1dSLionel Sambuc   const char *SaveString(const char *Str) override {
151f4a2713aSLionel Sambuc     return strdup(Str);
152f4a2713aSLionel Sambuc   }
153f4a2713aSLionel Sambuc };
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver,
156*0a6a1f1dSLionel Sambuc                             SmallVectorImpl<const char *> &NewArgv,
157*0a6a1f1dSLionel Sambuc                             bool MarkEOLs);
158f4a2713aSLionel Sambuc 
testCommandLineTokenizer(ParserFunction * parse,const char * Input,const char * const Output[],size_t OutputSize)159f4a2713aSLionel Sambuc void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
160f4a2713aSLionel Sambuc                               const char *const Output[], size_t OutputSize) {
161f4a2713aSLionel Sambuc   SmallVector<const char *, 0> Actual;
162f4a2713aSLionel Sambuc   StrDupSaver Saver;
163*0a6a1f1dSLionel Sambuc   parse(Input, Saver, Actual, /*MarkEOLs=*/false);
164f4a2713aSLionel Sambuc   EXPECT_EQ(OutputSize, Actual.size());
165f4a2713aSLionel Sambuc   for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
166f4a2713aSLionel Sambuc     if (I < OutputSize)
167f4a2713aSLionel Sambuc       EXPECT_STREQ(Output[I], Actual[I]);
168f4a2713aSLionel Sambuc     free(const_cast<char *>(Actual[I]));
169f4a2713aSLionel Sambuc   }
170f4a2713aSLionel Sambuc }
171f4a2713aSLionel Sambuc 
TEST(CommandLineTest,TokenizeGNUCommandLine)172f4a2713aSLionel Sambuc TEST(CommandLineTest, TokenizeGNUCommandLine) {
173f4a2713aSLionel Sambuc   const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' "
174f4a2713aSLionel Sambuc                       "foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\"";
175f4a2713aSLionel Sambuc   const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar",
176f4a2713aSLionel Sambuc                                  "foobarbaz", "C:\\src\\foo.cpp",
177f4a2713aSLionel Sambuc                                  "C:\\src\\foo.cpp" };
178f4a2713aSLionel Sambuc   testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
179f4a2713aSLionel Sambuc                            array_lengthof(Output));
180f4a2713aSLionel Sambuc }
181f4a2713aSLionel Sambuc 
TEST(CommandLineTest,TokenizeWindowsCommandLine)182f4a2713aSLionel Sambuc TEST(CommandLineTest, TokenizeWindowsCommandLine) {
183f4a2713aSLionel Sambuc   const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
184f4a2713aSLionel Sambuc                       "\"st \\\"u\" \\v";
185f4a2713aSLionel Sambuc   const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
186f4a2713aSLionel Sambuc                                  "lmn", "o", "pqr", "st \"u", "\\v" };
187f4a2713aSLionel Sambuc   testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
188f4a2713aSLionel Sambuc                            array_lengthof(Output));
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc 
TEST(CommandLineTest,AliasesWithArguments)191*0a6a1f1dSLionel Sambuc TEST(CommandLineTest, AliasesWithArguments) {
192*0a6a1f1dSLionel Sambuc   static const size_t ARGC = 3;
193*0a6a1f1dSLionel Sambuc   const char *const Inputs[][ARGC] = {
194*0a6a1f1dSLionel Sambuc     { "-tool", "-actual=x", "-extra" },
195*0a6a1f1dSLionel Sambuc     { "-tool", "-actual", "x" },
196*0a6a1f1dSLionel Sambuc     { "-tool", "-alias=x", "-extra" },
197*0a6a1f1dSLionel Sambuc     { "-tool", "-alias", "x" }
198*0a6a1f1dSLionel Sambuc   };
199*0a6a1f1dSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc   for (size_t i = 0, e = array_lengthof(Inputs); i < e; ++i) {
201*0a6a1f1dSLionel Sambuc     StackOption<std::string> Actual("actual");
202*0a6a1f1dSLionel Sambuc     StackOption<bool> Extra("extra");
203*0a6a1f1dSLionel Sambuc     StackOption<std::string> Input(cl::Positional);
204*0a6a1f1dSLionel Sambuc 
205*0a6a1f1dSLionel Sambuc     cl::alias Alias("alias", llvm::cl::aliasopt(Actual));
206*0a6a1f1dSLionel Sambuc 
207*0a6a1f1dSLionel Sambuc     cl::ParseCommandLineOptions(ARGC, Inputs[i]);
208*0a6a1f1dSLionel Sambuc     EXPECT_EQ("x", Actual);
209*0a6a1f1dSLionel Sambuc     EXPECT_EQ(0, Input.getNumOccurrences());
210*0a6a1f1dSLionel Sambuc 
211*0a6a1f1dSLionel Sambuc     Alias.removeArgument();
212*0a6a1f1dSLionel Sambuc   }
213*0a6a1f1dSLionel Sambuc }
214*0a6a1f1dSLionel Sambuc 
testAliasRequired(int argc,const char * const * argv)215*0a6a1f1dSLionel Sambuc void testAliasRequired(int argc, const char *const *argv) {
216*0a6a1f1dSLionel Sambuc   StackOption<std::string> Option("option", cl::Required);
217*0a6a1f1dSLionel Sambuc   cl::alias Alias("o", llvm::cl::aliasopt(Option));
218*0a6a1f1dSLionel Sambuc 
219*0a6a1f1dSLionel Sambuc   cl::ParseCommandLineOptions(argc, argv);
220*0a6a1f1dSLionel Sambuc   EXPECT_EQ("x", Option);
221*0a6a1f1dSLionel Sambuc   EXPECT_EQ(1, Option.getNumOccurrences());
222*0a6a1f1dSLionel Sambuc 
223*0a6a1f1dSLionel Sambuc   Alias.removeArgument();
224*0a6a1f1dSLionel Sambuc }
225*0a6a1f1dSLionel Sambuc 
TEST(CommandLineTest,AliasRequired)226*0a6a1f1dSLionel Sambuc TEST(CommandLineTest, AliasRequired) {
227*0a6a1f1dSLionel Sambuc   const char *opts1[] = { "-tool", "-option=x" };
228*0a6a1f1dSLionel Sambuc   const char *opts2[] = { "-tool", "-o", "x" };
229*0a6a1f1dSLionel Sambuc   testAliasRequired(array_lengthof(opts1), opts1);
230*0a6a1f1dSLionel Sambuc   testAliasRequired(array_lengthof(opts2), opts2);
231*0a6a1f1dSLionel Sambuc }
232*0a6a1f1dSLionel Sambuc 
233*0a6a1f1dSLionel Sambuc 
234f4a2713aSLionel Sambuc }  // anonymous namespace
235