blob: cd235d274e6c0a62dfc6f5aa1fa181f4e9910a9e [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
10#include "llvm/Support/CommandLine.h"
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000011#include "llvm/Config/config.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000012#include "gtest/gtest.h"
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000013#include <stdlib.h>
Chandler Carruth5a88dda2012-12-04 10:23:08 +000014#include <string>
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000015
16using namespace llvm;
17
18namespace {
19
20class 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 Yasskinb7a8d402009-09-25 21:07:20 +000026#if HAVE_SETENV
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000027 setenv(name, value, true);
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000028#else
29# define SKIP_ENVIRONMENT_TESTS
30#endif
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000031 }
32
33 ~TempEnvVar() {
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000034#if HAVE_SETENV
35 // Assume setenv and unsetenv come together.
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000036 unsetenv(name);
Jeffrey Yasskinb7a8d402009-09-25 21:07:20 +000037#endif
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000038 }
39
40 private:
41 const char *const name;
42};
43
Andrew Trick61e01722013-05-06 21:56:35 +000044cl::OptionCategory TestCategory("Test Options", "Description");
45cl::opt<int> TestOption("test-option", cl::desc("old description"));
46TEST(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 Yasskinb7a8d402009-09-25 21:07:20 +000083#ifndef SKIP_ENVIRONMENT_TESTS
84
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +000085const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
86
87cl::opt<std::string> EnvironmentTestOption("env-test-opt");
88TEST(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 Kornienko8932fe42012-08-13 10:43:36 +000095// This test used to make valgrind complain
96// ("Conditional jump or move depends on uninitialised value(s)")
Andrew Trick61e01722013-05-06 21:56:35 +000097//
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 Kornienko8932fe42012-08-13 10:43:36 +0000103TEST(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 Yasskinb7a8d402009-09-25 21:07:20 +0000112#endif // SKIP_ENVIRONMENT_TESTS
113
Andrew Trickb7ad33b2013-05-06 21:56:23 +0000114TEST(CommandLineTest, UseOptionCategory) {
Andrew Trick61e01722013-05-06 21:56:35 +0000115 cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
Andrew Trickb7ad33b2013-05-06 21:56:23 +0000116
Andrew Trick61e01722013-05-06 21:56:35 +0000117 ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
Andrew Trickb7ad33b2013-05-06 21:56:23 +0000118 "Category.";
119}
120
Jeffrey Yasskin1d75d3a2009-09-24 01:14:07 +0000121} // anonymous namespace