Path: blob/main/contrib/llvm-project/llvm/lib/ProfileData/SymbolRemappingReader.cpp
35233 views
//===- SymbolRemappingReader.cpp - Read symbol remapping file -------------===//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// This file contains definitions needed for reading and applying symbol9// remapping files.10//11//===----------------------------------------------------------------------===//1213#include "llvm/ProfileData/SymbolRemappingReader.h"14#include "llvm/ADT/StringSwitch.h"15#include "llvm/ADT/Twine.h"16#include "llvm/Support/LineIterator.h"17#include "llvm/Support/MemoryBuffer.h"1819using namespace llvm;2021char SymbolRemappingParseError::ID;2223/// Load a set of name remappings from a text file.24///25/// See the documentation at the top of the file for an explanation of26/// the expected format.27Error SymbolRemappingReader::read(MemoryBuffer &B) {28line_iterator LineIt(B, /*SkipBlanks=*/true, '#');2930auto ReportError = [&](Twine Msg) {31return llvm::make_error<SymbolRemappingParseError>(32B.getBufferIdentifier(), LineIt.line_number(), Msg);33};3435for (; !LineIt.is_at_eof(); ++LineIt) {36StringRef Line = *LineIt;37Line = Line.ltrim(' ');38// line_iterator only detects comments starting in column 1.39if (Line.starts_with("#") || Line.empty())40continue;4142SmallVector<StringRef, 4> Parts;43Line.split(Parts, ' ', /*MaxSplits*/-1, /*KeepEmpty*/false);4445if (Parts.size() != 3)46return ReportError("Expected 'kind mangled_name mangled_name', "47"found '" + Line + "'");4849using FK = ItaniumManglingCanonicalizer::FragmentKind;50std::optional<FK> FragmentKind = StringSwitch<std::optional<FK>>(Parts[0])51.Case("name", FK::Name)52.Case("type", FK::Type)53.Case("encoding", FK::Encoding)54.Default(std::nullopt);55if (!FragmentKind)56return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"57" found '" + Parts[0] + "'");5859using EE = ItaniumManglingCanonicalizer::EquivalenceError;60switch (Canonicalizer.addEquivalence(*FragmentKind, Parts[1], Parts[2])) {61case EE::Success:62break;6364case EE::ManglingAlreadyUsed:65return ReportError("Manglings '" + Parts[1] + "' and '" + Parts[2] + "' "66"have both been used in prior remappings. Move this "67"remapping earlier in the file.");6869case EE::InvalidFirstMangling:70return ReportError("Could not demangle '" + Parts[1] + "' "71"as a <" + Parts[0] + ">; invalid mangling?");7273case EE::InvalidSecondMangling:74return ReportError("Could not demangle '" + Parts[2] + "' "75"as a <" + Parts[0] + ">; invalid mangling?");76}77}7879return Error::success();80}818283