LLVM  14.0.0git
RemarkStringTable.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Remarks/Remark.h"
18 #include <vector>
19 
20 using namespace llvm;
21 using namespace llvm::remarks;
22 
23 StringTable::StringTable(const ParsedStringTable &Other) : StrTab() {
24  for (unsigned i = 0, e = Other.size(); i < e; ++i)
25  if (Expected<StringRef> MaybeStr = Other[i])
26  add(*MaybeStr);
27  else
28  llvm_unreachable("Unexpected error while building remarks string table.");
29 }
30 
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.
35  if (KV.second)
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()};
39 }
40 
42  auto Impl = [&](StringRef &S) { S = add(S).second; };
43  Impl(R.PassName);
44  Impl(R.RemarkName);
45  Impl(R.FunctionName);
46  if (R.Loc)
47  Impl(R.Loc->SourceFilePath);
48  for (Argument &Arg : R.Args) {
49  Impl(Arg.Key);
50  Impl(Arg.Val);
51  if (Arg.Loc)
52  Impl(Arg.Loc->SourceFilePath);
53  }
54 }
55 
57  // Emit the sequence of strings.
58  for (StringRef Str : serialize()) {
59  OS << Str;
60  // Explicitly emit a '\0'.
61  OS.write('\0');
62  }
63 }
64 
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();
69  return Strings;
70 }
i
i
Definition: README.txt:29
llvm::remarks::StringTable::internalize
void internalize(Remark &R)
Modify R to use strings from this string table.
Definition: RemarkStringTable.cpp:41
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::remarks::StringTable::StrTab
StringMap< unsigned, BumpPtrAllocator > StrTab
The string table containing all the unique strings used in the output.
Definition: RemarkStringTable.h:39
StringRef.h
RemarkStringTable.h
RemarkParser.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::remarks::StringTable::serialize
std::vector< StringRef > serialize() const
Serialize the string table to a vector.
Definition: RemarkStringTable.cpp:65
Remark.h
llvm::raw_ostream::write
raw_ostream & write(unsigned char C)
Definition: raw_ostream.cpp:220
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
llvm::remarks::StringTable::StringTable
StringTable()=default
llvm::remarks
Definition: AsmPrinter.h:77
llvm::remarks::StringTable::SerializedSize
size_t SerializedSize
Total size of the string table when serialized.
Definition: RemarkStringTable.h:41
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::remarks::ParsedStringTable
In-memory representation of the string table parsed from a buffer (e.g.
Definition: RemarkParser.h:59
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::remarks::Argument
A key-value pair with a debug location that is used to display the remarks at the right place in the ...
Definition: Remark.h:42
llvm::remarks::StringTable::add
std::pair< unsigned, StringRef > add(StringRef Str)
Add a string to the table. It returns an unique ID of the string.
Definition: RemarkStringTable.cpp:31
raw_ostream.h
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172