diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -760,11 +760,15 @@ } } + SmallString<256> buffer = root; + // "root" could be "/", which may need to be translated into "\". + make_preferred(buffer, style); + needs_change |= root != buffer; + // Avoid rewriting the path unless we have to. if (!needs_change) return false; - SmallString<256> buffer = root; if (!components.empty()) { buffer += components[0]; for (StringRef C : makeArrayRef(components).drop_front()) { diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -1544,16 +1544,14 @@ EXPECT_EQ("C:\\a\\c", remove_dots("C:\\foo\\bar//..\\..\\a\\c", true, path::Style::windows)); - // FIXME: These leading forward slashes are emergent behavior. VFS depends on - // this behavior now. - EXPECT_EQ("C:/bar", + EXPECT_EQ("C:\\bar", remove_dots("C:/foo/../bar", true, path::Style::windows)); - EXPECT_EQ("C:/foo\\bar", + EXPECT_EQ("C:\\foo\\bar", remove_dots("C:/foo/bar", true, path::Style::windows)); - EXPECT_EQ("C:/foo\\bar", + EXPECT_EQ("C:\\foo\\bar", remove_dots("C:/foo\\bar", true, path::Style::windows)); - EXPECT_EQ("/", remove_dots("/", true, path::Style::windows)); - EXPECT_EQ("C:/", remove_dots("C:/", true, path::Style::windows)); + EXPECT_EQ("\\", remove_dots("/", true, path::Style::windows)); + EXPECT_EQ("C:\\", remove_dots("C:/", true, path::Style::windows)); // Some clients of remove_dots expect it to remove trailing slashes. Again, // this is emergent behavior that VFS relies on, and not inherently part of @@ -1564,7 +1562,7 @@ remove_dots("/foo/bar/", true, path::Style::posix)); // A double separator is rewritten. - EXPECT_EQ("C:/foo\\bar", remove_dots("C:/foo//bar", true, path::Style::windows)); + EXPECT_EQ("C:\\foo\\bar", remove_dots("C:/foo//bar", true, path::Style::windows)); SmallString<64> Path1(".\\.\\c"); EXPECT_TRUE(path::remove_dots(Path1, true, path::Style::windows));