Skip to content

Commit 405b6c1

Browse files
committed
Convert SpecialCaseList::Sections from StringMap to vector.
In this way, SpecialCaseList::Sections can keep the order of Sections when parsing the case list. Reviewers: Pull Request: #140115
1 parent 7e690db commit 405b6c1

File tree

5 files changed

+114
-98
lines changed

5 files changed

+114
-98
lines changed

clang/lib/Basic/Diagnostic.cpp

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
6767
return DB;
6868
}
6969

70-
static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
71-
StringRef Modifier, StringRef Argument,
72-
ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
73-
SmallVectorImpl<char> &Output,
74-
void *Cookie,
75-
ArrayRef<intptr_t> QualTypeVals) {
70+
static void
71+
DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
72+
StringRef Modifier, StringRef Argument,
73+
ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
74+
SmallVectorImpl<char> &Output, void *Cookie,
75+
ArrayRef<intptr_t> QualTypeVals) {
7676
StringRef Str = "<can't format argument>";
7777
Output.append(Str.begin(), Str.end());
7878
}
@@ -94,9 +94,7 @@ DiagnosticsEngine::~DiagnosticsEngine() {
9494
setClient(nullptr);
9595
}
9696

97-
void DiagnosticsEngine::dump() const {
98-
DiagStatesByLoc.dump(*SourceMgr);
99-
}
97+
void DiagnosticsEngine::dump() const { DiagStatesByLoc.dump(*SourceMgr); }
10098

10199
void DiagnosticsEngine::dump(StringRef DiagName) const {
102100
DiagStatesByLoc.dump(*SourceMgr, DiagName);
@@ -259,7 +257,8 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
259257

260258
bool PrintedOuterHeading = false;
261259
auto PrintOuterHeading = [&] {
262-
if (PrintedOuterHeading) return;
260+
if (PrintedOuterHeading)
261+
return;
263262
PrintedOuterHeading = true;
264263

265264
llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue()
@@ -272,8 +271,8 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
272271
llvm::errs() << " parent " << File.Parent << " <FileID "
273272
<< Decomp.first.getHashValue() << "> ";
274273
SrcMgr.getLocForStartOfFile(Decomp.first)
275-
.getLocWithOffset(Decomp.second)
276-
.print(llvm::errs(), SrcMgr);
274+
.getLocWithOffset(Decomp.second)
275+
.print(llvm::errs(), SrcMgr);
277276
}
278277
if (File.HasLocalTransitions)
279278
llvm::errs() << " has_local_transitions";
@@ -286,14 +285,15 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
286285
for (DiagStatePoint &Transition : File.StateTransitions) {
287286
bool PrintedInnerHeading = false;
288287
auto PrintInnerHeading = [&] {
289-
if (PrintedInnerHeading) return;
288+
if (PrintedInnerHeading)
289+
return;
290290
PrintedInnerHeading = true;
291291

292292
PrintOuterHeading();
293293
llvm::errs() << " ";
294294
SrcMgr.getLocForStartOfFile(ID)
295-
.getLocWithOffset(Transition.Offset)
296-
.print(llvm::errs(), SrcMgr);
295+
.getLocWithOffset(Transition.Offset)
296+
.print(llvm::errs(), SrcMgr);
297297
llvm::errs() << ": state " << Transition.State << ":\n";
298298
};
299299

@@ -316,11 +316,21 @@ void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr,
316316
llvm::errs() << ": ";
317317

318318
switch (Mapping.second.getSeverity()) {
319-
case diag::Severity::Ignored: llvm::errs() << "ignored"; break;
320-
case diag::Severity::Remark: llvm::errs() << "remark"; break;
321-
case diag::Severity::Warning: llvm::errs() << "warning"; break;
322-
case diag::Severity::Error: llvm::errs() << "error"; break;
323-
case diag::Severity::Fatal: llvm::errs() << "fatal"; break;
319+
case diag::Severity::Ignored:
320+
llvm::errs() << "ignored";
321+
break;
322+
case diag::Severity::Remark:
323+
llvm::errs() << "remark";
324+
break;
325+
case diag::Severity::Warning:
326+
llvm::errs() << "warning";
327+
break;
328+
case diag::Severity::Error:
329+
llvm::errs() << "error";
330+
break;
331+
case diag::Severity::Fatal:
332+
llvm::errs() << "fatal";
333+
break;
324334
}
325335

326336
if (!Mapping.second.isUser())
@@ -533,24 +543,28 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
533543
// Drop the default section introduced by special case list, we only support
534544
// exact diagnostic group names.
535545
// FIXME: We should make this configurable in the parser instead.
536-
Sections.erase("*");
546+
// FIXME: C++20 can use std::erase_if(Sections, [](Section &sec) { return
547+
// sec.SectionStr == "*"; });
548+
Sections.erase(
549+
std::remove_if(Sections.begin(), Sections.end(),
550+
[](Section &sec) { return sec.SectionStr == "*"; }),
551+
Sections.end());
537552
// Make sure we iterate sections by their line numbers.
538-
std::vector<std::pair<unsigned, const llvm::StringMapEntry<Section> *>>
539-
LineAndSectionEntry;
553+
std::vector<std::pair<unsigned, const Section *>> LineAndSectionEntry;
540554
LineAndSectionEntry.reserve(Sections.size());
541555
for (const auto &Entry : Sections) {
542-
StringRef DiagName = Entry.getKey();
556+
StringRef DiagName = Entry.SectionStr;
543557
// Each section has a matcher with that section's name, attached to that
544558
// line.
545-
const auto &DiagSectionMatcher = Entry.getValue().SectionMatcher;
559+
const auto &DiagSectionMatcher = Entry.SectionMatcher;
546560
unsigned DiagLine = DiagSectionMatcher->Globs.at(DiagName).second;
547561
LineAndSectionEntry.emplace_back(DiagLine, &Entry);
548562
}
549563
llvm::sort(LineAndSectionEntry);
550564
static constexpr auto WarningFlavor = clang::diag::Flavor::WarningOrError;
551565
for (const auto &[_, SectionEntry] : LineAndSectionEntry) {
552566
SmallVector<diag::kind> GroupDiags;
553-
StringRef DiagGroup = SectionEntry->getKey();
567+
StringRef DiagGroup = SectionEntry->SectionStr;
554568
if (Diags.getDiagnosticIDs()->getDiagnosticsInGroup(
555569
WarningFlavor, DiagGroup, GroupDiags)) {
556570
StringRef Suggestion =
@@ -563,7 +577,7 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
563577
for (diag::kind Diag : GroupDiags)
564578
// We're intentionally overwriting any previous mappings here to make sure
565579
// latest one takes precedence.
566-
DiagToSection[Diag] = &SectionEntry->getValue();
580+
DiagToSection[Diag] = SectionEntry;
567581
}
568582
}
569583

@@ -662,8 +676,8 @@ bool DiagnosticsEngine::EmitDiagnostic(const DiagnosticBuilder &DB,
662676
Diagnostic Info(this, DB);
663677

664678
// Figure out the diagnostic level of this message.
665-
DiagnosticIDs::Level DiagLevel
666-
= Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
679+
DiagnosticIDs::Level DiagLevel =
680+
Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
667681

668682
Emitted = (DiagLevel != DiagnosticIDs::Ignored);
669683
if (Emitted) {
@@ -715,7 +729,7 @@ Diagnostic::Diagnostic(const DiagnosticsEngine *DO, SourceLocation DiagLoc,
715729
DiagnosticConsumer::~DiagnosticConsumer() = default;
716730

717731
void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
718-
const Diagnostic &Info) {
732+
const Diagnostic &Info) {
719733
if (!IncludeInDiagnosticCounts())
720734
return;
721735

@@ -729,28 +743,33 @@ void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
729743
template <std::size_t StrLen>
730744
static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
731745
const char (&Str)[StrLen]) {
732-
return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0;
746+
return StrLen - 1 == ModifierLen && memcmp(Modifier, Str, StrLen - 1) == 0;
733747
}
734748

735749
/// ScanForward - Scans forward, looking for the given character, skipping
736750
/// nested clauses and escaped characters.
737751
static const char *ScanFormat(const char *I, const char *E, char Target) {
738752
unsigned Depth = 0;
739753

740-
for ( ; I != E; ++I) {
741-
if (Depth == 0 && *I == Target) return I;
742-
if (Depth != 0 && *I == '}') Depth--;
754+
for (; I != E; ++I) {
755+
if (Depth == 0 && *I == Target)
756+
return I;
757+
if (Depth != 0 && *I == '}')
758+
Depth--;
743759

744760
if (*I == '%') {
745761
I++;
746-
if (I == E) break;
762+
if (I == E)
763+
break;
747764

748765
// Escaped characters get implicitly skipped here.
749766

750767
// Format specifier.
751768
if (!isDigit(*I) && !isPunctuation(*I)) {
752-
for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
753-
if (I == E) break;
769+
for (I++; I != E && !isDigit(*I) && *I != '{'; I++)
770+
;
771+
if (I == E)
772+
break;
754773
if (*I == '{')
755774
Depth++;
756775
}
@@ -767,14 +786,15 @@ static const char *ScanFormat(const char *I, const char *E, char Target) {
767786
static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
768787
const char *Argument, unsigned ArgumentLen,
769788
SmallVectorImpl<char> &OutStr) {
770-
const char *ArgumentEnd = Argument+ArgumentLen;
789+
const char *ArgumentEnd = Argument + ArgumentLen;
771790

772791
// Skip over 'ValNo' |'s.
773792
while (ValNo) {
774793
const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
775-
assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
794+
assert(NextVal != ArgumentEnd &&
795+
"Value for integer select modifier was"
776796
" larger than the number of options in the diagnostic string!");
777-
Argument = NextVal+1; // Skip this string.
797+
Argument = NextVal + 1; // Skip this string.
778798
--ValNo;
779799
}
780800

@@ -973,15 +993,13 @@ static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
973993
/// FormatDiagnostic - Format this diagnostic into a string, substituting the
974994
/// formal arguments into the %0 slots. The result is appended onto the Str
975995
/// array.
976-
void Diagnostic::
977-
FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
996+
void Diagnostic::FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
978997
if (StoredDiagMessage.has_value()) {
979998
OutStr.append(StoredDiagMessage->begin(), StoredDiagMessage->end());
980999
return;
9811000
}
9821001

983-
StringRef Diag =
984-
getDiags()->getDiagnosticIDs()->getDescription(getID());
1002+
StringRef Diag = getDiags()->getDiagnosticIDs()->getDescription(getID());
9851003

9861004
FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
9871005
}
@@ -1031,9 +1049,8 @@ void clang::EscapeStringForDiagnostic(StringRef Str,
10311049
}
10321050
}
10331051

1034-
void Diagnostic::
1035-
FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
1036-
SmallVectorImpl<char> &OutStr) const {
1052+
void Diagnostic::FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
1053+
SmallVectorImpl<char> &OutStr) const {
10371054
// When the diagnostic string is only "%0", the entire string is being given
10381055
// by an outside source. Remove unprintable characters from this string
10391056
// and skip all the other string processing.
@@ -1067,7 +1084,7 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
10671084
DiagStr = StrEnd;
10681085
continue;
10691086
} else if (isPunctuation(DiagStr[1])) {
1070-
OutStr.push_back(DiagStr[1]); // %% -> %.
1087+
OutStr.push_back(DiagStr[1]); // %% -> %.
10711088
DiagStr += 2;
10721089
continue;
10731090
}
@@ -1086,10 +1103,9 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
10861103
// Check to see if we have a modifier. If so eat it.
10871104
if (!isDigit(DiagStr[0])) {
10881105
Modifier = DiagStr;
1089-
while (DiagStr[0] == '-' ||
1090-
(DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
1106+
while (DiagStr[0] == '-' || (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
10911107
++DiagStr;
1092-
ModifierLen = DiagStr-Modifier;
1108+
ModifierLen = DiagStr - Modifier;
10931109

10941110
// If we have an argument, get it next.
10951111
if (DiagStr[0] == '{') {
@@ -1098,8 +1114,8 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
10981114

10991115
DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
11001116
assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
1101-
ArgumentLen = DiagStr-Argument;
1102-
++DiagStr; // Skip }.
1117+
ArgumentLen = DiagStr - Argument;
1118+
++DiagStr; // Skip }.
11031119
}
11041120
}
11051121

@@ -1113,7 +1129,7 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
11131129
if (ModifierIs(Modifier, ModifierLen, "diff")) {
11141130
assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
11151131
"Invalid format for diff modifier");
1116-
++DiagStr; // Comma.
1132+
++DiagStr; // Comma.
11171133
ArgNo2 = *DiagStr++ - '0';
11181134
DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
11191135
if (Kind == DiagnosticsEngine::ak_qualtype &&
@@ -1131,8 +1147,8 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
11311147
"Found too many '|'s in a %diff modifier!");
11321148
const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
11331149
const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
1134-
const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
1135-
const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
1150+
const char ArgStr1[] = {'%', static_cast<char>('0' + ArgNo)};
1151+
const char ArgStr2[] = {'%', static_cast<char>('0' + ArgNo2)};
11361152
FormatDiagnostic(Argument, FirstDollar, OutStr);
11371153
FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
11381154
FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
@@ -1256,8 +1272,7 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
12561272
getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
12571273
StringRef(Modifier, ModifierLen),
12581274
StringRef(Argument, ArgumentLen),
1259-
FormattedArgs,
1260-
OutStr, QualTypeVals);
1275+
FormattedArgs, OutStr, QualTypeVals);
12611276
break;
12621277
case DiagnosticsEngine::ak_qualtype_pair: {
12631278
// Create a struct with all the info needed for printing.
@@ -1280,8 +1295,7 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
12801295
getDiags()->ConvertArgToString(Kind, val,
12811296
StringRef(Modifier, ModifierLen),
12821297
StringRef(Argument, ArgumentLen),
1283-
FormattedArgs,
1284-
Tree, QualTypeVals);
1298+
FormattedArgs, Tree, QualTypeVals);
12851299
// If there is no tree information, fall back to regular printing.
12861300
if (!Tree.empty()) {
12871301
FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
@@ -1303,11 +1317,10 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
13031317
getDiags()->ConvertArgToString(Kind, val,
13041318
StringRef(Modifier, ModifierLen),
13051319
StringRef(Argument, ArgumentLen),
1306-
FormattedArgs,
1307-
OutStr, QualTypeVals);
1320+
FormattedArgs, OutStr, QualTypeVals);
13081321
if (!TDT.TemplateDiffUsed)
1309-
FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1310-
TDT.FromType));
1322+
FormattedArgs.push_back(
1323+
std::make_pair(DiagnosticsEngine::ak_qualtype, TDT.FromType));
13111324

13121325
// Append middle text
13131326
FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
@@ -1317,11 +1330,10 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
13171330
getDiags()->ConvertArgToString(Kind, val,
13181331
StringRef(Modifier, ModifierLen),
13191332
StringRef(Argument, ArgumentLen),
1320-
FormattedArgs,
1321-
OutStr, QualTypeVals);
1333+
FormattedArgs, OutStr, QualTypeVals);
13221334
if (!TDT.TemplateDiffUsed)
1323-
FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
1324-
TDT.ToType));
1335+
FormattedArgs.push_back(
1336+
std::make_pair(DiagnosticsEngine::ak_qualtype, TDT.ToType));
13251337

13261338
// Append end text
13271339
FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
@@ -1337,8 +1349,9 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
13371349
else if (Kind != DiagnosticsEngine::ak_std_string)
13381350
FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
13391351
else
1340-
FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
1341-
(intptr_t)getArgStdStr(ArgNo).c_str()));
1352+
FormattedArgs.push_back(
1353+
std::make_pair(DiagnosticsEngine::ak_c_string,
1354+
(intptr_t)getArgStdStr(ArgNo).c_str()));
13421355
}
13431356

13441357
// Append the type tree to the end of the diagnostics.
@@ -1352,8 +1365,9 @@ StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
13521365
StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
13531366
const Diagnostic &Info)
13541367
: ID(Info.getID()), Level(Level) {
1355-
assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1356-
"Valid source location without setting a source manager for diagnostic");
1368+
assert(
1369+
(Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
1370+
"Valid source location without setting a source manager for diagnostic");
13571371
if (Info.getLocation().isValid())
13581372
Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
13591373
SmallString<64> Message;
@@ -1368,9 +1382,8 @@ StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
13681382
ArrayRef<CharSourceRange> Ranges,
13691383
ArrayRef<FixItHint> FixIts)
13701384
: ID(ID), Level(Level), Loc(Loc), Message(Message),
1371-
Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
1372-
{
1373-
}
1385+
Ranges(Ranges.begin(), Ranges.end()),
1386+
FixIts(FixIts.begin(), FixIts.end()) {}
13741387

13751388
llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
13761389
const StoredDiagnostic &SD) {
@@ -1391,8 +1404,7 @@ void IgnoringDiagConsumer::anchor() {}
13911404
ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() = default;
13921405

13931406
void ForwardingDiagnosticConsumer::HandleDiagnostic(
1394-
DiagnosticsEngine::Level DiagLevel,
1395-
const Diagnostic &Info) {
1407+
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
13961408
Target.HandleDiagnostic(DiagLevel, Info);
13971409
}
13981410

0 commit comments

Comments
 (0)