| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 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" | 
| Francis Visoiu Mistrih | e14c0c5 | 2019-09-06 17:22:51 +0000 | [diff] [blame^] | 14 | #include "llvm/Remarks/Remark.h" | 
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 15 | #include "llvm/Remarks/RemarkParser.h" | 
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 16 | #include "llvm/Support/EndianStream.h" | 
|  | 17 | #include "llvm/Support/Error.h" | 
|  | 18 | #include <vector> | 
|  | 19 |  | 
|  | 20 | using namespace llvm; | 
|  | 21 | using namespace llvm::remarks; | 
|  | 22 |  | 
| Francis Visoiu Mistrih | 4287c95 | 2019-07-23 22:50:08 +0000 | [diff] [blame] | 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 |  | 
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 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 |  | 
| Francis Visoiu Mistrih | e14c0c5 | 2019-09-06 17:22:51 +0000 | [diff] [blame^] | 41 | void StringTable::internalize(Remark &R) { | 
|  | 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 | // We need to mutate elements from an ArrayRef here. | 
|  | 50 | Impl(Arg.Key); | 
|  | 51 | Impl(Arg.Val); | 
|  | 52 | if (Arg.Loc) | 
|  | 53 | Impl(Arg.Loc->SourceFilePath); | 
|  | 54 | } | 
|  | 55 | } | 
|  | 56 |  | 
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 57 | void StringTable::serialize(raw_ostream &OS) const { | 
| Francis Visoiu Mistrih | 7fee2b8 | 2019-04-24 00:06:24 +0000 | [diff] [blame] | 58 | // Emit the sequence of strings. | 
|  | 59 | for (StringRef Str : serialize()) { | 
|  | 60 | OS << Str; | 
|  | 61 | // Explicitly emit a '\0'. | 
|  | 62 | OS.write('\0'); | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | std::vector<StringRef> StringTable::serialize() const { | 
|  | 67 | std::vector<StringRef> Strings{StrTab.size()}; | 
|  | 68 | for (const auto &KV : StrTab) | 
|  | 69 | Strings[KV.second] = KV.first(); | 
|  | 70 | return Strings; | 
|  | 71 | } |