aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/Option
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2012-12-05 00:29:32 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2012-12-05 00:29:32 +0000
commit96a564f2beec8c1930f0640844eec433e03bbce8 (patch)
treedb18bf84800e8860865f55dddd773256547604db /unittests/Option
parentee47edfd8e2dd048522ebd47305aeefbe9d8729c (diff)
downloadexternal_llvm-96a564f2beec8c1930f0640844eec433e03bbce8.zip
external_llvm-96a564f2beec8c1930f0640844eec433e03bbce8.tar.gz
external_llvm-96a564f2beec8c1930f0640844eec433e03bbce8.tar.bz2
Copy clang/Driver/<Option parsing stuff> to llvm.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169344 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Option')
-rw-r--r--unittests/Option/CMakeLists.txt15
-rw-r--r--unittests/Option/OptionParsingTest.cpp102
-rw-r--r--unittests/Option/Opts.td13
3 files changed, 130 insertions, 0 deletions
diff --git a/unittests/Option/CMakeLists.txt b/unittests/Option/CMakeLists.txt
new file mode 100644
index 0000000..185d503
--- /dev/null
+++ b/unittests/Option/CMakeLists.txt
@@ -0,0 +1,15 @@
+set(LLVM_LINK_COMPONENTS
+ Option
+ Support
+ )
+
+set(LLVM_TARGET_DEFINITIONS Opts.td)
+
+tablegen(LLVM Opts.inc -gen-opt-parser-defs)
+add_public_tablegen_target(OptsTestTableGen)
+
+add_llvm_unittest(OptionTests
+ OptionParsingTest.cpp
+ )
+
+add_dependencies(OptionTests OptsTestTableGen)
diff --git a/unittests/Option/OptionParsingTest.cpp b/unittests/Option/OptionParsingTest.cpp
new file mode 100644
index 0000000..10e4be8
--- /dev/null
+++ b/unittests/Option/OptionParsingTest.cpp
@@ -0,0 +1,102 @@
+//===- unittest/Support/OptionParsingTest.cpp - OptTable tests ------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Option/Option.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::opt;
+
+enum ID {
+ OPT_INVALID = 0, // This is not an option ID.
+#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
+ HELPTEXT, METAVAR) OPT_##ID,
+#include "Opts.inc"
+ LastOption
+#undef OPTION
+};
+
+#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
+#include "Opts.inc"
+#undef PREFIX
+
+static const OptTable::Info InfoTable[] = {
+#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, FLAGS, PARAM, \
+ HELPTEXT, METAVAR) \
+ { PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
+ FLAGS, OPT_##GROUP, OPT_##ALIAS },
+#include "Opts.inc"
+#undef OPTION
+};
+
+namespace {
+class TestOptTable : public OptTable {
+public:
+ TestOptTable()
+ : OptTable(InfoTable, sizeof(InfoTable) / sizeof(InfoTable[0])) {}
+};
+}
+
+const char *Args[] = {
+ "-A",
+ "-Bhi",
+ "--C=desu",
+ "-C", "bye",
+ "-D,adena",
+ "-E", "apple", "bloom",
+ "-Fblarg",
+ "-F", "42",
+ "-Gchuu", "2"
+ };
+
+TEST(Support, OptionParsing) {
+ TestOptTable T;
+ unsigned MAI, MAC;
+ InputArgList *AL = T.ParseArgs(Args, Args + (sizeof(Args) / sizeof(Args[0])), MAI, MAC);
+
+ // Check they all exist.
+ EXPECT_TRUE(AL->hasArg(OPT_A));
+ EXPECT_TRUE(AL->hasArg(OPT_B));
+ EXPECT_TRUE(AL->hasArg(OPT_C));
+ EXPECT_TRUE(AL->hasArg(OPT_D));
+ EXPECT_TRUE(AL->hasArg(OPT_E));
+ EXPECT_TRUE(AL->hasArg(OPT_F));
+ EXPECT_TRUE(AL->hasArg(OPT_G));
+
+ // Check the values.
+ EXPECT_EQ(AL->getLastArgValue(OPT_B), "hi");
+ EXPECT_EQ(AL->getLastArgValue(OPT_C), "bye");
+ EXPECT_EQ(AL->getLastArgValue(OPT_D), "adena");
+ std::vector<std::string> Es = AL->getAllArgValues(OPT_E);
+ EXPECT_EQ(Es[0], "apple");
+ EXPECT_EQ(Es[1], "bloom");
+ EXPECT_EQ(AL->getLastArgValue(OPT_F), "42");
+ std::vector<std::string> Gs = AL->getAllArgValues(OPT_G);
+ EXPECT_EQ(Gs[0], "chuu");
+ EXPECT_EQ(Gs[1], "2");
+
+ // Check the help text.
+ std::string Help;
+ raw_string_ostream RSO(Help);
+ T.PrintHelp(RSO, "test", "title!");
+ EXPECT_NE(Help.find("-A"), std::string::npos);
+
+ // Test aliases.
+ arg_iterator Cs = AL->filtered_begin(OPT_C);
+ ASSERT_NE(Cs, AL->filtered_end());
+ EXPECT_EQ(StringRef((*Cs)->getValue()), "desu");
+ ArgStringList ASL;
+ (*Cs)->render(*AL, ASL);
+ ASSERT_EQ(ASL.size(), 2u);
+ EXPECT_EQ(StringRef(ASL[0]), "-C");
+ EXPECT_EQ(StringRef(ASL[1]), "desu");
+}
diff --git a/unittests/Option/Opts.td b/unittests/Option/Opts.td
new file mode 100644
index 0000000..3d6242f
--- /dev/null
+++ b/unittests/Option/Opts.td
@@ -0,0 +1,13 @@
+include "llvm/Option/OptParser.td"
+
+def A : Flag<["-"], "A">, HelpText<"The A option">;
+def B : Joined<["-"], "B">, HelpText<"The B option">, MetaVarName<"B">;
+def C : Separate<["-"], "C">, HelpText<"The C option">, MetaVarName<"C">;
+def D : CommaJoined<["-"], "D">, HelpText<"The D option">, MetaVarName<"D">;
+def E : MultiArg<["-"], "E", 2>;
+def F : JoinedOrSeparate<["-"], "F">, HelpText<"The F option">, MetaVarName<"F">;
+def G : JoinedAndSeparate<["-"], "G">, HelpText<"The G option">, MetaVarName<"G">;
+
+def Ceq : Joined<["-", "--"], "C=">, Alias<C>;
+
+def H : Flag<["-"], "H">, Flags<[HelpHidden]>;