LLVM  4.0.0
StringToOffsetTable.h
Go to the documentation of this file.
1 //===- StringToOffsetTable.h - Emit a big concatenated string ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
11 #define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
12 
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringMap.h"
17 #include <cctype>
18 
19 namespace 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 
29 public:
30  unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
31  auto IterBool =
32  StringOffset.insert(std::make_pair(Str, AggregateString.size()));
33  if (IterBool.second) {
34  // Add the string to the aggregate if this is the first time found.
35  AggregateString.append(Str.begin(), Str.end());
36  if (appendZero)
37  AggregateString += '\0';
38  }
39 
40  return IterBool.first->second;
41  }
42 
44  // Escape the string.
45  SmallString<256> Str;
46  raw_svector_ostream(Str).write_escaped(AggregateString);
47  AggregateString = Str.str();
48 
49  O << " \"";
50  unsigned CharsPrinted = 0;
51  for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
52  if (CharsPrinted > 70) {
53  O << "\"\n \"";
54  CharsPrinted = 0;
55  }
56  O << AggregateString[i];
57  ++CharsPrinted;
58 
59  // Print escape sequences all together.
60  if (AggregateString[i] != '\\')
61  continue;
62 
63  assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
64  if (isdigit(AggregateString[i+1])) {
65  assert(isdigit(AggregateString[i+2]) &&
66  isdigit(AggregateString[i+3]) &&
67  "Expected 3 digit octal escape!");
68  O << AggregateString[++i];
69  O << AggregateString[++i];
70  O << AggregateString[++i];
71  CharsPrinted += 3;
72  } else {
73  O << AggregateString[++i];
74  ++CharsPrinted;
75  }
76  }
77  O << "\"";
78  }
79 
80  /// Emit the string using character literals. MSVC has a limitation that
81  /// string literals cannot be longer than 64K.
83  assert(AggregateString.find(')') == std::string::npos &&
84  "can't emit raw string with closing parens");
85  int Count = 0;
86  O << ' ';
87  for (char C : AggregateString) {
88  O << " \'";
89  O.write_escaped(StringRef(&C, 1));
90  O << "\',";
91  Count++;
92  if (Count > 14) {
93  O << "\n ";
94  Count = 0;
95  }
96  }
97  O << '\n';
98  }
99 };
100 
101 } // end namespace llvm
102 
103 #endif
size_t i
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
unsigned GetOrAddStringOffset(StringRef Str, bool appendZero=true)
StringToOffsetTable - This class uniques a bunch of nul-terminated strings and keeps track of their o...
iterator begin() const
Definition: StringRef.h:103
void EmitString(raw_ostream &O)
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy std::isprint into an escape...
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator end() const
Definition: StringRef.h:105
void EmitCharArray(raw_ostream &O)
Emit the string using character literals.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47