OptionParsingTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===- unittest/Support/OptionParsingTest.cpp - OptTable 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/Option/Arg.h"
  11. #include "llvm/Option/ArgList.h"
  12. #include "llvm/Option/Option.h"
  13. #include "gtest/gtest.h"
  14. using namespace llvm;
  15. using namespace llvm::opt;
  16. enum ID {
  17. OPT_INVALID = 0, // This is not an option ID.
  18. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  19. HELPTEXT, METAVAR) OPT_##ID,
  20. #include "Opts.inc"
  21. LastOption
  22. #undef OPTION
  23. };
  24. #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
  25. #include "Opts.inc"
  26. #undef PREFIX
  27. enum OptionFlags {
  28. OptFlag1 = (1 << 4),
  29. OptFlag2 = (1 << 5),
  30. OptFlag3 = (1 << 6)
  31. };
  32. static const OptTable::Info InfoTable[] = {
  33. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  34. HELPTEXT, METAVAR) \
  35. { PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
  36. FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
  37. #include "Opts.inc"
  38. #undef OPTION
  39. };
  40. namespace {
  41. class TestOptTable : public OptTable {
  42. public:
  43. TestOptTable(bool IgnoreCase = false)
  44. : OptTable(InfoTable, array_lengthof(InfoTable), IgnoreCase) {}
  45. };
  46. }
  47. const char *Args[] = {
  48. "-A",
  49. "-Bhi",
  50. "--C=desu",
  51. "-C", "bye",
  52. "-D,adena",
  53. "-E", "apple", "bloom",
  54. "-Fblarg",
  55. "-F", "42",
  56. "-Gchuu", "2"
  57. };
  58. TEST(Option, OptionParsing) {
  59. TestOptTable T;
  60. unsigned MAI, MAC;
  61. InputArgList AL = T.ParseArgs(Args, MAI, MAC);
  62. // Check they all exist.
  63. EXPECT_TRUE(AL.hasArg(OPT_A));
  64. EXPECT_TRUE(AL.hasArg(OPT_B));
  65. EXPECT_TRUE(AL.hasArg(OPT_C));
  66. EXPECT_TRUE(AL.hasArg(OPT_D));
  67. EXPECT_TRUE(AL.hasArg(OPT_E));
  68. EXPECT_TRUE(AL.hasArg(OPT_F));
  69. EXPECT_TRUE(AL.hasArg(OPT_G));
  70. // Check the values.
  71. EXPECT_EQ(AL.getLastArgValue(OPT_B), "hi");
  72. EXPECT_EQ(AL.getLastArgValue(OPT_C), "bye");
  73. EXPECT_EQ(AL.getLastArgValue(OPT_D), "adena");
  74. std::vector<std::string> Es = AL.getAllArgValues(OPT_E);
  75. EXPECT_EQ(Es[0], "apple");
  76. EXPECT_EQ(Es[1], "bloom");
  77. EXPECT_EQ(AL.getLastArgValue(OPT_F), "42");
  78. std::vector<std::string> Gs = AL.getAllArgValues(OPT_G);
  79. EXPECT_EQ(Gs[0], "chuu");
  80. EXPECT_EQ(Gs[1], "2");
  81. // Check the help text.
  82. std::string Help;
  83. raw_string_ostream RSO(Help);
  84. T.PrintHelp(RSO, "test", "title!");
  85. EXPECT_NE(Help.find("-A"), std::string::npos);
  86. // Test aliases.
  87. arg_iterator Cs = AL.filtered_begin(OPT_C);
  88. ASSERT_NE(Cs, AL.filtered_end());
  89. EXPECT_EQ(StringRef((*Cs)->getValue()), "desu");
  90. ArgStringList ASL;
  91. (*Cs)->render(AL, ASL);
  92. ASSERT_EQ(ASL.size(), 2u);
  93. EXPECT_EQ(StringRef(ASL[0]), "-C");
  94. EXPECT_EQ(StringRef(ASL[1]), "desu");
  95. }
  96. TEST(Option, ParseWithFlagExclusions) {
  97. TestOptTable T;
  98. unsigned MAI, MAC;
  99. // Exclude flag3 to avoid parsing as OPT_SLASH_C.
  100. InputArgList AL = T.ParseArgs(Args, MAI, MAC,
  101. /*FlagsToInclude=*/0,
  102. /*FlagsToExclude=*/OptFlag3);
  103. EXPECT_TRUE(AL.hasArg(OPT_A));
  104. EXPECT_TRUE(AL.hasArg(OPT_C));
  105. EXPECT_FALSE(AL.hasArg(OPT_SLASH_C));
  106. // Exclude flag1 to avoid parsing as OPT_C.
  107. AL = T.ParseArgs(Args, MAI, MAC,
  108. /*FlagsToInclude=*/0,
  109. /*FlagsToExclude=*/OptFlag1);
  110. EXPECT_TRUE(AL.hasArg(OPT_B));
  111. EXPECT_FALSE(AL.hasArg(OPT_C));
  112. EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
  113. const char *NewArgs[] = { "/C", "foo", "--C=bar" };
  114. AL = T.ParseArgs(NewArgs, MAI, MAC);
  115. EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
  116. EXPECT_TRUE(AL.hasArg(OPT_C));
  117. EXPECT_EQ(AL.getLastArgValue(OPT_SLASH_C), "foo");
  118. EXPECT_EQ(AL.getLastArgValue(OPT_C), "bar");
  119. }
  120. TEST(Option, ParseAliasInGroup) {
  121. TestOptTable T;
  122. unsigned MAI, MAC;
  123. const char *MyArgs[] = { "-I" };
  124. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  125. EXPECT_TRUE(AL.hasArg(OPT_H));
  126. }
  127. TEST(Option, AliasArgs) {
  128. TestOptTable T;
  129. unsigned MAI, MAC;
  130. const char *MyArgs[] = { "-J", "-Joo" };
  131. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  132. EXPECT_TRUE(AL.hasArg(OPT_B));
  133. EXPECT_EQ(AL.getAllArgValues(OPT_B)[0], "foo");
  134. EXPECT_EQ(AL.getAllArgValues(OPT_B)[1], "bar");
  135. }
  136. TEST(Option, IgnoreCase) {
  137. TestOptTable T(true);
  138. unsigned MAI, MAC;
  139. const char *MyArgs[] = { "-a", "-joo" };
  140. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  141. EXPECT_TRUE(AL.hasArg(OPT_A));
  142. EXPECT_TRUE(AL.hasArg(OPT_B));
  143. }
  144. TEST(Option, DoNotIgnoreCase) {
  145. TestOptTable T;
  146. unsigned MAI, MAC;
  147. const char *MyArgs[] = { "-a", "-joo" };
  148. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  149. EXPECT_FALSE(AL.hasArg(OPT_A));
  150. EXPECT_FALSE(AL.hasArg(OPT_B));
  151. }
  152. TEST(Option, SlurpEmpty) {
  153. TestOptTable T;
  154. unsigned MAI, MAC;
  155. const char *MyArgs[] = { "-A", "-slurp" };
  156. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  157. EXPECT_TRUE(AL.hasArg(OPT_A));
  158. EXPECT_TRUE(AL.hasArg(OPT_Slurp));
  159. EXPECT_EQ(AL.getAllArgValues(OPT_Slurp).size(), 0U);
  160. }
  161. TEST(Option, Slurp) {
  162. TestOptTable T;
  163. unsigned MAI, MAC;
  164. const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
  165. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  166. EXPECT_EQ(AL.size(), 2U);
  167. EXPECT_TRUE(AL.hasArg(OPT_A));
  168. EXPECT_FALSE(AL.hasArg(OPT_B));
  169. EXPECT_TRUE(AL.hasArg(OPT_Slurp));
  170. EXPECT_EQ(AL.getAllArgValues(OPT_Slurp).size(), 3U);
  171. EXPECT_EQ(AL.getAllArgValues(OPT_Slurp)[0], "-B");
  172. EXPECT_EQ(AL.getAllArgValues(OPT_Slurp)[1], "--");
  173. EXPECT_EQ(AL.getAllArgValues(OPT_Slurp)[2], "foo");
  174. }
  175. TEST(Option, FlagAliasToJoined) {
  176. TestOptTable T;
  177. unsigned MAI, MAC;
  178. // Check that a flag alias provides an empty argument to a joined option.
  179. const char *MyArgs[] = { "-K" };
  180. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  181. EXPECT_EQ(AL.size(), 1U);
  182. EXPECT_TRUE(AL.hasArg(OPT_B));
  183. EXPECT_EQ(AL.getAllArgValues(OPT_B).size(), 1U);
  184. EXPECT_EQ(AL.getAllArgValues(OPT_B)[0], "");
  185. }