aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/CommandLineTest.cpp
diff options
context:
space:
mode:
authorSerge Pavlov <sepavloff@gmail.com>2022-10-31 12:59:15 +0700
committerSerge Pavlov <sepavloff@gmail.com>2022-10-31 15:36:41 +0700
commitfd3d7a9f8cbb5ad81fb96d92d5f7b1d51a4d4127 (patch)
tree085b3e47f468106d61b52a438fb1617156afd46f /llvm/unittests/Support/CommandLineTest.cpp
parent1af81ab4846f0d07d3ac3079f95d66779a4d9430 (diff)
downloadllvm-fd3d7a9f8cbb5ad81fb96d92d5f7b1d51a4d4127.zip
llvm-fd3d7a9f8cbb5ad81fb96d92d5f7b1d51a4d4127.tar.gz
llvm-fd3d7a9f8cbb5ad81fb96d92d5f7b1d51a4d4127.tar.bz2
Handle errors in expansion of response files
Previously an error raised during an expansion of response files (including configuration files) was ignored and only the fact of its presence was reported to the user with generic error messages. This made it difficult to analyze problems. For example, if a configuration file tried to read an inexistent file, the error message said that 'configuration file cannot be found', which is wrong and misleading. This change enhances handling errors in the expansion so that users could get more informative error messages. Differential Revision: https://reviews.llvm.org/D136090
Diffstat (limited to 'llvm/unittests/Support/CommandLineTest.cpp')
-rw-r--r--llvm/unittests/Support/CommandLineTest.cpp45
1 files changed, 38 insertions, 7 deletions
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index dd6e122..26e82d1 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -871,7 +871,7 @@ TEST(CommandLineTest, ResponseFiles) {
llvm::BumpPtrAllocator A;
llvm::cl::ExpansionContext ECtx(A, llvm::cl::TokenizeGNUCommandLine);
ECtx.setVFS(&FS).setCurrentDir(TestRoot).setRelativeNames(true);
- ASSERT_TRUE(ECtx.expandResponseFiles(Argv));
+ ASSERT_FALSE((bool)ECtx.expandResponseFiles(Argv));
EXPECT_THAT(Argv, testing::Pointwise(
StringEquality(),
{"test/test", "-flag_1", "-option_1", "-option_2",
@@ -933,7 +933,14 @@ TEST(CommandLineTest, RecursiveResponseFiles) {
#endif
llvm::cl::ExpansionContext ECtx(A, Tokenizer);
ECtx.setVFS(&FS).setCurrentDir(TestRoot);
- ASSERT_FALSE(ECtx.expandResponseFiles(Argv));
+ llvm::Error Err = ECtx.expandResponseFiles(Argv);
+ ASSERT_TRUE((bool)Err);
+ SmallString<128> FilePath = SelfFilePath;
+ std::error_code EC = FS.makeAbsolute(FilePath);
+ ASSERT_FALSE((bool)EC);
+ std::string ExpectedMessage =
+ std::string("recursive expansion of: '") + std::string(FilePath) + "'";
+ ASSERT_TRUE(toString(std::move(Err)) == ExpectedMessage);
EXPECT_THAT(Argv,
testing::Pointwise(StringEquality(),
@@ -971,7 +978,7 @@ TEST(CommandLineTest, ResponseFilesAtArguments) {
BumpPtrAllocator A;
llvm::cl::ExpansionContext ECtx(A, cl::TokenizeGNUCommandLine);
ECtx.setVFS(&FS).setCurrentDir(TestRoot);
- ASSERT_FALSE(ECtx.expandResponseFiles(Argv));
+ ASSERT_FALSE((bool)ECtx.expandResponseFiles(Argv));
// ASSERT instead of EXPECT to prevent potential out-of-bounds access.
ASSERT_EQ(Argv.size(), 1 + NON_RSP_AT_ARGS + 2);
@@ -1005,7 +1012,7 @@ TEST(CommandLineTest, ResponseFileRelativePath) {
BumpPtrAllocator A;
llvm::cl::ExpansionContext ECtx(A, cl::TokenizeGNUCommandLine);
ECtx.setVFS(&FS).setCurrentDir(TestRoot).setRelativeNames(true);
- ASSERT_TRUE(ECtx.expandResponseFiles(Argv));
+ ASSERT_FALSE((bool)ECtx.expandResponseFiles(Argv));
EXPECT_THAT(Argv,
testing::Pointwise(StringEquality(), {"test/test", "-flag"}));
}
@@ -1025,7 +1032,7 @@ TEST(CommandLineTest, ResponseFileEOLs) {
llvm::cl::ExpansionContext ECtx(A, cl::TokenizeWindowsCommandLine);
ECtx.setVFS(&FS).setCurrentDir(TestRoot).setMarkEOLs(true).setRelativeNames(
true);
- ASSERT_TRUE(ECtx.expandResponseFiles(Argv));
+ ASSERT_FALSE((bool)ECtx.expandResponseFiles(Argv));
const char *Expected[] = {"clang", "-Xclang", "-Wno-whatever", nullptr,
"input.cpp"};
ASSERT_EQ(std::size(Expected), Argv.size());
@@ -1038,6 +1045,30 @@ TEST(CommandLineTest, ResponseFileEOLs) {
}
}
+TEST(CommandLineTest, BadResponseFile) {
+ BumpPtrAllocator A;
+ StringSaver Saver(A);
+ TempDir ADir("dir", /*Unique*/ true);
+ SmallString<128> AFilePath = ADir.path();
+ llvm::sys::path::append(AFilePath, "file.rsp");
+ std::string AFileExp = std::string("@") + std::string(AFilePath.str());
+ SmallVector<const char *, 2> Argv = {"clang", AFileExp.c_str()};
+
+ bool Res = cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Argv);
+ ASSERT_TRUE(Res);
+ ASSERT_EQ(2U, Argv.size());
+ ASSERT_STREQ(Argv[0], "clang");
+ ASSERT_STREQ(Argv[1], AFileExp.c_str());
+
+ std::string ADirExp = std::string("@") + std::string(ADir.path());
+ Argv = {"clang", ADirExp.c_str()};
+ Res = cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Argv);
+ ASSERT_FALSE(Res);
+ ASSERT_EQ(2U, Argv.size());
+ ASSERT_STREQ(Argv[0], "clang");
+ ASSERT_STREQ(Argv[1], ADirExp.c_str());
+}
+
TEST(CommandLineTest, SetDefaultValue) {
cl::ResetCommandLineParser();
@@ -1145,9 +1176,9 @@ TEST(CommandLineTest, ReadConfigFile) {
llvm::BumpPtrAllocator A;
llvm::cl::ExpansionContext ECtx(A, cl::tokenizeConfigFile);
- bool Result = ECtx.readConfigFile(ConfigFile.path(), Argv);
+ llvm::Error Result = ECtx.readConfigFile(ConfigFile.path(), Argv);
- EXPECT_TRUE(Result);
+ EXPECT_FALSE((bool)Result);
EXPECT_EQ(Argv.size(), 13U);
EXPECT_STREQ(Argv[0], "-option_1");
EXPECT_STREQ(Argv[1],