LLVM 20.0.0git
StringToOffsetTable.h
Go to the documentation of this file.
1//===- StringToOffsetTable.h - Emit a big concatenated string ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
10#define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
11
14#include "llvm/ADT/StringMap.h"
17#include <optional>
18
19namespace llvm {
20
21/// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
22/// and keeps track of their offset in a massive contiguous string allocation.
23/// It can then output this string blob and use indexes into the string to
24/// reference each piece.
26 StringMap<unsigned> StringOffset;
27 std::string AggregateString;
28
29public:
30 bool empty() const { return StringOffset.empty(); }
31 size_t size() const { return AggregateString.size(); }
32
33 unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
34 auto [II, Inserted] = StringOffset.insert({Str, size()});
35 if (Inserted) {
36 // Add the string to the aggregate if this is the first time found.
37 AggregateString.append(Str.begin(), Str.end());
38 if (appendZero)
39 AggregateString += '\0';
40 }
41
42 return II->second;
43 }
44
45 // Returns the offset of `Str` in the table if its preset, else return
46 // std::nullopt.
47 std::optional<unsigned> GetStringOffset(StringRef Str) const {
48 auto II = StringOffset.find(Str);
49 if (II == StringOffset.end())
50 return std::nullopt;
51 return II->second;
52 }
53
54 // Emit the string using string literal concatenation, for better readability
55 // and searchability.
57 const Twine &Indent = " ") const {
58 OS << formatv(R"(
59#ifdef __GNUC__
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Woverlength-strings"
62#endif
63{0}{1} = )",
64 Indent, Decl);
65
66 for (StringRef Str : split(AggregateString, '\0')) {
67 OS << "\n" << Indent << " \"";
68 OS.write_escaped(Str);
69 OS << "\\0\"";
70 }
71 OS << R"(;
72#ifdef __GNUC__
73#pragma GCC diagnostic pop
74#endif
75)";
76 }
77
78 // Emit the string as one single string.
79 void EmitString(raw_ostream &O) const {
80 // Escape the string.
81 SmallString<256> EscapedStr;
82 raw_svector_ostream(EscapedStr).write_escaped(AggregateString);
83
84 O << " \"";
85 unsigned CharsPrinted = 0;
86 for (unsigned i = 0, e = EscapedStr.size(); i != e; ++i) {
87 if (CharsPrinted > 70) {
88 O << "\"\n \"";
89 CharsPrinted = 0;
90 }
91 O << EscapedStr[i];
92 ++CharsPrinted;
93
94 // Print escape sequences all together.
95 if (EscapedStr[i] != '\\')
96 continue;
97
98 assert(i + 1 < EscapedStr.size() && "Incomplete escape sequence!");
99 if (isDigit(EscapedStr[i + 1])) {
100 assert(isDigit(EscapedStr[i + 2]) && isDigit(EscapedStr[i + 3]) &&
101 "Expected 3 digit octal escape!");
102 O << EscapedStr[++i];
103 O << EscapedStr[++i];
104 O << EscapedStr[++i];
105 CharsPrinted += 3;
106 } else {
107 O << EscapedStr[++i];
108 ++CharsPrinted;
109 }
110 }
111 O << "\"";
113
114 /// Emit the string using character literals. MSVC has a limitation that
115 /// string literals cannot be longer than 64K.
116 void EmitCharArray(raw_ostream &O) {
117 assert(AggregateString.find(')') == std::string::npos &&
118 "can't emit raw string with closing parens");
119 int Count = 0;
120 O << ' ';
121 for (char C : AggregateString) {
122 O << " \'";
123 O.write_escaped(StringRef(&C, 1));
124 O << "\',";
125 Count++;
126 if (Count > 14) {
127 O << "\n ";
128 Count = 0;
129 }
130 }
131 O << '\n';
132 }
133};
134
135} // end namespace llvm
136
137#endif
This file defines the StringMap class.
uint64_t IntrinsicInst * II
static bool isDigit(const char C)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
size_t size() const
Definition: SmallVector.h:78
bool empty() const
Definition: StringMap.h:103
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
StringToOffsetTable - This class uniques a bunch of nul-terminated strings and keeps track of their o...
void EmitString(raw_ostream &O) const
void EmitCharArray(raw_ostream &O)
Emit the string using character literals.
unsigned GetOrAddStringOffset(StringRef Str, bool appendZero=true)
void EmitStringLiteralDef(raw_ostream &OS, const Twine &Decl, const Twine &Indent=" ") const
std::optional< unsigned > GetStringOffset(StringRef Str) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '\t', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an esca...
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)