Skip to content

Commit 67afd76

Browse files
committed
[NFC][TableGen] Adopt NamespaceEmitter in DirectiveEmitter
1 parent 67ce4ab commit 67afd76

File tree

3 files changed

+16
-34
lines changed

3 files changed

+16
-34
lines changed

llvm/test/TableGen/directive1.td

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
186186
// IMPL: #ifdef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
187187
// IMPL-NEXT: #undef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
188188
// IMPL-EMPTY:
189-
// IMPL-NEXT: namespace llvm {
190-
// IMPL-NEXT: namespace tdl {
189+
// IMPL-NEXT: namespace llvm::tdl {
191190
// IMPL-EMPTY:
192191
// IMPL-NEXT: // Sets for dira
193192
// IMPL-EMPTY:
@@ -204,8 +203,8 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
204203
// IMPL-EMPTY:
205204
// IMPL-NEXT: static requiredClauses_TDLD_dira {
206205
// IMPL-NEXT: };
207-
// IMPL-NEXT: } // namespace tdl
208-
// IMPL-NEXT: } // namespace llvm
206+
// IMPL-EMPTY:
207+
// IMPL-NEXT: } // namespace llvm::tdl
209208
// IMPL-EMPTY:
210209
// IMPL-NEXT: #endif // GEN_FLANG_DIRECTIVE_CLAUSE_SETS
211210
// IMPL-EMPTY:

llvm/test/TableGen/directive2.td

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
159159
// IMPL: #ifdef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
160160
// IMPL-NEXT: #undef GEN_FLANG_DIRECTIVE_CLAUSE_SETS
161161
// IMPL-EMPTY:
162-
// IMPL-NEXT: namespace llvm {
163-
// IMPL-NEXT: namespace tdl {
162+
// IMPL-NEXT: namespace llvm::tdl {
164163
// IMPL-EMPTY:
165164
// IMPL-NEXT: // Sets for dira
166165
// IMPL-EMPTY:
@@ -177,8 +176,8 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
177176
// IMPL-EMPTY:
178177
// IMPL-NEXT: static requiredClauses_TDLD_dira {
179178
// IMPL-NEXT: };
180-
// IMPL-NEXT: } // namespace tdl
181-
// IMPL-NEXT: } // namespace llvm
179+
// IMPL-EMPTY:
180+
// IMPL-NEXT: } // namespace llvm::tdl
182181
// IMPL-EMPTY:
183182
// IMPL-NEXT: #endif // GEN_FLANG_DIRECTIVE_CLAUSE_SETS
184183
// IMPL-EMPTY:

llvm/utils/TableGen/Basic/DirectiveEmitter.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
359359
OS << " static constexpr bool is_iterable = true;\n";
360360
OS << "};\n";
361361
}
362-
LlvmNS.close();
363362
}
364363

365364
// Given a list of spellings (for a given clause/directive), order them
@@ -931,27 +930,20 @@ static void generateClauseSet(ArrayRef<const Record *> VerClauses,
931930
// Generate an enum set for the 4 kinds of clauses linked to a directive.
932931
static void generateDirectiveClauseSets(const DirectiveLanguage &DirLang,
933932
Frontend FE, raw_ostream &OS) {
933+
IfDefEmitter Scope(OS, "GEN_" + getFESpelling(FE).upper() +
934+
"_DIRECTIVE_CLAUSE_SETS");
934935

935-
std::string IfDefName{"GEN_"};
936-
IfDefName += getFESpelling(FE).upper();
937-
IfDefName += "_DIRECTIVE_CLAUSE_SETS";
938-
IfDefEmitter Scope(OS, IfDefName);
939-
940-
StringRef Namespace =
941-
getFESpelling(FE == Frontend::Flang ? Frontend::LLVM : FE);
936+
std::string Namespace =
937+
getFESpelling(FE == Frontend::Flang ? Frontend::LLVM : FE).str();
942938
// The namespace has to be different for clang vs flang, as 2 structs with the
943939
// same name but different layout is UB. So just put the 'clang' on in the
944940
// clang namespace.
945-
OS << "namespace " << Namespace << " {\n";
946-
947-
// Open namespaces defined in the directive language.
948-
SmallVector<StringRef, 2> Namespaces;
949-
SplitString(DirLang.getCppNamespace(), Namespaces, "::");
950-
for (auto Ns : Namespaces)
951-
OS << "namespace " << Ns << " {\n";
941+
// Additionally, open namespaces defined in the directive language.
942+
if (!DirLang.getCppNamespace().empty())
943+
Namespace += "::" + DirLang.getCppNamespace().str();
944+
NamespaceEmitter NS(OS, Namespace);
952945

953946
for (const Directive Dir : DirLang.getDirectives()) {
954-
OS << "\n";
955947
OS << "// Sets for " << Dir.getSpellingForIdentifier() << "\n";
956948

957949
generateClauseSet(Dir.getAllowedClauses(), OS, "allowedClauses_", Dir,
@@ -963,23 +955,15 @@ static void generateDirectiveClauseSets(const DirectiveLanguage &DirLang,
963955
generateClauseSet(Dir.getRequiredClauses(), OS, "requiredClauses_", Dir,
964956
DirLang, FE);
965957
}
966-
967-
// Closing namespaces
968-
for (auto Ns : reverse(Namespaces))
969-
OS << "} // namespace " << Ns << "\n";
970-
971-
OS << "} // namespace " << Namespace << "\n";
972958
}
973959

974960
// Generate a map of directive (key) with DirectiveClauses struct as values.
975961
// The struct holds the 4 sets of enumeration for the 4 kinds of clauses
976962
// allowances (allowed, allowed once, allowed exclusive and required).
977963
static void generateDirectiveClauseMap(const DirectiveLanguage &DirLang,
978964
Frontend FE, raw_ostream &OS) {
979-
std::string IfDefName{"GEN_"};
980-
IfDefName += getFESpelling(FE).upper();
981-
IfDefName += "_DIRECTIVE_CLAUSE_MAP";
982-
IfDefEmitter Scope(OS, IfDefName);
965+
IfDefEmitter Scope(OS, "GEN_" + getFESpelling(FE).upper() +
966+
"_DIRECTIVE_CLAUSE_MAP");
983967

984968
OS << "{\n";
985969

0 commit comments

Comments
 (0)