Skip to content

Commit 3b4c62c

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6
2 parents b98ac06 + 9b36239 commit 3b4c62c

File tree

8 files changed

+34
-52
lines changed

8 files changed

+34
-52
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Potentially Breaking Changes
6969
call the member ``operator delete`` instead of the expected global
7070
delete operator. The old behavior is retained under ``-fclang-abi-compat=21``
7171
flag.
72+
- Clang warning suppressions file, ``--warning-suppression-mappings=``, now will
73+
use the last matching entry instead of the longest one.
7274

7375
C/C++ Language Potentially Breaking Changes
7476
-------------------------------------------

clang/docs/WarningSuppressionMappings.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Format
6363
Warning suppression mappings uses the same format as
6464
:doc:`SanitizerSpecialCaseList`.
6565

66-
Sections describe which diagnostic group's behaviour to change, e.g.
66+
Sections describe which diagnostic group's behavior to change, e.g.
6767
``[unused]``. When a diagnostic is matched by multiple sections, the latest
6868
section takes precedence.
6969

@@ -76,7 +76,7 @@ Source files are matched against these globs either:
7676
- as paths relative to the current working directory
7777
- as absolute paths.
7878

79-
When a source file matches multiple globs in a section, the longest one takes
79+
When a source file matches multiple globs in a section, the last one takes
8080
precedence.
8181

8282
.. code-block:: bash

clang/include/clang/Basic/Diagnostic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
971971
/// diagnostics in specific files.
972972
/// Mapping file is expected to be a special case list with sections denoting
973973
/// diagnostic groups and `src` entries for globs to suppress. `emit` category
974-
/// can be used to disable suppression. Longest glob that matches a filepath
974+
/// can be used to disable suppression. THe last glob that matches a filepath
975975
/// takes precedence. For example:
976976
/// [unused]
977977
/// src:clang/*

clang/lib/Basic/Diagnostic.cpp

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,6 @@ class WarningsSpecialCaseList : public llvm::SpecialCaseList {
517517
const SourceManager &SM) const;
518518

519519
private:
520-
// Find the longest glob pattern that matches FilePath amongst
521-
// CategoriesToMatchers, return true iff the match exists and belongs to a
522-
// positive category.
523-
bool globsMatches(const llvm::StringMap<Matcher> &CategoriesToMatchers,
524-
StringRef FilePath) const;
525-
526520
llvm::DenseMap<diag::kind, const Section *> DiagToSection;
527521
};
528522
} // namespace
@@ -584,43 +578,26 @@ void DiagnosticsEngine::setDiagSuppressionMapping(llvm::MemoryBuffer &Input) {
584578
bool WarningsSpecialCaseList::isDiagSuppressed(diag::kind DiagId,
585579
SourceLocation DiagLoc,
586580
const SourceManager &SM) const {
581+
PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc);
582+
if (!PLoc.isValid())
583+
return false;
587584
const Section *DiagSection = DiagToSection.lookup(DiagId);
588585
if (!DiagSection)
589586
return false;
590-
const SectionEntries &EntityTypeToCategories = DiagSection->Entries;
591-
auto SrcEntriesIt = EntityTypeToCategories.find("src");
592-
if (SrcEntriesIt == EntityTypeToCategories.end())
587+
588+
StringRef F = llvm::sys::path::remove_leading_dotslash(PLoc.getFilename());
589+
590+
unsigned SuppressLineNo =
591+
llvm::SpecialCaseList::inSectionBlame(DiagSection->Entries, "src", F, "");
592+
if (!SuppressLineNo)
593593
return false;
594-
const llvm::StringMap<llvm::SpecialCaseList::Matcher> &CategoriesToMatchers =
595-
SrcEntriesIt->getValue();
596-
// We also use presumed locations here to improve reproducibility for
597-
// preprocessed inputs.
598-
if (PresumedLoc PLoc = SM.getPresumedLoc(DiagLoc); PLoc.isValid())
599-
return globsMatches(
600-
CategoriesToMatchers,
601-
llvm::sys::path::remove_leading_dotslash(PLoc.getFilename()));
602-
return false;
603-
}
604594

605-
bool WarningsSpecialCaseList::globsMatches(
606-
const llvm::StringMap<Matcher> &CategoriesToMatchers,
607-
StringRef FilePath) const {
608-
StringRef LongestMatch;
609-
bool LongestIsPositive = false;
610-
for (const auto &Entry : CategoriesToMatchers) {
611-
StringRef Category = Entry.getKey();
612-
const llvm::SpecialCaseList::Matcher &Matcher = Entry.getValue();
613-
bool IsPositive = Category != "emit";
614-
for (const auto &Glob : Matcher.Globs) {
615-
if (Glob->Name.size() < LongestMatch.size())
616-
continue;
617-
if (!Glob->Pattern.match(FilePath))
618-
continue;
619-
LongestMatch = Glob->Name;
620-
LongestIsPositive = IsPositive;
621-
}
622-
}
623-
return LongestIsPositive;
595+
unsigned EmitLineNo = llvm::SpecialCaseList::inSectionBlame(
596+
DiagSection->Entries, "src", F, "emit");
597+
if (!EmitLineNo)
598+
return true;
599+
600+
return SuppressLineNo > EmitLineNo;
624601
}
625602

626603
bool DiagnosticsEngine::isSuppressedViaMapping(diag::kind DiagId,

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)) \
4646
Mask |= SanitizerKind::ID;
4747
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)
4848

clang/unittests/Basic/DiagnosticTest.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ TEST_F(SuppressionMappingTest, EmitCategoryIsExcluded) {
294294
locForFile("foo.cpp")));
295295
}
296296

297-
TEST_F(SuppressionMappingTest, LongestMatchWins) {
297+
TEST_F(SuppressionMappingTest, LastMatchWins) {
298298
llvm::StringLiteral SuppressionMappingFile = R"(
299299
[unused]
300300
src:*clang/*
@@ -327,10 +327,8 @@ TEST_F(SuppressionMappingTest, LongShortMatch) {
327327

328328
EXPECT_TRUE(Diags.isSuppressedViaMapping(diag::warn_unused_function,
329329
locForFile("test/t1.cpp")));
330-
331-
// FIXME: This is confusing.
332-
EXPECT_TRUE(Diags.isSuppressedViaMapping(diag::warn_unused_function,
333-
locForFile("lld/test/t2.cpp")));
330+
EXPECT_FALSE(Diags.isSuppressedViaMapping(diag::warn_unused_function,
331+
locForFile("lld/test/t2.cpp")));
334332
}
335333

336334
TEST_F(SuppressionMappingTest, ShortLongMatch) {

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,15 @@ class SpecialCaseList {
121121
/// Represents a set of globs and their line numbers
122122
class Matcher {
123123
public:
124-
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
125-
bool UseRegex);
126124
// Returns the line number in the source file that this query matches to.
127125
// Returns zero if no match is found.
128126
LLVM_ABI unsigned match(StringRef Query) const;
129127

128+
private:
129+
friend class SpecialCaseList;
130+
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
131+
bool UseRegex);
132+
130133
struct Glob {
131134
std::string Name;
132135
unsigned LineNo;
@@ -147,7 +150,9 @@ class SpecialCaseList {
147150
Section(StringRef Str, unsigned FileIdx)
148151
: SectionStr(Str), FileIdx(FileIdx) {};
149152

150-
std::unique_ptr<Matcher> SectionMatcher = std::make_unique<Matcher>();
153+
Section(Section &&) = default;
154+
155+
Matcher SectionMatcher;
151156
SectionEntries Entries;
152157
std::string SectionStr;
153158
unsigned FileIdx;

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ SpecialCaseList::addSection(StringRef SectionStr, unsigned FileNo,
135135
Sections.emplace_back(SectionStr, FileNo);
136136
auto &Section = Sections.back();
137137

138-
if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs)) {
138+
if (auto Err = Section.SectionMatcher.insert(SectionStr, LineNo, UseGlobs)) {
139139
return createStringError(errc::invalid_argument,
140140
"malformed section at line " + Twine(LineNo) +
141141
": '" + SectionStr +
@@ -218,7 +218,7 @@ std::pair<unsigned, unsigned>
218218
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
219219
StringRef Query, StringRef Category) const {
220220
for (const auto &S : reverse(Sections)) {
221-
if (S.SectionMatcher->match(Section)) {
221+
if (S.SectionMatcher.match(Section)) {
222222
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
223223
if (Blame)
224224
return {S.FileIdx, Blame};

0 commit comments

Comments
 (0)