CommandLineTest.cpp 9.1 KB

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