1 //===-- RemarkStringTable.h - Serializing string table ----------*- 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 // This class is used to deduplicate and serialize a string table used for
10 // generating remarks.
12 // For parsing a string table, use ParsedStringTable in RemarkParser.h
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_REMARKS_REMARK_STRING_TABLE_H
17 #define LLVM_REMARKS_REMARK_STRING_TABLE_H
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/Allocator.h"
30 struct ParsedStringTable;
33 /// The string table used for serializing remarks.
34 /// This table can be for example serialized in a section to be consumed after
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;
43 StringTable() = default;
46 StringTable(const StringTable &) = delete;
47 StringTable &operator=(const StringTable &) = delete;
48 /// Should be movable.
49 StringTable(StringTable &&) = default;
50 StringTable &operator=(StringTable &&) = default;
52 /// Construct a string table from a ParsedStringTable.
53 StringTable(const ParsedStringTable &Other);
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;
71 } // end namespace remarks
72 } // end namespace llvm
74 #endif /* LLVM_REMARKS_REMARK_STRING_TABLE_H */