1 //===- RemarkStringTable.cpp ----------------------------------------------===// 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 // Implementation of the Remark string table used at remark generation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Remarks/RemarkStringTable.h" 14 #include "llvm/Support/EndianStream.h" 15 #include "llvm/Support/Error.h" 16 #include <vector> 17 18 using namespace llvm; 19 using namespace llvm::remarks; 20 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. 25 if (KV.second) 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()}; 29 } 30 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()) { 37 OS << Str; 38 // Explicitly emit a '\0'. 39 OS.write('\0'); 40 } 41 } 42 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(); 47 return Strings; 48 } 49