LLVM 22.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"
15#include <optional>
16
17namespace llvm {
18
19/// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
20/// and keeps track of their offset in a massive contiguous string allocation.
21/// It can then output this string blob and use indexes into the string to
22/// reference each piece.
24 StringMap<unsigned> StringOffset;
25 std::string AggregateString;
26
27 /// If this is to be a static class member, the prefix to use (i.e. class name
28 /// plus ::)
29 const StringRef ClassPrefix;
30 const bool AppendZero;
31
32public:
33 StringToOffsetTable(bool AppendZero = true, StringRef ClassPrefix = "")
34 : ClassPrefix(ClassPrefix), AppendZero(AppendZero) {
35 // Ensure we always put the empty string at offset zero. That lets empty
36 // initialization also be zero initialization for offsets into the table.
38 }
39
40 bool empty() const { return StringOffset.empty(); }
41 size_t size() const { return AggregateString.size(); }
42
43 unsigned GetOrAddStringOffset(StringRef Str);
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 a string table definition with the provided name.
55 //
56 // When possible, this uses string-literal concatenation to emit the string
57 // contents in a readable and searchable way. However, for (very) large string
58 // tables MSVC cannot reliably use string literals and so there we use a large
59 // character array. We still use a line oriented emission and add comments to
60 // provide searchability even in this case.
61 //
62 // The string table, and its input string contents, are always emitted as both
63 // `static` and `constexpr`. Both `Name` and (`Name` + "Storage") must be
64 // valid identifiers to declare.
65 void EmitStringTableDef(raw_ostream &OS, const Twine &Name) const;
66
67 // Emit the string as one single string.
68 void EmitString(raw_ostream &O) const;
69};
70
71} // end namespace llvm
72
73#endif
This file defines the StringMap class.
std::string Name
uint64_t IntrinsicInst * II
raw_pwrite_stream & OS
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
bool empty() const
Definition: StringMap.h:108
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
iterator end()
Definition: StringMap.h:224
iterator find(StringRef Key)
Definition: StringMap.h:237
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
StringToOffsetTable - This class uniques a bunch of nul-terminated strings and keeps track of their o...
unsigned GetOrAddStringOffset(StringRef Str)
void EmitStringTableDef(raw_ostream &OS, const Twine &Name) const
void EmitString(raw_ostream &O) const
StringToOffsetTable(bool AppendZero=true, StringRef ClassPrefix="")
std::optional< unsigned > GetStringOffset(StringRef Str) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18