2009-09-24 09:14:07 +08:00
|
|
|
//===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-19 00:52:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2009-09-24 09:14:07 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-09-26 05:07:20 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2009-09-24 09:14:07 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <stdlib.h>
|
2012-12-04 18:23:08 +08:00
|
|
|
#include <string>
|
2009-09-24 09:14:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TempEnvVar {
|
|
|
|
public:
|
|
|
|
TempEnvVar(const char *name, const char *value)
|
|
|
|
: name(name) {
|
|
|
|
const char *old_value = getenv(name);
|
|
|
|
EXPECT_EQ(NULL, old_value) << old_value;
|
2009-09-26 05:07:20 +08:00
|
|
|
#if HAVE_SETENV
|
2009-09-24 09:14:07 +08:00
|
|
|
setenv(name, value, true);
|
2009-09-26 05:07:20 +08:00
|
|
|
#else
|
|
|
|
# define SKIP_ENVIRONMENT_TESTS
|
|
|
|
#endif
|
2009-09-24 09:14:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~TempEnvVar() {
|
2009-09-26 05:07:20 +08:00
|
|
|
#if HAVE_SETENV
|
|
|
|
// Assume setenv and unsetenv come together.
|
2009-09-24 09:14:07 +08:00
|
|
|
unsetenv(name);
|
2009-09-26 05:07:20 +08:00
|
|
|
#endif
|
2009-09-24 09:14:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char *const name;
|
|
|
|
};
|
|
|
|
|
2013-05-07 05:56:35 +08:00
|
|
|
cl::OptionCategory TestCategory("Test Options", "Description");
|
|
|
|
cl::opt<int> TestOption("test-option", cl::desc("old description"));
|
|
|
|
TEST(CommandLineTest, ModifyExisitingOption) {
|
|
|
|
const char Description[] = "New description";
|
|
|
|
const char ArgString[] = "new-test-option";
|
|
|
|
const char ValueString[] = "Integer";
|
|
|
|
|
|
|
|
StringMap<cl::Option*> Map;
|
|
|
|
cl::getRegisteredOptions(Map);
|
|
|
|
|
|
|
|
ASSERT_TRUE(Map.count("test-option") == 1) <<
|
|
|
|
"Could not find option in map.";
|
|
|
|
|
|
|
|
cl::Option *Retrieved = Map["test-option"];
|
|
|
|
ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
|
|
|
|
|
|
|
|
ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
|
|
|
|
"Incorrect default option category.";
|
|
|
|
|
|
|
|
Retrieved->setCategory(TestCategory);
|
|
|
|
ASSERT_EQ(&TestCategory,Retrieved->Category) <<
|
|
|
|
"Failed to modify option's option category.";
|
|
|
|
|
|
|
|
Retrieved->setDescription(Description);
|
|
|
|
ASSERT_STREQ(Retrieved->HelpStr, Description) <<
|
|
|
|
"Changing option description failed.";
|
|
|
|
|
|
|
|
Retrieved->setArgStr(ArgString);
|
|
|
|
ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
|
|
|
|
"Failed to modify option's Argument string.";
|
|
|
|
|
|
|
|
Retrieved->setValueStr(ValueString);
|
|
|
|
ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
|
|
|
|
"Failed to modify option's Value string.";
|
|
|
|
|
|
|
|
Retrieved->setHiddenFlag(cl::Hidden);
|
|
|
|
ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
|
|
|
|
"Failed to modify option's hidden flag.";
|
|
|
|
}
|
2009-09-26 05:07:20 +08:00
|
|
|
#ifndef SKIP_ENVIRONMENT_TESTS
|
|
|
|
|
2009-09-24 09:14:07 +08:00
|
|
|
const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
|
|
|
|
|
|
|
|
cl::opt<std::string> EnvironmentTestOption("env-test-opt");
|
|
|
|
TEST(CommandLineTest, ParseEnvironment) {
|
|
|
|
TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
|
|
|
|
EXPECT_EQ("", EnvironmentTestOption);
|
|
|
|
cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
|
|
|
|
EXPECT_EQ("hello", EnvironmentTestOption);
|
|
|
|
}
|
|
|
|
|
2012-08-13 18:43:36 +08:00
|
|
|
// This test used to make valgrind complain
|
|
|
|
// ("Conditional jump or move depends on uninitialised value(s)")
|
2013-05-07 05:56:35 +08:00
|
|
|
//
|
|
|
|
// Warning: Do not run any tests after this one that try to gain access to
|
|
|
|
// registered command line options because this will likely result in a
|
|
|
|
// SEGFAULT. This can occur because the cl::opt in the test below is declared
|
|
|
|
// on the stack which will be destroyed after the test completes but the
|
|
|
|
// command line system will still hold a pointer to a deallocated cl::Option.
|
2012-08-13 18:43:36 +08:00
|
|
|
TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
|
|
|
|
// Put cl::opt on stack to check for proper initialization of fields.
|
|
|
|
cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
|
|
|
|
TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
|
|
|
|
EXPECT_EQ("", EnvironmentTestOptionLocal);
|
|
|
|
cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
|
|
|
|
EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
|
|
|
|
}
|
|
|
|
|
2009-09-26 05:07:20 +08:00
|
|
|
#endif // SKIP_ENVIRONMENT_TESTS
|
|
|
|
|
2013-05-07 05:56:23 +08:00
|
|
|
TEST(CommandLineTest, UseOptionCategory) {
|
2013-05-07 05:56:35 +08:00
|
|
|
cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
|
2013-05-07 05:56:23 +08:00
|
|
|
|
2013-05-07 05:56:35 +08:00
|
|
|
ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
|
2013-05-07 05:56:23 +08:00
|
|
|
"Category.";
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:52:05 +08:00
|
|
|
class StrDupSaver : public cl::StringSaver {
|
|
|
|
const char *SaveString(const char *Str) LLVM_OVERRIDE {
|
|
|
|
return strdup(Str);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-31 03:03:20 +08:00
|
|
|
typedef void ParserFunction(StringRef Source, llvm::cl::StringSaver &Saver,
|
|
|
|
SmallVectorImpl<const char *> &NewArgv);
|
|
|
|
|
|
|
|
|
|
|
|
void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
|
|
|
|
const char *const Output[], size_t OutputSize) {
|
2013-07-19 00:52:05 +08:00
|
|
|
SmallVector<const char *, 0> Actual;
|
|
|
|
StrDupSaver Saver;
|
2013-07-31 03:03:20 +08:00
|
|
|
parse(Input, Saver, Actual);
|
|
|
|
EXPECT_EQ(OutputSize, Actual.size());
|
2013-07-19 00:52:05 +08:00
|
|
|
for (unsigned I = 0, E = Actual.size(); I != E; ++I) {
|
2013-07-31 03:03:20 +08:00
|
|
|
if (I < OutputSize)
|
2013-07-19 00:52:05 +08:00
|
|
|
EXPECT_STREQ(Output[I], Actual[I]);
|
|
|
|
free(const_cast<char *>(Actual[I]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 03:03:20 +08:00
|
|
|
TEST(CommandLineTest, TokenizeGNUCommandLine) {
|
|
|
|
const char *Input = "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' "
|
|
|
|
"foo\"bar\"baz C:\\src\\foo.cpp \"C:\\src\\foo.cpp\"";
|
|
|
|
const char *const Output[] = { "foo bar", "foo bar", "foo bar", "foo\\bar",
|
|
|
|
"foobarbaz", "C:\\src\\foo.cpp",
|
|
|
|
"C:\\src\\foo.cpp" };
|
|
|
|
testCommandLineTokenizer(cl::TokenizeGNUCommandLine, Input, Output,
|
|
|
|
array_lengthof(Output));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CommandLineTest, TokenizeWindowsCommandLine) {
|
|
|
|
const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
|
|
|
|
"\"st \\\"u\" \\v";
|
|
|
|
const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
|
|
|
|
"lmn", "o", "pqr", "st \"u", "\\v" };
|
|
|
|
testCommandLineTokenizer(cl::TokenizeWindowsCommandLine, Input, Output,
|
|
|
|
array_lengthof(Output));
|
|
|
|
}
|
|
|
|
|
2009-09-24 09:14:07 +08:00
|
|
|
} // anonymous namespace
|