Skip to content

Commit 04b7434

Browse files
committed
[NFC][Clang] Use StringRef instead of string in ClangDiagnosticEmitter
Use StringRef instead of std::string in ClangDiagnosticEmitter.
1 parent 7b5e285 commit 04b7434

File tree

1 file changed

+32
-47
lines changed

1 file changed

+32
-47
lines changed

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 32 additions & 47 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, std::less<>>;
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,16 +159,14 @@ 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

@@ -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

@@ -1178,15 +1175,11 @@ std::string DiagnosticTextBuilder::buildForDefinition(const Record *R) {
11781175
//===----------------------------------------------------------------------===//
11791176

11801177
static bool isError(const Record &Diag) {
1181-
const std::string &ClsName =
1182-
std::string(Diag.getValueAsDef("Class")->getName());
1183-
return ClsName == "CLASS_ERROR";
1178+
return Diag.getValueAsDef("Class")->getName() == "CLASS_ERROR";
11841179
}
11851180

11861181
static bool isRemark(const Record &Diag) {
1187-
const std::string &ClsName =
1188-
std::string(Diag.getValueAsDef("Class")->getName());
1189-
return ClsName == "CLASS_REMARK";
1182+
return Diag.getValueAsDef("Class")->getName() == "CLASS_REMARK";
11901183
}
11911184

11921185
// Presumes the text has been split at the first whitespace or hyphen.
@@ -1414,16 +1407,14 @@ void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS,
14141407
InferPedantic inferPedantic(DGParentMap, Diags, DiagGroups, DiagsInGroup);
14151408
inferPedantic.compute(&DiagsInPedantic, (RecordVec*)nullptr);
14161409

1417-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
1418-
const Record &R = *Diags[i];
1419-
1410+
for (const Record *RP : Diags) {
1411+
const Record &R = *RP;
14201412
// Check if this is an error that is accidentally in a warning
14211413
// group.
14221414
if (isError(R)) {
14231415
if (const auto *Group = dyn_cast<DefInit>(R.getValueInit("Group"))) {
14241416
const Record *GroupRec = Group->getDef();
1425-
const std::string &GroupName =
1426-
std::string(GroupRec->getValueAsString("GroupName"));
1417+
StringRef GroupName = GroupRec->getValueAsString("GroupName");
14271418
PrintFatalError(R.getLoc(), "Error " + R.getName() +
14281419
" cannot be in a warning group [" + GroupName + "]");
14291420
}
@@ -1456,13 +1447,11 @@ void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS,
14561447
// Warning group associated with the diagnostic. This is stored as an index
14571448
// into the alphabetically sorted warning group table.
14581449
if (const auto *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
1459-
std::map<std::string, GroupInfo>::iterator I = DiagsInGroup.find(
1460-
std::string(DI->getDef()->getValueAsString("GroupName")));
1450+
auto I = DiagsInGroup.find(DI->getDef()->getValueAsString("GroupName"));
14611451
assert(I != DiagsInGroup.end());
14621452
OS << ", " << I->second.IDNo;
14631453
} else if (DiagsInPedantic.count(&R)) {
1464-
std::map<std::string, GroupInfo>::iterator I =
1465-
DiagsInGroup.find("pedantic");
1454+
auto I = DiagsInGroup.find("pedantic");
14661455
assert(I != DiagsInGroup.end() && "pedantic group not defined");
14671456
OS << ", " << I->second.IDNo;
14681457
} else {
@@ -1532,19 +1521,18 @@ static void emitDiagSubGroups(DiagsInGroupTy &DiagsInGroup,
15321521
<< " /* Empty */ -1,\n";
15331522
for (auto const &[Name, Group] : DiagsInGroup) {
15341523
const bool IsPedantic = Name == "pedantic";
1535-
const std::vector<std::string> &SubGroups = Group.SubGroups;
1524+
const std::vector<StringRef> &SubGroups = Group.SubGroups;
15361525
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
15371526
OS << " /* DiagSubGroup" << Group.IDNo << " */ ";
1538-
for (auto const &SubGroup : SubGroups) {
1527+
for (StringRef SubGroup : SubGroups) {
15391528
auto RI = DiagsInGroup.find(SubGroup);
15401529
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15411530
OS << RI->second.IDNo << ", ";
15421531
}
15431532
// Emit the groups implicitly in "pedantic".
15441533
if (IsPedantic) {
15451534
for (auto const &Group : GroupsInPedantic) {
1546-
const std::string &GroupName =
1547-
std::string(Group->getValueAsString("GroupName"));
1535+
StringRef GroupName = Group->getValueAsString("GroupName");
15481536
auto RI = DiagsInGroup.find(GroupName);
15491537
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15501538
OS << RI->second.IDNo << ", ";
@@ -1677,7 +1665,7 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
16771665
PrintFatalError("Invalid character in diagnostic group '" + Name + "'");
16781666
OS << Name << " */, ";
16791667
// Store a pascal-style length byte at the beginning of the string.
1680-
std::string PascalName = char(Name.size()) + Name;
1668+
std::string PascalName = char(Name.size()) + Name.str();
16811669
OS << *GroupNames.GetStringOffset(PascalName) << ", ";
16821670

16831671
// Special handling for 'pedantic'.
@@ -1698,7 +1686,7 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
16981686
}
16991687

17001688
// Subgroups.
1701-
const std::vector<std::string> &SubGroups = GroupInfo.SubGroups;
1689+
const std::vector<StringRef> &SubGroups = GroupInfo.SubGroups;
17021690
const bool hasSubGroups =
17031691
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
17041692
if (hasSubGroups) {
@@ -1766,13 +1754,10 @@ void clang::EmitClangDiagGroups(const RecordKeeper &Records, raw_ostream &OS) {
17661754
inferPedantic.compute(&DiagsInPedantic, &GroupsInPedantic);
17671755

17681756
StringToOffsetTable GroupNames;
1769-
for (std::map<std::string, GroupInfo>::const_iterator
1770-
I = DiagsInGroup.begin(),
1771-
E = DiagsInGroup.end();
1772-
I != E; ++I) {
1757+
for (const auto &[Name, Group] : DiagsInGroup) {
17731758
// Store a pascal-style length byte at the beginning of the string.
1774-
std::string Name = char(I->first.size()) + I->first;
1775-
GroupNames.GetOrAddStringOffset(Name, false);
1759+
std::string PascalName = char(Name.size()) + Name.str();
1760+
GroupNames.GetOrAddStringOffset(PascalName, false);
17761761
}
17771762

17781763
emitAllDiagArrays(DiagsInGroup, DiagsInPedantic, GroupsInPedantic, GroupNames,

0 commit comments

Comments
 (0)