Support: Use sys::path::is_style_{posix,windows}() in a few places

Use the new sys::path::is_style_posix() and is_style_windows() in a few
places that need to detect the system's native path style.

In llvm/lib/Support/Path.cpp, this patch removes most uses of the
private `real_style()`, where is_style_posix() and is_style_windows()
are just a little tidier.

Elsewhere, this removes `_WIN32` macro checks. Added a FIXME to a
FileManagerTest that seemed fishy, but maintained the existing
behaviour.

Differential Revision: https://reviews.llvm.org/D112289
This commit is contained in:
Duncan P. N. Exon Smith 2021-10-21 18:33:24 -07:00
parent 51ce567b38
commit 9902362701
13 changed files with 103 additions and 135 deletions

View File

@ -12,6 +12,7 @@
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <iterator>
@ -98,18 +99,19 @@ inline void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc,
if (AddBraces)
Out << "{ ";
std::string filename(PLoc.getFilename());
#ifdef _WIN32
// Remove forbidden Windows path characters
auto RemoveIt =
std::remove_if(filename.begin(), filename.end(), [](auto Char) {
static const char ForbiddenChars[] = "<>*?\"|";
return std::find(std::begin(ForbiddenChars), std::end(ForbiddenChars),
Char) != std::end(ForbiddenChars);
});
filename.erase(RemoveIt, filename.end());
// Handle windows-specific path delimiters.
std::replace(filename.begin(), filename.end(), '\\', '/');
#endif
if (is_style_windows(llvm::sys::path::Style::native)) {
// Remove forbidden Windows path characters
auto RemoveIt =
std::remove_if(filename.begin(), filename.end(), [](auto Char) {
static const char ForbiddenChars[] = "<>*?\"|";
return std::find(std::begin(ForbiddenChars),
std::end(ForbiddenChars),
Char) != std::end(ForbiddenChars);
});
filename.erase(RemoveIt, filename.end());
// Handle windows-specific path delimiters.
std::replace(filename.begin(), filename.end(), '\\', '/');
}
Out << "\"line\": " << PLoc.getLine()
<< ", \"column\": " << PLoc.getColumn()
<< ", \"file\": \"" << filename << "\"";

View File

@ -123,16 +123,16 @@ FileManager::getDirectoryRef(StringRef DirName, bool CacheFailure) {
DirName != llvm::sys::path::root_path(DirName) &&
llvm::sys::path::is_separator(DirName.back()))
DirName = DirName.substr(0, DirName.size()-1);
#ifdef _WIN32
// Fixing a problem with "clang C:test.c" on Windows.
// Stat("C:") does not recognize "C:" as a valid directory
std::string DirNameStr;
if (DirName.size() > 1 && DirName.back() == ':' &&
DirName.equals_insensitive(llvm::sys::path::root_name(DirName))) {
DirNameStr = DirName.str() + '.';
DirName = DirNameStr;
if (is_style_windows(llvm::sys::path::Style::native)) {
// Fixing a problem with "clang C:test.c" on Windows.
// Stat("C:") does not recognize "C:" as a valid directory
std::string DirNameStr;
if (DirName.size() > 1 && DirName.back() == ':' &&
DirName.equals_insensitive(llvm::sys::path::root_name(DirName))) {
DirNameStr = DirName.str() + '.';
DirName = DirNameStr;
}
}
#endif
++NumDirLookups;

View File

@ -4899,11 +4899,11 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA,
bool MultipleArchs,
StringRef OffloadingPrefix) const {
std::string BoundArch = OrigBoundArch.str();
#if defined(_WIN32)
// BoundArch may contains ':', which is invalid in file names on Windows,
// therefore replace it with '%'.
std::replace(BoundArch.begin(), BoundArch.end(), ':', '@');
#endif
if (is_style_windows(llvm::sys::path::Style::native)) {
// BoundArch may contains ':', which is invalid in file names on Windows,
// therefore replace it with '%'.
std::replace(BoundArch.begin(), BoundArch.end(), ':', '@');
}
llvm::PrettyStackTraceString CrashInfo("Computing output path");
// Output to a user requested destination?

View File

@ -169,10 +169,11 @@ static const DriverSuffix *FindDriverSuffix(StringRef ProgName, size_t &Pos) {
/// present and lower-casing the string on Windows.
static std::string normalizeProgramName(llvm::StringRef Argv0) {
std::string ProgName = std::string(llvm::sys::path::stem(Argv0));
#ifdef _WIN32
// Transform to lowercase for case insensitive file systems.
std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), ::tolower);
#endif
if (is_style_windows(llvm::sys::path::Style::native)) {
// Transform to lowercase for case insensitive file systems.
std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(),
::tolower);
}
return ProgName;
}

View File

@ -2012,20 +2012,16 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
SourceLocation FilenameLoc = FilenameTok.getLocation();
StringRef LookupFilename = Filename;
#ifdef _WIN32
llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::windows;
#else
// Normalize slashes when compiling with -fms-extensions on non-Windows. This
// is unnecessary on Windows since the filesystem there handles backslashes.
SmallString<128> NormalizedPath;
llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::posix;
if (LangOpts.MicrosoftExt) {
llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native;
if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) {
NormalizedPath = Filename.str();
llvm::sys::path::native(NormalizedPath);
LookupFilename = NormalizedPath;
BackslashStyle = llvm::sys::path::Style::windows;
}
#endif
Optional<FileEntryRef> File = LookupHeaderIncludeOrImport(
CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,

View File

@ -31,18 +31,18 @@ private:
void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile,
const char *StatPath) {
#ifndef _WIN32
SmallString<128> NormalizedPath(Path);
llvm::sys::path::native(NormalizedPath);
Path = NormalizedPath.c_str();
SmallString<128> NormalizedStatPath;
if (StatPath) {
NormalizedStatPath = StatPath;
llvm::sys::path::native(NormalizedStatPath);
StatPath = NormalizedStatPath.c_str();
if (is_style_posix(llvm::sys::path::Style::native)) {
llvm::sys::path::native(NormalizedPath);
Path = NormalizedPath.c_str();
if (StatPath) {
NormalizedStatPath = StatPath;
llvm::sys::path::native(NormalizedStatPath);
StatPath = NormalizedStatPath.c_str();
}
}
#endif
if (!StatPath)
StatPath = Path;
@ -74,11 +74,11 @@ public:
bool isFile,
std::unique_ptr<llvm::vfs::File> *F,
llvm::vfs::FileSystem &FS) override {
#ifndef _WIN32
SmallString<128> NormalizedPath(Path);
llvm::sys::path::native(NormalizedPath);
Path = NormalizedPath.c_str();
#endif
if (is_style_posix(llvm::sys::path::Style::native)) {
llvm::sys::path::native(NormalizedPath);
Path = NormalizedPath.c_str();
}
if (StatCalls.count(Path) != 0) {
Status = StatCalls[Path];
@ -436,13 +436,16 @@ TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
#endif // !_WIN32
static StringRef getSystemRoot() {
return is_style_windows(llvm::sys::path::Style::native) ? "C:/" : "/";
}
TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
SmallString<64> CustomWorkingDir;
#ifdef _WIN32
CustomWorkingDir = "C:";
#else
CustomWorkingDir = "/";
#endif
// FIXME: Should this be using a root path / call getSystemRoot()? For now,
// avoiding that and leaving the test as-is.
SmallString<64> CustomWorkingDir =
is_style_windows(llvm::sys::path::Style::native) ? StringRef("C:")
: StringRef("/");
llvm::sys::path::append(CustomWorkingDir, "some", "weird", "path");
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
@ -464,12 +467,7 @@ TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
// getVirtualFile should always fill the real path.
TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
SmallString<64> CustomWorkingDir;
#ifdef _WIN32
CustomWorkingDir = "C:/";
#else
CustomWorkingDir = "/";
#endif
SmallString<64> CustomWorkingDir = getSystemRoot();
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
new llvm::vfs::InMemoryFileSystem);
@ -497,12 +495,7 @@ TEST_F(FileManagerTest, getVirtualFileFillsRealPathName) {
}
TEST_F(FileManagerTest, getFileDontOpenRealPath) {
SmallString<64> CustomWorkingDir;
#ifdef _WIN32
CustomWorkingDir = "C:/";
#else
CustomWorkingDir = "/";
#endif
SmallString<64> CustomWorkingDir = getSystemRoot();
auto FS = IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>(
new llvm::vfs::InMemoryFileSystem);

View File

@ -85,9 +85,8 @@ TEST(ToolChainTest, VFSGCCInstallation) {
llvm::raw_string_ostream OS(S);
C->getDefaultToolChain().printVerboseInfo(OS);
}
#if _WIN32
std::replace(S.begin(), S.end(), '\\', '/');
#endif
if (is_style_windows(llvm::sys::path::Style::native))
std::replace(S.begin(), S.end(), '\\', '/');
EXPECT_EQ(
"Found candidate GCC installation: "
"/usr/lib/gcc/arm-linux-gnueabihf/4.6.3\n"
@ -110,9 +109,8 @@ TEST(ToolChainTest, VFSGCCInstallation) {
llvm::raw_string_ostream OS(S);
C->getDefaultToolChain().printVerboseInfo(OS);
}
#if _WIN32
std::replace(S.begin(), S.end(), '\\', '/');
#endif
if (is_style_windows(llvm::sys::path::Style::native))
std::replace(S.begin(), S.end(), '\\', '/');
// Test that 4.5.3 from --sysroot is not overridden by 4.6.3 (larger
// version) from /usr.
EXPECT_EQ("Found candidate GCC installation: "
@ -153,9 +151,8 @@ TEST(ToolChainTest, VFSGCCInstallationRelativeDir) {
llvm::raw_string_ostream OS(S);
C->getDefaultToolChain().printVerboseInfo(OS);
}
#if _WIN32
std::replace(S.begin(), S.end(), '\\', '/');
#endif
if (is_style_windows(llvm::sys::path::Style::windows))
std::replace(S.begin(), S.end(), '\\', '/');
EXPECT_EQ("Found candidate GCC installation: "
"/home/test/bin/../lib/gcc/arm-linux-gnueabi/4.6.1\n"
"Selected GCC installation: "

View File

@ -188,11 +188,11 @@ TEST_F(HeaderSearchTest, HeaderMapFrameworkLookup) {
std::string HeaderDirName = "/tmp/Sources/Foo/Headers/";
std::string HeaderName = "Foo.h";
#ifdef _WIN32
// Force header path to be absolute on windows.
// As headermap content should represent absolute locations.
HeaderDirName = "C:" + HeaderDirName;
#endif /*_WIN32*/
if (is_style_windows(llvm::sys::path::Style::native)) {
// Force header path to be absolute on windows.
// As headermap content should represent absolute locations.
HeaderDirName = "C:" + HeaderDirName;
}
test::HMapFileMockMaker<FileTy> Maker(File);
auto a = Maker.addString("Foo/Foo.h");

View File

@ -1031,18 +1031,17 @@ TEST_F(MergeReplacementsTest, OverlappingRanges) {
toReplacements({{"", 0, 3, "cc"}, {"", 3, 3, "dd"}}));
}
static constexpr bool usesWindowsPaths() {
return is_style_windows(llvm::sys::path::Style::native);
}
TEST(DeduplicateByFileTest, PathsWithDots) {
std::map<std::string, Replacements> FileToReplaces;
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS(
new llvm::vfs::InMemoryFileSystem());
FileManager FileMgr(FileSystemOptions(), VFS);
#if !defined(_WIN32)
StringRef Path1 = "a/b/.././c.h";
StringRef Path2 = "a/c.h";
#else
StringRef Path1 = "a\\b\\..\\.\\c.h";
StringRef Path2 = "a\\c.h";
#endif
StringRef Path1 = usesWindowsPaths() ? "a\\b\\..\\.\\c.h" : "a/b/.././c.h";
StringRef Path2 = usesWindowsPaths() ? "a\\c.h" : "a/c.h";
EXPECT_TRUE(VFS->addFile(Path1, 0, llvm::MemoryBuffer::getMemBuffer("")));
EXPECT_TRUE(VFS->addFile(Path2, 0, llvm::MemoryBuffer::getMemBuffer("")));
FileToReplaces[std::string(Path1)] = Replacements();
@ -1057,13 +1056,8 @@ TEST(DeduplicateByFileTest, PathWithDotSlash) {
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS(
new llvm::vfs::InMemoryFileSystem());
FileManager FileMgr(FileSystemOptions(), VFS);
#if !defined(_WIN32)
StringRef Path1 = "./a/b/c.h";
StringRef Path2 = "a/b/c.h";
#else
StringRef Path1 = ".\\a\\b\\c.h";
StringRef Path2 = "a\\b\\c.h";
#endif
StringRef Path1 = usesWindowsPaths() ? ".\\a\\b\\c.h" : "./a/b/c.h";
StringRef Path2 = usesWindowsPaths() ? "a\\b\\c.h" : "a/b/c.h";
EXPECT_TRUE(VFS->addFile(Path1, 0, llvm::MemoryBuffer::getMemBuffer("")));
EXPECT_TRUE(VFS->addFile(Path2, 0, llvm::MemoryBuffer::getMemBuffer("")));
FileToReplaces[std::string(Path1)] = Replacements();
@ -1078,13 +1072,8 @@ TEST(DeduplicateByFileTest, NonExistingFilePath) {
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS(
new llvm::vfs::InMemoryFileSystem());
FileManager FileMgr(FileSystemOptions(), VFS);
#if !defined(_WIN32)
StringRef Path1 = "./a/b/c.h";
StringRef Path2 = "a/b/c.h";
#else
StringRef Path1 = ".\\a\\b\\c.h";
StringRef Path2 = "a\\b\\c.h";
#endif
StringRef Path1 = usesWindowsPaths() ? ".\\a\\b\\c.h" : "./a/b/c.h";
StringRef Path2 = usesWindowsPaths() ? "a\\b\\c.h" : "a/b/c.h";
FileToReplaces[std::string(Path1)] = Replacements();
FileToReplaces[std::string(Path2)] = Replacements();
FileToReplaces = groupReplacementsByFile(FileMgr, FileToReplaces);

View File

@ -788,12 +788,7 @@ private:
/// Whether to perform case-sensitive comparisons.
///
/// Currently, case-insensitive matching only works correctly with ASCII.
bool CaseSensitive =
#ifdef _WIN32
false;
#else
true;
#endif
bool CaseSensitive = is_style_posix(sys::path::Style::native);
/// IsRelativeOverlay marks whether a ExternalContentsPrefixDir path must
/// be prefixed in every 'external-contents' when reading from YAML files.

View File

@ -23,11 +23,12 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <system_error>
#include <string>
#include <system_error>
#include <vector>
using namespace llvm;
@ -94,11 +95,8 @@ StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
static std::string replaceIllegalFilenameChars(std::string Filename,
const char ReplacementChar) {
#ifdef _WIN32
std::string IllegalChars = "\\/:?\"<>|";
#else
std::string IllegalChars = "/";
#endif
std::string IllegalChars =
is_style_windows(sys::path::Style::native) ? "\\/:?\"<>|" : "/";
for (char IllegalChar : IllegalChars) {
std::replace(Filename.begin(), Filename.end(), IllegalChar,

View File

@ -64,7 +64,7 @@ namespace {
if (path.empty())
return path;
if (real_style(style) == Style::windows) {
if (is_style_windows(style)) {
// C:
if (path.size() >= 2 &&
std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
@ -96,7 +96,7 @@ namespace {
size_t pos = str.find_last_of(separators(style), str.size() - 1);
if (real_style(style) == Style::windows) {
if (is_style_windows(style)) {
if (pos == StringRef::npos)
pos = str.find_last_of(':', str.size() - 2);
}
@ -111,7 +111,7 @@ namespace {
// directory in str, it returns StringRef::npos.
size_t root_dir_start(StringRef str, Style style) {
// case "c:/"
if (real_style(style) == Style::windows) {
if (is_style_windows(style)) {
if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
return 2;
}
@ -257,7 +257,7 @@ const_iterator &const_iterator::operator++() {
// Root dir.
if (was_net ||
// c:/
(real_style(S) == Style::windows && Component.endswith(":"))) {
(is_style_windows(S) && Component.endswith(":"))) {
Component = Path.substr(Position, 1);
return *this;
}
@ -346,7 +346,7 @@ StringRef root_path(StringRef path, Style style) {
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
bool has_drive = is_style_windows(style) && b->endswith(":");
if (has_net || has_drive) {
if ((++pos != e) && is_separator((*pos)[0], style)) {
@ -371,7 +371,7 @@ StringRef root_name(StringRef path, Style style) {
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
bool has_drive = is_style_windows(style) && b->endswith(":");
if (has_net || has_drive) {
// just {C:,//net}, return the first component.
@ -388,7 +388,7 @@ StringRef root_directory(StringRef path, Style style) {
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
bool has_drive = is_style_windows(style) && b->endswith(":");
if ((has_net || has_drive) &&
// {C:,//net}, skip to the next component.
@ -495,7 +495,7 @@ void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
static bool starts_with(StringRef Path, StringRef Prefix,
Style style = Style::native) {
// Windows prefix matching : case and separator insensitive
if (real_style(style) == Style::windows) {
if (is_style_windows(style)) {
if (Path.size() < Prefix.size())
return false;
for (size_t I = 0, E = Prefix.size(); I != E; ++I) {
@ -546,7 +546,7 @@ void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
void native(SmallVectorImpl<char> &Path, Style style) {
if (Path.empty())
return;
if (real_style(style) == Style::windows) {
if (is_style_windows(style)) {
std::replace(Path.begin(), Path.end(), '/', '\\');
if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
SmallString<128> PathHome;
@ -560,7 +560,7 @@ void native(SmallVectorImpl<char> &Path, Style style) {
}
std::string convert_to_slash(StringRef path, Style style) {
if (real_style(style) != Style::windows)
if (is_style_posix(style))
return std::string(path);
std::string s = path.str();
@ -595,13 +595,13 @@ StringRef extension(StringRef path, Style style) {
bool is_separator(char value, Style style) {
if (value == '/')
return true;
if (real_style(style) == Style::windows)
if (is_style_windows(style))
return value == '\\';
return false;
}
StringRef get_separator(Style style) {
if (real_style(style) == Style::windows)
if (is_style_windows(style))
return "\\";
return "/";
}
@ -667,8 +667,7 @@ bool is_absolute(const Twine &path, Style style) {
StringRef p = path.toStringRef(path_storage);
bool rootDir = has_root_directory(p, style);
bool rootName =
(real_style(style) != Style::windows) || has_root_name(p, style);
bool rootName = is_style_posix(style) || has_root_name(p, style);
return rootDir && rootName;
}
@ -682,7 +681,7 @@ bool is_absolute_gnu(const Twine &path, Style style) {
if (!p.empty() && is_separator(p.front(), style))
return true;
if (real_style(style) == Style::windows) {
if (is_style_windows(style)) {
// Handle drive letter pattern (a character followed by ':') on Windows.
if (p.size() >= 2 && (p[0] && p[1] == ':'))
return true;
@ -902,8 +901,7 @@ void make_absolute(const Twine &current_directory,
bool rootName = path::has_root_name(p);
// Already absolute.
if ((rootName || real_style(Style::native) != Style::windows) &&
rootDirectory)
if ((rootName || is_style_posix(Style::native)) && rootDirectory)
return;
// All of the following conditions will need the current directory.

View File

@ -355,13 +355,12 @@ private:
return false;
std::string CacheSubdir = ModID.substr(PrefixLength);
#if defined(_WIN32)
// Transform "X:\foo" => "/X\foo" for convenience.
if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
// Transform "X:\foo" => "/X\foo" for convenience on Windows.
if (is_style_windows(llvm::sys::path::Style::native) &&
isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
CacheSubdir[1] = CacheSubdir[0];
CacheSubdir[0] = '/';
}
#endif
CacheName = CacheDir + CacheSubdir;
size_t pos = CacheName.rfind('.');