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