Skip to content

Commit 17c6ec6

Browse files
authored
[NFC][Clang] Use StringRef instead of string in ClangDiagnosticEmitter (#115959)
Use StringRef instead of std::string in ClangDiagnosticEmitter.
1 parent 6684eb4 commit 17c6ec6

File tree

1 file changed

+35
-52
lines changed

1 file changed

+35
-52
lines changed

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class DiagGroupParentMap {
5555
};
5656
} // end anonymous namespace.
5757

58-
static std::string
58+
static StringRef
5959
getCategoryFromDiagGroup(const Record *Group,
6060
DiagGroupParentMap &DiagGroupParents) {
6161
// If the DiagGroup has a category, return it.
62-
std::string CatName = std::string(Group->getValueAsString("CategoryName"));
62+
StringRef CatName = Group->getValueAsString("CategoryName");
6363
if (!CatName.empty()) return CatName;
6464

6565
// The diag group may the subgroup of one or more other diagnostic groups,
@@ -73,25 +73,26 @@ getCategoryFromDiagGroup(const Record *Group,
7373

7474
/// getDiagnosticCategory - Return the category that the specified diagnostic
7575
/// lives in.
76-
static std::string getDiagnosticCategory(const Record *R,
77-
DiagGroupParentMap &DiagGroupParents) {
76+
static StringRef getDiagnosticCategory(const Record *R,
77+
DiagGroupParentMap &DiagGroupParents) {
7878
// If the diagnostic is in a group, and that group has a category, use it.
7979
if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
8080
// Check the diagnostic's diag group for a category.
81-
std::string CatName = getCategoryFromDiagGroup(Group->getDef(),
82-
DiagGroupParents);
81+
StringRef CatName =
82+
getCategoryFromDiagGroup(Group->getDef(), DiagGroupParents);
8383
if (!CatName.empty()) return CatName;
8484
}
8585

8686
// If the diagnostic itself has a category, get it.
87-
return std::string(R->getValueAsString("CategoryName"));
87+
return R->getValueAsString("CategoryName");
8888
}
8989

9090
namespace {
9191
class DiagCategoryIDMap {
9292
const RecordKeeper &Records;
9393
StringMap<unsigned> CategoryIDs;
94-
std::vector<std::string> CategoryStrings;
94+
std::vector<StringRef> CategoryStrings;
95+
9596
public:
9697
DiagCategoryIDMap(const RecordKeeper &records) : Records(records) {
9798
DiagGroupParentMap ParentInfo(Records);
@@ -102,7 +103,7 @@ namespace {
102103

103104
for (const Record *Diag :
104105
Records.getAllDerivedDefinitions("Diagnostic")) {
105-
std::string Category = getDiagnosticCategory(Diag, ParentInfo);
106+
StringRef Category = getDiagnosticCategory(Diag, ParentInfo);
106107
if (Category.empty()) continue; // Skip diags with no category.
107108

108109
unsigned &ID = CategoryIDs[Category];
@@ -117,15 +118,15 @@ namespace {
117118
return CategoryIDs[CategoryString];
118119
}
119120

120-
typedef std::vector<std::string>::const_iterator const_iterator;
121+
typedef std::vector<StringRef>::const_iterator const_iterator;
121122
const_iterator begin() const { return CategoryStrings.begin(); }
122123
const_iterator end() const { return CategoryStrings.end(); }
123124
};
124125

125126
struct GroupInfo {
126127
StringRef GroupName;
127128
std::vector<const Record*> DiagsInGroup;
128-
std::vector<std::string> SubGroups;
129+
std::vector<StringRef> SubGroups;
129130
unsigned IDNo = 0;
130131

131132
SmallVector<const Record *, 1> Defs;
@@ -145,7 +146,7 @@ static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) {
145146
RHS->getValueAsString("GroupName");
146147
}
147148

148-
using DiagsInGroupTy = std::map<std::string, GroupInfo, std::less<>>;
149+
using DiagsInGroupTy = std::map<StringRef, GroupInfo>;
149150

150151
/// Invert the 1-[0/1] mapping of diags to group into a one to many
151152
/// mapping of groups to diags in the group.
@@ -158,21 +159,19 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
158159
continue;
159160
assert(R->getValueAsDef("Class")->getName() != "CLASS_NOTE" &&
160161
"Note can't be in a DiagGroup");
161-
std::string GroupName =
162-
std::string(DI->getDef()->getValueAsString("GroupName"));
162+
StringRef GroupName = DI->getDef()->getValueAsString("GroupName");
163163
DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
164164
}
165165

166166
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
167167
// groups (these are warnings that GCC supports that clang never produces).
168168
for (const Record *Group : DiagGroups) {
169-
GroupInfo &GI =
170-
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
169+
GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
171170
GI.GroupName = Group->getName();
172171
GI.Defs.push_back(Group);
173172

174173
for (const Record *SubGroup : Group->getValueAsListOfDefs("SubGroups"))
175-
GI.SubGroups.push_back(SubGroup->getValueAsString("GroupName").str());
174+
GI.SubGroups.push_back(SubGroup->getValueAsString("GroupName"));
176175
}
177176

178177
// Assign unique ID numbers to the groups.
@@ -281,8 +280,7 @@ class InferPedantic {
281280
} // end anonymous namespace
282281

283282
bool InferPedantic::isSubGroupOfGroup(const Record *Group, StringRef GName) {
284-
const std::string &GroupName =
285-
std::string(Group->getValueAsString("GroupName"));
283+
StringRef GroupName = Group->getValueAsString("GroupName");
286284
if (GName == GroupName)
287285
return true;
288286

@@ -307,8 +305,7 @@ bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
307305
GMap::mapped_type &V = GroupCount[Group];
308306
// Lazily compute the threshold value for the group count.
309307
if (!V.second) {
310-
const GroupInfo &GI =
311-
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
308+
const GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
312309
V.second = GI.SubGroups.size() + GI.DiagsInGroup.size();
313310
}
314311

@@ -1183,15 +1180,11 @@ std::string DiagnosticTextBuilder::buildForDefinition(const Record *R) {
11831180
//===----------------------------------------------------------------------===//
11841181

11851182
static bool isError(const Record &Diag) {
1186-
const std::string &ClsName =
1187-
std::string(Diag.getValueAsDef("Class")->getName());
1188-
return ClsName == "CLASS_ERROR";
1183+
return Diag.getValueAsDef("Class")->getName() == "CLASS_ERROR";
11891184
}
11901185

11911186
static bool isRemark(const Record &Diag) {
1192-
const std::string &ClsName =
1193-
std::string(Diag.getValueAsDef("Class")->getName());
1194-
return ClsName == "CLASS_REMARK";
1187+
return Diag.getValueAsDef("Class")->getName() == "CLASS_REMARK";
11951188
}
11961189

11971190
// Presumes the text has been split at the first whitespace or hyphen.
@@ -1419,16 +1412,13 @@ void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS,
14191412
InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
14201413
inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr);
14211414

1422-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
1423-
const Record &R = *Diags[i];
1424-
1415+
for (const Record &R : make_pointee_range(Diags)) {
14251416
// Check if this is an error that is accidentally in a warning
14261417
// group.
14271418
if (isError(R)) {
14281419
if (const auto *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
14291420
const Record *GroupRec = Group->getDef();
1430-
const std::string &GroupName =
1431-
std::string(GroupRec->getValueAsString("GroupName"));
1421+
StringRef GroupName = GroupRec->getValueAsString("GroupName");
14321422
PrintFatalError(R.getLoc(), "Error " + R.getName() +
14331423
" cannot be in a warning group [" + GroupName + "]");
14341424
}
@@ -1461,13 +1451,11 @@ void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS,
14611451
// Warning group associated with the diagnostic. This is stored as an index
14621452
// into the alphabetically sorted warning group table.
14631453
if (const auto *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
1464-
std::map<std::string, GroupInfo>::iterator I = DiagsInGroup.find(
1465-
std::string(DI->getDef()->getValueAsString("GroupName")));
1454+
auto I = DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
14661455
assert(I != DiagsInGroup.end());
14671456
OS << ", " << I->second.IDNo;
14681457
} else if (DiagsInPedantic.count(&R)) {
1469-
std::map<std::string, GroupInfo>::iterator I =
1470-
DiagsInGroup.find("pedantic");
1458+
auto I = DiagsInGroup.find("pedantic");
14711459
assert(I != DiagsInGroup.end() && "pedantic group not defined");
14721460
OS << ", " << I->second.IDNo;
14731461
} else {
@@ -1537,19 +1525,18 @@ static void emitDiagSubGroups(DiagsInGroupTy &DiagsInGroup,
15371525
<< " /* Empty */ -1,\n";
15381526
for (auto const &[Name, Group] : DiagsInGroup) {
15391527
const bool IsPedantic = Name == "pedantic";
1540-
const std::vector<std::string> &SubGroups = Group.SubGroups;
1528+
const std::vector<StringRef> &SubGroups = Group.SubGroups;
15411529
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
15421530
OS << " /* DiagSubGroup" << Group.IDNo << " */ ";
1543-
for (auto const &SubGroup : SubGroups) {
1531+
for (StringRef SubGroup : SubGroups) {
15441532
auto RI = DiagsInGroup.find(SubGroup);
15451533
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15461534
OS << RI->second.IDNo << ", ";
15471535
}
15481536
// Emit the groups implicitly in "pedantic".
15491537
if (IsPedantic) {
15501538
for (auto const &Group : GroupsInPedantic) {
1551-
const std::string &GroupName =
1552-
std::string(Group->getValueAsString("GroupName"));
1539+
StringRef GroupName = Group->getValueAsString("GroupName");
15531540
auto RI = DiagsInGroup.find(GroupName);
15541541
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15551542
OS << RI->second.IDNo << ", ";
@@ -1682,7 +1669,7 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
16821669
PrintFatalError("Invalid character in diagnostic group '" + Name + "'");
16831670
OS << Name << " */, ";
16841671
// Store a pascal-style length byte at the beginning of the string.
1685-
std::string PascalName = char(Name.size()) + Name;
1672+
std::string PascalName = char(Name.size()) + Name.str();
16861673
OS << *GroupNames.GetStringOffset(PascalName) << ", ";
16871674

16881675
// Special handling for 'pedantic'.
@@ -1703,7 +1690,7 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
17031690
}
17041691

17051692
// Subgroups.
1706-
const std::vector<std::string> &SubGroups = GroupInfo.SubGroups;
1693+
const std::vector<StringRef> &SubGroups = GroupInfo.SubGroups;
17071694
const bool hasSubGroups =
17081695
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
17091696
if (hasSubGroups) {
@@ -1771,13 +1758,10 @@ void clang::EmitClangDiagGroups(const RecordKeeper &Records, raw_ostream &OS) {
17711758
inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
17721759

17731760
StringToOffsetTable GroupNames;
1774-
for (std::map<std::string, GroupInfo>::const_iterator
1775-
I = DiagsInGroup.begin(),
1776-
E = DiagsInGroup.end();
1777-
I != E; ++I) {
1761+
for (const auto &[Name, Group] : DiagsInGroup) {
17781762
// Store a pascal-style length byte at the beginning of the string.
1779-
std::string Name = char(I->first.size()) + I->first;
1780-
GroupNames.GetOrAddStringOffset(Name, false);
1763+
std::string PascalName = char(Name.size()) + Name.str();
1764+
GroupNames.GetOrAddStringOffset(PascalName, false);
17811765
}
17821766

17831767
emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,
@@ -1819,7 +1803,7 @@ bool isRemarkGroup(const Record *DiagGroup,
18191803
auto &GroupInfo = DiagsInGroup.find(GroupName)->second;
18201804
for (const Record *Diag : GroupInfo.DiagsInGroup)
18211805
(isRemark(*Diag) ? AnyRemarks : AnyNonRemarks) = true;
1822-
for (const auto &Name : GroupInfo.SubGroups)
1806+
for (StringRef Name : GroupInfo.SubGroups)
18231807
Visit(Name);
18241808
};
18251809
Visit(DiagGroup->getValueAsString("GroupName"));
@@ -1916,8 +1900,7 @@ void clang::EmitClangDiagDocs(const RecordKeeper &Records, raw_ostream &OS) {
19161900
DiagsInPedantic.begin(),
19171901
DiagsInPedantic.end());
19181902
for (auto *Group : GroupsInPedantic)
1919-
PedDiags.SubGroups.push_back(
1920-
std::string(Group->getValueAsString("GroupName")));
1903+
PedDiags.SubGroups.push_back(Group->getValueAsString("GroupName"));
19211904
}
19221905

19231906
// FIXME: Write diagnostic categories and link to diagnostic groups in each.
@@ -1963,7 +1946,7 @@ void clang::EmitClangDiagDocs(const RecordKeeper &Records, raw_ostream &OS) {
19631946

19641947
bool First = true;
19651948
sort(GroupInfo.SubGroups);
1966-
for (const auto &Name : GroupInfo.SubGroups) {
1949+
for (StringRef Name : GroupInfo.SubGroups) {
19671950
if (!First) OS << ", ";
19681951
OS << "`" << (IsRemarkGroup ? "-R" : "-W") << Name << "`_";
19691952
First = false;

0 commit comments

Comments
 (0)