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/Remarks/RemarkParser.h"
15 #include "llvm/Support/EndianStream.h"
16 #include "llvm/Support/Error.h"
20 using namespace llvm::remarks
;
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
])
27 llvm_unreachable("Unexpected error while building remarks string table.");
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.
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()};
40 void StringTable::serialize(raw_ostream
&OS
) const {
41 // Emit the sequence of strings.
42 for (StringRef Str
: serialize()) {
44 // Explicitly emit a '\0'.
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();