aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2022-11-24 17:50:54 +0100
committerSam McCall <sam.mccall@gmail.com>2022-11-25 11:01:22 +0100
commit8bed59c7e7da2fea41a9167e15c15a8f58a5ede7 (patch)
treeec5712544b5781b1f90ae8207c3953c87028ced8
parent2e5366ac2e05fb49ad4b62b86c23359ad95740bb (diff)
downloadllvm-8bed59c7e7da2fea41a9167e15c15a8f58a5ede7.zip
llvm-8bed59c7e7da2fea41a9167e15c15a8f58a5ede7.tar.gz
llvm-8bed59c7e7da2fea41a9167e15c15a8f58a5ede7.tar.bz2
[Lex] Fix suggested spelling of /usr/bin/../include/foo
Since D60873 we remove dotdots from the search path entries, but not the filenames we're matching against, so do the latter too. Since this also removes (single) dots, drop the logic to skip over them. (Some of this was already dead, some is newly dead). See D138676 for motivation. Differential Revision: https://reviews.llvm.org/D138677
-rw-r--r--clang/lib/Lex/HeaderSearch.cpp26
-rw-r--r--clang/unittests/Lex/HeaderSearchTest.cpp8
2 files changed, 17 insertions, 17 deletions
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index e6af122..0615002 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -1928,32 +1928,24 @@ std::string HeaderSearch::suggestPathToFileForDiagnostics(
llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
bool *IsSystem) {
using namespace llvm::sys;
+
+ llvm::SmallString<32> FilePath = File;
+ path::remove_dots(FilePath, /*remove_dot_dot=*/true);
+ File = FilePath;
unsigned BestPrefixLength = 0;
// Checks whether `Dir` is a strict path prefix of `File`. If so and that's
// the longest prefix we've seen so for it, returns true and updates the
// `BestPrefixLength` accordingly.
- auto CheckDir = [&](llvm::StringRef Dir) -> bool {
- llvm::SmallString<32> DirPath(Dir.begin(), Dir.end());
+ auto CheckDir = [&](llvm::SmallString<32> Dir) -> bool {
if (!WorkingDir.empty() && !path::is_absolute(Dir))
- fs::make_absolute(WorkingDir, DirPath);
- path::remove_dots(DirPath, /*remove_dot_dot=*/true);
- Dir = DirPath;
+ fs::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);
- /*termination condition in loop*/; ++NI, ++DI) {
- // '.' components in File are ignored.
- while (NI != NE && *NI == ".")
- ++NI;
- if (NI == NE)
- break;
-
- // '.' components in Dir are ignored.
- while (DI != DE && *DI == ".")
- ++DI;
+ NI != NE; ++NI, ++DI) {
if (DI == DE) {
- // Dir is a prefix of File, up to '.' components and choice of path
- // separators.
+ // Dir is a prefix of File, up to choice of path separators.
unsigned PrefixLength = NI - path::begin(File);
if (PrefixLength > BestPrefixLength) {
BestPrefixLength = PrefixLength;
diff --git a/clang/unittests/Lex/HeaderSearchTest.cpp b/clang/unittests/Lex/HeaderSearchTest.cpp
index 5024f0d..939f5a7 100644
--- a/clang/unittests/Lex/HeaderSearchTest.cpp
+++ b/clang/unittests/Lex/HeaderSearchTest.cpp
@@ -150,6 +150,14 @@ TEST_F(HeaderSearchTest, DotDotsWithAbsPath) {
"z");
}
+TEST_F(HeaderSearchTest, BothDotDots) {
+ addSearchDir("/x/../y/");
+ EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/x/../y/z",
+ /*WorkingDir=*/"",
+ /*MainFile=*/""),
+ "z");
+}
+
TEST_F(HeaderSearchTest, IncludeFromSameDirectory) {
EXPECT_EQ(Search.suggestPathToFileForDiagnostics("/y/z/t.h",
/*WorkingDir=*/"",