Index: llvm/lib/Support/CommandLine.cpp =================================================================== --- llvm/lib/Support/CommandLine.cpp +++ llvm/lib/Support/CommandLine.cpp @@ -1097,43 +1097,73 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, SmallVectorImpl &Argv, bool MarkEOLs, bool RelativeNames) { - unsigned ExpandedRspFiles = 0; bool AllExpanded = true; + struct ResponseFileRecord { + const char *File; + size_t End; + }; + + SmallVector FileStack; + + // Push a dummy entry that represents the initial command line, removing + // the need to check for an empty list. + FileStack.push_back({"", Argv.size()}); // Don't cache Argv.size() because it can change. for (unsigned I = 0; I != Argv.size();) { + while (I == FileStack.back().End) { + FileStack.pop_back(); + } + const char *Arg = Argv[I]; // Check if it is an EOL marker if (Arg == nullptr) { ++I; continue; } + if (Arg[0] != '@') { ++I; continue; } - // If we have too many response files, leave some unexpanded. This avoids - // crashing on self-referential response files. - if (ExpandedRspFiles > 20) - return false; + const char *FName = Arg + 1; + auto IsEquivalent = [FName](const ResponseFileRecord &RFile) { + return sys::fs::equivalent(RFile.File, FName); + }; + + // Check for recursize response files. + if (std::any_of(FileStack.begin()+1, FileStack.end(), IsEquivalent)) { + // This file is recursive, so we leave it in the argument stream and + // move on. + AllExpanded = false; + ++I; + continue; + } // Replace this response file argument with the tokenization of its // contents. Nested response files are expanded in subsequent iterations. SmallVector ExpandedArgv; - if (ExpandResponseFile(Arg + 1, Saver, Tokenizer, ExpandedArgv, MarkEOLs, - RelativeNames)) { - ++ExpandedRspFiles; - } else { + if (!ExpandResponseFile(FName, Saver, Tokenizer, ExpandedArgv, MarkEOLs, + RelativeNames)) { // We couldn't read this file, so we leave it in the argument stream and // move on. AllExpanded = false; ++I; continue; } + + for (ResponseFileRecord &Record : FileStack) { + // All records are now bigger. Don't include the @resp file itself, so -1 + Record.End += ExpandedArgv.size() - 1; + } + + FileStack.push_back({FName, I + ExpandedArgv.size()}); Argv.erase(Argv.begin() + I); Argv.insert(Argv.begin() + I, ExpandedArgv.begin(), ExpandedArgv.end()); } + + assert(Argv.size() == FileStack.back().End); return AllExpanded; } Index: llvm/unittests/Support/CommandLineTest.cpp =================================================================== --- llvm/unittests/Support/CommandLineTest.cpp +++ llvm/unittests/Support/CommandLineTest.cpp @@ -790,7 +790,9 @@ EXPECT_TRUE(IncludedFile.is_open()); IncludedFile << "-option_1 -option_2\n" "@incdir/resp2\n" - "-option_3=abcd\n"; + "-option_3=abcd\n" + "@incdir/resp3\n" + "-option_4=efjk\n"; IncludedFile.close(); // Directory for included file. @@ -808,6 +810,15 @@ IncludedFile2 << "-option_23=abcd\n"; IncludedFile2.close(); + // Create second included response file of second level. + llvm::SmallString<128> IncludedFileName3; + llvm::sys::path::append(IncludedFileName3, IncDir, "resp3"); + std::ofstream IncludedFile3(IncludedFileName3.c_str()); + EXPECT_TRUE(IncludedFile3.is_open()); + IncludedFile3 << "-option_31 -option_32\n"; + IncludedFile3 << "-option_33=abcd\n"; + IncludedFile3.close(); + // Prepare 'file' with reference to response file. SmallString<128> IncRef; IncRef.append(1, '@'); @@ -821,7 +832,7 @@ bool Res = llvm::cl::ExpandResponseFiles( Saver, llvm::cl::TokenizeGNUCommandLine, Argv, false, true); EXPECT_TRUE(Res); - EXPECT_EQ(Argv.size(), 9U); + EXPECT_EQ(Argv.size(), 13U); EXPECT_STREQ(Argv[0], "test/test"); EXPECT_STREQ(Argv[1], "-flag_1"); EXPECT_STREQ(Argv[2], "-option_1"); @@ -830,8 +841,13 @@ EXPECT_STREQ(Argv[5], "-option_22"); EXPECT_STREQ(Argv[6], "-option_23=abcd"); EXPECT_STREQ(Argv[7], "-option_3=abcd"); - EXPECT_STREQ(Argv[8], "-flag_2"); + EXPECT_STREQ(Argv[8], "-option_31"); + EXPECT_STREQ(Argv[9], "-option_32"); + EXPECT_STREQ(Argv[10], "-option_33=abcd"); + EXPECT_STREQ(Argv[11], "-option_4=efjk"); + EXPECT_STREQ(Argv[12], "-flag_2"); + llvm::sys::fs::remove(IncludedFileName3); llvm::sys::fs::remove(IncludedFileName2); llvm::sys::fs::remove(IncDir); llvm::sys::fs::remove(IncludedFileName);