mirror of
https://github.com/intel/llvm.git
synced 2026-01-13 11:02:04 +08:00
[llvm][support] Move make_absolute from sys::fs to sys::path (#161459)
The `llvm::sys::fs::make_absolute(const Twine &, SmallVectorImpl<char> &)` functions doesn't perform any FS access - it only modifies the second parameter via path/string operations. This function should live in the `llvm::sys::path` namespace for consistency and for making it easier to spot function calls that perform IO.
This commit is contained in:
@@ -1662,7 +1662,7 @@ void BinaryContext::preprocessDWODebugInfo() {
|
||||
"files.\n";
|
||||
}
|
||||
// Prevent failures when DWOName is already an absolute path.
|
||||
sys::fs::make_absolute(DWOCompDir, AbsolutePath);
|
||||
sys::path::make_absolute(DWOCompDir, AbsolutePath);
|
||||
DWARFUnit *DWOCU =
|
||||
DwarfUnit->getNonSkeletonUnitDIE(false, AbsolutePath).getDwarfUnit();
|
||||
if (!DWOCU->isDWOUnit()) {
|
||||
|
||||
@@ -1853,7 +1853,7 @@ void DWARFRewriter::writeDWOFiles(
|
||||
else if (!sys::fs::exists(CompDir))
|
||||
CompDir = ".";
|
||||
// Prevent failures when DWOName is already an absolute path.
|
||||
sys::fs::make_absolute(CompDir, AbsolutePath);
|
||||
sys::path::make_absolute(CompDir, AbsolutePath);
|
||||
|
||||
std::error_code EC;
|
||||
std::unique_ptr<ToolOutputFile> TempOut =
|
||||
|
||||
@@ -142,7 +142,7 @@ groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs,
|
||||
// build directories, make them absolute immediately.
|
||||
SmallString<128> Path = R.getFilePath();
|
||||
if (BuildDir)
|
||||
llvm::sys::fs::make_absolute(*BuildDir, Path);
|
||||
llvm::sys::path::make_absolute(*BuildDir, Path);
|
||||
else
|
||||
SM.getFileManager().makeAbsolutePath(Path);
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ std::string MakeAbsolutePath(StringRef CurrentDir, StringRef Path) {
|
||||
return "";
|
||||
llvm::SmallString<128> InitialDirectory(CurrentDir);
|
||||
llvm::SmallString<128> AbsolutePath(Path);
|
||||
llvm::sys::fs::make_absolute(InitialDirectory, AbsolutePath);
|
||||
llvm::sys::path::make_absolute(InitialDirectory, AbsolutePath);
|
||||
return CleanPath(std::move(AbsolutePath));
|
||||
}
|
||||
|
||||
|
||||
@@ -131,7 +131,7 @@ struct FragmentCompiler {
|
||||
return std::nullopt;
|
||||
}
|
||||
llvm::SmallString<256> AbsPath = llvm::StringRef(*Path);
|
||||
llvm::sys::fs::make_absolute(FragmentDirectory, AbsPath);
|
||||
llvm::sys::path::make_absolute(FragmentDirectory, AbsPath);
|
||||
llvm::sys::path::native(AbsPath, Style);
|
||||
return AbsPath.str().str();
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ struct DriverArgs {
|
||||
// relative or absolute).
|
||||
if (llvm::any_of(Driver,
|
||||
[](char C) { return llvm::sys::path::is_separator(C); })) {
|
||||
llvm::sys::fs::make_absolute(Cmd.Directory, Driver);
|
||||
llvm::sys::path::make_absolute(Cmd.Directory, Driver);
|
||||
}
|
||||
this->Driver = Driver.str().str();
|
||||
for (size_t I = 0, E = Cmd.CommandLine.size(); I < E; ++I) {
|
||||
|
||||
@@ -325,7 +325,7 @@ private:
|
||||
if (R.second) {
|
||||
llvm::SmallString<256> AbsPath = Path;
|
||||
if (!llvm::sys::path::is_absolute(AbsPath) && !FallbackDir.empty())
|
||||
llvm::sys::fs::make_absolute(FallbackDir, AbsPath);
|
||||
llvm::sys::path::make_absolute(FallbackDir, AbsPath);
|
||||
assert(llvm::sys::path::is_absolute(AbsPath) &&
|
||||
"If the VFS can't make paths absolute, a FallbackDir must be "
|
||||
"provided");
|
||||
|
||||
@@ -578,7 +578,7 @@ public:
|
||||
Body = Body.ltrim('/');
|
||||
llvm::SmallString<16> Path(Body);
|
||||
path::native(Path);
|
||||
fs::make_absolute(TestScheme::TestDir, Path);
|
||||
path::make_absolute(TestScheme::TestDir, Path);
|
||||
return std::string(Path);
|
||||
}
|
||||
|
||||
|
||||
@@ -344,7 +344,7 @@ mapInputsToAbsPaths(clang::tooling::CompilationDatabase &CDB,
|
||||
}
|
||||
for (const auto &Cmd : Cmds) {
|
||||
llvm::SmallString<256> CDBPath(Cmd.Filename);
|
||||
llvm::sys::fs::make_absolute(Cmd.Directory, CDBPath);
|
||||
llvm::sys::path::make_absolute(Cmd.Directory, CDBPath);
|
||||
CDBToAbsPaths[std::string(CDBPath)] = std::string(AbsPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2077,7 +2077,7 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
|
||||
|
||||
llvm::SmallString<32> FilePath = File;
|
||||
if (!WorkingDir.empty() && !path::is_absolute(FilePath))
|
||||
fs::make_absolute(WorkingDir, FilePath);
|
||||
path::make_absolute(WorkingDir, FilePath);
|
||||
// remove_dots switches to backslashes on windows as a side-effect!
|
||||
// We always want to suggest forward slashes for includes.
|
||||
// (not remove_dots(..., posix) as that misparses windows paths).
|
||||
@@ -2091,7 +2091,7 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
|
||||
// `BestPrefixLength` accordingly.
|
||||
auto CheckDir = [&](llvm::SmallString<32> Dir) -> bool {
|
||||
if (!WorkingDir.empty() && !path::is_absolute(Dir))
|
||||
fs::make_absolute(WorkingDir, Dir);
|
||||
path::make_absolute(WorkingDir, Dir);
|
||||
path::remove_dots(Dir, /*remove_dot_dot=*/true);
|
||||
for (auto NI = path::begin(File), NE = path::end(File),
|
||||
DI = path::begin(Dir), DE = path::end(Dir);
|
||||
|
||||
@@ -31,7 +31,7 @@ public:
|
||||
for (const auto &File : getDependencies()) {
|
||||
CanonPath = File;
|
||||
llvm::sys::path::remove_dots(CanonPath, /*remove_dot_dot=*/true);
|
||||
llvm::sys::fs::make_absolute(WorkingDirectory, CanonPath);
|
||||
llvm::sys::path::make_absolute(WorkingDirectory, CanonPath);
|
||||
C.handleFileDependency(CanonPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
|
||||
|
||||
SmallString<256> CurrentPath;
|
||||
sys::fs::current_path(CurrentPath);
|
||||
sys::fs::make_absolute(CurrentPath, FileName);
|
||||
sys::path::make_absolute(CurrentPath, FileName);
|
||||
|
||||
// Mount the VFS file itself on the path 'virtual.file'. Makes this test
|
||||
// a bit shorter than creating a new dummy file just for this purpose.
|
||||
|
||||
@@ -266,18 +266,6 @@ public:
|
||||
/// @name Physical Operators
|
||||
/// @{
|
||||
|
||||
/// Make \a path an absolute path.
|
||||
///
|
||||
/// Makes \a path absolute using the \a current_directory if it is not already.
|
||||
/// An empty \a path will result in the \a current_directory.
|
||||
///
|
||||
/// /absolute/path => /absolute/path
|
||||
/// relative/../path => <current-directory>/relative/../path
|
||||
///
|
||||
/// @param path A path that is modified to be an absolute path.
|
||||
LLVM_ABI void make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path);
|
||||
|
||||
/// Make \a path an absolute path.
|
||||
///
|
||||
/// Makes \a path absolute using the current directory if it is not already. An
|
||||
|
||||
@@ -566,6 +566,18 @@ LLVM_ABI bool is_absolute_gnu(const Twine &path, Style style = Style::native);
|
||||
/// @result True if the path is relative, false if it is not.
|
||||
LLVM_ABI bool is_relative(const Twine &path, Style style = Style::native);
|
||||
|
||||
/// Make \a path an absolute path.
|
||||
///
|
||||
/// Makes \a path absolute using the \a current_directory if it is not already.
|
||||
/// An empty \a path will result in the \a current_directory.
|
||||
///
|
||||
/// /absolute/path => /absolute/path
|
||||
/// relative/../path => <current-directory>/relative/../path
|
||||
///
|
||||
/// @param path A path that is modified to be an absolute path.
|
||||
LLVM_ABI void make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path);
|
||||
|
||||
} // end namespace path
|
||||
} // end namespace sys
|
||||
} // end namespace llvm
|
||||
|
||||
@@ -700,6 +700,55 @@ bool is_relative(const Twine &path, Style style) {
|
||||
return !is_absolute(path, style);
|
||||
}
|
||||
|
||||
void make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path) {
|
||||
StringRef p(path.data(), path.size());
|
||||
|
||||
bool rootDirectory = has_root_directory(p);
|
||||
bool rootName = has_root_name(p);
|
||||
|
||||
// Already absolute.
|
||||
if ((rootName || is_style_posix(Style::native)) && rootDirectory)
|
||||
return;
|
||||
|
||||
// All the following conditions will need the current directory.
|
||||
SmallString<128> current_dir;
|
||||
current_directory.toVector(current_dir);
|
||||
|
||||
// Relative path. Prepend the current directory.
|
||||
if (!rootName && !rootDirectory) {
|
||||
// Append path to the current directory.
|
||||
append(current_dir, p);
|
||||
// Set path to the result.
|
||||
path.swap(current_dir);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rootName && rootDirectory) {
|
||||
StringRef cdrn = root_name(current_dir);
|
||||
SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
|
||||
append(curDirRootName, p);
|
||||
// Set path to the result.
|
||||
path.swap(curDirRootName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rootName && !rootDirectory) {
|
||||
StringRef pRootName = root_name(p);
|
||||
StringRef bRootDirectory = root_directory(current_dir);
|
||||
StringRef bRelativePath = relative_path(current_dir);
|
||||
StringRef pRelativePath = relative_path(p);
|
||||
|
||||
SmallString<128> res;
|
||||
append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
|
||||
path.swap(res);
|
||||
return;
|
||||
}
|
||||
|
||||
llvm_unreachable("All rootName and rootDirectory combinations should have "
|
||||
"occurred above!");
|
||||
}
|
||||
|
||||
StringRef remove_leading_dotslash(StringRef Path, Style style) {
|
||||
// Remove leading "./" (or ".//" or "././" etc.)
|
||||
while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
|
||||
@@ -903,55 +952,6 @@ getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix,
|
||||
return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
|
||||
}
|
||||
|
||||
void make_absolute(const Twine ¤t_directory,
|
||||
SmallVectorImpl<char> &path) {
|
||||
StringRef p(path.data(), path.size());
|
||||
|
||||
bool rootDirectory = path::has_root_directory(p);
|
||||
bool rootName = path::has_root_name(p);
|
||||
|
||||
// Already absolute.
|
||||
if ((rootName || is_style_posix(Style::native)) && rootDirectory)
|
||||
return;
|
||||
|
||||
// All of the following conditions will need the current directory.
|
||||
SmallString<128> current_dir;
|
||||
current_directory.toVector(current_dir);
|
||||
|
||||
// Relative path. Prepend the current directory.
|
||||
if (!rootName && !rootDirectory) {
|
||||
// Append path to the current directory.
|
||||
path::append(current_dir, p);
|
||||
// Set path to the result.
|
||||
path.swap(current_dir);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rootName && rootDirectory) {
|
||||
StringRef cdrn = path::root_name(current_dir);
|
||||
SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
|
||||
path::append(curDirRootName, p);
|
||||
// Set path to the result.
|
||||
path.swap(curDirRootName);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rootName && !rootDirectory) {
|
||||
StringRef pRootName = path::root_name(p);
|
||||
StringRef bRootDirectory = path::root_directory(current_dir);
|
||||
StringRef bRelativePath = path::relative_path(current_dir);
|
||||
StringRef pRelativePath = path::relative_path(p);
|
||||
|
||||
SmallString<128> res;
|
||||
path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
|
||||
path.swap(res);
|
||||
return;
|
||||
}
|
||||
|
||||
llvm_unreachable("All rootName and rootDirectory combinations should have "
|
||||
"occurred above!");
|
||||
}
|
||||
|
||||
std::error_code make_absolute(SmallVectorImpl<char> &path) {
|
||||
if (path::is_absolute(path))
|
||||
return {};
|
||||
@@ -960,7 +960,7 @@ std::error_code make_absolute(SmallVectorImpl<char> &path) {
|
||||
if (std::error_code ec = current_path(current_dir))
|
||||
return ec;
|
||||
|
||||
make_absolute(current_dir, path);
|
||||
path::make_absolute(current_dir, path);
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
|
||||
if (!WorkingDir)
|
||||
return WorkingDir.getError();
|
||||
|
||||
llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
|
||||
sys::path::make_absolute(WorkingDir.get(), Path);
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -300,7 +300,7 @@ private:
|
||||
if (!WD || !*WD)
|
||||
return Path;
|
||||
Path.toVector(Storage);
|
||||
sys::fs::make_absolute(WD->get().Resolved, Storage);
|
||||
sys::path::make_absolute(WD->get().Resolved, Storage);
|
||||
return Storage;
|
||||
}
|
||||
|
||||
|
||||
@@ -357,18 +357,18 @@ int main(int argc, char **argv) {
|
||||
ActivePrefix = CurrentExecPrefix;
|
||||
{
|
||||
SmallString<256> Path(LLVM_INSTALL_INCLUDEDIR);
|
||||
sys::fs::make_absolute(ActivePrefix, Path);
|
||||
sys::path::make_absolute(ActivePrefix, Path);
|
||||
ActiveIncludeDir = std::string(Path);
|
||||
}
|
||||
{
|
||||
SmallString<256> Path(LLVM_TOOLS_INSTALL_DIR);
|
||||
sys::fs::make_absolute(ActivePrefix, Path);
|
||||
sys::path::make_absolute(ActivePrefix, Path);
|
||||
ActiveBinDir = std::string(Path);
|
||||
}
|
||||
ActiveLibDir = ActivePrefix + "/lib" + LLVM_LIBDIR_SUFFIX;
|
||||
{
|
||||
SmallString<256> Path(LLVM_INSTALL_PACKAGE_DIR);
|
||||
sys::fs::make_absolute(ActivePrefix, Path);
|
||||
sys::path::make_absolute(ActivePrefix, Path);
|
||||
ActiveCMakeDir = std::string(Path);
|
||||
}
|
||||
ActiveIncludeOption = "-I" + ActiveIncludeDir;
|
||||
|
||||
@@ -94,7 +94,7 @@ getDWOFilenames(StringRef ExecFilename) {
|
||||
dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
|
||||
if (!DWOCompDir.empty()) {
|
||||
SmallString<16> DWOPath(DWOName);
|
||||
sys::fs::make_absolute(DWOCompDir, DWOPath);
|
||||
sys::path::make_absolute(DWOCompDir, DWOPath);
|
||||
if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
|
||||
DWOPaths.push_back(std::move(DWOName));
|
||||
else
|
||||
|
||||
@@ -274,7 +274,7 @@ static bool writeReport(LocationInfoTy &LocationInfo) {
|
||||
for (auto &FI : LocationInfo) {
|
||||
SmallString<128> FileName(FI.first);
|
||||
if (!InputRelDir.empty())
|
||||
sys::fs::make_absolute(InputRelDir, FileName);
|
||||
sys::path::make_absolute(InputRelDir, FileName);
|
||||
|
||||
const auto &FileInfo = FI.second;
|
||||
|
||||
|
||||
@@ -255,14 +255,14 @@ TEST(Support, Path) {
|
||||
|
||||
{
|
||||
SmallString<32> Relative("foo.cpp");
|
||||
sys::fs::make_absolute("/root", Relative);
|
||||
path::make_absolute("/root", Relative);
|
||||
Relative[5] = '/'; // Fix up windows paths.
|
||||
ASSERT_EQ("/root/foo.cpp", Relative);
|
||||
}
|
||||
|
||||
{
|
||||
SmallString<32> Relative("foo.cpp");
|
||||
sys::fs::make_absolute("//root", Relative);
|
||||
path::make_absolute("//root", Relative);
|
||||
Relative[6] = '/'; // Fix up windows paths.
|
||||
ASSERT_EQ("//root/foo.cpp", Relative);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user