blob: 6d528a2b0a7d9064101afe2a4a11f21e23f7a841 [file] [log] [blame]
Chris Lattner44da5fb2009-09-14 01:19:16 +00001//===- 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 TBLGEN_STRING_TO_OFFSET_TABLE_H
11#define TBLGEN_STRING_TO_OFFSET_TABLE_H
12
Daniel Dunbar3446cf12009-10-17 20:43:19 +000013#include "llvm/ADT/SmallString.h"
Chris Lattner44da5fb2009-09-14 01:19:16 +000014#include "llvm/ADT/StringMap.h"
Chris Lattner44da5fb2009-09-14 01:19:16 +000015#include "llvm/ADT/StringExtras.h"
Daniel Dunbar3446cf12009-10-17 20:43:19 +000016#include "llvm/Support/raw_ostream.h"
Chris Lattner44da5fb2009-09-14 01:19:16 +000017
18namespace llvm {
19
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.
24class StringToOffsetTable {
25 StringMap<unsigned> StringOffset;
26 std::string AggregateString;
27public:
28
Benjamin Kramer02ee7532012-03-03 19:13:20 +000029 unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
30 StringMapEntry<unsigned> &Entry = StringOffset.GetOrCreateValue(Str, -1U);
31 if (Entry.getValue() == -1U) {
Chris Lattner44da5fb2009-09-14 01:19:16 +000032 // Add the string to the aggregate if this is the first time found.
Benjamin Kramer02ee7532012-03-03 19:13:20 +000033 Entry.setValue(AggregateString.size());
Chris Lattner44da5fb2009-09-14 01:19:16 +000034 AggregateString.append(Str.begin(), Str.end());
Benjamin Kramer02ee7532012-03-03 19:13:20 +000035 if (appendZero)
36 AggregateString += '\0';
Chris Lattner44da5fb2009-09-14 01:19:16 +000037 }
38
Benjamin Kramer02ee7532012-03-03 19:13:20 +000039 return Entry.getValue();
Chris Lattner44da5fb2009-09-14 01:19:16 +000040 }
41
42 void EmitString(raw_ostream &O) {
Craig Topper88d2fa42012-03-08 06:55:27 +000043 assert(AggregateString.size() <= 65536 &&
44 "Aggregate string too large to be portable");
45
Daniel Dunbar3446cf12009-10-17 20:43:19 +000046 // Escape the string.
47 SmallString<256> Str;
48 raw_svector_ostream(Str).write_escaped(AggregateString);
49 AggregateString = Str.str();
50
Chris Lattner44da5fb2009-09-14 01:19:16 +000051 O << " \"";
52 unsigned CharsPrinted = 0;
Chris Lattner44da5fb2009-09-14 01:19:16 +000053 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
54 if (CharsPrinted > 70) {
55 O << "\"\n \"";
56 CharsPrinted = 0;
57 }
58 O << AggregateString[i];
59 ++CharsPrinted;
60
61 // Print escape sequences all together.
62 if (AggregateString[i] != '\\')
63 continue;
64
65 assert(i+1 < AggregateString.size() && "Incomplete escape sequence!");
66 if (isdigit(AggregateString[i+1])) {
67 assert(isdigit(AggregateString[i+2]) &&
68 isdigit(AggregateString[i+3]) &&
69 "Expected 3 digit octal escape!");
70 O << AggregateString[++i];
71 O << AggregateString[++i];
72 O << AggregateString[++i];
73 CharsPrinted += 3;
74 } else {
75 O << AggregateString[++i];
76 ++CharsPrinted;
77 }
78 }
79 O << "\"";
80 }
81};
82
83} // end namespace llvm
84
85#endif