16c8fe965SSimon Pilgrim //===- SymbolRemappingReader.cpp - Read symbol remapping file -------------===//
26c8fe965SSimon Pilgrim //
36c8fe965SSimon Pilgrim // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
46c8fe965SSimon Pilgrim // See https://llvm.org/LICENSE.txt for license information.
56c8fe965SSimon Pilgrim // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66c8fe965SSimon Pilgrim //
76c8fe965SSimon Pilgrim //===----------------------------------------------------------------------===//
86c8fe965SSimon Pilgrim //
96c8fe965SSimon Pilgrim // This file contains definitions needed for reading and applying symbol
106c8fe965SSimon Pilgrim // remapping files.
116c8fe965SSimon Pilgrim //
126c8fe965SSimon Pilgrim //===----------------------------------------------------------------------===//
136c8fe965SSimon Pilgrim
146c8fe965SSimon Pilgrim #include "llvm/ProfileData/SymbolRemappingReader.h"
156c8fe965SSimon Pilgrim #include "llvm/ADT/StringSwitch.h"
166c8fe965SSimon Pilgrim #include "llvm/ADT/Twine.h"
176c8fe965SSimon Pilgrim #include "llvm/Support/LineIterator.h"
186c8fe965SSimon Pilgrim #include "llvm/Support/MemoryBuffer.h"
196c8fe965SSimon Pilgrim
206c8fe965SSimon Pilgrim using namespace llvm;
216c8fe965SSimon Pilgrim
226c8fe965SSimon Pilgrim char SymbolRemappingParseError::ID;
236c8fe965SSimon Pilgrim
246c8fe965SSimon Pilgrim /// Load a set of name remappings from a text file.
256c8fe965SSimon Pilgrim ///
266c8fe965SSimon Pilgrim /// See the documentation at the top of the file for an explanation of
276c8fe965SSimon Pilgrim /// the expected format.
read(MemoryBuffer & B)286c8fe965SSimon Pilgrim Error SymbolRemappingReader::read(MemoryBuffer &B) {
296c8fe965SSimon Pilgrim line_iterator LineIt(B, /*SkipBlanks=*/true, '#');
306c8fe965SSimon Pilgrim
316c8fe965SSimon Pilgrim auto ReportError = [&](Twine Msg) {
326c8fe965SSimon Pilgrim return llvm::make_error<SymbolRemappingParseError>(
336c8fe965SSimon Pilgrim B.getBufferIdentifier(), LineIt.line_number(), Msg);
346c8fe965SSimon Pilgrim };
356c8fe965SSimon Pilgrim
366c8fe965SSimon Pilgrim for (; !LineIt.is_at_eof(); ++LineIt) {
376c8fe965SSimon Pilgrim StringRef Line = *LineIt;
386c8fe965SSimon Pilgrim Line = Line.ltrim(' ');
396c8fe965SSimon Pilgrim // line_iterator only detects comments starting in column 1.
40*586ecdf2SKazu Hirata if (Line.starts_with("#") || Line.empty())
416c8fe965SSimon Pilgrim continue;
426c8fe965SSimon Pilgrim
436c8fe965SSimon Pilgrim SmallVector<StringRef, 4> Parts;
446c8fe965SSimon Pilgrim Line.split(Parts, ' ', /*MaxSplits*/-1, /*KeepEmpty*/false);
456c8fe965SSimon Pilgrim
466c8fe965SSimon Pilgrim if (Parts.size() != 3)
476c8fe965SSimon Pilgrim return ReportError("Expected 'kind mangled_name mangled_name', "
486c8fe965SSimon Pilgrim "found '" + Line + "'");
496c8fe965SSimon Pilgrim
506c8fe965SSimon Pilgrim using FK = ItaniumManglingCanonicalizer::FragmentKind;
516c8fe965SSimon Pilgrim std::optional<FK> FragmentKind = StringSwitch<std::optional<FK>>(Parts[0])
526c8fe965SSimon Pilgrim .Case("name", FK::Name)
536c8fe965SSimon Pilgrim .Case("type", FK::Type)
546c8fe965SSimon Pilgrim .Case("encoding", FK::Encoding)
556c8fe965SSimon Pilgrim .Default(std::nullopt);
566c8fe965SSimon Pilgrim if (!FragmentKind)
576c8fe965SSimon Pilgrim return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"
586c8fe965SSimon Pilgrim " found '" + Parts[0] + "'");
596c8fe965SSimon Pilgrim
606c8fe965SSimon Pilgrim using EE = ItaniumManglingCanonicalizer::EquivalenceError;
616c8fe965SSimon Pilgrim switch (Canonicalizer.addEquivalence(*FragmentKind, Parts[1], Parts[2])) {
626c8fe965SSimon Pilgrim case EE::Success:
636c8fe965SSimon Pilgrim break;
646c8fe965SSimon Pilgrim
656c8fe965SSimon Pilgrim case EE::ManglingAlreadyUsed:
666c8fe965SSimon Pilgrim return ReportError("Manglings '" + Parts[1] + "' and '" + Parts[2] + "' "
676c8fe965SSimon Pilgrim "have both been used in prior remappings. Move this "
686c8fe965SSimon Pilgrim "remapping earlier in the file.");
696c8fe965SSimon Pilgrim
706c8fe965SSimon Pilgrim case EE::InvalidFirstMangling:
716c8fe965SSimon Pilgrim return ReportError("Could not demangle '" + Parts[1] + "' "
726c8fe965SSimon Pilgrim "as a <" + Parts[0] + ">; invalid mangling?");
736c8fe965SSimon Pilgrim
746c8fe965SSimon Pilgrim case EE::InvalidSecondMangling:
756c8fe965SSimon Pilgrim return ReportError("Could not demangle '" + Parts[2] + "' "
766c8fe965SSimon Pilgrim "as a <" + Parts[0] + ">; invalid mangling?");
776c8fe965SSimon Pilgrim }
786c8fe965SSimon Pilgrim }
796c8fe965SSimon Pilgrim
806c8fe965SSimon Pilgrim return Error::success();
816c8fe965SSimon Pilgrim }
82