Skip to content

Commit 381405f

Browse files
authored
[NFC][TableGen] Emit more readable builtin string table (#105445)
- Add `EmitStringLiteralDef` to StringToOffsetTable class to emit more readable string table. - Use that in `EmitIntrinsicToBuiltinMap`.
1 parent f06563a commit 381405f

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

llvm/include/llvm/TableGen/StringToOffsetTable.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/ADT/SmallString.h"
1313
#include "llvm/ADT/StringExtras.h"
1414
#include "llvm/ADT/StringMap.h"
15+
#include "llvm/Support/FormatVariadic.h"
1516
#include "llvm/Support/raw_ostream.h"
1617
#include <cctype>
1718
#include <optional>
@@ -52,6 +53,31 @@ class StringToOffsetTable {
5253
return II->second;
5354
}
5455

56+
// Emit the string using string literal concatenation, for better readability
57+
// and searchability.
58+
void EmitStringLiteralDef(raw_ostream &OS, const Twine &Decl,
59+
const Twine &Indent = " ") const {
60+
OS << formatv(R"(
61+
#ifdef __GNUC__
62+
#pragma GCC diagnostic push
63+
#pragma GCC diagnostic ignored "-Woverlength-strings"
64+
#endif
65+
{0}{1} = )",
66+
Indent, Decl);
67+
68+
for (StringRef Str : split(AggregateString, '\0')) {
69+
OS << "\n" << Indent << " \"";
70+
OS.write_escaped(Str);
71+
OS << "\\0\"";
72+
}
73+
OS << R"(;
74+
#ifdef __GNUC__
75+
#pragma GCC diagnostic pop
76+
#endif
77+
)";
78+
}
79+
80+
// Emit the string as one single string.
5581
void EmitString(raw_ostream &O) {
5682
// Escape the string.
5783
SmallString<256> Str;

llvm/utils/TableGen/IntrinsicEmitter.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,7 @@ Intrinsic::getIntrinsicFor{1}Builtin(StringRef TargetPrefix,
669669
}
670670

671671
if (!Table.empty()) {
672-
OS << " static constexpr char BuiltinNames[] = {\n";
673-
Table.EmitCharArray(OS);
674-
OS << " };\n\n";
672+
Table.EmitStringLiteralDef(OS, "static constexpr char BuiltinNames[]");
675673

676674
OS << R"(
677675
struct BuiltinEntry {

0 commit comments

Comments
 (0)