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/Support/CommandLine.h"
11 #include "llvm/Config/config.h"
12 #include "gtest/gtest.h"
22 TempEnvVar(const char *name, const char *value)
24 const char *old_value = getenv(name);
25 EXPECT_EQ(NULL, old_value) << old_value;
27 setenv(name, value, true);
29 # define SKIP_ENVIRONMENT_TESTS
35 // Assume setenv and unsetenv come together.
41 const char *const name;
44 cl::OptionCategory TestCategory("Test Options", "Description");
45 cl::opt<int> TestOption("test-option", cl::desc("old description"));
46 TEST(CommandLineTest, ModifyExisitingOption) {
47 const char Description[] = "New description";
48 const char ArgString[] = "new-test-option";
49 const char ValueString[] = "Integer";
51 StringMap<cl::Option*> Map;
52 cl::getRegisteredOptions(Map);
54 ASSERT_TRUE(Map.count("test-option") == 1) <<
55 "Could not find option in map.";
57 cl::Option *Retrieved = Map["test-option"];
58 ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
60 ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
61 "Incorrect default option category.";
63 Retrieved->setCategory(TestCategory);
64 ASSERT_EQ(&TestCategory,Retrieved->Category) <<
65 "Failed to modify option's option category.";
67 Retrieved->setDescription(Description);
68 ASSERT_STREQ(Retrieved->HelpStr, Description) <<
69 "Changing option description failed.";
71 Retrieved->setArgStr(ArgString);
72 ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
73 "Failed to modify option's Argument string.";
75 Retrieved->setValueStr(ValueString);
76 ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
77 "Failed to modify option's Value string.";
79 Retrieved->setHiddenFlag(cl::Hidden);
80 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
81 "Failed to modify option's hidden flag.";
83 #ifndef SKIP_ENVIRONMENT_TESTS
85 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
87 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
88 TEST(CommandLineTest, ParseEnvironment) {
89 TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
90 EXPECT_EQ("", EnvironmentTestOption);
91 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
92 EXPECT_EQ("hello", EnvironmentTestOption);
95 // This test used to make valgrind complain
96 // ("Conditional jump or move depends on uninitialised value(s)")
98 // Warning: Do not run any tests after this one that try to gain access to
99 // registered command line options because this will likely result in a
100 // SEGFAULT. This can occur because the cl::opt in the test below is declared
101 // on the stack which will be destroyed after the test completes but the
102 // command line system will still hold a pointer to a deallocated cl::Option.
103 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
104 // Put cl::opt on stack to check for proper initialization of fields.
105 cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
106 TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
107 EXPECT_EQ("", EnvironmentTestOptionLocal);
108 cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
109 EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
112 #endif // SKIP_ENVIRONMENT_TESTS
114 TEST(CommandLineTest, UseOptionCategory) {
115 cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
117 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
121 } // anonymous namespace