llvm-project
81 строка · 3.0 Кб
1//===- SymbolRemappingReader.cpp - Read symbol remapping file -------------===//
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// This file contains definitions needed for reading and applying symbol
10// remapping files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ProfileData/SymbolRemappingReader.h"15#include "llvm/ADT/StringSwitch.h"16#include "llvm/ADT/Twine.h"17#include "llvm/Support/LineIterator.h"18#include "llvm/Support/MemoryBuffer.h"19
20using namespace llvm;21
22char SymbolRemappingParseError::ID;23
24/// Load a set of name remappings from a text file.
25///
26/// See the documentation at the top of the file for an explanation of
27/// the expected format.
28Error SymbolRemappingReader::read(MemoryBuffer &B) {29line_iterator LineIt(B, /*SkipBlanks=*/true, '#');30
31auto ReportError = [&](Twine Msg) {32return llvm::make_error<SymbolRemappingParseError>(33B.getBufferIdentifier(), LineIt.line_number(), Msg);34};35
36for (; !LineIt.is_at_eof(); ++LineIt) {37StringRef Line = *LineIt;38Line = Line.ltrim(' ');39// line_iterator only detects comments starting in column 1.40if (Line.starts_with("#") || Line.empty())41continue;42
43SmallVector<StringRef, 4> Parts;44Line.split(Parts, ' ', /*MaxSplits*/-1, /*KeepEmpty*/false);45
46if (Parts.size() != 3)47return ReportError("Expected 'kind mangled_name mangled_name', "48"found '" + Line + "'");49
50using FK = ItaniumManglingCanonicalizer::FragmentKind;51std::optional<FK> FragmentKind = StringSwitch<std::optional<FK>>(Parts[0])52.Case("name", FK::Name)53.Case("type", FK::Type)54.Case("encoding", FK::Encoding)55.Default(std::nullopt);56if (!FragmentKind)57return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"58" found '" + Parts[0] + "'");59
60using EE = ItaniumManglingCanonicalizer::EquivalenceError;61switch (Canonicalizer.addEquivalence(*FragmentKind, Parts[1], Parts[2])) {62case EE::Success:63break;64
65case EE::ManglingAlreadyUsed:66return ReportError("Manglings '" + Parts[1] + "' and '" + Parts[2] + "' "67"have both been used in prior remappings. Move this "68"remapping earlier in the file.");69
70case EE::InvalidFirstMangling:71return ReportError("Could not demangle '" + Parts[1] + "' "72"as a <" + Parts[0] + ">; invalid mangling?");73
74case EE::InvalidSecondMangling:75return ReportError("Could not demangle '" + Parts[2] + "' "76"as a <" + Parts[0] + ">; invalid mangling?");77}78}79
80return Error::success();81}
82