1 //===- StringToOffsetTable.h - Emit a big concatenated string ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
10 #define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/raw_ostream.h"
20 /// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
21 /// and keeps track of their offset in a massive contiguous string allocation.
22 /// It can then output this string blob and use indexes into the string to
23 /// reference each piece.
24 class StringToOffsetTable
{
25 StringMap
<unsigned> StringOffset
;
26 std::string AggregateString
;
29 bool Empty() const { return StringOffset
.empty(); }
31 unsigned GetOrAddStringOffset(StringRef Str
, bool appendZero
= true) {
33 StringOffset
.insert(std::make_pair(Str
, AggregateString
.size()));
34 if (IterBool
.second
) {
35 // Add the string to the aggregate if this is the first time found.
36 AggregateString
.append(Str
.begin(), Str
.end());
38 AggregateString
+= '\0';
41 return IterBool
.first
->second
;
44 void EmitString(raw_ostream
&O
) {
47 raw_svector_ostream(Str
).write_escaped(AggregateString
);
48 AggregateString
= Str
.str();
51 unsigned CharsPrinted
= 0;
52 for (unsigned i
= 0, e
= AggregateString
.size(); i
!= e
; ++i
) {
53 if (CharsPrinted
> 70) {
57 O
<< AggregateString
[i
];
60 // Print escape sequences all together.
61 if (AggregateString
[i
] != '\\')
64 assert(i
+ 1 < AggregateString
.size() && "Incomplete escape sequence!");
65 if (isdigit(AggregateString
[i
+ 1])) {
66 assert(isdigit(AggregateString
[i
+ 2]) &&
67 isdigit(AggregateString
[i
+ 3]) &&
68 "Expected 3 digit octal escape!");
69 O
<< AggregateString
[++i
];
70 O
<< AggregateString
[++i
];
71 O
<< AggregateString
[++i
];
74 O
<< AggregateString
[++i
];
81 /// Emit the string using character literals. MSVC has a limitation that
82 /// string literals cannot be longer than 64K.
83 void EmitCharArray(raw_ostream
&O
) {
84 assert(AggregateString
.find(')') == std::string::npos
&&
85 "can't emit raw string with closing parens");
88 for (char C
: AggregateString
) {
90 O
.write_escaped(StringRef(&C
, 1));
102 } // end namespace llvm