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