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/ADT/StringRef.h"
15 #include "llvm/Remarks/Remark.h"
16 #include "llvm/Remarks/RemarkParser.h"
17 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm::remarks
;
23 StringTable::StringTable(const ParsedStringTable
&Other
) {
24 for (unsigned i
= 0, e
= Other
.size(); i
< e
; ++i
)
25 if (Expected
<StringRef
> MaybeStr
= Other
[i
])
28 llvm_unreachable("Unexpected error while building remarks string table.");
31 std::pair
<unsigned, StringRef
> StringTable::add(StringRef Str
) {
32 size_t NextID
= StrTab
.size();
33 auto KV
= StrTab
.insert({Str
, NextID
});
34 // If it's a new string, add it to the final size.
36 SerializedSize
+= KV
.first
->first().size() + 1; // +1 for the '\0'
37 // Can be either NextID or the previous ID if the string is already there.
38 return {KV
.first
->second
, KV
.first
->first()};
41 void StringTable::internalize(Remark
&R
) {
42 auto Impl
= [&](StringRef
&S
) { S
= add(S
).second
; };
47 Impl(R
.Loc
->SourceFilePath
);
48 for (Argument
&Arg
: R
.Args
) {
52 Impl(Arg
.Loc
->SourceFilePath
);
56 void StringTable::serialize(raw_ostream
&OS
) const {
57 // Emit the sequence of strings.
58 for (StringRef Str
: serialize()) {
60 // Explicitly emit a '\0'.
65 std::vector
<StringRef
> StringTable::serialize() const {
66 std::vector
<StringRef
> Strings
{StrTab
.size()};
67 for (const auto &KV
: StrTab
)
68 Strings
[KV
.second
] = KV
.first();