2012-12-05 08:29:32 +08:00
|
|
|
//===- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-20 02:05:13 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-05 08:29:32 +08:00
|
|
|
#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.
|
2013-08-01 06:44:41 +08:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
2013-08-29 04:04:31 +08:00
|
|
|
HELPTEXT, METAVAR) OPT_##ID,
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "Opts.inc"
|
|
|
|
LastOption
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
2013-07-20 02:05:13 +08:00
|
|
|
enum OptionFlags {
|
|
|
|
OptFlag1 = (1 << 4),
|
|
|
|
OptFlag2 = (1 << 5),
|
|
|
|
OptFlag3 = (1 << 6)
|
|
|
|
};
|
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
static const OptTable::Info InfoTable[] = {
|
2013-08-01 06:44:41 +08:00
|
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
2012-12-05 08:29:32 +08:00
|
|
|
HELPTEXT, METAVAR) \
|
|
|
|
{ PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
|
2013-08-01 06:44:41 +08:00
|
|
|
FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
|
2012-12-05 08:29:32 +08:00
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class TestOptTable : public OptTable {
|
|
|
|
public:
|
2013-08-29 04:04:31 +08:00
|
|
|
TestOptTable(bool IgnoreCase = false)
|
2015-10-22 00:30:42 +08:00
|
|
|
: OptTable(InfoTable, IgnoreCase) {}
|
2012-12-05 08:29:32 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Args[] = {
|
|
|
|
"-A",
|
|
|
|
"-Bhi",
|
|
|
|
"--C=desu",
|
|
|
|
"-C", "bye",
|
|
|
|
"-D,adena",
|
|
|
|
"-E", "apple", "bloom",
|
|
|
|
"-Fblarg",
|
|
|
|
"-F", "42",
|
|
|
|
"-Gchuu", "2"
|
|
|
|
};
|
|
|
|
|
2013-07-20 02:05:13 +08:00
|
|
|
TEST(Option, OptionParsing) {
|
2012-12-05 08:29:32 +08:00
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(Args, MAI, MAC);
|
2013-01-23 16:30:15 +08:00
|
|
|
|
2012-12-05 08:29:32 +08:00
|
|
|
// Check they all exist.
|
2015-06-23 06:06:37 +08:00
|
|
|
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));
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
// Check the values.
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ("hi", AL.getLastArgValue(OPT_B));
|
|
|
|
EXPECT_EQ("bye", AL.getLastArgValue(OPT_C));
|
|
|
|
EXPECT_EQ("adena", AL.getLastArgValue(OPT_D));
|
2015-06-23 06:06:37 +08:00
|
|
|
std::vector<std::string> Es = AL.getAllArgValues(OPT_E);
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ("apple", Es[0]);
|
|
|
|
EXPECT_EQ("bloom", Es[1]);
|
|
|
|
EXPECT_EQ("42", AL.getLastArgValue(OPT_F));
|
2015-06-23 06:06:37 +08:00
|
|
|
std::vector<std::string> Gs = AL.getAllArgValues(OPT_G);
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ("chuu", Gs[0]);
|
|
|
|
EXPECT_EQ("2", Gs[1]);
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
// Check the help text.
|
|
|
|
std::string Help;
|
|
|
|
raw_string_ostream RSO(Help);
|
|
|
|
T.PrintHelp(RSO, "test", "title!");
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_NE(std::string::npos, Help.find("-A"));
|
2012-12-05 08:29:32 +08:00
|
|
|
|
|
|
|
// Test aliases.
|
2017-04-13 07:43:58 +08:00
|
|
|
auto Cs = AL.filtered(OPT_C);
|
|
|
|
ASSERT_NE(Cs.begin(), Cs.end());
|
|
|
|
EXPECT_EQ("desu", StringRef((*Cs.begin())->getValue()));
|
2012-12-05 08:29:32 +08:00
|
|
|
ArgStringList ASL;
|
2017-04-13 07:43:58 +08:00
|
|
|
(*Cs.begin())->render(AL, ASL);
|
2016-04-15 08:23:15 +08:00
|
|
|
ASSERT_EQ(2u, ASL.size());
|
|
|
|
EXPECT_EQ("-C", StringRef(ASL[0]));
|
|
|
|
EXPECT_EQ("desu", StringRef(ASL[1]));
|
2012-12-05 08:29:32 +08:00
|
|
|
}
|
2013-07-20 02:05:13 +08:00
|
|
|
|
|
|
|
TEST(Option, ParseWithFlagExclusions) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
// Exclude flag3 to avoid parsing as OPT_SLASH_C.
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(Args, MAI, MAC,
|
|
|
|
/*FlagsToInclude=*/0,
|
|
|
|
/*FlagsToExclude=*/OptFlag3);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_C));
|
|
|
|
EXPECT_FALSE(AL.hasArg(OPT_SLASH_C));
|
2013-07-20 02:05:13 +08:00
|
|
|
|
|
|
|
// Exclude flag1 to avoid parsing as OPT_C.
|
2015-06-23 06:06:37 +08:00
|
|
|
AL = T.ParseArgs(Args, MAI, MAC,
|
|
|
|
/*FlagsToInclude=*/0,
|
|
|
|
/*FlagsToExclude=*/OptFlag1);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_B));
|
|
|
|
EXPECT_FALSE(AL.hasArg(OPT_C));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
|
2013-07-20 02:05:13 +08:00
|
|
|
|
|
|
|
const char *NewArgs[] = { "/C", "foo", "--C=bar" };
|
2015-06-23 06:06:37 +08:00
|
|
|
AL = T.ParseArgs(NewArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_SLASH_C));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_C));
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ("foo", AL.getLastArgValue(OPT_SLASH_C));
|
|
|
|
EXPECT_EQ("bar", AL.getLastArgValue(OPT_C));
|
2013-07-20 02:05:13 +08:00
|
|
|
}
|
2013-07-23 00:18:13 +08:00
|
|
|
|
|
|
|
TEST(Option, ParseAliasInGroup) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-I" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_H));
|
2013-07-23 00:18:13 +08:00
|
|
|
}
|
2013-08-01 06:44:41 +08:00
|
|
|
|
|
|
|
TEST(Option, AliasArgs) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-J", "-Joo" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_B));
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ("foo", AL.getAllArgValues(OPT_B)[0]);
|
|
|
|
EXPECT_EQ("bar", AL.getAllArgValues(OPT_B)[1]);
|
2013-08-01 06:44:41 +08:00
|
|
|
}
|
2013-08-03 05:20:27 +08:00
|
|
|
|
2013-08-29 04:04:31 +08:00
|
|
|
TEST(Option, IgnoreCase) {
|
|
|
|
TestOptTable T(true);
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-a", "-joo" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_B));
|
2013-08-29 04:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Option, DoNotIgnoreCase) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-a", "-joo" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_FALSE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_FALSE(AL.hasArg(OPT_B));
|
2013-08-29 04:04:31 +08:00
|
|
|
}
|
|
|
|
|
2013-08-14 05:09:50 +08:00
|
|
|
TEST(Option, SlurpEmpty) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-A", "-slurp" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_Slurp));
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ(0U, AL.getAllArgValues(OPT_Slurp).size());
|
2013-08-14 05:09:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Option, Slurp) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-A", "-slurp", "-B", "--", "foo" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_EQ(AL.size(), 2U);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_FALSE(AL.hasArg(OPT_B));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_Slurp));
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ(3U, AL.getAllArgValues(OPT_Slurp).size());
|
|
|
|
EXPECT_EQ("-B", AL.getAllArgValues(OPT_Slurp)[0]);
|
|
|
|
EXPECT_EQ("--", AL.getAllArgValues(OPT_Slurp)[1]);
|
|
|
|
EXPECT_EQ("foo", AL.getAllArgValues(OPT_Slurp)[2]);
|
2013-08-14 05:09:50 +08:00
|
|
|
}
|
2015-05-05 02:00:13 +08:00
|
|
|
|
2016-04-15 08:23:30 +08:00
|
|
|
TEST(Option, SlurpJoinedEmpty) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-A", "-slurpjoined" };
|
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
|
|
|
|
EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 0U);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Option, SlurpJoinedOneJoined) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-A", "-slurpjoinedfoo" };
|
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
|
|
|
|
EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined).size(), 1U);
|
|
|
|
EXPECT_EQ(AL.getAllArgValues(OPT_SlurpJoined)[0], "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Option, SlurpJoinedAndSeparate) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-A", "-slurpjoinedfoo", "bar", "baz" };
|
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
|
|
|
|
EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
|
|
|
|
EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
|
|
|
|
EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
|
|
|
|
EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Option, SlurpJoinedButSeparate) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
const char *MyArgs[] = { "-A", "-slurpjoined", "foo", "bar", "baz" };
|
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_A));
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_SlurpJoined));
|
|
|
|
EXPECT_EQ(3U, AL.getAllArgValues(OPT_SlurpJoined).size());
|
|
|
|
EXPECT_EQ("foo", AL.getAllArgValues(OPT_SlurpJoined)[0]);
|
|
|
|
EXPECT_EQ("bar", AL.getAllArgValues(OPT_SlurpJoined)[1]);
|
|
|
|
EXPECT_EQ("baz", AL.getAllArgValues(OPT_SlurpJoined)[2]);
|
|
|
|
}
|
|
|
|
|
2015-05-05 02:00:13 +08:00
|
|
|
TEST(Option, FlagAliasToJoined) {
|
|
|
|
TestOptTable T;
|
|
|
|
unsigned MAI, MAC;
|
|
|
|
|
|
|
|
// Check that a flag alias provides an empty argument to a joined option.
|
|
|
|
const char *MyArgs[] = { "-K" };
|
2015-06-23 06:06:37 +08:00
|
|
|
InputArgList AL = T.ParseArgs(MyArgs, MAI, MAC);
|
|
|
|
EXPECT_EQ(AL.size(), 1U);
|
|
|
|
EXPECT_TRUE(AL.hasArg(OPT_B));
|
2016-04-15 08:23:15 +08:00
|
|
|
EXPECT_EQ(1U, AL.getAllArgValues(OPT_B).size());
|
|
|
|
EXPECT_EQ("", AL.getAllArgValues(OPT_B)[0]);
|
2015-05-05 02:00:13 +08:00
|
|
|
}
|