Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 3 additions & 101 deletions llvm/include/llvm/TableGen/StringToOffsetTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#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 <optional>

namespace llvm {
Expand All @@ -36,17 +34,7 @@ class StringToOffsetTable {
bool empty() const { return StringOffset.empty(); }
size_t size() const { return AggregateString.size(); }

unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
auto [II, Inserted] = StringOffset.insert({Str, size()});
if (Inserted) {
// Add the string to the aggregate if this is the first time found.
AggregateString.append(Str.begin(), Str.end());
if (appendZero)
AggregateString += '\0';
}

return II->second;
}
unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true);

// Returns the offset of `Str` in the table if its preset, else return
// std::nullopt.
Expand All @@ -69,96 +57,10 @@ class StringToOffsetTable {
// `static` and `constexpr`. Both `Name` and (`Name` + "Storage") must be
// valid identifiers to declare.
void EmitStringTableDef(raw_ostream &OS, const Twine &Name,
const Twine &Indent = "") const {
OS << formatv(R"(
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
{0}static constexpr char {1}Storage[] = )",
Indent, Name);

// MSVC silently miscompiles string literals longer than 64k in some
// circumstances. When the string table is longer, emit it as an array of
// character literals.
bool UseChars = AggregateString.size() > (64 * 1024);
OS << (UseChars ? "{\n" : "\n");

llvm::ListSeparator LineSep(UseChars ? ",\n" : "\n");
llvm::SmallVector<StringRef> Strings(split(AggregateString, '\0'));
// We should always have an empty string at the start, and because these are
// null terminators rather than separators, we'll have one at the end as
// well. Skip the end one.
assert(Strings.front().empty() && "Expected empty initial string!");
assert(Strings.back().empty() &&
"Expected empty string at the end due to terminators!");
Strings.pop_back();
for (StringRef Str : Strings) {
OS << LineSep << Indent << " ";
// If we can, just emit this as a string literal to be concatenated.
if (!UseChars) {
OS << "\"";
OS.write_escaped(Str);
OS << "\\0\"";
continue;
}

llvm::ListSeparator CharSep(", ");
for (char C : Str) {
OS << CharSep << "'";
OS.write_escaped(StringRef(&C, 1));
OS << "'";
}
OS << CharSep << "'\\0'";
}
OS << LineSep << Indent << (UseChars ? "};" : " ;");

OS << formatv(R"(
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

{0}static constexpr llvm::StringTable {1} =
{0} {1}Storage;
)",
Indent, Name);
}
const Twine &Indent = "") const;

// Emit the string as one single string.
void EmitString(raw_ostream &O) const {
// Escape the string.
SmallString<256> EscapedStr;
raw_svector_ostream(EscapedStr).write_escaped(AggregateString);

O << " \"";
unsigned CharsPrinted = 0;
for (unsigned i = 0, e = EscapedStr.size(); i != e; ++i) {
if (CharsPrinted > 70) {
O << "\"\n \"";
CharsPrinted = 0;
}
O << EscapedStr[i];
++CharsPrinted;

// Print escape sequences all together.
if (EscapedStr[i] != '\\')
continue;

assert(i + 1 < EscapedStr.size() && "Incomplete escape sequence!");
if (isDigit(EscapedStr[i + 1])) {
assert(isDigit(EscapedStr[i + 2]) && isDigit(EscapedStr[i + 3]) &&
"Expected 3 digit octal escape!");
O << EscapedStr[++i];
O << EscapedStr[++i];
O << EscapedStr[++i];
CharsPrinted += 3;
} else {
O << EscapedStr[++i];
++CharsPrinted;
}
}
O << "\"";
}
void EmitString(raw_ostream &O) const;
};

} // end namespace llvm
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/TableGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_llvm_component_library(LLVMTableGen
Record.cpp
SetTheory.cpp
StringMatcher.cpp
StringToOffsetTable.cpp
TableGenBackend.cpp
TableGenBackendSkeleton.cpp
TGLexer.cpp
Expand Down
129 changes: 129 additions & 0 deletions llvm/lib/TableGen/StringToOffsetTable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//===- StringToOffsetTable.cpp - Emit a big concatenated string -*- C++ -*-===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note that the updated coding standard doesn't require this line to contain filename/description/c++.)

//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/TableGen/StringToOffsetTable.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

namespace llvm {
cl::opt<bool> EmitLongStrLiterals(
"long-string-literals",
cl::desc("when emitting large string tables, prefer string literals over "
"comma-separated char literals. This can be a readability and "
"compile-time performance win, but upsets some compilers"),
cl::Hidden, cl::init(true));
} // end namespace llvm

unsigned StringToOffsetTable::GetOrAddStringOffset(StringRef Str,
bool appendZero) {
auto [II, Inserted] = StringOffset.insert({Str, size()});
if (Inserted) {
// Add the string to the aggregate if this is the first time found.
AggregateString.append(Str.begin(), Str.end());
if (appendZero)
AggregateString += '\0';
}

return II->second;
}

void StringToOffsetTable::EmitStringTableDef(raw_ostream &OS, const Twine &Name,
const Twine &Indent) const {
OS << formatv(R"(
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Woverlength-strings"
#endif
{0}static constexpr char {1}Storage[] = )",
Indent, Name);

// MSVC silently miscompiles string literals longer than 64k in some
// circumstances. The build system sets EmitLongStrLiterals to false when it
// detects that it is targetting MSVC. When that option is false and the
// string table is longer than 64k, emit it as an array of character
// literals.
bool UseChars = !EmitLongStrLiterals && AggregateString.size() > (64 * 1024);
OS << (UseChars ? "{\n" : "\n");

llvm::ListSeparator LineSep(UseChars ? ",\n" : "\n");
llvm::SmallVector<StringRef> Strings(split(AggregateString, '\0'));
// We should always have an empty string at the start, and because these are
// null terminators rather than separators, we'll have one at the end as
// well. Skip the end one.
assert(Strings.front().empty() && "Expected empty initial string!");
assert(Strings.back().empty() &&
"Expected empty string at the end due to terminators!");
Strings.pop_back();
for (StringRef Str : Strings) {
OS << LineSep << Indent << " ";
// If we can, just emit this as a string literal to be concatenated.
if (!UseChars) {
OS << "\"";
OS.write_escaped(Str);
OS << "\\0\"";
continue;
}

llvm::ListSeparator CharSep(", ");
for (char C : Str) {
OS << CharSep << "'";
OS.write_escaped(StringRef(&C, 1));
OS << "'";
}
OS << CharSep << "'\\0'";
}
OS << LineSep << Indent << (UseChars ? "};" : " ;");

OS << formatv(R"(
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

{0}static constexpr llvm::StringTable {1} =
{0} {1}Storage;
)",
Indent, Name);
}

void StringToOffsetTable::EmitString(raw_ostream &O) const {
// Escape the string.
SmallString<256> EscapedStr;
raw_svector_ostream(EscapedStr).write_escaped(AggregateString);

O << " \"";
unsigned CharsPrinted = 0;
for (unsigned i = 0, e = EscapedStr.size(); i != e; ++i) {
if (CharsPrinted > 70) {
O << "\"\n \"";
CharsPrinted = 0;
}
O << EscapedStr[i];
++CharsPrinted;

// Print escape sequences all together.
if (EscapedStr[i] != '\\')
continue;

assert(i + 1 < EscapedStr.size() && "Incomplete escape sequence!");
if (isDigit(EscapedStr[i + 1])) {
assert(isDigit(EscapedStr[i + 2]) && isDigit(EscapedStr[i + 3]) &&
"Expected 3 digit octal escape!");
O << EscapedStr[++i];
O << EscapedStr[++i];
O << EscapedStr[++i];
CharsPrinted += 3;
} else {
O << EscapedStr[++i];
++CharsPrinted;
}
}
O << "\"";
}
1 change: 1 addition & 0 deletions llvm/utils/TableGen/AsmMatcherEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/StringMatcher.h"
Expand Down
9 changes: 0 additions & 9 deletions llvm/utils/TableGen/Basic/TableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@

using namespace llvm;

namespace llvm {
cl::opt<bool> EmitLongStrLiterals(
"long-string-literals",
cl::desc("when emitting large string tables, prefer string literals over "
"comma-separated char literals. This can be a readability and "
"compile-time performance win, but upsets some compilers"),
cl::Hidden, cl::init(true));
} // end namespace llvm

static cl::OptionCategory PrintEnumsCat("Options for -print-enums");
static cl::opt<std::string> Class("class",
cl::desc("Print Enum list for this class"),
Expand Down
1 change: 1 addition & 0 deletions llvm/utils/TableGen/SDNodeInfoEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Basic/SequenceToOffsetTable.h"
#include "Common/CodeGenDAGPatterns.h" // For SDNodeInfo.
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/StringToOffsetTable.h"
#include "llvm/TableGen/TableGenBackend.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/utils/gn/secondary/llvm/lib/TableGen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ static_library("TableGen") {
"Record.cpp",
"SetTheory.cpp",
"StringMatcher.cpp",
"StringToOffsetTable.cpp",
"TGLexer.cpp",
"TGParser.cpp",
"TGTimer.cpp",
Expand Down