Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Nov 13, 2024

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

Use StringRef and range for loops in Clang SACheckers and Syntax
emitters.
@jurahul jurahul marked this pull request as ready for review November 13, 2024 04:28
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Nov 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 13, 2024

@llvm/pr-subscribers-clang

Author: Rahul Joshi (jurahul)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/115972.diff

2 Files Affected:

  • (modified) clang/utils/TableGen/ClangSACheckersEmitter.cpp (+15-22)
  • (modified) clang/utils/TableGen/ClangSyntaxEmitter.cpp (+2-2)
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.
 };
 

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@jurahul jurahul merged commit 46b2757 into llvm:main Nov 13, 2024
12 checks passed
@jurahul jurahul deleted the clang_sa_syntax_emitter_strref_range_loops branch November 13, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants