Skip to content

Commit 3f48497

Browse files
committed
Incorporate review feedback
1 parent 4fd9a34 commit 3f48497

File tree

9 files changed

+48
-44
lines changed

9 files changed

+48
-44
lines changed

clang/docs/SanitizerSpecialCaseList.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ tool-specific docs.
174174
# Lines starting with # are ignored.
175175
# Turn off checks for the source file
176176
# Entries without sections are placed into [*] and apply to all sanitizers
177-
# On windows, "/" matches both styles of path separator ("/" and "\")
177+
# On windows, "/" also matches "\" in filenames
178178
src:path/to/source/file.c
179179
src:*/source/file.c
180180
# Turn off checks for this main file, including files included by it.

clang/lib/Basic/Diagnostic.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ bool WarningsSpecialCaseList::isDiagSuppressed(diag::kind DiagId,
622622
bool WarningsSpecialCaseList::globsMatches(
623623
const llvm::StringMap<Matcher> &CategoriesToMatchers,
624624
StringRef FilePath) const {
625+
static bool HaveWindowsPathStyle =
626+
llvm::sys::path::is_style_windows(llvm::sys::path::Style::native);
625627
StringRef LongestMatch;
626628
bool LongestIsPositive = false;
627629
for (const auto &Entry : CategoriesToMatchers) {
@@ -631,7 +633,8 @@ bool WarningsSpecialCaseList::globsMatches(
631633
for (const auto &Glob : Matcher.Globs) {
632634
if (Glob->Name.size() < LongestMatch.size())
633635
continue;
634-
if (!Glob->Pattern.match(FilePath))
636+
if (!Glob->Pattern.match(FilePath,
637+
/*IsSlashAgnostic=*/HaveWindowsPathStyle))
635638
continue;
636639
LongestMatch = Glob->Name;
637640
LongestIsPositive = IsPositive;

clang/lib/Basic/SanitizerSpecialCaseList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() {
4242
SanitizerMask Mask;
4343

4444
#define SANITIZER(NAME, ID) \
45-
if (S.SectionMatcher->match(NAME)) \
45+
if (S.SectionMatcher->match(NAME, /*IsFilename=*/false)) \
4646
Mask |= SanitizerKind::ID;
4747
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)
4848

clang/unittests/Basic/DiagnosticTest.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,25 +363,27 @@ TEST_F(SuppressionMappingTest, ParsingRespectsOtherWarningOpts) {
363363

364364
#ifdef _WIN32
365365
// We're only slash-agnostic on windows hosts
366-
TEST_F(SuppressionMappingTest, ForwardSlashMatchesBothDirections) {
366+
TEST_F(SuppressionMappingTest, TreatsFilesAsSlashAgnosticOnWindows) {
367367
llvm::StringLiteral SuppressionMappingFile = R"(
368368
[unused]
369369
src:*clang/*
370370
src:*clang/lib/Sema/*=emit
371-
src:*clang/lib\\Sema/foo*)";
371+
src:*clang/lib\\Sema/foo*
372+
fun:suppress/me)";
372373
Diags.getDiagnosticOptions().DiagnosticSuppressionMappingsFile = "foo.txt";
373374
FS->addFile("foo.txt", /*ModificationTime=*/{},
374375
llvm::MemoryBuffer::getMemBuffer(SuppressionMappingFile));
375376
clang::ProcessWarningOptions(Diags, Diags.getDiagnosticOptions(), *FS);
376377
EXPECT_THAT(diags(), IsEmpty());
377378

378379
EXPECT_TRUE(Diags.isSuppressedViaMapping(
379-
diag::warn_unused_function, locForFile(R"(clang/lib/Basic/foo.h)")));
380+
diag::warn_unused_function, locForFile(R"(clang/lib/Basic/bar.h)")));
380381
EXPECT_FALSE(Diags.isSuppressedViaMapping(
381-
diag::warn_unused_function, locForFile(R"(clang/lib/Sema\bar.h)")));
382+
diag::warn_unused_function, locForFile(R"(clang/lib/Sema\baz.h)")));
383+
384+
// We require a literal backslash before "Sema"
382385
EXPECT_TRUE(Diags.isSuppressedViaMapping(
383386
diag::warn_unused_function, locForFile(R"(clang\lib\Sema/foo.h)")));
384-
// The third pattern requires a literal backslash before Sema
385387
EXPECT_FALSE(Diags.isSuppressedViaMapping(
386388
diag::warn_unused_function, locForFile(R"(clang/lib/Sema/foo.h)")));
387389
}

llvm/docs/ReleaseNotes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ Changes to Sanitizers
175175
---------------------
176176

177177
* On windows hosts, the [sanitizer special case list format](https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format)
178-
now treats forward slashes as either a forward or a backslash, to handle
179-
paths with mixed unix and windows styles.
178+
now treats forward slashes in filenames as matching either a forward or a
179+
backslash, to accommodate paths with mixed unix and windows styles.
180180

181181
Other Changes
182182
-------------

llvm/include/llvm/Support/GlobPattern.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ namespace llvm {
3535
/// expansions are not supported. If \p MaxSubPatterns is empty then
3636
/// brace expansions are not supported and characters `{,}` are treated as
3737
/// literals.
38-
/// * If IsSlashAgnostic is passed, `/` matches both unix and windows path
39-
/// separators: `/` and `\`.
4038
/// * `\` escapes the next character so it is treated as a literal.
39+
/// * If \p IsSlashAgnostic is passed to the match function, then forward
40+
/// slashes `/` also match backslashes `\`.
4141
///
4242
/// Some known edge cases are:
4343
/// * The literal `]` is allowed as the first character in a character class,
@@ -57,12 +57,11 @@ class GlobPattern {
5757
/// \param MaxSubPatterns if provided limit the number of allowed subpatterns
5858
/// created from expanding braces otherwise disable
5959
/// brace expansion
60-
/// \param IsSlashAgnostic whether to treat '/' as matching '\\' as well
6160
LLVM_ABI static Expected<GlobPattern>
62-
create(StringRef Pat, std::optional<size_t> MaxSubPatterns = {},
63-
bool IsSlashAgnostic = false);
61+
create(StringRef Pat, std::optional<size_t> MaxSubPatterns = {});
62+
/// \param IsSlashAgnostic whether to treat '/' as also matching '\'
6463
/// \returns \p true if \p S matches this glob pattern
65-
LLVM_ABI bool match(StringRef S) const;
64+
LLVM_ABI bool match(StringRef S, bool IsSlashAgnostic = false) const;
6665

6766
// Returns true for glob pattern "*". Can be used to avoid expensive
6867
// preparation/acquisition of the input for match().
@@ -79,11 +78,10 @@ class GlobPattern {
7978

8079
struct SubGlobPattern {
8180
/// \param Pat the pattern to match against
82-
/// \param SlashAgnostic whether to treat '/' as matching '\\' as well
83-
LLVM_ABI static Expected<SubGlobPattern> create(StringRef Pat,
84-
bool SlashAgnostic);
81+
LLVM_ABI static Expected<SubGlobPattern> create(StringRef Pat);
82+
/// \param IsSlashAgnostic whether to treat '/' as also matching '\'
8583
/// \returns \p true if \p S matches this glob pattern
86-
LLVM_ABI bool match(StringRef S) const;
84+
LLVM_ABI bool match(StringRef S, bool IsSlashAgnostic) const;
8785
StringRef getPat() const { return StringRef(Pat.data(), Pat.size()); }
8886

8987
// Brackets with their end position and matched bytes.
@@ -93,7 +91,6 @@ class GlobPattern {
9391
};
9492
SmallVector<Bracket, 0> Brackets;
9593
SmallVector<char, 0> Pat;
96-
bool IsSlashAgnostic;
9794
};
9895
SmallVector<SubGlobPattern, 1> SubGlobs;
9996
};

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ class SpecialCaseList {
124124
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
125125
bool UseRegex);
126126
// Returns the line number in the source file that this query matches to.
127-
// Returns zero if no match is found.
128-
LLVM_ABI unsigned match(StringRef Query) const;
127+
// On windows, treat '/' as also matching '\' in filenames when using globs.
128+
// Returns zero if no match is found
129+
LLVM_ABI unsigned match(StringRef Query, bool IsFilename) const;
129130

130131
struct Glob {
131132
std::string Name;

llvm/lib/Support/GlobPattern.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ parseBraceExpansions(StringRef S, std::optional<size_t> MaxSubPatterns) {
132132
return std::move(SubPatterns);
133133
}
134134

135-
Expected<GlobPattern> GlobPattern::create(StringRef S,
136-
std::optional<size_t> MaxSubPatterns,
137-
bool IsSlashAgnostic) {
135+
Expected<GlobPattern>
136+
GlobPattern::create(StringRef S, std::optional<size_t> MaxSubPatterns) {
138137
GlobPattern Pat;
139138

140139
// Store the prefix that does not contain any metacharacter.
@@ -148,7 +147,7 @@ Expected<GlobPattern> GlobPattern::create(StringRef S,
148147
if (auto Err = parseBraceExpansions(S, MaxSubPatterns).moveInto(SubPats))
149148
return std::move(Err);
150149
for (StringRef SubPat : SubPats) {
151-
auto SubGlobOrErr = SubGlobPattern::create(SubPat, IsSlashAgnostic);
150+
auto SubGlobOrErr = SubGlobPattern::create(SubPat);
152151
if (!SubGlobOrErr)
153152
return SubGlobOrErr.takeError();
154153
Pat.SubGlobs.push_back(*SubGlobOrErr);
@@ -158,9 +157,8 @@ Expected<GlobPattern> GlobPattern::create(StringRef S,
158157
}
159158

160159
Expected<GlobPattern::SubGlobPattern>
161-
GlobPattern::SubGlobPattern::create(StringRef S, bool SlashAgnostic) {
160+
GlobPattern::SubGlobPattern::create(StringRef S) {
162161
SubGlobPattern Pat;
163-
Pat.IsSlashAgnostic = SlashAgnostic;
164162

165163
// Parse brackets.
166164
Pat.Pat.assign(S.begin(), S.end());
@@ -192,21 +190,22 @@ GlobPattern::SubGlobPattern::create(StringRef S, bool SlashAgnostic) {
192190
return Pat;
193191
}
194192

195-
bool GlobPattern::match(StringRef S) const {
193+
bool GlobPattern::match(StringRef S, bool IsSlashAgnostic) const {
196194
if (!S.consume_front(Prefix))
197195
return false;
198196
if (SubGlobs.empty() && S.empty())
199197
return true;
200198
for (auto &Glob : SubGlobs)
201-
if (Glob.match(S))
199+
if (Glob.match(S, IsSlashAgnostic))
202200
return true;
203201
return false;
204202
}
205203

206204
// Factor the pattern into segments split by '*'. The segment is matched
207-
// sequentianlly by finding the first occurrence past the end of the previous
205+
// sequentially by finding the first occurrence past the end of the previous
208206
// match.
209-
bool GlobPattern::SubGlobPattern::match(StringRef Str) const {
207+
bool GlobPattern::SubGlobPattern::match(StringRef Str,
208+
bool IsSlashAgnostic) const {
210209
const char *P = Pat.data(), *SegmentBegin = nullptr, *S = Str.data(),
211210
*SavedS = S;
212211
const char *const PEnd = P + Pat.size(), *const End = S + Str.size();
@@ -233,7 +232,7 @@ bool GlobPattern::SubGlobPattern::match(StringRef Str) const {
233232
++S;
234233
continue;
235234
}
236-
} else if (IsSlashAgnostic && *P == '/' && (*S == '/' || *S == '\\')) {
235+
} else if (IsSlashAgnostic && *P == '/' && *S == '\\') {
237236
++P;
238237
++S;
239238
continue;

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
#include "llvm/ADT/STLExtras.h"
1818
#include "llvm/Support/LineIterator.h"
1919
#include "llvm/Support/MemoryBuffer.h"
20+
#include "llvm/Support/Path.h"
2021
#include "llvm/Support/VirtualFileSystem.h"
21-
#include "llvm/TargetParser/Host.h"
22-
#include "llvm/TargetParser/Triple.h"
2322
#include <stdio.h>
2423
#include <string>
2524
#include <system_error>
@@ -59,21 +58,22 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
5958
auto Glob = std::make_unique<Matcher::Glob>();
6059
Glob->Name = Pattern.str();
6160
Glob->LineNo = LineNumber;
62-
// Backslashes are valid in posix-style filenames.
63-
bool IsSlashAgnostic = Triple(sys::getDefaultTargetTriple()).isOSWindows();
6461
// We must be sure to use the string in `Glob` rather than the provided
6562
// reference which could be destroyed before match() is called
66-
if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024,
67-
/*IsSlashAgnostic=*/IsSlashAgnostic)
63+
if (auto Err = GlobPattern::create(Glob->Name, /*MaxSubPatterns=*/1024)
6864
.moveInto(Glob->Pattern))
6965
return Err;
7066
Globs.push_back(std::move(Glob));
7167
return Error::success();
7268
}
7369

74-
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
70+
unsigned SpecialCaseList::Matcher::match(StringRef Query,
71+
bool IsFilename) const {
72+
static bool HaveWindowsPathStyle =
73+
llvm::sys::path::is_style_windows(llvm::sys::path::Style::native);
7574
for (const auto &Glob : reverse(Globs))
76-
if (Glob->Pattern.match(Query))
75+
if (Glob->Pattern.match(
76+
Query, /*IsSlashAgnostic=*/(HaveWindowsPathStyle && IsFilename)))
7777
return Glob->LineNo;
7878
for (const auto &[Regex, LineNumber] : reverse(RegExes))
7979
if (Regex->match(Query))
@@ -223,7 +223,8 @@ std::pair<unsigned, unsigned>
223223
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
224224
StringRef Query, StringRef Category) const {
225225
for (const auto &S : reverse(Sections)) {
226-
if (S.SectionMatcher->match(Section)) {
226+
bool IsFilename = Prefix == "src" || Prefix == "mainfile";
227+
if (S.SectionMatcher->match(Section, IsFilename)) {
227228
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
228229
if (Blame)
229230
return {S.FileIdx, Blame};
@@ -242,7 +243,8 @@ unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
242243
if (II == I->second.end())
243244
return 0;
244245

245-
return II->getValue().match(Query);
246+
bool IsFilename = Prefix == "src" || Prefix == "mainfile";
247+
return II->getValue().match(Query, IsFilename);
246248
}
247249

248250
} // namespace llvm

0 commit comments

Comments
 (0)