Skip to content

[NFC][TableGen] Refactor StringToOffsetTable #105655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Changes from all 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
36 changes: 16 additions & 20 deletions llvm/include/llvm/TableGen/StringToOffsetTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <cctype>
#include <optional>

namespace llvm {
Expand All @@ -32,16 +31,15 @@ class StringToOffsetTable {
size_t size() const { return AggregateString.size(); }

unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
auto IterBool =
StringOffset.insert(std::make_pair(Str, AggregateString.size()));
if (IterBool.second) {
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 IterBool.first->second;
return II->second;
}

// Returns the offset of `Str` in the table if its preset, else return
Expand Down Expand Up @@ -78,37 +76,35 @@ class StringToOffsetTable {
}

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

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

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

assert(i + 1 < AggregateString.size() && "Incomplete escape sequence!");
if (isdigit(AggregateString[i + 1])) {
assert(isdigit(AggregateString[i + 2]) &&
isdigit(AggregateString[i + 3]) &&
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 << AggregateString[++i];
O << AggregateString[++i];
O << AggregateString[++i];
O << EscapedStr[++i];
O << EscapedStr[++i];
O << EscapedStr[++i];
CharsPrinted += 3;
} else {
O << AggregateString[++i];
O << EscapedStr[++i];
++CharsPrinted;
}
}
Expand Down
Loading