Path: blob/main/contrib/llvm-project/llvm/lib/Remarks/RemarkStringTable.cpp
35262 views
//===- RemarkStringTable.cpp ----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Implementation of the Remark string table used at remark generation.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Remarks/RemarkStringTable.h"13#include "llvm/ADT/StringRef.h"14#include "llvm/Remarks/Remark.h"15#include "llvm/Remarks/RemarkParser.h"16#include "llvm/Support/raw_ostream.h"17#include <vector>1819using namespace llvm;20using namespace llvm::remarks;2122StringTable::StringTable(const ParsedStringTable &Other) {23for (unsigned i = 0, e = Other.size(); i < e; ++i)24if (Expected<StringRef> MaybeStr = Other[i])25add(*MaybeStr);26else27llvm_unreachable("Unexpected error while building remarks string table.");28}2930std::pair<unsigned, StringRef> StringTable::add(StringRef Str) {31size_t NextID = StrTab.size();32auto KV = StrTab.insert({Str, NextID});33// If it's a new string, add it to the final size.34if (KV.second)35SerializedSize += KV.first->first().size() + 1; // +1 for the '\0'36// Can be either NextID or the previous ID if the string is already there.37return {KV.first->second, KV.first->first()};38}3940void StringTable::internalize(Remark &R) {41auto Impl = [&](StringRef &S) { S = add(S).second; };42Impl(R.PassName);43Impl(R.RemarkName);44Impl(R.FunctionName);45if (R.Loc)46Impl(R.Loc->SourceFilePath);47for (Argument &Arg : R.Args) {48Impl(Arg.Key);49Impl(Arg.Val);50if (Arg.Loc)51Impl(Arg.Loc->SourceFilePath);52}53}5455void StringTable::serialize(raw_ostream &OS) const {56// Emit the sequence of strings.57for (StringRef Str : serialize()) {58OS << Str;59// Explicitly emit a '\0'.60OS.write('\0');61}62}6364std::vector<StringRef> StringTable::serialize() const {65std::vector<StringRef> Strings{StrTab.size()};66for (const auto &KV : StrTab)67Strings[KV.second] = KV.first();68return Strings;69}707172