From c613019535f3251d3201b0f1408988366f5732c2 Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Mon, 21 Jul 2025 19:07:23 +0000 Subject: [PATCH 1/6] Make special case matcher slash-agnostic --- clang/docs/SanitizerSpecialCaseList.rst | 1 + clang/unittests/Basic/DiagnosticTest.cpp | 23 +++++++++++++++++++++++ llvm/docs/ReleaseNotes.md | 4 ++++ llvm/include/llvm/Support/GlobPattern.h | 1 + llvm/lib/Support/GlobPattern.cpp | 4 ++++ 5 files changed, 33 insertions(+) diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst index 307c001664fba..f2a04dc9adcf1 100644 --- a/clang/docs/SanitizerSpecialCaseList.rst +++ b/clang/docs/SanitizerSpecialCaseList.rst @@ -174,6 +174,7 @@ tool-specific docs. # Lines starting with # are ignored. # Turn off checks for the source file # Entries without sections are placed into [*] and apply to all sanitizers + # "/" matches both windows and unix path separators ("/" and "\") src:path/to/source/file.c src:*/source/file.c # Turn off checks for this main file, including files included by it. diff --git a/clang/unittests/Basic/DiagnosticTest.cpp b/clang/unittests/Basic/DiagnosticTest.cpp index 4b3af00c3b0ce..a6557b1e35c4b 100644 --- a/clang/unittests/Basic/DiagnosticTest.cpp +++ b/clang/unittests/Basic/DiagnosticTest.cpp @@ -360,4 +360,27 @@ TEST_F(SuppressionMappingTest, ParsingRespectsOtherWarningOpts) { clang::ProcessWarningOptions(Diags, Diags.getDiagnosticOptions(), *FS); EXPECT_THAT(diags(), IsEmpty()); } + +TEST_F(SuppressionMappingTest, ForwardSlashMatchesBothDirections) { + llvm::StringLiteral SuppressionMappingFile = R"( + [unused] + src:*clang/* + src:*clang/lib/Sema/*=emit + src:*clang/lib\\Sema/foo*)"; + Diags.getDiagnosticOptions().DiagnosticSuppressionMappingsFile = "foo.txt"; + FS->addFile("foo.txt", /*ModificationTime=*/{}, + llvm::MemoryBuffer::getMemBuffer(SuppressionMappingFile)); + clang::ProcessWarningOptions(Diags, Diags.getDiagnosticOptions(), *FS); + EXPECT_THAT(diags(), IsEmpty()); + + EXPECT_TRUE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang/lib/Basic/foo.h)"))); + EXPECT_FALSE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang/lib/Sema\bar.h)"))); + EXPECT_TRUE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang\lib\Sema/foo.h)"))); + // The third pattern requires a literal backslash before Sema + EXPECT_FALSE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang/lib/Sema/foo.h)"))); +} } // namespace diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md index 85c16b9c33f10..6ff8d18b07e84 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -174,6 +174,10 @@ Changes to BOLT Changes to Sanitizers --------------------- +* The [sanitizer special case list format](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format) + now treats forward slashes as either a forward or a backslash, to handle + paths with mixed unix and window styles. + Other Changes ------------- diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h index 62ed4a0f23fd9..af92c63331282 100644 --- a/llvm/include/llvm/Support/GlobPattern.h +++ b/llvm/include/llvm/Support/GlobPattern.h @@ -35,6 +35,7 @@ namespace llvm { /// expansions are not supported. If \p MaxSubPatterns is empty then /// brace expansions are not supported and characters `{,}` are treated as /// literals. +/// * `/` matches both unix and windows path separators: `/` and `\`. /// * `\` escapes the next character so it is treated as a literal. /// /// Some known edge cases are: diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp index 7004adf461a0c..26b3724863ee8 100644 --- a/llvm/lib/Support/GlobPattern.cpp +++ b/llvm/lib/Support/GlobPattern.cpp @@ -231,6 +231,10 @@ bool GlobPattern::SubGlobPattern::match(StringRef Str) const { ++S; continue; } + } else if (*P == '/' && (*S == '/' || *S == '\\')) { + ++P; + ++S; + continue; } else if (*P == *S || *P == '?') { ++P; ++S; From a81b5509f3d9633eee2276c2242c595378d1cfdc Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Tue, 22 Jul 2025 14:57:34 +0000 Subject: [PATCH 2/6] Enable only for special case list --- llvm/include/llvm/Support/GlobPattern.h | 9 +++++++-- llvm/lib/Support/GlobPattern.cpp | 12 +++++++----- llvm/lib/Support/SpecialCaseList.cpp | 3 ++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h index af92c63331282..2729ba9a56649 100644 --- a/llvm/include/llvm/Support/GlobPattern.h +++ b/llvm/include/llvm/Support/GlobPattern.h @@ -56,8 +56,10 @@ class GlobPattern { /// \param MaxSubPatterns if provided limit the number of allowed subpatterns /// created from expanding braces otherwise disable /// brace expansion + /// \param IsSlashAgnostic whether to treat '/' as matching '\\' as well LLVM_ABI static Expected - create(StringRef Pat, std::optional MaxSubPatterns = {}); + create(StringRef Pat, std::optional MaxSubPatterns = {}, + bool IsSlashAgnostic = false); /// \returns \p true if \p S matches this glob pattern LLVM_ABI bool match(StringRef S) const; @@ -76,7 +78,9 @@ class GlobPattern { struct SubGlobPattern { /// \param Pat the pattern to match against - LLVM_ABI static Expected create(StringRef Pat); + /// \param SlashAgnostic whether to treat '/' as matching '\\' as well + LLVM_ABI static Expected create(StringRef Pat, + bool SlashAgnostic); /// \returns \p true if \p S matches this glob pattern LLVM_ABI bool match(StringRef S) const; StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); } @@ -88,6 +92,7 @@ class GlobPattern { }; SmallVector Brackets; SmallVector Pat; + bool IsSlashAgnostic; }; SmallVector SubGlobs; }; diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp index 26b3724863ee8..4aa30a81c3fbf 100644 --- a/llvm/lib/Support/GlobPattern.cpp +++ b/llvm/lib/Support/GlobPattern.cpp @@ -132,8 +132,9 @@ parseBraceExpansions(StringRef S, std::optional MaxSubPatterns) { return std::move(SubPatterns); } -Expected -GlobPattern::create(StringRef S, std::optional MaxSubPatterns) { +Expected GlobPattern::create(StringRef S, + std::optional MaxSubPatterns, + bool IsSlashAgnostic) { GlobPattern Pat; // Store the prefix that does not contain any metacharacter. @@ -147,7 +148,7 @@ GlobPattern::create(StringRef S, std::optional MaxSubPatterns) { if (auto Err = parseBraceExpansions(S, MaxSubPatterns).moveInto(SubPats)) return std::move(Err); for (StringRef SubPat : SubPats) { - auto SubGlobOrErr = SubGlobPattern::create(SubPat); + auto SubGlobOrErr = SubGlobPattern::create(SubPat, IsSlashAgnostic); if (!SubGlobOrErr) return SubGlobOrErr.takeError(); Pat.SubGlobs.push_back(*SubGlobOrErr); @@ -157,8 +158,9 @@ GlobPattern::create(StringRef S, std::optional MaxSubPatterns) { } Expected -GlobPattern::SubGlobPattern::create(StringRef S) { +GlobPattern::SubGlobPattern::create(StringRef S, bool SlashAgnostic) { SubGlobPattern Pat; + Pat.IsSlashAgnostic = SlashAgnostic; // Parse brackets. Pat.Pat.assign(S.begin(), S.end()); @@ -231,7 +233,7 @@ bool GlobPattern::SubGlobPattern::match(StringRef Str) const { ++S; continue; } - } else if (*P == '/' && (*S == '/' || *S == '\\')) { + } else if (IsSlashAgnostic && *P == '/' && (*S == '/' || *S == '\\')) { ++P; ++S; continue; diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp index 8d4e043bc1c9f..c597f03188507 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -59,7 +59,8 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber, Glob->LineNo = LineNumber; // We must be sure to use the string in `Glob` rather than the provided // reference which could be destroyed before match() is called - if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024) + if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024, + /*IsSlashAgnostic=*/true) .moveInto(Glob->Pattern)) return Err; Globs.push_back(std::move(Glob)); From 7bfc6ad6afbcafe9f230d3854b28b4406f83537b Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Fri, 5 Sep 2025 16:48:17 +0000 Subject: [PATCH 3/6] Enable only on windows --- clang/docs/SanitizerSpecialCaseList.rst | 2 +- clang/unittests/Basic/DiagnosticTest.cpp | 3 +++ llvm/docs/ReleaseNotes.md | 4 ++-- llvm/include/llvm/Support/GlobPattern.h | 3 ++- llvm/lib/Support/SpecialCaseList.cpp | 6 +++++- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst index f2a04dc9adcf1..e14b654536b8a 100644 --- a/clang/docs/SanitizerSpecialCaseList.rst +++ b/clang/docs/SanitizerSpecialCaseList.rst @@ -174,7 +174,7 @@ tool-specific docs. # Lines starting with # are ignored. # Turn off checks for the source file # Entries without sections are placed into [*] and apply to all sanitizers - # "/" matches both windows and unix path separators ("/" and "\") + # On windows, "/" matches both styles of path separator ("/" and "\") src:path/to/source/file.c src:*/source/file.c # Turn off checks for this main file, including files included by it. diff --git a/clang/unittests/Basic/DiagnosticTest.cpp b/clang/unittests/Basic/DiagnosticTest.cpp index a6557b1e35c4b..7e9653bdd3c7e 100644 --- a/clang/unittests/Basic/DiagnosticTest.cpp +++ b/clang/unittests/Basic/DiagnosticTest.cpp @@ -361,6 +361,8 @@ TEST_F(SuppressionMappingTest, ParsingRespectsOtherWarningOpts) { EXPECT_THAT(diags(), IsEmpty()); } +#ifdef _WIN32 +// We're only slash-agnostic on windows hosts TEST_F(SuppressionMappingTest, ForwardSlashMatchesBothDirections) { llvm::StringLiteral SuppressionMappingFile = R"( [unused] @@ -383,4 +385,5 @@ TEST_F(SuppressionMappingTest, ForwardSlashMatchesBothDirections) { EXPECT_FALSE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang/lib/Sema/foo.h)"))); } +#endif } // namespace diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md index 6ff8d18b07e84..ae6222d2fa145 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -174,9 +174,9 @@ Changes to BOLT Changes to Sanitizers --------------------- -* The [sanitizer special case list format](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format) +* On windows hosts, the [sanitizer special case list format](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format) now treats forward slashes as either a forward or a backslash, to handle - paths with mixed unix and window styles. + paths with mixed unix and windows styles. Other Changes ------------- diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h index 2729ba9a56649..4abd6b1874593 100644 --- a/llvm/include/llvm/Support/GlobPattern.h +++ b/llvm/include/llvm/Support/GlobPattern.h @@ -35,7 +35,8 @@ namespace llvm { /// expansions are not supported. If \p MaxSubPatterns is empty then /// brace expansions are not supported and characters `{,}` are treated as /// literals. -/// * `/` matches both unix and windows path separators: `/` and `\`. +/// * If IsSlashAgnostic is passed, `/` matches both unix and windows path +/// separators: `/` and `\`. /// * `\` escapes the next character so it is treated as a literal. /// /// Some known edge cases are: diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp index c597f03188507..89ec193e1991c 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -18,6 +18,8 @@ #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/VirtualFileSystem.h" +#include "llvm/TargetParser/Host.h" +#include "llvm/TargetParser/Triple.h" #include #include #include @@ -57,10 +59,12 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber, auto Glob = std::make_unique(); Glob->Name = Pattern.str(); Glob->LineNo = LineNumber; + // Backslashes are valid in posix-style filenames. + bool IsSlashAgnostic = Triple(sys::getDefaultTargetTriple()).isOSWindows(); // We must be sure to use the string in `Glob` rather than the provided // reference which could be destroyed before match() is called if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024, - /*IsSlashAgnostic=*/true) + /*IsSlashAgnostic=*/IsSlashAgnostic) .moveInto(Glob->Pattern)) return Err; Globs.push_back(std::move(Glob)); From a5eaf5880c295a6a50f0be504d3c421641f28932 Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Mon, 8 Sep 2025 15:00:04 +0000 Subject: [PATCH 4/6] Incorporate review feedback --- clang/docs/SanitizerSpecialCaseList.rst | 2 +- clang/lib/Basic/Diagnostic.cpp | 5 ++++- clang/lib/Basic/SanitizerSpecialCaseList.cpp | 2 +- clang/unittests/Basic/DiagnosticTest.cpp | 12 ++++++----- llvm/docs/ReleaseNotes.md | 4 ++-- llvm/include/llvm/Support/GlobPattern.h | 19 +++++++---------- llvm/include/llvm/Support/SpecialCaseList.h | 5 +++-- llvm/lib/Support/GlobPattern.cpp | 21 +++++++++---------- llvm/lib/Support/SpecialCaseList.cpp | 22 +++++++++++--------- 9 files changed, 48 insertions(+), 44 deletions(-) diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst index e14b654536b8a..752602c1b3093 100644 --- a/clang/docs/SanitizerSpecialCaseList.rst +++ b/clang/docs/SanitizerSpecialCaseList.rst @@ -174,7 +174,7 @@ tool-specific docs. # Lines starting with # are ignored. # Turn off checks for the source file # Entries without sections are placed into [*] and apply to all sanitizers - # On windows, "/" matches both styles of path separator ("/" and "\") + # On windows, "/" also matches "\" in filenames src:path/to/source/file.c src:*/source/file.c # Turn off checks for this main file, including files included by it. diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp index dc3778bbf339c..9dd133cb4c03e 100644 --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -622,6 +622,8 @@ bool WarningsSpecialCaseList::isDiagSuppressed(diag::kind DiagId, bool WarningsSpecialCaseList::globsMatches( const llvm::StringMap &CategoriesToMatchers, StringRef FilePath) const { + static bool HaveWindowsPathStyle = + llvm::sys::path::is_style_windows(llvm::sys::path::Style::native); StringRef LongestMatch; bool LongestIsPositive = false; for (const auto &Entry : CategoriesToMatchers) { @@ -631,7 +633,8 @@ bool WarningsSpecialCaseList::globsMatches( for (const auto &Glob : Matcher.Globs) { if (Glob->Name.size() < LongestMatch.size()) continue; - if (!Glob->Pattern.match(FilePath)) + if (!Glob->Pattern.match(FilePath, + /*IsSlashAgnostic=*/HaveWindowsPathStyle)) continue; LongestMatch = Glob->Name; LongestIsPositive = IsPositive; diff --git a/clang/lib/Basic/SanitizerSpecialCaseList.cpp b/clang/lib/Basic/SanitizerSpecialCaseList.cpp index f7bc1d5545d75..4ad35d4d73fdd 100644 --- a/clang/lib/Basic/SanitizerSpecialCaseList.cpp +++ b/clang/lib/Basic/SanitizerSpecialCaseList.cpp @@ -42,7 +42,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() { SanitizerMask Mask; #define SANITIZER(NAME, ID) \ - if (S.SectionMatcher->match(NAME)) \ + if (S.SectionMatcher->match(NAME, /*IsFilename=*/false)) \ Mask |= SanitizerKind::ID; #define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID) diff --git a/clang/unittests/Basic/DiagnosticTest.cpp b/clang/unittests/Basic/DiagnosticTest.cpp index 7e9653bdd3c7e..2af86b6a5ef38 100644 --- a/clang/unittests/Basic/DiagnosticTest.cpp +++ b/clang/unittests/Basic/DiagnosticTest.cpp @@ -363,12 +363,13 @@ TEST_F(SuppressionMappingTest, ParsingRespectsOtherWarningOpts) { #ifdef _WIN32 // We're only slash-agnostic on windows hosts -TEST_F(SuppressionMappingTest, ForwardSlashMatchesBothDirections) { +TEST_F(SuppressionMappingTest, TreatsFilesAsSlashAgnosticOnWindows) { llvm::StringLiteral SuppressionMappingFile = R"( [unused] src:*clang/* src:*clang/lib/Sema/*=emit - src:*clang/lib\\Sema/foo*)"; + src:*clang/lib\\Sema/foo* + fun:suppress/me)"; Diags.getDiagnosticOptions().DiagnosticSuppressionMappingsFile = "foo.txt"; FS->addFile("foo.txt", /*ModificationTime=*/{}, llvm::MemoryBuffer::getMemBuffer(SuppressionMappingFile)); @@ -376,12 +377,13 @@ TEST_F(SuppressionMappingTest, ForwardSlashMatchesBothDirections) { EXPECT_THAT(diags(), IsEmpty()); EXPECT_TRUE(Diags.isSuppressedViaMapping( - diag::warn_unused_function, locForFile(R"(clang/lib/Basic/foo.h)"))); + diag::warn_unused_function, locForFile(R"(clang/lib/Basic/bar.h)"))); EXPECT_FALSE(Diags.isSuppressedViaMapping( - diag::warn_unused_function, locForFile(R"(clang/lib/Sema\bar.h)"))); + diag::warn_unused_function, locForFile(R"(clang/lib/Sema\baz.h)"))); + + // We require a literal backslash before "Sema" EXPECT_TRUE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang\lib\Sema/foo.h)"))); - // The third pattern requires a literal backslash before Sema EXPECT_FALSE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang/lib/Sema/foo.h)"))); } diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md index ae6222d2fa145..39c2a82239fe0 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -175,8 +175,8 @@ Changes to Sanitizers --------------------- * On windows hosts, the [sanitizer special case list format](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format) - now treats forward slashes as either a forward or a backslash, to handle - paths with mixed unix and windows styles. + now treats forward slashes in filenames as matching either a forward or a + backslash, to accommodate paths with mixed unix and windows styles. Other Changes ------------- diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h index 4abd6b1874593..5fd1e0764cc7a 100644 --- a/llvm/include/llvm/Support/GlobPattern.h +++ b/llvm/include/llvm/Support/GlobPattern.h @@ -35,9 +35,9 @@ namespace llvm { /// expansions are not supported. If \p MaxSubPatterns is empty then /// brace expansions are not supported and characters `{,}` are treated as /// literals. -/// * If IsSlashAgnostic is passed, `/` matches both unix and windows path -/// separators: `/` and `\`. /// * `\` escapes the next character so it is treated as a literal. +/// * If \p IsSlashAgnostic is passed to the match function, then forward +/// slashes `/` also match backslashes `\`. /// /// Some known edge cases are: /// * The literal `]` is allowed as the first character in a character class, @@ -57,12 +57,11 @@ class GlobPattern { /// \param MaxSubPatterns if provided limit the number of allowed subpatterns /// created from expanding braces otherwise disable /// brace expansion - /// \param IsSlashAgnostic whether to treat '/' as matching '\\' as well LLVM_ABI static Expected - create(StringRef Pat, std::optional MaxSubPatterns = {}, - bool IsSlashAgnostic = false); + create(StringRef Pat, std::optional MaxSubPatterns = {}); + /// \param IsSlashAgnostic whether to treat '/' as also matching '\' /// \returns \p true if \p S matches this glob pattern - LLVM_ABI bool match(StringRef S) const; + LLVM_ABI bool match(StringRef S, bool IsSlashAgnostic = false) const; // Returns true for glob pattern "*". Can be used to avoid expensive // preparation/acquisition of the input for match(). @@ -79,11 +78,10 @@ class GlobPattern { struct SubGlobPattern { /// \param Pat the pattern to match against - /// \param SlashAgnostic whether to treat '/' as matching '\\' as well - LLVM_ABI static Expected create(StringRef Pat, - bool SlashAgnostic); + LLVM_ABI static Expected create(StringRef Pat); + /// \param IsSlashAgnostic whether to treat '/' as also matching '\' /// \returns \p true if \p S matches this glob pattern - LLVM_ABI bool match(StringRef S) const; + LLVM_ABI bool match(StringRef S, bool IsSlashAgnostic) const; StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); } // Brackets with their end position and matched bytes. @@ -93,7 +91,6 @@ class GlobPattern { }; SmallVector Brackets; SmallVector Pat; - bool IsSlashAgnostic; }; SmallVector SubGlobs; }; diff --git a/llvm/include/llvm/Support/SpecialCaseList.h b/llvm/include/llvm/Support/SpecialCaseList.h index 22a62eac9e01a..951f27eed8ee8 100644 --- a/llvm/include/llvm/Support/SpecialCaseList.h +++ b/llvm/include/llvm/Support/SpecialCaseList.h @@ -124,8 +124,9 @@ class SpecialCaseList { LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber, bool UseRegex); // Returns the line number in the source file that this query matches to. - // Returns zero if no match is found. - LLVM_ABI unsigned match(StringRef Query) const; + // On windows, treat '/' as also matching '\' in filenames when using globs. + // Returns zero if no match is found + LLVM_ABI unsigned match(StringRef Query, bool IsFilename) const; struct Glob { std::string Name; diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp index 4aa30a81c3fbf..578c0dd0760d2 100644 --- a/llvm/lib/Support/GlobPattern.cpp +++ b/llvm/lib/Support/GlobPattern.cpp @@ -132,9 +132,8 @@ parseBraceExpansions(StringRef S, std::optional MaxSubPatterns) { return std::move(SubPatterns); } -Expected GlobPattern::create(StringRef S, - std::optional MaxSubPatterns, - bool IsSlashAgnostic) { +Expected +GlobPattern::create(StringRef S, std::optional MaxSubPatterns) { GlobPattern Pat; // Store the prefix that does not contain any metacharacter. @@ -148,7 +147,7 @@ Expected GlobPattern::create(StringRef S, if (auto Err = parseBraceExpansions(S, MaxSubPatterns).moveInto(SubPats)) return std::move(Err); for (StringRef SubPat : SubPats) { - auto SubGlobOrErr = SubGlobPattern::create(SubPat, IsSlashAgnostic); + auto SubGlobOrErr = SubGlobPattern::create(SubPat); if (!SubGlobOrErr) return SubGlobOrErr.takeError(); Pat.SubGlobs.push_back(*SubGlobOrErr); @@ -158,9 +157,8 @@ Expected GlobPattern::create(StringRef S, } Expected -GlobPattern::SubGlobPattern::create(StringRef S, bool SlashAgnostic) { +GlobPattern::SubGlobPattern::create(StringRef S) { SubGlobPattern Pat; - Pat.IsSlashAgnostic = SlashAgnostic; // Parse brackets. Pat.Pat.assign(S.begin(), S.end()); @@ -192,21 +190,22 @@ GlobPattern::SubGlobPattern::create(StringRef S, bool SlashAgnostic) { return Pat; } -bool GlobPattern::match(StringRef S) const { +bool GlobPattern::match(StringRef S, bool IsSlashAgnostic) const { if (!S.consume_front(Prefix)) return false; if (SubGlobs.empty() && S.empty()) return true; for (auto &Glob : SubGlobs) - if (Glob.match(S)) + if (Glob.match(S, IsSlashAgnostic)) return true; return false; } // Factor the pattern into segments split by '*'. The segment is matched -// sequentianlly by finding the first occurrence past the end of the previous +// sequentially by finding the first occurrence past the end of the previous // match. -bool GlobPattern::SubGlobPattern::match(StringRef Str) const { +bool GlobPattern::SubGlobPattern::match(StringRef Str, + bool IsSlashAgnostic) const { const char *P = Pat.data(), *SegmentBegin = nullptr, *S = Str.data(), *SavedS = S; const char *const PEnd = P + Pat.size(), *const End = S + Str.size(); @@ -233,7 +232,7 @@ bool GlobPattern::SubGlobPattern::match(StringRef Str) const { ++S; continue; } - } else if (IsSlashAgnostic && *P == '/' && (*S == '/' || *S == '\\')) { + } else if (IsSlashAgnostic && *P == '/' && *S == '\\') { ++P; ++S; continue; diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp index 89ec193e1991c..c65cb977c005c 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -17,9 +17,8 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" #include "llvm/Support/VirtualFileSystem.h" -#include "llvm/TargetParser/Host.h" -#include "llvm/TargetParser/Triple.h" #include #include #include @@ -59,21 +58,22 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber, auto Glob = std::make_unique(); Glob->Name = Pattern.str(); Glob->LineNo = LineNumber; - // Backslashes are valid in posix-style filenames. - bool IsSlashAgnostic = Triple(sys::getDefaultTargetTriple()).isOSWindows(); // We must be sure to use the string in `Glob` rather than the provided // reference which could be destroyed before match() is called - if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024, - /*IsSlashAgnostic=*/IsSlashAgnostic) + if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024) .moveInto(Glob->Pattern)) return Err; Globs.push_back(std::move(Glob)); return Error::success(); } -unsigned SpecialCaseList::Matcher::match(StringRef Query) const { +unsigned SpecialCaseList::Matcher::match(StringRef Query, + bool IsFilename) const { + static bool HaveWindowsPathStyle = + llvm::sys::path::is_style_windows(llvm::sys::path::Style::native); for (const auto &Glob : reverse(Globs)) - if (Glob->Pattern.match(Query)) + if (Glob->Pattern.match( + Query, /*IsSlashAgnostic=*/(HaveWindowsPathStyle && IsFilename))) return Glob->LineNo; for (const auto &[Regex, LineNumber] : reverse(RegExes)) if (Regex->match(Query)) @@ -223,7 +223,8 @@ std::pair SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category) const { for (const auto &S : reverse(Sections)) { - if (S.SectionMatcher->match(Section)) { + bool IsFilename = Prefix == "src" || Prefix == "mainfile"; + if (S.SectionMatcher->match(Section, IsFilename)) { unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category); if (Blame) return {S.FileIdx, Blame}; @@ -242,7 +243,8 @@ unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries, if (II == I->second.end()) return 0; - return II->getValue().match(Query); + bool IsFilename = Prefix == "src" || Prefix == "mainfile"; + return II->getValue().match(Query, IsFilename); } } // namespace llvm From 47236f1866050ac23126a64a3d340b755fa89918 Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Thu, 2 Oct 2025 14:56:49 +0000 Subject: [PATCH 5/6] Add glob pattern unittest --- llvm/lib/Support/GlobPattern.cpp | 2 +- llvm/unittests/Support/GlobPatternTest.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp index 578c0dd0760d2..e19c8c84d19a9 100644 --- a/llvm/lib/Support/GlobPattern.cpp +++ b/llvm/lib/Support/GlobPattern.cpp @@ -137,7 +137,7 @@ GlobPattern::create(StringRef S, std::optional MaxSubPatterns) { GlobPattern Pat; // Store the prefix that does not contain any metacharacter. - size_t PrefixSize = S.find_first_of("?*[{\\"); + size_t PrefixSize = S.find_first_of("?*[{\\/"); Pat.Prefix = S.substr(0, PrefixSize); if (PrefixSize == std::string::npos) return Pat; diff --git a/llvm/unittests/Support/GlobPatternTest.cpp b/llvm/unittests/Support/GlobPatternTest.cpp index e4f1025b00956..ba33e233c70bc 100644 --- a/llvm/unittests/Support/GlobPatternTest.cpp +++ b/llvm/unittests/Support/GlobPatternTest.cpp @@ -271,4 +271,13 @@ TEST_F(GlobPatternTest, Pathological) { EXPECT_FALSE(Pat->match(S)); EXPECT_TRUE(Pat->match(S + 'b')); } + +TEST_F(GlobPatternTest, SlashAgnostic) { + auto Pat = GlobPattern::create("clang/*"); + ASSERT_TRUE((bool)Pat); + EXPECT_TRUE(Pat->match("clang/foo")); + EXPECT_FALSE(Pat->match(R"(clang\foo)")); + EXPECT_TRUE(Pat->match("clang/foo", /*isSlashAgnostic=*/true)); + EXPECT_TRUE(Pat->match(R"(clang\foo)", /*isSlashAgnostic=*/true)); +} } From 8535a1144ebad85b6282cac531d6048c7f45b4d7 Mon Sep 17 00:00:00 2001 From: Devon Loehr Date: Mon, 6 Oct 2025 17:48:19 +0000 Subject: [PATCH 6/6] Canonicalize special case list filenames when loading --- clang/docs/SanitizerSpecialCaseList.rst | 18 ++++++++++- clang/lib/Basic/Diagnostic.cpp | 21 ++++++++----- clang/lib/Basic/SanitizerSpecialCaseList.cpp | 2 +- clang/unittests/Basic/DiagnosticTest.cpp | 16 +++++++--- llvm/docs/ReleaseNotes.md | 7 +++-- llvm/include/llvm/Support/GlobPattern.h | 8 ++--- llvm/include/llvm/Support/SpecialCaseList.h | 8 ++--- llvm/lib/Support/GlobPattern.cpp | 15 +++------ llvm/lib/Support/SpecialCaseList.cpp | 31 ++++++++++--------- llvm/unittests/Support/GlobPatternTest.cpp | 9 ------ .../unittests/Support/SpecialCaseListTest.cpp | 18 +++++++++++ 11 files changed, 92 insertions(+), 61 deletions(-) diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst index 752602c1b3093..f5c45c1b81df0 100644 --- a/clang/docs/SanitizerSpecialCaseList.rst +++ b/clang/docs/SanitizerSpecialCaseList.rst @@ -174,7 +174,6 @@ tool-specific docs. # Lines starting with # are ignored. # Turn off checks for the source file # Entries without sections are placed into [*] and apply to all sanitizers - # On windows, "/" also matches "\" in filenames src:path/to/source/file.c src:*/source/file.c # Turn off checks for this main file, including files included by it. @@ -197,6 +196,23 @@ tool-specific docs. [{cfi-vcall,cfi-icall}] fun:*BadCfiCall + +.. note:: + + By default, ``src`` and ``mainfile`` are matched against the filename as seen + by LLVM. On Windows, this might involve a mix of forward and backslashes as + file separators, and writing patterns to match both variants can be + inconvenient. + + If the special case list file begins with ``#!canonical-paths``, then paths + will be canonicalized before patterns are matched against them. This involves + stripping any leading dots and slashes, and (on Windows only) converting all + backslashes to forward slashes. + + If the file uses both ``#!special-case-list-v1`` and ``#!canonical-paths``, + then they should occupy the first two lines, and ``#!canonical-paths`` must + appear on the second line. + ``mainfile`` is similar to applying ``-fno-sanitize=`` to a set of files but does not need plumbing into the build system. This works well for internal linkage functions but has a caveat for C++ vague linkage functions. diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp index 9dd133cb4c03e..71762d10aefa6 100644 --- a/clang/lib/Basic/Diagnostic.cpp +++ b/clang/lib/Basic/Diagnostic.cpp @@ -612,18 +612,24 @@ bool WarningsSpecialCaseList::isDiagSuppressed(diag::kind DiagId, SrcEntriesIt->getValue(); // We also use presumed locations here to improve reproducibility for // preprocessed inputs. - if (PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc); PLoc.isValid()) - return globsMatches( - CategoriesToMatchers, - llvm::sys::path::remove_leading_dotslash(PLoc.getFilename())); + if (PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc); PLoc.isValid()) { + if (CanonicalizePaths) { + return globsMatches( + CategoriesToMatchers, + llvm::sys::path::convert_to_slash( + llvm::sys::path::remove_leading_dotslash(PLoc.getFilename()))); + } else { + return globsMatches( + CategoriesToMatchers, + llvm::sys::path::remove_leading_dotslash(PLoc.getFilename())); + } + } return false; } bool WarningsSpecialCaseList::globsMatches( const llvm::StringMap &CategoriesToMatchers, StringRef FilePath) const { - static bool HaveWindowsPathStyle = - llvm::sys::path::is_style_windows(llvm::sys::path::Style::native); StringRef LongestMatch; bool LongestIsPositive = false; for (const auto &Entry : CategoriesToMatchers) { @@ -633,8 +639,7 @@ bool WarningsSpecialCaseList::globsMatches( for (const auto &Glob : Matcher.Globs) { if (Glob->Name.size() < LongestMatch.size()) continue; - if (!Glob->Pattern.match(FilePath, - /*IsSlashAgnostic=*/HaveWindowsPathStyle)) + if (!Glob->Pattern.match(FilePath)) continue; LongestMatch = Glob->Name; LongestIsPositive = IsPositive; diff --git a/clang/lib/Basic/SanitizerSpecialCaseList.cpp b/clang/lib/Basic/SanitizerSpecialCaseList.cpp index 4ad35d4d73fdd..f7bc1d5545d75 100644 --- a/clang/lib/Basic/SanitizerSpecialCaseList.cpp +++ b/clang/lib/Basic/SanitizerSpecialCaseList.cpp @@ -42,7 +42,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() { SanitizerMask Mask; #define SANITIZER(NAME, ID) \ - if (S.SectionMatcher->match(NAME, /*IsFilename=*/false)) \ + if (S.SectionMatcher->match(NAME)) \ Mask |= SanitizerKind::ID; #define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID) diff --git a/clang/unittests/Basic/DiagnosticTest.cpp b/clang/unittests/Basic/DiagnosticTest.cpp index 2af86b6a5ef38..2e052b9e5eaf3 100644 --- a/clang/unittests/Basic/DiagnosticTest.cpp +++ b/clang/unittests/Basic/DiagnosticTest.cpp @@ -362,9 +362,8 @@ TEST_F(SuppressionMappingTest, ParsingRespectsOtherWarningOpts) { } #ifdef _WIN32 -// We're only slash-agnostic on windows hosts -TEST_F(SuppressionMappingTest, TreatsFilesAsSlashAgnosticOnWindows) { - llvm::StringLiteral SuppressionMappingFile = R"( +TEST_F(SuppressionMappingTest, CanonicalizesSlashesOnWindows) { + llvm::StringLiteral SuppressionMappingFile = R"(#!canonical-paths [unused] src:*clang/* src:*clang/lib/Sema/*=emit @@ -378,14 +377,21 @@ TEST_F(SuppressionMappingTest, TreatsFilesAsSlashAgnosticOnWindows) { EXPECT_TRUE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang/lib/Basic/bar.h)"))); + EXPECT_TRUE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang/lib/Basic\bar.h)"))); + EXPECT_TRUE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang\lib/Basic/bar.h)"))); + EXPECT_FALSE(Diags.isSuppressedViaMapping( + diag::warn_unused_function, locForFile(R"(clang/lib/Sema/baz.h)"))); EXPECT_FALSE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang/lib/Sema\baz.h)"))); - // We require a literal backslash before "Sema" - EXPECT_TRUE(Diags.isSuppressedViaMapping( + // The backslash gets canonicalized so we never match the third pattern + EXPECT_FALSE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang\lib\Sema/foo.h)"))); EXPECT_FALSE(Diags.isSuppressedViaMapping( diag::warn_unused_function, locForFile(R"(clang/lib/Sema/foo.h)"))); } #endif + } // namespace diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md index 39c2a82239fe0..5a7f41ef3f0dd 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -174,9 +174,10 @@ Changes to BOLT Changes to Sanitizers --------------------- -* On windows hosts, the [sanitizer special case list format](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format) - now treats forward slashes in filenames as matching either a forward or a - backslash, to accommodate paths with mixed unix and windows styles. +* (Sanitizer Special Case Lists)[https://clang.llvm.org/docs/SanitizerSpecialCaseList.html] + may now be prefixed with ``#!canonical-paths`` to specify that filename patterns + should be matched against canonicalized paths, without leading dots or slashes + and (on Windows only) without any backslashes. Other Changes ------------- diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h index 5fd1e0764cc7a..62ed4a0f23fd9 100644 --- a/llvm/include/llvm/Support/GlobPattern.h +++ b/llvm/include/llvm/Support/GlobPattern.h @@ -36,8 +36,6 @@ namespace llvm { /// brace expansions are not supported and characters `{,}` are treated as /// literals. /// * `\` escapes the next character so it is treated as a literal. -/// * If \p IsSlashAgnostic is passed to the match function, then forward -/// slashes `/` also match backslashes `\`. /// /// Some known edge cases are: /// * The literal `]` is allowed as the first character in a character class, @@ -59,9 +57,8 @@ class GlobPattern { /// brace expansion LLVM_ABI static Expected create(StringRef Pat, std::optional MaxSubPatterns = {}); - /// \param IsSlashAgnostic whether to treat '/' as also matching '\' /// \returns \p true if \p S matches this glob pattern - LLVM_ABI bool match(StringRef S, bool IsSlashAgnostic = false) const; + LLVM_ABI bool match(StringRef S) const; // Returns true for glob pattern "*". Can be used to avoid expensive // preparation/acquisition of the input for match(). @@ -79,9 +76,8 @@ class GlobPattern { struct SubGlobPattern { /// \param Pat the pattern to match against LLVM_ABI static Expected create(StringRef Pat); - /// \param IsSlashAgnostic whether to treat '/' as also matching '\' /// \returns \p true if \p S matches this glob pattern - LLVM_ABI bool match(StringRef S, bool IsSlashAgnostic) const; + LLVM_ABI bool match(StringRef S) const; StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); } // Brackets with their end position and matched bytes. diff --git a/llvm/include/llvm/Support/SpecialCaseList.h b/llvm/include/llvm/Support/SpecialCaseList.h index 951f27eed8ee8..5c5df23a8623d 100644 --- a/llvm/include/llvm/Support/SpecialCaseList.h +++ b/llvm/include/llvm/Support/SpecialCaseList.h @@ -122,11 +122,10 @@ class SpecialCaseList { class Matcher { public: LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber, - bool UseRegex); + bool UseGlobs); // Returns the line number in the source file that this query matches to. - // On windows, treat '/' as also matching '\' in filenames when using globs. - // Returns zero if no match is found - LLVM_ABI unsigned match(StringRef Query, bool IsFilename) const; + // Returns zero if no match is found. + LLVM_ABI unsigned match(StringRef Query) const; struct Glob { std::string Name; @@ -155,6 +154,7 @@ class SpecialCaseList { }; std::vector
Sections; + bool CanonicalizePaths = false; LLVM_ABI Expected
addSection(StringRef SectionStr, unsigned FileIdx, unsigned LineNo, diff --git a/llvm/lib/Support/GlobPattern.cpp b/llvm/lib/Support/GlobPattern.cpp index e19c8c84d19a9..7004adf461a0c 100644 --- a/llvm/lib/Support/GlobPattern.cpp +++ b/llvm/lib/Support/GlobPattern.cpp @@ -137,7 +137,7 @@ GlobPattern::create(StringRef S, std::optional MaxSubPatterns) { GlobPattern Pat; // Store the prefix that does not contain any metacharacter. - size_t PrefixSize = S.find_first_of("?*[{\\/"); + size_t PrefixSize = S.find_first_of("?*[{\\"); Pat.Prefix = S.substr(0, PrefixSize); if (PrefixSize == std::string::npos) return Pat; @@ -190,22 +190,21 @@ GlobPattern::SubGlobPattern::create(StringRef S) { return Pat; } -bool GlobPattern::match(StringRef S, bool IsSlashAgnostic) const { +bool GlobPattern::match(StringRef S) const { if (!S.consume_front(Prefix)) return false; if (SubGlobs.empty() && S.empty()) return true; for (auto &Glob : SubGlobs) - if (Glob.match(S, IsSlashAgnostic)) + if (Glob.match(S)) return true; return false; } // Factor the pattern into segments split by '*'. The segment is matched -// sequentially by finding the first occurrence past the end of the previous +// sequentianlly by finding the first occurrence past the end of the previous // match. -bool GlobPattern::SubGlobPattern::match(StringRef Str, - bool IsSlashAgnostic) const { +bool GlobPattern::SubGlobPattern::match(StringRef Str) const { const char *P = Pat.data(), *SegmentBegin = nullptr, *S = Str.data(), *SavedS = S; const char *const PEnd = P + Pat.size(), *const End = S + Str.size(); @@ -232,10 +231,6 @@ bool GlobPattern::SubGlobPattern::match(StringRef Str, ++S; continue; } - } else if (IsSlashAgnostic && *P == '/' && *S == '\\') { - ++P; - ++S; - continue; } else if (*P == *S || *P == '?') { ++P; ++S; diff --git a/llvm/lib/Support/SpecialCaseList.cpp b/llvm/lib/Support/SpecialCaseList.cpp index c65cb977c005c..4de7478aaf53b 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -17,7 +17,6 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/Path.h" #include "llvm/Support/VirtualFileSystem.h" #include #include @@ -67,13 +66,9 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber, return Error::success(); } -unsigned SpecialCaseList::Matcher::match(StringRef Query, - bool IsFilename) const { - static bool HaveWindowsPathStyle = - llvm::sys::path::is_style_windows(llvm::sys::path::Style::native); +unsigned SpecialCaseList::Matcher::match(StringRef Query) const { for (const auto &Glob : reverse(Globs)) - if (Glob->Pattern.match( - Query, /*IsSlashAgnostic=*/(HaveWindowsPathStyle && IsFilename))) + if (Glob->Pattern.match(Query)) return Glob->LineNo; for (const auto &[Regex, LineNumber] : reverse(RegExes)) if (Regex->match(Query)) @@ -158,12 +153,17 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB, return false; } + // Scan the start of the file for special comments. These don't appear when + // iterating below because comment lines are automatically skipped. + StringRef Buffer = MB->getBuffer(); // In https://reviews.llvm.org/D154014 we added glob support and planned to // remove regex support in patterns. We temporarily support the original - // behavior using regexes if "#!special-case-list-v1" is the first line of the - // file. For more details, see + // behavior using regexes if "#!special-case-list-v1" is the first line of + // the file. For more details, see // https://discourse.llvm.org/t/use-glob-instead-of-regex-for-specialcaselists/71666 - bool UseGlobs = !MB->getBuffer().starts_with("#!special-case-list-v1\n"); + bool UseGlobs = !Buffer.consume_front("#!special-case-list-v1\n"); + // Specifies that patterns should be matched against canonicalized filepaths. + CanonicalizePaths = Buffer.consume_front("#!canonical-paths\n"); for (line_iterator LineIt(*MB, /*SkipBlanks=*/true, /*CommentMarker=*/'#'); !LineIt.is_at_eof(); LineIt++) { @@ -223,8 +223,7 @@ std::pair SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query, StringRef Category) const { for (const auto &S : reverse(Sections)) { - bool IsFilename = Prefix == "src" || Prefix == "mainfile"; - if (S.SectionMatcher->match(Section, IsFilename)) { + if (S.SectionMatcher->match(Section)) { unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category); if (Blame) return {S.FileIdx, Blame}; @@ -243,8 +242,12 @@ unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries, if (II == I->second.end()) return 0; - bool IsFilename = Prefix == "src" || Prefix == "mainfile"; - return II->getValue().match(Query, IsFilename); + if (CanonicalizePaths && (Prefix == "src" || Prefix == "mainfile")) { + return II->getValue().match(llvm::sys::path::convert_to_slash( + llvm::sys::path::remove_leading_dotslash(Query))); + } else { + return II->getValue().match(Query); + } } } // namespace llvm diff --git a/llvm/unittests/Support/GlobPatternTest.cpp b/llvm/unittests/Support/GlobPatternTest.cpp index ba33e233c70bc..e4f1025b00956 100644 --- a/llvm/unittests/Support/GlobPatternTest.cpp +++ b/llvm/unittests/Support/GlobPatternTest.cpp @@ -271,13 +271,4 @@ TEST_F(GlobPatternTest, Pathological) { EXPECT_FALSE(Pat->match(S)); EXPECT_TRUE(Pat->match(S + 'b')); } - -TEST_F(GlobPatternTest, SlashAgnostic) { - auto Pat = GlobPattern::create("clang/*"); - ASSERT_TRUE((bool)Pat); - EXPECT_TRUE(Pat->match("clang/foo")); - EXPECT_FALSE(Pat->match(R"(clang\foo)")); - EXPECT_TRUE(Pat->match("clang/foo", /*isSlashAgnostic=*/true)); - EXPECT_TRUE(Pat->match(R"(clang\foo)", /*isSlashAgnostic=*/true)); -} } diff --git a/llvm/unittests/Support/SpecialCaseListTest.cpp b/llvm/unittests/Support/SpecialCaseListTest.cpp index 5be2b9e3a7a5d..5fc077f3d94ac 100644 --- a/llvm/unittests/Support/SpecialCaseListTest.cpp +++ b/llvm/unittests/Support/SpecialCaseListTest.cpp @@ -372,4 +372,22 @@ TEST_F(SpecialCaseListTest, FileIdx) { sys::fs::remove(Path); } +#ifdef _WIN32 +TEST_F(SpecialCaseListTest, CanonicalizePathsOnWindows) { + std::unique_ptr SCL = + makeSpecialCaseList("#!canonical-paths\n" + "\n" + "src:*foo/bar*\n" + "src:*foo\\\\baz\n" + "fun:hi\\\\bye=category\n"); + EXPECT_TRUE(SCL->inSection("", "src", "foo/bar")); + EXPECT_TRUE(SCL->inSection("", "src", "foo\\bar")); + // The baz pattern doesn't match because paths are canonicalized first + EXPECT_FALSE(SCL->inSection("", "src", "foo/baz")); + EXPECT_FALSE(SCL->inSection("", "src", "foo\\baz")); + // The canonicalization only applies to files + EXPECT_TRUE(SCL->inSection("", "fun", "hi\\bye", "category")); +} +#endif + } // namespace