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/Remarks/RemarkParser.h" 15 #include "llvm/Support/EndianStream.h" 16 #include "llvm/Support/Error.h" 17 #include <vector> 18 19 using namespace llvm; 20 using namespace llvm::remarks; 21 22 StringTable::StringTable(const ParsedStringTable &Other) : StrTab() { 23 for (unsigned i = 0, e = Other.size(); i < e; ++i) 24 if (Expected<StringRef> MaybeStr = Other[i]) 25 add(*MaybeStr); 26 else 27 llvm_unreachable("Unexpected error while building remarks string table."); 28 } 29 30 std::pair<unsigned, StringRef> StringTable::add(StringRef Str) { 31 size_t NextID = StrTab.size(); 32 auto KV = StrTab.insert({Str, NextID}); 33 // If it's a new string, add it to the final size. 34 if (KV.second) 35 SerializedSize += KV.first->first().size() + 1; // +1 for the '\0' 36 // Can be either NextID or the previous ID if the string is already there. 37 return {KV.first->second, KV.first->first()}; 38 } 39 40 void StringTable::serialize(raw_ostream &OS) const { 41 // Emit the sequence of strings. 42 for (StringRef Str : serialize()) { 43 OS << Str; 44 // Explicitly emit a '\0'. 45 OS.write('\0'); 46 } 47 } 48 49 std::vector<StringRef> StringTable::serialize() const { 50 std::vector<StringRef> Strings{StrTab.size()}; 51 for (const auto &KV : StrTab) 52 Strings[KV.second] = KV.first(); 53 return Strings; 54 } 55