[llvm] f7479b5 - [NFC][TableGen] Simplify DirectiveEmitter using range for loops (#107909)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 9 18:24:10 PDT 2024


Author: Rahul Joshi
Date: 2024-09-09T18:24:07-07:00
New Revision: f7479b5ff43261a20258743da5fa583a0c729564

URL: https://github.com/llvm/llvm-project/commit/f7479b5ff43261a20258743da5fa583a0c729564
DIFF: https://github.com/llvm/llvm-project/commit/f7479b5ff43261a20258743da5fa583a0c729564.diff

LOG: [NFC][TableGen] Simplify DirectiveEmitter using range for loops (#107909)

Make constructors that take const Record * implicit, allowing us to
simplify some range based loops to use that class instance as the loop
variable.

Change remaining constructor calls to use () instead of {} to construct
objects.

Added: 
    

Modified: 
    llvm/include/llvm/TableGen/DirectiveEmitter.h
    llvm/utils/TableGen/DirectiveEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/TableGen/DirectiveEmitter.h b/llvm/include/llvm/TableGen/DirectiveEmitter.h
index ca4ba78330d0a3..4ab0e889078ffd 100644
--- a/llvm/include/llvm/TableGen/DirectiveEmitter.h
+++ b/llvm/include/llvm/TableGen/DirectiveEmitter.h
@@ -90,11 +90,20 @@ class DirectiveLanguage {
   }
 };
 
+// Note: In all the classes below, allow implicit construction from Record *,
+// to allow writing code like:
+//  for (const Directive D : getDirectives()) {
+//
+//  instead of:
+//
+//  for (const Record *R : getDirectives()) {
+//    Directive D(R);
+
 // Base record class used for Directive and Clause class defined in
 // DirectiveBase.td.
 class BaseRecord {
 public:
-  explicit BaseRecord(const Record *Def) : Def(Def) {}
+  BaseRecord(const Record *Def) : Def(Def) {}
 
   StringRef getName() const { return Def->getValueAsString("name"); }
 
@@ -104,7 +113,7 @@ class BaseRecord {
 
   // Returns the name of the directive formatted for output. Whitespace are
   // replaced with underscores.
-  std::string getFormattedName() {
+  std::string getFormattedName() const {
     StringRef Name = Def->getValueAsString("name");
     std::string N = Name.str();
     std::replace(N.begin(), N.end(), ' ', '_');
@@ -124,7 +133,7 @@ class BaseRecord {
 // DirectiveBase.td and provides helper methods for accessing it.
 class Directive : public BaseRecord {
 public:
-  explicit Directive(const Record *Def) : BaseRecord(Def) {}
+  Directive(const Record *Def) : BaseRecord(Def) {}
 
   std::vector<Record *> getAllowedClauses() const {
     return Def->getValueAsListOfDefs("allowedClauses");
@@ -155,7 +164,7 @@ class Directive : public BaseRecord {
 // and provides helper methods for accessing it.
 class Clause : public BaseRecord {
 public:
-  explicit Clause(const Record *Def) : BaseRecord(Def) {}
+  Clause(const Record *Def) : BaseRecord(Def) {}
 
   // Optional field.
   StringRef getClangClass() const {
@@ -172,7 +181,7 @@ class Clause : public BaseRecord {
   // captitalized and the underscores are removed.
   // ex: async -> Async
   //     num_threads -> NumThreads
-  std::string getFormattedParserClassName() {
+  std::string getFormattedParserClassName() const {
     StringRef Name = Def->getValueAsString("name");
     std::string N = Name.str();
     bool Cap = true;
@@ -223,10 +232,10 @@ class Clause : public BaseRecord {
 // DirectiveBase.td and provides helper methods for accessing it.
 class VersionedClause {
 public:
-  explicit VersionedClause(const Record *Def) : Def(Def) {}
+  VersionedClause(const Record *Def) : Def(Def) {}
 
   // Return the specific clause record wrapped in the Clause class.
-  Clause getClause() const { return Clause{Def->getValueAsDef("clause")}; }
+  Clause getClause() const { return Clause(Def->getValueAsDef("clause")); }
 
   int64_t getMinVersion() const { return Def->getValueAsInt("minVersion"); }
 

diff  --git a/llvm/utils/TableGen/DirectiveEmitter.cpp b/llvm/utils/TableGen/DirectiveEmitter.cpp
index a179d52eee5b2f..aaad0fc861529f 100644
--- a/llvm/utils/TableGen/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/DirectiveEmitter.cpp
@@ -53,7 +53,7 @@ static void GenerateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
   OS << "\n";
   OS << "enum class " << Enum << " {\n";
   for (const auto &R : Records) {
-    BaseRecord Rec{R};
+    BaseRecord Rec(R);
     OS << "  " << Prefix << Rec.getFormattedName() << ",\n";
   }
   OS << "};\n";
@@ -69,7 +69,7 @@ static void GenerateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
   if (ExportEnums) {
     OS << "\n";
     for (const auto &R : Records) {
-      BaseRecord Rec{R};
+      BaseRecord Rec(R);
       OS << "constexpr auto " << Prefix << Rec.getFormattedName() << " = "
          << "llvm::" << DirLang.getCppNamespace() << "::" << Enum
          << "::" << Prefix << Rec.getFormattedName() << ";\n";
@@ -84,7 +84,7 @@ static void GenerateEnumClauseVal(ArrayRef<const Record *> Records,
                                   const DirectiveLanguage &DirLang,
                                   std::string &EnumHelperFuncs) {
   for (const auto &R : Records) {
-    Clause C{R};
+    Clause C(R);
     const auto &ClauseVals = C.getClauseVals();
     if (ClauseVals.size() <= 0)
       continue;
@@ -99,7 +99,7 @@ static void GenerateEnumClauseVal(ArrayRef<const Record *> Records,
     OS << "\n";
     OS << "enum class " << EnumName << " {\n";
     for (const auto &CV : ClauseVals) {
-      ClauseVal CVal{CV};
+      ClauseVal CVal(CV);
       OS << "  " << CV->getName() << "=" << CVal.getValue() << ",\n";
     }
     OS << "};\n";
@@ -127,8 +127,7 @@ static bool HasDuplicateClauses(ArrayRef<const Record *> Clauses,
                                 const Directive &Directive,
                                 StringSet<> &CrtClauses) {
   bool HasError = false;
-  for (const Record *C : Clauses) {
-    VersionedClause VerClause{C};
+  for (const VersionedClause VerClause : Clauses) {
     const auto insRes = CrtClauses.insert(VerClause.getClause().getName());
     if (!insRes.second) {
       PrintError("Clause " + VerClause.getClause().getRecordName() +
@@ -145,8 +144,7 @@ static bool HasDuplicateClauses(ArrayRef<const Record *> Clauses,
 static bool
 HasDuplicateClausesInDirectives(ArrayRef<const Record *> Directives) {
   bool HasDuplicate = false;
-  for (const auto &D : Directives) {
-    Directive Dir{D};
+  for (const Directive Dir : Directives) {
     StringSet<> Clauses;
     // Check for duplicates in the three allowed lists.
     if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) ||
@@ -183,17 +181,15 @@ bool DirectiveLanguage::HasValidityErrors() const {
 // Count the maximum number of leaf constituents per construct.
 static size_t GetMaxLeafCount(const DirectiveLanguage &DirLang) {
   size_t MaxCount = 0;
-  for (const Record *R : DirLang.getDirectives()) {
-    size_t Count = Directive{R}.getLeafConstructs().size();
-    MaxCount = std::max(MaxCount, Count);
-  }
+  for (const Directive D : DirLang.getDirectives())
+    MaxCount = std::max(MaxCount, D.getLeafConstructs().size());
   return MaxCount;
 }
 
 // Generate the declaration section for the enumeration in the directive
 // language.
 static void EmitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
-  const auto DirLang = DirectiveLanguage{Records};
+  const auto DirLang = DirectiveLanguage(Records);
   if (DirLang.HasValidityErrors())
     return;
 
@@ -288,8 +284,7 @@ static void GenerateGetName(ArrayRef<const Record *> Records, raw_ostream &OS,
   OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
      << DirLang.getName() << Enum << "Name(" << Enum << " Kind) {\n";
   OS << "  switch (Kind) {\n";
-  for (const auto &R : Records) {
-    BaseRecord Rec{R};
+  for (const BaseRecord Rec : Records) {
     OS << "    case " << Prefix << Rec.getFormattedName() << ":\n";
     OS << "      return \"";
     if (Rec.getAlternativeName().empty())
@@ -317,7 +312,7 @@ static void GenerateGetKind(ArrayRef<const Record *> Records, raw_ostream &OS,
     return;
   }
 
-  BaseRecord DefaultRec{*DefaultIt};
+  BaseRecord DefaultRec(*DefaultIt);
 
   OS << "\n";
   OS << Enum << " llvm::" << DirLang.getCppNamespace() << "::get"
@@ -325,7 +320,7 @@ static void GenerateGetKind(ArrayRef<const Record *> Records, raw_ostream &OS,
   OS << "  return llvm::StringSwitch<" << Enum << ">(Str)\n";
 
   for (const auto &R : Records) {
-    BaseRecord Rec{R};
+    BaseRecord Rec(R);
     if (ImplicitAsUnknown && R->getValueAsBit("isImplicit")) {
       OS << "    .Case(\"" << Rec.getName() << "\"," << Prefix
          << DefaultRec.getFormattedName() << ")\n";
@@ -341,8 +336,7 @@ static void GenerateGetKind(ArrayRef<const Record *> Records, raw_ostream &OS,
 // Generate function implementation for get<ClauseVal>Kind(StringRef Str)
 static void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang,
                                      raw_ostream &OS) {
-  for (const Record *R : DirLang.getClauses()) {
-    Clause C{R};
+  for (const Clause C : DirLang.getClauses()) {
     const auto &ClauseVals = C.getClauseVals();
     if (ClauseVals.size() <= 0)
       continue;
@@ -369,7 +363,7 @@ static void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang,
        << EnumName << "(llvm::StringRef Str) {\n";
     OS << "  return llvm::StringSwitch<" << EnumName << ">(Str)\n";
     for (const auto &CV : ClauseVals) {
-      ClauseVal CVal{CV};
+      ClauseVal CVal(CV);
       OS << "    .Case(\"" << CVal.getFormattedName() << "\"," << CV->getName()
          << ")\n";
     }
@@ -383,7 +377,7 @@ static void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang,
        << " x) {\n";
     OS << "  switch (x) {\n";
     for (const auto &CV : ClauseVals) {
-      ClauseVal CVal{CV};
+      ClauseVal CVal(CV);
       OS << "    case " << CV->getName() << ":\n";
       OS << "      return \"" << CVal.getFormattedName() << "\";\n";
     }
@@ -399,9 +393,7 @@ static void GenerateCaseForVersionedClauses(ArrayRef<const Record *> Clauses,
                                             StringRef DirectiveName,
                                             const DirectiveLanguage &DirLang,
                                             StringSet<> &Cases) {
-  for (const Record *C : Clauses) {
-    VersionedClause VerClause{C};
-
+  for (const VersionedClause VerClause : Clauses) {
     const auto ClauseFormattedName = VerClause.getClause().getFormattedName();
 
     if (Cases.insert(ClauseFormattedName).second) {
@@ -415,7 +407,7 @@ static void GenerateCaseForVersionedClauses(ArrayRef<const Record *> Clauses,
 
 static std::string GetDirectiveName(const DirectiveLanguage &DirLang,
                                     const Record *Rec) {
-  Directive Dir{Rec};
+  Directive Dir(Rec);
   return (Twine("llvm::") + DirLang.getCppNamespace() +
           "::" + DirLang.getDirectivePrefix() + Dir.getFormattedName())
       .str();
@@ -439,9 +431,7 @@ static void GenerateIsAllowedClause(const DirectiveLanguage &DirLang,
 
   OS << "  switch (D) {\n";
 
-  for (const Record *D : DirLang.getDirectives()) {
-    Directive Dir{D};
-
+  for (const Directive Dir : DirLang.getDirectives()) {
     OS << "    case " << DirLang.getDirectivePrefix() << Dir.getFormattedName()
        << ":\n";
     if (Dir.getAllowedClauses().empty() &&
@@ -512,7 +502,7 @@ static void EmitLeafTable(const DirectiveLanguage &DirLang, raw_ostream &OS,
   // The initial leaf table, rows order is same as directive order.
   std::vector<LeafList> LeafTable(Directives.size());
   for (auto [Idx, Rec] : enumerate(Directives)) {
-    Directive Dir{Rec};
+    Directive Dir(Rec);
     std::vector<Record *> Leaves = Dir.getLeafConstructs();
 
     auto &List = LeafTable[Idx];
@@ -540,7 +530,7 @@ static void EmitLeafTable(const DirectiveLanguage &DirLang, raw_ostream &OS,
   // (such as "end declare target").
   DenseSet<int> EndDirectives;
   for (auto [Rec, Id] : DirId) {
-    if (Directive{Rec}.getName().starts_with_insensitive("end "))
+    if (Directive(Rec).getName().starts_with_insensitive("end "))
       EndDirectives.insert(Id);
   }
 
@@ -676,7 +666,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
     if (auto F = AsMap.find(R); F != AsMap.end())
       return F->second;
 
-    Directive D{R};
+    Directive D(R);
     Association AS = getAssocValue(D.getAssociation()->getName());
     if (AS == Association::Invalid) {
       PrintFatalError(errorPrefixFor(D) +
@@ -735,7 +725,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
   OS << "  switch (Dir) {\n";
   for (const Record *R : DirLang.getDirectives()) {
     if (auto F = AsMap.find(R); F != AsMap.end()) {
-      Directive Dir{R};
+      Directive Dir(R);
       OS << "  case " << getQualifiedName(Dir.getFormattedName()) << ":\n";
       OS << "    return " << AssociationTypeName
          << "::" << getAssocName(F->second) << ";\n";
@@ -758,7 +748,7 @@ static void GenerateGetDirectiveCategory(const DirectiveLanguage &DirLang,
   OS << "  switch (Dir) {\n";
 
   for (const Record *R : DirLang.getDirectives()) {
-    Directive D{R};
+    Directive D(R);
     OS << "  case " << GetDirectiveName(DirLang, R) << ":\n";
     OS << "    return " << CategoryNamespace
        << D.getCategory()->getValueAsString("name") << ";\n";
@@ -770,7 +760,7 @@ static void GenerateGetDirectiveCategory(const DirectiveLanguage &DirLang,
 
 // Generate a simple enum set with the give clauses.
 static void GenerateClauseSet(ArrayRef<const Record *> Clauses, raw_ostream &OS,
-                              StringRef ClauseSetPrefix, Directive &Dir,
+                              StringRef ClauseSetPrefix, const Directive &Dir,
                               const DirectiveLanguage &DirLang) {
 
   OS << "\n";
@@ -778,7 +768,7 @@ static void GenerateClauseSet(ArrayRef<const Record *> Clauses, raw_ostream &OS,
      << DirLang.getDirectivePrefix() << Dir.getFormattedName() << " {\n";
 
   for (const auto &C : Clauses) {
-    VersionedClause VerClause{C};
+    VersionedClause VerClause(C);
     OS << "    llvm::" << DirLang.getCppNamespace()
        << "::Clause::" << DirLang.getClausePrefix()
        << VerClause.getClause().getFormattedName() << ",\n";
@@ -801,9 +791,7 @@ static void GenerateDirectiveClauseSets(const DirectiveLanguage &DirLang,
   for (auto Ns : Namespaces)
     OS << "namespace " << Ns << " {\n";
 
-  for (const Record *D : DirLang.getDirectives()) {
-    Directive Dir{D};
-
+  for (const Directive Dir : DirLang.getDirectives()) {
     OS << "\n";
     OS << "  // Sets for " << Dir.getName() << "\n";
 
@@ -835,8 +823,7 @@ static void GenerateDirectiveClauseMap(const DirectiveLanguage &DirLang,
   OS << "\n";
   OS << "{\n";
 
-  for (const Record *D : DirLang.getDirectives()) {
-    Directive Dir{D};
+  for (const Directive Dir : DirLang.getDirectives()) {
     OS << "  {llvm::" << DirLang.getCppNamespace()
        << "::Directive::" << DirLang.getDirectivePrefix()
        << Dir.getFormattedName() << ",\n";
@@ -869,8 +856,7 @@ static void GenerateFlangClauseParserClass(const DirectiveLanguage &DirLang,
 
   OS << "\n";
 
-  for (const Record *C : DirLang.getClauses()) {
-    Clause Clause{C};
+  for (const Clause Clause : DirLang.getClauses()) {
     if (!Clause.getFlangClass().empty()) {
       OS << "WRAPPER_CLASS(" << Clause.getFormattedParserClassName() << ", ";
       if (Clause.isValueOptional() && Clause.isValueList()) {
@@ -897,7 +883,7 @@ static void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang,
 
   OS << "\n";
   interleaveComma(DirLang.getClauses(), OS, [&](const Record *C) {
-    Clause Clause{C};
+    Clause Clause(C);
     OS << Clause.getFormattedParserClassName() << "\n";
   });
 }
@@ -909,8 +895,7 @@ static void GenerateFlangClauseDump(const DirectiveLanguage &DirLang,
   IfDefScope Scope("GEN_FLANG_DUMP_PARSE_TREE_CLAUSES", OS);
 
   OS << "\n";
-  for (const Record *C : DirLang.getClauses()) {
-    Clause Clause{C};
+  for (const Clause Clause : DirLang.getClauses()) {
     OS << "NODE(" << DirLang.getFlangClauseBaseClass() << ", "
        << Clause.getFormattedParserClassName() << ")\n";
   }
@@ -925,8 +910,7 @@ static void GenerateFlangClauseUnparse(const DirectiveLanguage &DirLang,
 
   OS << "\n";
 
-  for (const Record *C : DirLang.getClauses()) {
-    Clause Clause{C};
+  for (const Clause Clause : DirLang.getClauses()) {
     if (!Clause.getFlangClass().empty()) {
       if (Clause.isValueOptional() && Clause.getDefaultValue().empty()) {
         OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
@@ -976,8 +960,7 @@ static void GenerateFlangClauseCheckPrototypes(const DirectiveLanguage &DirLang,
   IfDefScope Scope("GEN_FLANG_CLAUSE_CHECK_ENTER", OS);
 
   OS << "\n";
-  for (const Record *C : DirLang.getClauses()) {
-    Clause Clause{C};
+  for (const Clause Clause : DirLang.getClauses()) {
     OS << "void Enter(const parser::" << DirLang.getFlangClauseBaseClass()
        << "::" << Clause.getFormattedParserClassName() << " &);\n";
   }
@@ -991,8 +974,7 @@ static void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
   IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_KIND_MAP", OS);
 
   OS << "\n";
-  for (const Record *C : DirLang.getClauses()) {
-    Clause Clause{C};
+  for (const Clause Clause : DirLang.getClauses()) {
     OS << "if constexpr (std::is_same_v<A, parser::"
        << DirLang.getFlangClauseBaseClass()
        << "::" << Clause.getFormattedParserClassName();
@@ -1007,8 +989,8 @@ static void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
 }
 
 static bool compareClauseName(const Record *R1, const Record *R2) {
-  Clause C1{R1};
-  Clause C2{R2};
+  Clause C1(R1);
+  Clause C2(R2);
   return (C1.getName() > C2.getName());
 }
 
@@ -1024,8 +1006,7 @@ static void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
   unsigned index = 0;
   unsigned lastClauseIndex = Clauses.size() - 1;
   OS << "TYPE_PARSER(\n";
-  for (const Record *C : Clauses) {
-    Clause Clause{C};
+  for (const Clause Clause : Clauses) {
     if (Clause.getAliases().empty()) {
       OS << "  \"" << Clause.getName() << "\"";
     } else {
@@ -1150,8 +1131,7 @@ static void GenerateClauseClassMacro(const DirectiveLanguage &DirLang,
   OS << "  CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, Str)\n";
   OS << "\n";
 
-  for (const Record *R : DirLang.getClauses()) {
-    Clause C{R};
+  for (const Clause C : DirLang.getClauses()) {
     if (C.getClangClass().empty()) { // NO_CLASS
       if (C.isImplicit()) {
         OS << "__IMPLICIT_CLAUSE_NO_CLASS(" << C.getFormattedName() << ", \""
@@ -1224,7 +1204,7 @@ void EmitDirectivesBasicImpl(const DirectiveLanguage &DirLang,
 // Generate the implemenation section for the enumeration in the directive
 // language.
 static void EmitDirectivesImpl(const RecordKeeper &Records, raw_ostream &OS) {
-  const auto DirLang = DirectiveLanguage{Records};
+  const auto DirLang = DirectiveLanguage(Records);
   if (DirLang.HasValidityErrors())
     return;
 


        


More information about the llvm-commits mailing list