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"
16#include <cctype>
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 IterBool =
35 StringOffset.insert(std::make_pair(Str, AggregateString.size()));
36 if (IterBool.second) {
37 // Add the string to the aggregate if this is the first time found.
38 AggregateString.append(Str.begin(), Str.end());
39 if (appendZero)
40 AggregateString += '\0';
41 }
42
43 return IterBool.first->second;
44 }
45
46 // Returns the offset of `Str` in the table if its preset, else return
47 // std::nullopt.
48 std::optional<unsigned> GetStringOffset(StringRef Str) const {
49 auto II = StringOffset.find(Str);
50 if (II == StringOffset.end())
51 return std::nullopt;
52 return II->second;
53 }
54
56 // Escape the string.
58 raw_svector_ostream(Str).write_escaped(AggregateString);
59 AggregateString = std::string(Str);
60
61 O << " \"";
62 unsigned CharsPrinted = 0;
63 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
64 if (CharsPrinted > 70) {
65 O << "\"\n \"";
66 CharsPrinted = 0;
67 }
68 O << AggregateString[i];
69 ++CharsPrinted;
70
71 // Print escape sequences all together.
72 if (AggregateString[i] != '\\')
73 continue;
74
75 assert(i + 1 < AggregateString.size() && "Incomplete escape sequence!");
76 if (isdigit(AggregateString[i + 1])) {
77 assert(isdigit(AggregateString[i + 2]) &&
78 isdigit(AggregateString[i + 3]) &&
79 "Expected 3 digit octal escape!");
80 O << AggregateString[++i];
81 O << AggregateString[++i];
82 O << AggregateString[++i];
83 CharsPrinted += 3;
84 } else {
85 O << AggregateString[++i];
86 ++CharsPrinted;
87 }
88 }
89 O << "\"";
90 }
91
92 /// Emit the string using character literals. MSVC has a limitation that
93 /// string literals cannot be longer than 64K.
95 assert(AggregateString.find(')') == std::string::npos &&
96 "can't emit raw string with closing parens");
97 int Count = 0;
98 O << ' ';
99 for (char C : AggregateString) {
100 O << " \'";
101 O.write_escaped(StringRef(&C, 1));
102 O << "\',";
103 Count++;
104 if (Count > 14) {
105 O << "\n ";
106 Count = 0;
107 }
108 }
109 O << '\n';
110 }
111};
112
113} // end namespace llvm
114
115#endif
This file defines the StringMap class.
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
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:50
StringToOffsetTable - This class uniques a bunch of nul-terminated strings and keeps track of their o...
void EmitCharArray(raw_ostream &O)
Emit the string using character literals.
unsigned GetOrAddStringOffset(StringRef Str, bool appendZero=true)
std::optional< unsigned > GetStringOffset(StringRef Str) const
void EmitString(raw_ostream &O)
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