blob: 90e3db8ec40cb1c660c7fa02e5d5a2877a15a09b [file] [edit]
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <optional>
namespace test {
enum class Mode { A, B };
} // namespace test
#define OPTIONS_STRUCT_DECL
#include "LibraryOpts.inc"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/LibraryOptions.h"
#include "llvm/Support/LibraryOptionsContext.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#define OPTIONS_STRUCT_DEFS
#include "LibraryOpts.inc"
using namespace llvm;
using test::TestLibraryOptions;
namespace {
// The struct -gen-opt-parser-defs generates: every spelling sets its member.
TEST(LibraryOptionsTest, Apply) {
BumpPtrAllocator Alloc;
TestLibraryOptions O;
EXPECT_FALSE(O.enable);
EXPECT_EQ(O.count, 3u);
EXPECT_EQ(O.limit, std::nullopt);
EXPECT_THAT(O.list, testing::IsEmpty());
EXPECT_EQ(O.mode, test::Mode::A);
EXPECT_THAT(O.modes, testing::IsEmpty());
EXPECT_EQ(O.override, BoolOrDefault::Default);
EXPECT_EQ(O.print, std::nullopt);
EXPECT_EQ(O.ratio, 0.5);
EXPECT_EQ(O.tristate, BoolOrDefault::Default);
EXPECT_EQ(O.Path, "p");
auto Apply = [&](std::initializer_list<const char *> Argv) {
unsigned MissingIndex, MissingCount;
opt::InputArgList Args = TestLibraryOptions::optTable().ParseArgs(
Argv, MissingIndex, MissingCount);
std::vector<bool> Applied;
for (const opt::Arg *A : Args)
Applied.push_back(O.apply(*A, Alloc));
return Applied;
};
EXPECT_THAT(Apply({"-lib-enable", "--lib-count=7", "-lib-limit=0",
"-lib-mode", "b", "-lib-override", "-lib-ratio", "0.25",
"-lib-tristate=Disable", "-lib-path=a=b"}),
testing::Each(true));
EXPECT_TRUE(O.enable);
EXPECT_EQ(O.count, 7u);
EXPECT_EQ(O.limit, 0u);
EXPECT_EQ(O.mode, test::Mode::B);
EXPECT_EQ(O.override, BoolOrDefault::True);
EXPECT_EQ(O.ratio, 0.25);
EXPECT_EQ(O.tristate, BoolOrDefault::False);
EXPECT_EQ(O.Path, "a=b");
EXPECT_THAT(Apply({"-lib-enable=false"}), testing::Each(true));
EXPECT_FALSE(O.enable);
EXPECT_THAT(
Apply({"-lib-enable=1", "-lib-override=false", "-lib-tristate=Enable"}),
testing::Each(true));
EXPECT_TRUE(O.enable);
EXPECT_EQ(O.override, BoolOrDefault::False);
EXPECT_EQ(O.tristate, BoolOrDefault::True);
EXPECT_THAT(Apply({"-lib-tristate=Default"}), testing::Each(true));
EXPECT_EQ(O.tristate, BoolOrDefault::Default);
EXPECT_THAT(Apply({"-lib-print=a"}), testing::Each(true));
EXPECT_EQ(O.print, test::Mode::A);
EXPECT_THAT(Apply({"-lib-print"}), testing::Each(true));
EXPECT_EQ(O.print, test::Mode::B);
// Each occurrence of a list appends its comma-separated values.
EXPECT_THAT(Apply({"-lib-list=1,2", "-lib-list", "3"}), testing::Each(true));
EXPECT_THAT(O.list, testing::ElementsAre(1u, 2u, 3u));
EXPECT_THAT(Apply({"-lib-modes=b,a", "-lib-modes", "b"}),
testing::Each(true));
EXPECT_THAT(O.modes, testing::ElementsAre(test::Mode::B, test::Mode::A,
test::Mode::B));
// A rejected value leaves the member unchanged.
EXPECT_THAT(
Apply({"-lib-enable=2", "-lib-count=-1", "-lib-limit=x", "-lib-list=4,x",
"-lib-list=", "-lib-mode=c", "-lib-modes=a,c", "-lib-override=y",
"-lib-print=c", "-lib-ratio=y"}),
testing::Each(false));
EXPECT_TRUE(O.enable);
EXPECT_EQ(O.count, 7u);
EXPECT_EQ(O.limit, 0u);
EXPECT_THAT(O.list, testing::ElementsAre(1u, 2u, 3u));
EXPECT_EQ(O.mode, test::Mode::B);
EXPECT_THAT(O.modes, testing::ElementsAre(test::Mode::B, test::Mode::A,
test::Mode::B));
EXPECT_EQ(O.override, BoolOrDefault::False);
EXPECT_EQ(O.print, test::Mode::B);
EXPECT_EQ(O.ratio, 0.25);
}
TEST(LibraryOptionsTest, BoolOrDefault) {
EXPECT_TRUE(valueOr(BoolOrDefault::Default, true));
EXPECT_FALSE(valueOr(BoolOrDefault::Default, false));
EXPECT_TRUE(valueOr(BoolOrDefault::True, false));
EXPECT_FALSE(valueOr(BoolOrDefault::False, true));
}
// What cl:: sees of the struct, without cl::.
TEST(LibraryOptionsTest, Parser) {
opt::LibraryOptionsParser P(std::in_place_type<TestLibraryOptions>);
std::vector<std::string> Rows;
P.forEachOption([&](StringRef Spelling, StringRef MetaVar, StringRef Help) {
Rows.push_back((Spelling + "|" + MetaVar + "|" + Help).str());
});
EXPECT_THAT(Rows,
testing::ElementsAre(
"lib-count|=<value>|An unsigned", "lib-disable||",
"lib-enable||A bool", "lib-in-build||Available",
"lib-limit|=<value>|An optional", "lib-list|=<value>|A list",
"lib-mode-b||", "lib-modes|=<a|b>|An enum list",
"lib-mode|=<a|b>|An enum", "lib-number|=<value>|",
"lib-old-mode|=<value>|", "lib-override||An optional bool",
"lib-path|=<value>|A string",
"lib-print|[=<a|b>]|A flag or enum",
"lib-ratio|=<value>|A double",
"lib-tristate|=<Default|Enable|Disable>|A tri-state"));
BumpPtrAllocator Alloc;
TestLibraryOptions O;
auto Parse = [&](std::initializer_list<const char *> Argv) {
unsigned Consumed = 0;
std::string Err = toString(P.parse(&O, Argv, Consumed, Alloc));
return std::to_string(Consumed) + " " + Err;
};
EXPECT_EQ(Parse({"-lib-count", "5"}), "2 ");
EXPECT_EQ(O.count, 5u);
EXPECT_EQ(Parse({"-lib-count=x", "-lib-enable"}),
"1 invalid value 'x' in '-lib-count=x'");
EXPECT_EQ(Parse({"-lib-count"}),
"1 option '-lib-count' requires an argument");
EXPECT_EQ(Parse({"-lib-other"}), "1 unknown argument '-lib-other'");
EXPECT_EQ(Parse({"-lib-counts=1"}), "1 unknown argument '-lib-counts=1'");
EXPECT_EQ(Parse({"-lib-in-build"}), "1 ");
EXPECT_TRUE(O.in_build);
EXPECT_EQ(Parse({"-lib-not-in-build"}),
"1 unknown argument '-lib-not-in-build'");
EXPECT_EQ(Parse({"-lib-enable", "0"}), "1 ");
EXPECT_EQ(Parse({"-lib-print", "a"}), "1 ");
EXPECT_EQ(Parse({"-lib-count", "x"}),
"2 invalid value 'x' in '-lib-count=x'");
EXPECT_EQ(Parse({"-lib-enable=x"}), "1 invalid value 'x' in '-lib-enable=x'");
EXPECT_EQ(Parse({"-lib-disable"}), "1 ");
EXPECT_FALSE(O.enable);
EXPECT_EQ(Parse({"-lib-disable=1"}), "1 unknown argument '-lib-disable=1'");
EXPECT_EQ(Parse({"-lib-number", "6"}), "2 ");
EXPECT_EQ(O.count, 6u);
EXPECT_EQ(Parse({"-lib-mode-b"}), "1 ");
EXPECT_EQ(O.mode, test::Mode::B);
EXPECT_EQ(Parse({"-lib-old-mode", "a"}), "2 ");
EXPECT_EQ(O.mode, test::Mode::A);
EXPECT_EQ(Parse({"-lib-old-mode=c"}),
"1 invalid value 'c' in '-lib-old-mode=c'");
}
// A static RegisterLibraryOptions connects the struct to
// cl::parseCommandLineOptions.
TEST(LibraryOptionsTest, Register) {
cl::ResetCommandLineParser();
opt::RegisterLibraryOptions<TestLibraryOptions> Registration;
std::string Path = "-lib-path=q";
const char *Args[] = {"prog", "-lib-count", "5",
"-lib-enable", Path.c_str(), "-lib-list=1,2"};
cl::LibraryOptionsContext V;
EXPECT_TRUE(cl::parseCommandLineOptions(V, Args, "", &nulls()));
const TestLibraryOptions &O = V.get<TestLibraryOptions>();
EXPECT_EQ(O.count, 5u);
EXPECT_TRUE(O.enable);
EXPECT_THAT(O.list, testing::ElementsAre(1u, 2u));
// A StringRef member does not refer to the caller's argument.
Path.assign(Path.size(), 'x');
EXPECT_EQ(O.Path, "q");
// ParseCommandLineOptions, which takes no context, does not recognize them.
EXPECT_FALSE(
cl::ParseCommandLineOptions(std::size(Args), Args, "", &nulls()));
cl::ResetCommandLineParser();
}
// Values that override the defaults, as an LLVMContext holds them.
TEST(LibraryOptionsTest, Values) {
cl::ResetCommandLineParser();
opt::RegisterLibraryOptions<TestLibraryOptions> Registration;
opt::LibraryOptionsParser L{std::in_place_type<TestLibraryOptions>};
auto Parse = [&](cl::LibraryOptionsContext &V, ArrayRef<const char *> Args) {
unsigned N;
return toString(V.parse(L, Args, N));
};
cl::LibraryOptionsContext V;
std::string Path = "-lib-path=q";
EXPECT_EQ(Parse(V, {"-lib-count", "7"}), "");
EXPECT_EQ(Parse(V, Path.c_str()), "");
EXPECT_EQ(Parse(V, "--lib-list=1,2"), "");
Path.assign(Path.size(), ' ');
EXPECT_EQ(Parse(V, "-lib-count=x"), "invalid value 'x' in '-lib-count=x'");
// assign() replays the arguments, which outlive the caller's, and discards
// the earlier values.
cl::LibraryOptionsContext Copy;
EXPECT_EQ(Parse(Copy, "-lib-list=9"), "");
Copy.assign(V);
const TestLibraryOptions &C = Copy.get<TestLibraryOptions>();
EXPECT_EQ(C.count, 7u);
EXPECT_EQ(C.Path, "q");
EXPECT_THAT(C.list, testing::ElementsAre(1u, 2u));
EXPECT_EQ(Copy.args().size(), V.args().size());
cl::ResetCommandLineParser();
}
// A parse recognizes the options of the libraries that its caller passes, until
// a parse passes other libraries.
TEST(LibraryOptionsTest, ToolOptions) {
cl::ResetCommandLineParser();
cl::opt<bool> Unrelated("tool-options-unrelated");
opt::LibraryOptionsParser L{std::in_place_type<TestLibraryOptions>};
cl::LibraryOptionsContext V;
const char *Args[] = {"prog", "-lib-count=5"};
EXPECT_TRUE(cl::parseCommandLineOptions(V, &L, Args, "", &nulls()));
EXPECT_EQ(V.get<TestLibraryOptions>().count, 5u);
unsigned N;
EXPECT_EQ(toString(V.parse(L, {"-lib-count", "7"}, N)), "");
EXPECT_FALSE(cl::parseCommandLineOptions(V, Args, "", &nulls()));
// assign() replays each argument with the library that parsed it.
cl::LibraryOptionsContext Copy;
Copy.assign(V);
EXPECT_EQ(Copy.get<TestLibraryOptions>().count, 7u);
cl::ResetCommandLineParser();
}
} // namespace