Skip to content

Commit

Permalink
[NFC][TableGen] Emit more readable builtin string table (llvm#105445)
Browse files Browse the repository at this point in the history
- Add `EmitStringLiteralDef` to StringToOffsetTable class to emit more
readable string table.
- Use that in `EmitIntrinsicToBuiltinMap`.
  • Loading branch information
jurahul committed Aug 23, 2024
1 parent f06563a commit 381405f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
26 changes: 26 additions & 0 deletions llvm/include/llvm/TableGen/StringToOffsetTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <cctype>
#include <optional>
Expand Down Expand Up @@ -52,6 +53,31 @@ class StringToOffsetTable {
return II->second;
}

// Emit the string using string literal concatenation, for better readability
// and searchability.
void EmitStringLiteralDef(raw_ostream &OS, const Twine &Decl,
const Twine &Indent = " ") const {
OS << formatv(R"(
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
{0}{1} = )",
Indent, Decl);

for (StringRef Str : split(AggregateString, '\0')) {
OS << "\n" << Indent << " \"";
OS.write_escaped(Str);
OS << "\\0\"";
}
OS << R"(;
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
)";
}

// Emit the string as one single string.
void EmitString(raw_ostream &O) {
// Escape the string.
SmallString<256> Str;
Expand Down
4 changes: 1 addition & 3 deletions llvm/utils/TableGen/IntrinsicEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,7 @@ Intrinsic::getIntrinsicFor{1}Builtin(StringRef TargetPrefix,
}

if (!Table.empty()) {
OS << " static constexpr char BuiltinNames[] = {\n";
Table.EmitCharArray(OS);
OS << " };\n\n";
Table.EmitStringLiteralDef(OS, "static constexpr char BuiltinNames[]");

OS << R"(
struct BuiltinEntry {
Expand Down

0 comments on commit 381405f

Please sign in to comment.