[clang] [NFC][Clang] Use StringRef and range for loops in SA/Syntax Emitters (PR #115972)

Rahul Joshi via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 12 17:08:10 PST 2024


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/115972

Use StringRef and range for loops in Clang SACheckers and Syntax emitters.

>From 7e297e5641cead196772b33fc6633efe9837f09d Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 12 Nov 2024 17:06:08 -0800
Subject: [PATCH] [NFC][Clang] Use StringRef and range for loops in SA/Syntax
 Emitters

Use StringRef and range for loops in Clang SACheckers and Syntax
emitters.
---
 .../utils/TableGen/ClangSACheckersEmitter.cpp | 37 ++++++++-----------
 clang/utils/TableGen/ClangSyntaxEmitter.cpp   |  4 +-
 2 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/clang/utils/TableGen/ClangSACheckersEmitter.cpp b/clang/utils/TableGen/ClangSACheckersEmitter.cpp
index 36012dbf70791b..097cbf3edac041 100644
--- a/clang/utils/TableGen/ClangSACheckersEmitter.cpp
+++ b/clang/utils/TableGen/ClangSACheckersEmitter.cpp
@@ -28,10 +28,9 @@ static std::string getPackageFullName(const Record *R, StringRef Sep = ".");
 
 static std::string getParentPackageFullName(const Record *R,
                                             StringRef Sep = ".") {
-  std::string name;
   if (const DefInit *DI = dyn_cast<DefInit>(R->getValueInit("ParentPackage")))
-    name = getPackageFullName(DI->getDef(), Sep);
-  return name;
+    return getPackageFullName(DI->getDef(), Sep);
+  return "";
 }
 
 static std::string getPackageFullName(const Record *R, StringRef Sep) {
@@ -52,10 +51,10 @@ static std::string getCheckerFullName(const Record *R, StringRef Sep = ".") {
   return name;
 }
 
-static std::string getStringValue(const Record &R, StringRef field) {
+static StringRef getStringValue(const Record &R, StringRef field) {
   if (const StringInit *SI = dyn_cast<StringInit>(R.getValueInit(field)))
-    return std::string(SI->getValue());
-  return std::string();
+    return SI->getValue();
+  return "";
 }
 
 // Calculates the integer value representing the BitsInit object
@@ -93,7 +92,7 @@ static std::string getCheckerDocs(const Record &R) {
 /// Retrieves the type from a CmdOptionTypeEnum typed Record object. Note that
 /// the class itself has to be modified for adding a new option type in
 /// CheckerBase.td.
-static std::string getCheckerOptionType(const Record &R) {
+static StringRef getCheckerOptionType(const Record &R) {
   if (const BitsInit *BI = R.getValueAsBitsInit("Type")) {
     switch(getValueFromBitsInit(BI, R)) {
     case 0:
@@ -110,7 +109,7 @@ static std::string getCheckerOptionType(const Record &R) {
   return "";
 }
 
-static std::string getDevelopmentStage(const Record &R) {
+static StringRef getDevelopmentStage(const Record &R) {
   if (const BitsInit *BI = R.getValueAsBitsInit("DevelopmentStage")) {
     switch(getValueFromBitsInit(BI, R)) {
     case 0:
@@ -179,8 +178,6 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
   ArrayRef<const Record *> packages =
       Records.getAllDerivedDefinitions("Package");
 
-  using SortedRecords = StringMap<const Record *>;
-
   OS << "// This file is automatically generated. Do not edit this file by "
         "hand.\n";
 
@@ -191,16 +188,13 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
   OS << "\n"
         "#ifdef GET_PACKAGES\n";
   {
-    SortedRecords sortedPackages;
-    for (unsigned i = 0, e = packages.size(); i != e; ++i)
-      sortedPackages[getPackageFullName(packages[i])] = packages[i];
-  
-    for (SortedRecords::iterator
-           I = sortedPackages.begin(), E = sortedPackages.end(); I != E; ++I) {
-      const Record &R = *I->second;
-  
+    StringMap<const Record *> sortedPackages;
+    for (const Record *Package : packages)
+      sortedPackages[getPackageFullName(Package)] = Package;
+
+    for (const auto &[_, R] : sortedPackages) {
       OS << "PACKAGE(" << "\"";
-      OS.write_escaped(getPackageFullName(&R)) << '\"';
+      OS.write_escaped(getPackageFullName(R)) << '\"';
       OS << ")\n";
     }
   }
@@ -225,7 +219,6 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
   OS << "\n"
         "#ifdef GET_PACKAGE_OPTIONS\n";
   for (const Record *Package : packages) {
-
     if (Package->isValueUnset("PackageOptions"))
       continue;
 
@@ -250,9 +243,9 @@ void clang::EmitClangSACheckers(const RecordKeeper &Records, raw_ostream &OS) {
   OS << "\n"
         "#ifdef GET_CHECKERS\n"
         "\n";
-  for (const Record *checker : checkers) {
+  for (const Record *checker : checkers)
     printChecker(OS, *checker);
-  }
+
   OS << "\n"
         "#endif // GET_CHECKERS\n"
         "\n";
diff --git a/clang/utils/TableGen/ClangSyntaxEmitter.cpp b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
index 4098a5e88e6820..6800ad300acd3c 100644
--- a/clang/utils/TableGen/ClangSyntaxEmitter.cpp
+++ b/clang/utils/TableGen/ClangSyntaxEmitter.cpp
@@ -116,13 +116,13 @@ struct SyntaxConstraint {
     } else if (R.isSubClassOf("AnyToken")) {
       NodeType = "Leaf";
     } else if (R.isSubClassOf("NodeType")) {
-      NodeType = R.getName().str();
+      NodeType = R.getName();
     } else {
       assert(false && "Unhandled Syntax kind");
     }
   }
 
-  std::string NodeType;
+  StringRef NodeType;
   // optional and leaf types also go here, once we want to use them.
 };
 



More information about the cfe-commits mailing list