Skip to content

Commit 46b2757

Browse files
authored
[NFC][Clang] Use StringRef and range for loops in SA/Syntax Emitters (#115972)
Use StringRef and range for loops in Clang SACheckers and Syntax emitters.
1 parent 716a095 commit 46b2757

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

clang/utils/TableGen/ClangSACheckersEmitter.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ static std::string getPackageFullName(const Record *R, StringRef Sep = ".");
2828

2929
static std::string getParentPackageFullName(const Record *R,
3030
StringRef Sep = ".") {
31-
std::string name;
3231
if (const DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage")))
33-
name = getPackageFullName(DI->getDef(), Sep);
34-
return name;
32+
return getPackageFullName(DI->getDef(), Sep);
33+
return "";
3534
}
3635

3736
static std::string getPackageFullName(const Record *R, StringRef Sep) {
@@ -52,10 +51,10 @@ static std::string getCheckerFullName(const Record *R, StringRef Sep = ".") {
5251
return name;
5352
}
5453

55-
static std::string getStringValue(const Record &R, StringRef field) {
54+
static StringRef getStringValue(const Record &R, StringRef field) {
5655
if (const StringInit *SI = dyn_cast<StringInit>(R.getValueInit(field)))
57-
return std::string(SI->getValue());
58-
return std::string();
56+
return SI->getValue();
57+
return "";
5958
}
6059

6160
// Calculates the integer value representing the BitsInit object
@@ -93,7 +92,7 @@ static std::string getCheckerDocs(const Record &R) {
9392
/// Retrieves the type from a CmdOptionTypeEnum typed Record object. Note that
9493
/// the class itself has to be modified for adding a new option type in
9594
/// CheckerBase.td.
96-
static std::string getCheckerOptionType(const Record &R) {
95+
static StringRef getCheckerOptionType(const Record &R) {
9796
if (const BitsInit *BI = R.getValueAsBitsInit("Type")) {
9897
switch(getValueFromBitsInit(BI, R)) {
9998
case 0:
@@ -110,7 +109,7 @@ static std::string getCheckerOptionType(const Record &R) {
110109
return "";
111110
}
112111

113-
static std::string getDevelopmentStage(const Record &R) {
112+
static StringRef getDevelopmentStage(const Record &R) {
114113
if (const BitsInit *BI = R.getValueAsBitsInit("DevelopmentStage")) {
115114
switch(getValueFromBitsInit(BI, R)) {
116115
case 0:
@@ -179,8 +178,6 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
179178
ArrayRef<const Record *> packages =
180179
Records.getAllDerivedDefinitions("Package");
181180

182-
using SortedRecords = StringMap<const Record *>;
183-
184181
OS << "// This file is automatically generated. Do not edit this file by "
185182
"hand.\n";
186183

@@ -191,16 +188,13 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
191188
OS << "\n"
192189
"#ifdef GET_PACKAGES\n";
193190
{
194-
SortedRecords sortedPackages;
195-
for (unsigned i = 0, e = packages.size(); i != e; ++i)
196-
sortedPackages[getPackageFullName(packages[i])] = packages[i];
197-
198-
for (SortedRecords::iterator
199-
I = sortedPackages.begin(), E = sortedPackages.end(); I != E; ++I) {
200-
const Record &R = *I->second;
201-
191+
StringMap<const Record *> sortedPackages;
192+
for (const Record *Package : packages)
193+
sortedPackages[getPackageFullName(Package)] = Package;
194+
195+
for (const auto &[_, R] : sortedPackages) {
202196
OS << "PACKAGE(" << "\"";
203-
OS.write_escaped(getPackageFullName(&R)) << '\"';
197+
OS.write_escaped(getPackageFullName(R)) << '\"';
204198
OS << ")\n";
205199
}
206200
}
@@ -225,7 +219,6 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
225219
OS << "\n"
226220
"#ifdef GET_PACKAGE_OPTIONS\n";
227221
for (const Record *Package : packages) {
228-
229222
if (Package->isValueUnset("PackageOptions"))
230223
continue;
231224

@@ -250,9 +243,9 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
250243
OS << "\n"
251244
"#ifdef GET_CHECKERS\n"
252245
"\n";
253-
for (const Record *checker : checkers) {
246+
for (const Record *checker : checkers)
254247
printChecker(OS, *checker);
255-
}
248+
256249
OS << "\n"
257250
"#endif // GET_CHECKERS\n"
258251
"\n";

clang/utils/TableGen/ClangSyntaxEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ struct SyntaxConstraint {
116116
} else if (R.isSubClassOf("AnyToken")) {
117117
NodeType = "Leaf";
118118
} else if (R.isSubClassOf("NodeType")) {
119-
NodeType = R.getName().str();
119+
NodeType = R.getName();
120120
} else {
121121
assert(false && "Unhandled Syntax kind");
122122
}
123123
}
124124

125-
std::string NodeType;
125+
StringRef NodeType;
126126
// optional and leaf types also go here, once we want to use them.
127127
};
128128

0 commit comments

Comments
 (0)