OptionParsingTest.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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, VALUES) \
  20. OPT_##ID,
  21. #include "Opts.inc"
  22. LastOption
  23. #undef OPTION
  24. };
  25. #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
  26. #include "Opts.inc"
  27. #undef PREFIX
  28. enum OptionFlags {
  29. OptFlag1 = (1 << 4),
  30. OptFlag2 = (1 << 5),
  31. OptFlag3 = (1 << 6)
  32. };
  33. static const OptTable::Info InfoTable[] = {
  34. #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
  35. HELPTEXT, METAVAR, VALUES) \
  36. {PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, \
  37. PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS, VALUES},
  38. #include "Opts.inc"
  39. #undef OPTION
  40. };
  41. namespace {
  42. class TestOptTable : public OptTable {
  43. public:
  44. TestOptTable(bool IgnoreCase = false)
  45. : OptTable(InfoTable, IgnoreCase) {}
  46. };
  47. }
  48. const char *Args[] = {
  49. "-A",
  50. "-Bhi",
  51. "--C=desu",
  52. "-C", "bye",
  53. "-D,adena",
  54. "-E", "apple", "bloom",
  55. "-Fblarg",
  56. "-F", "42",
  57. "-Gchuu", "2"
  58. };
  59. TEST(Option, OptionParsing) {
  60. TestOptTable T;
  61. unsigned MAI, MAC;
  62. InputArgList AL = T.ParseArgs(Args, MAI, MAC);
  63. // Check they all exist.
  64. EXPECT_TRUE(AL.hasArg(OPT_A));
  65. EXPECT_TRUE(AL.hasArg(OPT_B));
  66. EXPECT_TRUE(AL.hasArg(OPT_C));
  67. EXPECT_TRUE(AL.hasArg(OPT_D));
  68. EXPECT_TRUE(AL.hasArg(OPT_E));
  69. EXPECT_TRUE(AL.hasArg(OPT_F));
  70. EXPECT_TRUE(AL.hasArg(OPT_G));
  71. // Check the values.
  72. EXPECT_EQ("hi", AL.getLastArgValue(OPT_B));
  73. EXPECT_EQ("bye", AL.getLastArgValue(OPT_C));
  74. EXPECT_EQ("adena", AL.getLastArgValue(OPT_D));
  75. std::vector<std::string> Es = AL.getAllArgValues(OPT_E);
  76. EXPECT_EQ("apple", Es[0]);
  77. EXPECT_EQ("bloom", Es[1]);
  78. EXPECT_EQ("42", AL.getLastArgValue(OPT_F));
  79. std::vector<std::string> Gs = AL.getAllArgValues(OPT_G);
  80. EXPECT_EQ("chuu", Gs[0]);
  81. EXPECT_EQ("2", Gs[1]);
  82. // Check the help text.
  83. std::string Help;
  84. raw_string_ostream RSO(Help);
  85. T.PrintHelp(RSO, "test", "title!");
  86. EXPECT_NE(std::string::npos, Help.find("-A"));
  87. // Test aliases.
  88. auto Cs = AL.filtered(OPT_C);
  89. ASSERT_NE(Cs.begin(), Cs.end());
  90. EXPECT_EQ("desu", StringRef((*Cs.begin())->getValue()));
  91. ArgStringList ASL;
  92. (*Cs.begin())->render(AL, ASL);
  93. ASSERT_EQ(2u, ASL.size());
  94. EXPECT_EQ("-C", StringRef(ASL[0]));
  95. EXPECT_EQ("desu", StringRef(ASL[1]));
  96. }
  97. TEST(Option, ParseWithFlagExclusions) {
  98. TestOptTable T;
  99. unsigned MAI, MAC;
  100. // Exclude flag3 to avoid parsing as OPT_SLASH_C.
  101. InputArgList AL = T.ParseArgs(Args, MAI, MAC,
  102. /*FlagsToInclude=*/0,
  103. /*FlagsToExclude=*/OptFlag3);
  104. EXPECT_TRUE(AL.hasArg(OPT_A));
  105. EXPECT_TRUE(AL.hasArg(OPT_C));
  106. EXPECT_FALSE(AL.hasArg(OPT_SLASH_C));
  107. // Exclude flag1 to avoid parsing as OPT_C.
  108. AL = T.ParseArgs(Args, MAI, MAC,
  109. /*FlagsToInclude=*/0,
  110. /*FlagsToExclude=*/OptFlag1);
  111. EXPECT_TRUE(AL.hasArg(OPT_B));
  112. EXPECT_FALSE(AL.hasArg(OPT_C));
  113. EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
  114. const char *NewArgs[] = { "/C", "foo", "--C=bar" };
  115. AL = T.ParseArgs(NewArgs, MAI, MAC);
  116. EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
  117. EXPECT_TRUE(AL.hasArg(OPT_C));
  118. EXPECT_EQ("foo", AL.getLastArgValue(OPT_SLASH_C));
  119. EXPECT_EQ("bar", AL.getLastArgValue(OPT_C));
  120. }
  121. TEST(Option, ParseAliasInGroup) {
  122. TestOptTable T;
  123. unsigned MAI, MAC;
  124. const char *MyArgs[] = { "-I" };
  125. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  126. EXPECT_TRUE(AL.hasArg(OPT_H));
  127. }
  128. TEST(Option, AliasArgs) {
  129. TestOptTable T;
  130. unsigned MAI, MAC;
  131. const char *MyArgs[] = { "-J", "-Joo" };
  132. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  133. EXPECT_TRUE(AL.hasArg(OPT_B));
  134. EXPECT_EQ("foo", AL.getAllArgValues(OPT_B)[0]);
  135. EXPECT_EQ("bar", AL.getAllArgValues(OPT_B)[1]);
  136. }
  137. TEST(Option, IgnoreCase) {
  138. TestOptTable T(true);
  139. unsigned MAI, MAC;
  140. const char *MyArgs[] = { "-a", "-joo" };
  141. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  142. EXPECT_TRUE(AL.hasArg(OPT_A));
  143. EXPECT_TRUE(AL.hasArg(OPT_B));
  144. }
  145. TEST(Option, DoNotIgnoreCase) {
  146. TestOptTable T;
  147. unsigned MAI, MAC;
  148. const char *MyArgs[] = { "-a", "-joo" };
  149. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  150. EXPECT_FALSE(AL.hasArg(OPT_A));
  151. EXPECT_FALSE(AL.hasArg(OPT_B));
  152. }
  153. TEST(Option, SlurpEmpty) {
  154. TestOptTable T;
  155. unsigned MAI, MAC;
  156. const char *MyArgs[] = { "-A", "-slurp" };
  157. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  158. EXPECT_TRUE(AL.hasArg(OPT_A));
  159. EXPECT_TRUE(AL.hasArg(OPT_Slurp));
  160. EXPECT_EQ(0U, AL.getAllArgValues(OPT_Slurp).size());
  161. }
  162. TEST(Option, Slurp) {
  163. TestOptTable T;
  164. unsigned MAI, MAC;
  165. const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
  166. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  167. EXPECT_EQ(AL.size(), 2U);
  168. EXPECT_TRUE(AL.hasArg(OPT_A));
  169. EXPECT_FALSE(AL.hasArg(OPT_B));
  170. EXPECT_TRUE(AL.hasArg(OPT_Slurp));
  171. EXPECT_EQ(3U, AL.getAllArgValues(OPT_Slurp).size());
  172. EXPECT_EQ("-B", AL.getAllArgValues(OPT_Slurp)[0]);
  173. EXPECT_EQ("--", AL.getAllArgValues(OPT_Slurp)[1]);
  174. EXPECT_EQ("foo", AL.getAllArgValues(OPT_Slurp)[2]);
  175. }
  176. TEST(Option, SlurpJoinedEmpty) {
  177. TestOptTable T;
  178. unsigned MAI, MAC;
  179. const char *MyArgs[] = { "-A", "-slurpjoined" };
  180. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  181. EXPECT_TRUE(AL.hasArg(OPT_A));
  182. EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
  183. EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 0U);
  184. }
  185. TEST(Option, SlurpJoinedOneJoined) {
  186. TestOptTable T;
  187. unsigned MAI, MAC;
  188. const char *MyArgs[] = { "-A", "-slurpjoinedfoo" };
  189. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  190. EXPECT_TRUE(AL.hasArg(OPT_A));
  191. EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
  192. EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 1U);
  193. EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined)[0], "foo");
  194. }
  195. TEST(Option, SlurpJoinedAndSeparate) {
  196. TestOptTable T;
  197. unsigned MAI, MAC;
  198. const char *MyArgs[] = { "-A", "-slurpjoinedfoo", "bar", "baz" };
  199. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  200. EXPECT_TRUE(AL.hasArg(OPT_A));
  201. EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
  202. EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
  203. EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
  204. EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
  205. EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
  206. }
  207. TEST(Option, SlurpJoinedButSeparate) {
  208. TestOptTable T;
  209. unsigned MAI, MAC;
  210. const char *MyArgs[] = { "-A", "-slurpjoined", "foo", "bar", "baz" };
  211. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  212. EXPECT_TRUE(AL.hasArg(OPT_A));
  213. EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
  214. EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
  215. EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
  216. EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
  217. EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
  218. }
  219. TEST(Option, FlagAliasToJoined) {
  220. TestOptTable T;
  221. unsigned MAI, MAC;
  222. // Check that a flag alias provides an empty argument to a joined option.
  223. const char *MyArgs[] = { "-K" };
  224. InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
  225. EXPECT_EQ(AL.size(), 1U);
  226. EXPECT_TRUE(AL.hasArg(OPT_B));
  227. EXPECT_EQ(1U, AL.getAllArgValues(OPT_B).size());
  228. EXPECT_EQ("", AL.getAllArgValues(OPT_B)[0]);
  229. }
  230. TEST(Option, FindNearest) {
  231. TestOptTable T;
  232. std::string Nearest;
  233. // Options that are too short should not be considered
  234. // "near" other short options.
  235. EXPECT_GT(T.findNearest("-A", Nearest), 4U);
  236. EXPECT_GT(T.findNearest("/C", Nearest), 4U);
  237. EXPECT_GT(T.findNearest("--C=foo", Nearest), 4U);
  238. // The nearest candidate should mirror the amount of prefix
  239. // characters used in the original string.
  240. EXPECT_EQ(1U, T.findNearest("-blorb", Nearest));
  241. EXPECT_EQ(Nearest, "-blorp");
  242. EXPECT_EQ(1U, T.findNearest("--blorm", Nearest));
  243. EXPECT_EQ(Nearest, "--blorp");
  244. EXPECT_EQ(1U, T.findNearest("-blarg", Nearest));
  245. EXPECT_EQ(Nearest, "-blarn");
  246. EXPECT_EQ(1U, T.findNearest("--blarm", Nearest));
  247. EXPECT_EQ(Nearest, "--blarn");
  248. EXPECT_EQ(1U, T.findNearest("-fjormp", Nearest));
  249. EXPECT_EQ(Nearest, "--fjormp");
  250. // The nearest candidate respects the prefix and value delimiter
  251. // of the original string.
  252. EXPECT_EQ(1U, T.findNearest("/framb:foo", Nearest));
  253. EXPECT_EQ(Nearest, "/cramb:foo");
  254. // Flags should be included and excluded as specified.
  255. EXPECT_EQ(1U, T.findNearest("-doopf", Nearest, /*FlagsToInclude=*/OptFlag2));
  256. EXPECT_EQ(Nearest, "-doopf2");
  257. EXPECT_EQ(1U, T.findNearest("-doopf", Nearest,
  258. /*FlagsToInclude=*/0,
  259. /*FlagsToExclude=*/OptFlag2));
  260. EXPECT_EQ(Nearest, "-doopf1");
  261. }
  262. TEST(DISABLED_Option, FindNearestFIXME) {
  263. TestOptTable T;
  264. std::string Nearest;
  265. // FIXME: Options with joined values should not have those values considered
  266. // when calculating distance. The test below would fail if run, but it should
  267. // succeed.
  268. EXPECT_EQ(1U, T.findNearest("--erbghFoo", Nearest));
  269. EXPECT_EQ(Nearest, "--ermghFoo");
  270. }