[clang] [NFC][lang][TableGen] Simplify `EmitClangDiagsIndexName` (PR #115962)

Rahul Joshi via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 12 20:29:20 PST 2024


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/115962

>From 07c74bee58d1a56978b1cba00755cc3ca14cb71f Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 12 Nov 2024 15:51:54 -0800
Subject: [PATCH 1/2] [NFC][lang][TableGen] Simplify `EmitClangDiagsIndexName`

Simplify `EmitClangDiagsIndexName` to directly sort records instead of
creating an array of `RecordIndexElement` containing record name and
sorting it.
---
 .../TableGen/ClangDiagnosticsEmitter.cpp      | 26 ++++---------------
 1 file changed, 5 insertions(+), 21 deletions(-)

diff --git a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
index b7fd59090cd995..6d42e927a3d630 100644
--- a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
+++ b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
@@ -1786,33 +1786,17 @@ void clang::EmitClangDiagGroups(const RecordKeeper &Records, raw_ostream &OS) {
 // Diagnostic name index generation
 //===----------------------------------------------------------------------===//
 
-namespace {
-struct RecordIndexElement
-{
-  RecordIndexElement() {}
-  explicit RecordIndexElement(Record const &R)
-      : Name(std::string(R.getName())) {}
-
-  std::string Name;
-};
-} // end anonymous namespace.
-
 void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
                                     raw_ostream &OS) {
-  ArrayRef<const Record *> Diags =
+  std::vector<const Record *> Diags =
       Records.getAllDerivedDefinitions("Diagnostic");
 
-  std::vector<RecordIndexElement> Index;
-  Index.reserve(Diags.size());
-  for (const Record *R : Diags)
-    Index.push_back(RecordIndexElement(*R));
-
-  sort(Index, [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) {
-    return Lhs.Name < Rhs.Name;
+  sort(Diags, [](const Record *Lhs, const Record *Rhs) {
+    return Lhs->getName() < Rhs->getName();
   });
 
-  for (const auto &Elem : Index)
-    OS << "DIAG_NAME_INDEX(" << Elem.Name << ")\n";
+  for (const Record *Elem : Diags)
+    OS << "DIAG_NAME_INDEX(" << Elem->getName() << ")\n";
 }
 
 //===----------------------------------------------------------------------===//

>From b9694971dd20f85a4528a387506419c3fdd1f45b Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 12 Nov 2024 20:29:13 -0800
Subject: [PATCH 2/2] Update clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Co-authored-by: Kazu Hirata <kazu at google.com>
---
 clang/utils/TableGen/ClangDiagnosticsEmitter.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
index 6d42e927a3d630..a8f66c840d2591 100644
--- a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
+++ b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
@@ -1791,8 +1791,8 @@ void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
   std::vector<const Record *> Diags =
       Records.getAllDerivedDefinitions("Diagnostic");
 
-  sort(Diags, [](const Record *Lhs, const Record *Rhs) {
-    return Lhs->getName() < Rhs->getName();
+  sort(Diags, [](const Record *LHS, const Record *RHS) {
+    return LHS->getName() < RHS->getName();
   });
 
   for (const Record *Elem : Diags)



More information about the cfe-commits mailing list