aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/CommandLine.cpp
diff options
context:
space:
mode:
authorSerge Pavlov <sepavloff@gmail.com>2022-10-19 17:19:04 +0700
committerSerge Pavlov <sepavloff@gmail.com>2022-10-19 17:20:14 +0700
commit0dec5e164f9d289b6e576655c7cf21a3dd0389f8 (patch)
treefb9af808f7b898b215aa387367d26cef40b206a3 /llvm/lib/Support/CommandLine.cpp
parent42230efccf8fe1185be5fa6c23dce0a8183d6ec9 (diff)
downloadllvm-0dec5e164f9d289b6e576655c7cf21a3dd0389f8.zip
llvm-0dec5e164f9d289b6e576655c7cf21a3dd0389f8.tar.gz
llvm-0dec5e164f9d289b6e576655c7cf21a3dd0389f8.tar.bz2
Keep configuration file search directories in ExpansionContext. NFC
Class ExpansionContext encapsulates options for search and expansion of response files, including configuration files. With this change the directories which are searched for configuration files are also stored in ExpansionContext. Differential Revision: https://reviews.llvm.org/D135439
Diffstat (limited to 'llvm/lib/Support/CommandLine.cpp')
-rw-r--r--llvm/lib/Support/CommandLine.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 899cc52..3986c91 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -1350,6 +1350,43 @@ bool cl::expandResponseFiles(int Argc, const char *const *Argv,
ExpansionContext::ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T)
: Saver(A), Tokenizer(T), FS(vfs::getRealFileSystem().get()) {}
+bool ExpansionContext::findConfigFile(StringRef FileName,
+ SmallVectorImpl<char> &FilePath) {
+ SmallString<128> CfgFilePath;
+ const auto FileExists = [this](SmallString<128> Path) -> bool {
+ auto Status = FS->status(Path);
+ return Status &&
+ Status->getType() == llvm::sys::fs::file_type::regular_file;
+ };
+
+ // If file name contains directory separator, treat it as a path to
+ // configuration file.
+ if (llvm::sys::path::has_parent_path(FileName)) {
+ CfgFilePath = FileName;
+ if (llvm::sys::path::is_relative(FileName) && FS->makeAbsolute(CfgFilePath))
+ return false;
+ if (!FileExists(CfgFilePath))
+ return false;
+ FilePath.assign(CfgFilePath.begin(), CfgFilePath.end());
+ return true;
+ }
+
+ // Look for the file in search directories.
+ for (const StringRef &Dir : SearchDirs) {
+ if (Dir.empty())
+ continue;
+ CfgFilePath.assign(Dir);
+ llvm::sys::path::append(CfgFilePath, FileName);
+ llvm::sys::path::native(CfgFilePath);
+ if (FileExists(CfgFilePath)) {
+ FilePath.assign(CfgFilePath.begin(), CfgFilePath.end());
+ return true;
+ }
+ }
+
+ return false;
+}
+
bool ExpansionContext::readConfigFile(StringRef CfgFile,
SmallVectorImpl<const char *> &Argv) {
SmallString<128> AbsPath;