diff options
author | Serge Pavlov <sepavloff@gmail.com> | 2022-08-22 00:30:28 +0700 |
---|---|---|
committer | Serge Pavlov <sepavloff@gmail.com> | 2022-09-28 11:47:59 +0700 |
commit | 6e491c48d6b9cadcc5b77f730dd83a1448197329 (patch) | |
tree | 3bd4844ff52cac131d05cb7999550cafad182bb1 /llvm/lib/Support/CommandLine.cpp | |
parent | 89e56e732d5e89d8715a501158793ac305bc4b70 (diff) | |
download | llvm-6e491c48d6b9cadcc5b77f730dd83a1448197329.zip llvm-6e491c48d6b9cadcc5b77f730dd83a1448197329.tar.gz llvm-6e491c48d6b9cadcc5b77f730dd83a1448197329.tar.bz2 |
[Support] Class for response file expansion (NFC)
Functions that implement expansion of response and config files depend
on many options, which are passes as arguments. Extending the expansion
requires new options, it in turn causes changing calls in various places
making them even more bulky.
This change introduces a class ExpansionContext, which represents set of
options that control the expansion. Its methods implements expansion of
responce files including config files. It makes extending the expansion
easier.
No functional changes.
Differential Revision: https://reviews.llvm.org/D132379
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r-- | llvm/lib/Support/CommandLine.cpp | 78 |
1 files changed, 30 insertions, 48 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp index d81e115..899cc52 100644 --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -1153,15 +1153,12 @@ static void ExpandBasePaths(StringRef BasePath, StringSaver &Saver, } // FName must be an absolute path. -static llvm::Error ExpandResponseFile(StringRef FName, StringSaver &Saver, - TokenizerCallback Tokenizer, - SmallVectorImpl<const char *> &NewArgv, - bool MarkEOLs, bool RelativeNames, - bool ExpandBasePath, - llvm::vfs::FileSystem &FS) { +llvm::Error +ExpansionContext::expandResponseFile(StringRef FName, + SmallVectorImpl<const char *> &NewArgv) { assert(sys::path::is_absolute(FName)); llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr = - FS.getBufferForFile(FName); + FS->getBufferForFile(FName); if (!MemBufOrErr) return llvm::errorCodeToError(MemBufOrErr.getError()); MemoryBuffer &MemBuf = *MemBufOrErr.get(); @@ -1196,7 +1193,7 @@ static llvm::Error ExpandResponseFile(StringRef FName, StringSaver &Saver, continue; // Substitute <CFGDIR> with the file's base path. - if (ExpandBasePath) + if (InConfigFile) ExpandBasePaths(BasePath, Saver, Arg); // Skip non-rsp file arguments. @@ -1219,11 +1216,8 @@ static llvm::Error ExpandResponseFile(StringRef FName, StringSaver &Saver, /// Expand response files on a command line recursively using the given /// StringSaver and tokenization strategy. -bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, - SmallVectorImpl<const char *> &Argv, bool MarkEOLs, - bool RelativeNames, bool ExpandBasePath, - llvm::Optional<llvm::StringRef> CurrentDir, - llvm::vfs::FileSystem &FS) { +bool ExpansionContext::expandResponseFiles( + SmallVectorImpl<const char *> &Argv) { bool AllExpanded = true; struct ResponseFileRecord { std::string File; @@ -1264,8 +1258,8 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, // always have an absolute path deduced from the containing file. SmallString<128> CurrDir; if (llvm::sys::path::is_relative(FName)) { - if (!CurrentDir) { - if (auto CWD = FS.getCurrentWorkingDirectory()) { + if (CurrentDir.empty()) { + if (auto CWD = FS->getCurrentWorkingDirectory()) { CurrDir = *CWD; } else { // TODO: The error should be propagated up the stack. @@ -1273,19 +1267,19 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, return false; } } else { - CurrDir = *CurrentDir; + CurrDir = CurrentDir; } llvm::sys::path::append(CurrDir, FName); FName = CurrDir.c_str(); } - auto IsEquivalent = [FName, &FS](const ResponseFileRecord &RFile) { - llvm::ErrorOr<llvm::vfs::Status> LHS = FS.status(FName); + auto IsEquivalent = [FName, this](const ResponseFileRecord &RFile) { + llvm::ErrorOr<llvm::vfs::Status> LHS = FS->status(FName); if (!LHS) { // TODO: The error should be propagated up the stack. llvm::consumeError(llvm::errorCodeToError(LHS.getError())); return false; } - llvm::ErrorOr<llvm::vfs::Status> RHS = FS.status(RFile.File); + llvm::ErrorOr<llvm::vfs::Status> RHS = FS->status(RFile.File); if (!RHS) { // TODO: The error should be propagated up the stack. llvm::consumeError(llvm::errorCodeToError(RHS.getError())); @@ -1306,9 +1300,7 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, // Replace this response file argument with the tokenization of its // contents. Nested response files are expanded in subsequent iterations. SmallVector<const char *, 0> ExpandedArgv; - if (llvm::Error Err = - ExpandResponseFile(FName, Saver, Tokenizer, ExpandedArgv, MarkEOLs, - RelativeNames, ExpandBasePath, FS)) { + if (llvm::Error Err = expandResponseFile(FName, ExpandedArgv)) { // We couldn't read this file, so we leave it in the argument stream and // move on. // TODO: The error should be propagated up the stack. @@ -1338,15 +1330,6 @@ bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, return AllExpanded; } -bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, - SmallVectorImpl<const char *> &Argv, bool MarkEOLs, - bool RelativeNames, bool ExpandBasePath, - llvm::Optional<StringRef> CurrentDir) { - return ExpandResponseFiles(Saver, std::move(Tokenizer), Argv, MarkEOLs, - RelativeNames, ExpandBasePath, - std::move(CurrentDir), *vfs::getRealFileSystem()); -} - bool cl::expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar, StringSaver &Saver, SmallVectorImpl<const char *> &NewArgv) { @@ -1360,30 +1343,30 @@ bool cl::expandResponseFiles(int Argc, const char *const *Argv, // Command line options can override the environment variable. NewArgv.append(Argv + 1, Argv + Argc); - return ExpandResponseFiles(Saver, Tokenize, NewArgv); + ExpansionContext ECtx(Saver.getAllocator(), Tokenize); + return ECtx.expandResponseFiles(NewArgv); } -bool cl::readConfigFile(StringRef CfgFile, StringSaver &Saver, - SmallVectorImpl<const char *> &Argv, - llvm::vfs::FileSystem &FS) { +ExpansionContext::ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T) + : Saver(A), Tokenizer(T), FS(vfs::getRealFileSystem().get()) {} + +bool ExpansionContext::readConfigFile(StringRef CfgFile, + SmallVectorImpl<const char *> &Argv) { SmallString<128> AbsPath; if (sys::path::is_relative(CfgFile)) { AbsPath.assign(CfgFile); - if (std::error_code EC = FS.makeAbsolute(AbsPath)) + if (std::error_code EC = FS->makeAbsolute(AbsPath)) return false; CfgFile = AbsPath.str(); } - if (llvm::Error Err = - ExpandResponseFile(CfgFile, Saver, cl::tokenizeConfigFile, Argv, - /*MarkEOLs=*/false, /*RelativeNames=*/true, - /*ExpandBasePath=*/true, FS)) { + InConfigFile = true; + RelativeNames = true; + if (llvm::Error Err = expandResponseFile(CfgFile, Argv)) { // TODO: The error should be propagated up the stack. llvm::consumeError(std::move(Err)); return false; } - return ExpandResponseFiles(Saver, cl::tokenizeConfigFile, Argv, - /*MarkEOLs=*/false, /*RelativeNames=*/true, - /*ExpandBasePath=*/true, llvm::None, FS); + return expandResponseFiles(Argv); } static void initCommonOptions(); @@ -1441,11 +1424,10 @@ bool CommandLineParser::ParseCommandLineOptions(int argc, // Expand response files. SmallVector<const char *, 20> newArgv(argv, argv + argc); BumpPtrAllocator A; - StringSaver Saver(A); - ExpandResponseFiles(Saver, - Triple(sys::getProcessTriple()).isOSWindows() ? - cl::TokenizeWindowsCommandLine : cl::TokenizeGNUCommandLine, - newArgv); + ExpansionContext ECtx(A, Triple(sys::getProcessTriple()).isOSWindows() + ? cl::TokenizeWindowsCommandLine + : cl::TokenizeGNUCommandLine); + ECtx.expandResponseFiles(newArgv); argv = &newArgv[0]; argc = static_cast<int>(newArgv.size()); |