1 //===- RemarkStringTable.cpp ----------------------------------------------===//
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 // Implementation of the Remark string table used at remark generation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Remarks/RemarkStringTable.h"
14 #include "llvm/Support/EndianStream.h"
15 #include "llvm/Support/Error.h"
19 using namespace llvm::remarks;
21 std::pair<unsigned, StringRef> StringTable::add(StringRef Str) {
22 size_t NextID = StrTab.size();
23 auto KV = StrTab.insert({Str, NextID});
24 // If it's a new string, add it to the final size.
26 SerializedSize += KV.first->first().size() + 1; // +1 for the '\0'
27 // Can be either NextID or the previous ID if the string is already there.
28 return {KV.first->second, KV.first->first()};
31 void StringTable::serialize(raw_ostream &OS) const {
32 // Emit the number of strings.
33 uint64_t StrTabSize = SerializedSize;
34 support::endian::write(OS, StrTabSize, support::little);
35 // Emit the sequence of strings.
36 for (StringRef Str : serialize()) {
38 // Explicitly emit a '\0'.
43 std::vector<StringRef> StringTable::serialize() const {
44 std::vector<StringRef> Strings{StrTab.size()};
45 for (const auto &KV : StrTab)
46 Strings[KV.second] = KV.first();