1 //===-- RemarkStringTable.h - Serializing string table ----------*- 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 // This class is used to deduplicate and serialize a string table used for
10 // generating remarks.
11 //
12 // For parsing a string table, use ParsedStringTable in RemarkParser.h
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_REMARKS_REMARK_STRING_TABLE_H
17 #define LLVM_REMARKS_REMARK_STRING_TABLE_H
18 
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/Allocator.h"
21 #include <vector>
22 
23 namespace llvm {
24 
25 class raw_ostream;
26 class StringRef;
27 
28 namespace remarks {
29 
30 struct ParsedStringTable;
31 struct Remark;
32 
33 /// The string table used for serializing remarks.
34 /// This table can be for example serialized in a section to be consumed after
35 /// the compilation.
36 struct StringTable {
37   /// The string table containing all the unique strings used in the output.
38   /// It maps a string to an unique ID.
39   StringMap<unsigned, BumpPtrAllocator> StrTab;
40   /// Total size of the string table when serialized.
41   size_t SerializedSize = 0;
42 
43   StringTable() = default;
44 
45   /// Disable copy.
46   StringTable(const StringTable &) = delete;
47   StringTable &operator=(const StringTable &) = delete;
48   /// Should be movable.
49   StringTable(StringTable &&) = default;
50   StringTable &operator=(StringTable &&) = default;
51 
52   /// Construct a string table from a ParsedStringTable.
53   StringTable(const ParsedStringTable &Other);
54 
55   /// Add a string to the table. It returns an unique ID of the string.
56   std::pair<unsigned, StringRef> add(StringRef Str);
57   /// Modify \p R to use strings from this string table. If the string table
58   /// does not contain the strings, it adds them.
59   void internalize(Remark &R);
60   /// Serialize the string table to a stream. It is serialized as a little
61   /// endian uint64 (the size of the table in bytes) followed by a sequence of
62   /// NULL-terminated strings, where the N-th string is the string with the ID N
63   /// in the StrTab map.
64   void serialize(raw_ostream &OS) const;
65   /// Serialize the string table to a vector. This allows users to do the actual
66   /// writing to file/memory/other.
67   /// The string with the ID == N should be the N-th element in the vector.
68   std::vector<StringRef> serialize() const;
69 };
70 
71 } // end namespace remarks
72 } // end namespace llvm
73 
74 #endif /* LLVM_REMARKS_REMARK_STRING_TABLE_H */
75