Skip to content

Commit c760588

Browse files
committed
[NFC][TableGen] Adopt NamespaceEmitter in DirectiveEmitter
1 parent 34a3488 commit c760588

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

llvm/test/TableGen/directive1.td

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
184184
// IMPL: #ifdef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
185185
// IMPL-NEXT: #undef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
186186
// IMPL-EMPTY:
187-
// IMPL-NEXT: namespace llvm {
188-
// IMPL-NEXT: namespace tdl {
187+
// IMPL-NEXT: namespace llvm::tdl {
189188
// IMPL-EMPTY:
190189
// IMPL-NEXT: // Sets for dira
191190
// IMPL-EMPTY:
@@ -202,8 +201,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
202201
// IMPL-EMPTY:
203202
// IMPL-NEXT: static requiredClauses_TDLD_dira {
204203
// IMPL-NEXT: };
205-
// IMPL-NEXT: } // namespace tdl
206-
// IMPL-NEXT: } // namespace llvm
204+
// IMPL-NEXT: } // namespace llvm::tdl
207205
// IMPL-EMPTY:
208206
// IMPL-NEXT: #endif // GEN_FLANG_DIRECTIVE_CLAUSE_SETS
209207
// IMPL-EMPTY:

llvm/test/TableGen/directive2.td

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
156156
// IMPL: #ifdef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
157157
// IMPL-NEXT: #undef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
158158
// IMPL-EMPTY:
159-
// IMPL-NEXT: namespace llvm {
160-
// IMPL-NEXT: namespace tdl {
159+
// IMPL-NEXT: namespace llvm::tdl {
161160
// IMPL-EMPTY:
162161
// IMPL-NEXT: // Sets for dira
163162
// IMPL-EMPTY:
@@ -174,8 +173,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
174173
// IMPL-EMPTY:
175174
// IMPL-NEXT: static requiredClauses_TDLD_dira {
176175
// IMPL-NEXT: };
177-
// IMPL-NEXT: } // namespace tdl
178-
// IMPL-NEXT: } // namespace llvm
176+
// IMPL-NEXT: } // namespace llvm::tdl
179177
// IMPL-EMPTY:
180178
// IMPL-NEXT: #endif // GEN_FLANG_DIRECTIVE_CLAUSE_SETS
181179
// IMPL-EMPTY:

llvm/utils/TableGen/Basic/DirectiveEmitter.cpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,8 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
353353
OS << "LLVM_ABI Association getDirectiveAssociation(Directive D);\n";
354354
OS << "LLVM_ABI Category getDirectiveCategory(Directive D);\n";
355355
OS << "LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);\n";
356-
if (EnumHelperFuncs.length() > 0) {
357-
OS << EnumHelperFuncs;
358-
OS << "\n";
359-
}
356+
if (EnumHelperFuncs.length() > 0)
357+
OS << EnumHelperFuncs << '\n';
360358

361359
DirLangNS.close();
362360

@@ -368,7 +366,6 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
368366
OS << " static constexpr bool is_iterable = true;\n";
369367
OS << "};\n";
370368
}
371-
LlvmNS.close();
372369
}
373370

374371
// Given a list of spellings (for a given clause/directive), order them
@@ -940,24 +937,18 @@ static void generateClauseSet(ArrayRef<const Record *> VerClauses,
940937
// Generate an enum set for the 4 kinds of clauses linked to a directive.
941938
static void generateDirectiveClauseSets(const DirectiveLanguage &DirLang,
942939
Frontend FE, raw_ostream &OS) {
940+
IfDefEmitter Scope(OS, "GEN_" + getFESpelling(FE).upper() +
941+
"_DIRECTIVE_CLAUSE_SETS");
943942

944-
std::string IfDefName{"GEN_"};
945-
IfDefName += getFESpelling(FE).upper();
946-
IfDefName += "_DIRECTIVE_CLAUSE_SETS";
947-
IfDefEmitter Scope(OS, IfDefName);
948-
949-
StringRef Namespace =
950-
getFESpelling(FE == Frontend::Flang ? Frontend::LLVM : FE);
943+
std::string Namespace =
944+
getFESpelling(FE == Frontend::Flang ? Frontend::LLVM : FE).str();
951945
// The namespace has to be different for clang vs flang, as 2 structs with the
952946
// same name but different layout is UB. So just put the 'clang' on in the
953947
// clang namespace.
954-
OS << "namespace " << Namespace << " {\n";
955-
956-
// Open namespaces defined in the directive language.
957-
SmallVector<StringRef, 2> Namespaces;
958-
SplitString(DirLang.getCppNamespace(), Namespaces, "::");
959-
for (auto Ns : Namespaces)
960-
OS << "namespace " << Ns << " {\n";
948+
// Additionally, open namespaces defined in the directive language.
949+
if (!DirLang.getCppNamespace().empty())
950+
Namespace += "::" + DirLang.getCppNamespace().str();
951+
NamespaceEmitter NS(OS, Namespace);
961952

962953
for (const Directive Dir : DirLang.getDirectives()) {
963954
OS << "\n";
@@ -972,23 +963,15 @@ static void generateDirectiveClauseSets(const DirectiveLanguage &DirLang,
972963
generateClauseSet(Dir.getRequiredClauses(), OS, "requiredClauses_", Dir,
973964
DirLang, FE);
974965
}
975-
976-
// Closing namespaces
977-
for (auto Ns : reverse(Namespaces))
978-
OS << "} // namespace " << Ns << "\n";
979-
980-
OS << "} // namespace " << Namespace << "\n";
981966
}
982967

983968
// Generate a map of directive (key) with DirectiveClauses struct as values.
984969
// The struct holds the 4 sets of enumeration for the 4 kinds of clauses
985970
// allowances (allowed, allowed once, allowed exclusive and required).
986971
static void generateDirectiveClauseMap(const DirectiveLanguage &DirLang,
987972
Frontend FE, raw_ostream &OS) {
988-
std::string IfDefName{"GEN_"};
989-
IfDefName += getFESpelling(FE).upper();
990-
IfDefName += "_DIRECTIVE_CLAUSE_MAP";
991-
IfDefEmitter Scope(OS, IfDefName);
973+
IfDefEmitter Scope(OS, "GEN_" + getFESpelling(FE).upper() +
974+
"_DIRECTIVE_CLAUSE_MAP");
992975

993976
OS << "{\n";
994977

0 commit comments

Comments
 (0)