diff --git a/clang/docs/SanitizerSpecialCaseList.rst b/clang/docs/SanitizerSpecialCaseList.rst index 307c001664fba..752602c1b3093 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 + # 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 4b3af00c3b0ce..2af86b6a5ef38 100644 --- a/clang/unittests/Basic/DiagnosticTest.cpp +++ b/clang/unittests/Basic/DiagnosticTest.cpp @@ -360,4 +360,32 @@ TEST_F(SuppressionMappingTest, ParsingRespectsOtherWarningOpts) { clang::ProcessWarningOptions(Diags, Diags.getDiagnosticOptions(), *FS); EXPECT_THAT(diags(), IsEmpty()); } + +#ifdef _WIN32 +// We're only slash-agnostic on windows hosts +TEST_F(SuppressionMappingTest, TreatsFilesAsSlashAgnosticOnWindows) { + llvm::StringLiteral SuppressionMappingFile = R"( + [unused] + src:*clang/* + src:*clang/lib/Sema/*=emit + src:*clang/lib\\Sema/foo* + fun:suppress/me)"; + 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/bar.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( + 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 85c16b9c33f10..39c2a82239fe0 100644 --- a/llvm/docs/ReleaseNotes.md +++ b/llvm/docs/ReleaseNotes.md @@ -174,6 +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. + Other Changes ------------- diff --git a/llvm/include/llvm/Support/GlobPattern.h b/llvm/include/llvm/Support/GlobPattern.h index 62ed4a0f23fd9..5fd1e0764cc7a 100644 --- a/llvm/include/llvm/Support/GlobPattern.h +++ b/llvm/include/llvm/Support/GlobPattern.h @@ -36,6 +36,8 @@ 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, @@ -57,8 +59,9 @@ 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) 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(). @@ -76,8 +79,9 @@ 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) 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. 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 7004adf461a0c..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; @@ -190,21 +190,22 @@ GlobPattern::SubGlobPattern::create(StringRef S) { 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(); @@ -231,6 +232,10 @@ bool GlobPattern::SubGlobPattern::match(StringRef Str) const { ++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 8d4e043bc1c9f..c65cb977c005c 100644 --- a/llvm/lib/Support/SpecialCaseList.cpp +++ b/llvm/lib/Support/SpecialCaseList.cpp @@ -17,6 +17,7 @@ #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 @@ -66,9 +67,13 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber, 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)) @@ -218,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}; @@ -237,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 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)); +} }