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/ADT/StringRef.h"
21 #include "llvm/Support/Allocator.h"
22 #include <vector>
23 
24 namespace llvm {
25 
26 class raw_ostream;
27 
28 namespace remarks {
29 
30 /// The string table used for serializing remarks.
31 /// This table can be for example serialized in a section to be consumed after
32 /// the compilation.
33 struct StringTable {
34   /// Allocator holding all the memory used by the map.
35   BumpPtrAllocator Allocator;
36   /// The string table containing all the unique strings used in the output.
37   /// It maps a string to an unique ID.
38   StringMap<unsigned, BumpPtrAllocator &> StrTab;
39   /// Total size of the string table when serialized.
40   size_t SerializedSize = 0;
41 
42   StringTable() : Allocator(), StrTab(Allocator) {}
43   /// Add a string to the table. It returns an unique ID of the string.
44   std::pair<unsigned, StringRef> add(StringRef Str);
45   /// Serialize the string table to a stream. It is serialized as a little
46   /// endian uint64 (the size of the table in bytes) followed by a sequence of
47   /// NULL-terminated strings, where the N-th string is the string with the ID N
48   /// in the StrTab map.
49   void serialize(raw_ostream &OS) const;
50   /// Serialize the string table to a vector. This allows users to do the actual
51   /// writing to file/memory/other.
52   /// The string with the ID == N should be the N-th element in the vector.
53   std::vector<StringRef> serialize() const;
54 };
55 
56 } // end namespace remarks
57 } // end namespace llvm
58 
59 #endif /* LLVM_REMARKS_REMARK_STRING_TABLE_H */
60