Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 10 | #include "llvm/Support/CommandLine.h" |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 11 | #include "llvm/Config/config.h" |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Chandler Carruth | 5a88dda | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 14 | #include <string> |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class TempEnvVar { |
| 21 | public: |
| 22 | TempEnvVar(const char *name, const char *value) |
| 23 | : name(name) { |
| 24 | const char *old_value = getenv(name); |
| 25 | EXPECT_EQ(NULL, old_value) << old_value; |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 26 | #if HAVE_SETENV |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 27 | setenv(name, value, true); |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 28 | #else |
| 29 | # define SKIP_ENVIRONMENT_TESTS |
| 30 | #endif |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | ~TempEnvVar() { |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 34 | #if HAVE_SETENV |
| 35 | // Assume setenv and unsetenv come together. |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 36 | unsetenv(name); |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 37 | #endif |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | private: |
| 41 | const char *const name; |
| 42 | }; |
| 43 | |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 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"; |
| 50 | |
| 51 | StringMap<cl::Option*> Map; |
| 52 | cl::getRegisteredOptions(Map); |
| 53 | |
| 54 | ASSERT_TRUE(Map.count("test-option") == 1) << |
| 55 | "Could not find option in map."; |
| 56 | |
| 57 | cl::Option *Retrieved = Map["test-option"]; |
| 58 | ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option."; |
| 59 | |
| 60 | ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) << |
| 61 | "Incorrect default option category."; |
| 62 | |
| 63 | Retrieved->setCategory(TestCategory); |
| 64 | ASSERT_EQ(&TestCategory,Retrieved->Category) << |
| 65 | "Failed to modify option's option category."; |
| 66 | |
| 67 | Retrieved->setDescription(Description); |
| 68 | ASSERT_STREQ(Retrieved->HelpStr, Description) << |
| 69 | "Changing option description failed."; |
| 70 | |
| 71 | Retrieved->setArgStr(ArgString); |
| 72 | ASSERT_STREQ(ArgString, Retrieved->ArgStr) << |
| 73 | "Failed to modify option's Argument string."; |
| 74 | |
| 75 | Retrieved->setValueStr(ValueString); |
| 76 | ASSERT_STREQ(Retrieved->ValueStr, ValueString) << |
| 77 | "Failed to modify option's Value string."; |
| 78 | |
| 79 | Retrieved->setHiddenFlag(cl::Hidden); |
| 80 | ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) << |
| 81 | "Failed to modify option's hidden flag."; |
| 82 | } |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 83 | #ifndef SKIP_ENVIRONMENT_TESTS |
| 84 | |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 85 | const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS"; |
| 86 | |
| 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); |
| 93 | } |
| 94 | |
Alexander Kornienko | 8932fe4 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 95 | // This test used to make valgrind complain |
| 96 | // ("Conditional jump or move depends on uninitialised value(s)") |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 97 | // |
| 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. |
Alexander Kornienko | 8932fe4 | 2012-08-13 10:43:36 +0000 | [diff] [blame] | 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); |
| 110 | } |
| 111 | |
Jeffrey Yasskin | b7a8d40 | 2009-09-25 21:07:20 +0000 | [diff] [blame] | 112 | #endif // SKIP_ENVIRONMENT_TESTS |
| 113 | |
Andrew Trick | b7ad33b | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 114 | TEST(CommandLineTest, UseOptionCategory) { |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 115 | cl::opt<int> TestOption2("test-option", cl::cat(TestCategory)); |
Andrew Trick | b7ad33b | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 116 | |
Andrew Trick | 61e0172 | 2013-05-06 21:56:35 +0000 | [diff] [blame] | 117 | ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option " |
Andrew Trick | b7ad33b | 2013-05-06 21:56:23 +0000 | [diff] [blame] | 118 | "Category."; |
| 119 | } |
| 120 | |
Jeffrey Yasskin | 1d75d3a | 2009-09-24 01:14:07 +0000 | [diff] [blame] | 121 | } // anonymous namespace |