10b57cec5SDimitry Andric //===- RemarkFormat.cpp --------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implementation of utilities to handle the different remark formats.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "llvm/Remarks/RemarkFormat.h"
140b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
15*480093f4SDimitry Andric #include "llvm/Remarks/BitstreamRemarkContainer.h"
160b57cec5SDimitry Andric
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric using namespace llvm::remarks;
190b57cec5SDimitry Andric
parseFormat(StringRef FormatStr)200b57cec5SDimitry Andric Expected<Format> llvm::remarks::parseFormat(StringRef FormatStr) {
210b57cec5SDimitry Andric auto Result = StringSwitch<Format>(FormatStr)
220b57cec5SDimitry Andric .Cases("", "yaml", Format::YAML)
238bcb0991SDimitry Andric .Case("yaml-strtab", Format::YAMLStrTab)
248bcb0991SDimitry Andric .Case("bitstream", Format::Bitstream)
250b57cec5SDimitry Andric .Default(Format::Unknown);
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric if (Result == Format::Unknown)
280b57cec5SDimitry Andric return createStringError(std::make_error_code(std::errc::invalid_argument),
298bcb0991SDimitry Andric "Unknown remark format: '%s'",
300b57cec5SDimitry Andric FormatStr.data());
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric return Result;
330b57cec5SDimitry Andric }
34*480093f4SDimitry Andric
magicToFormat(StringRef MagicStr)35*480093f4SDimitry Andric Expected<Format> llvm::remarks::magicToFormat(StringRef MagicStr) {
36*480093f4SDimitry Andric auto Result =
37*480093f4SDimitry Andric StringSwitch<Format>(MagicStr)
38*480093f4SDimitry Andric .StartsWith("--- ", Format::YAML) // This is only an assumption.
39*480093f4SDimitry Andric .StartsWith(remarks::Magic, Format::YAMLStrTab)
40*480093f4SDimitry Andric .StartsWith(remarks::ContainerMagic, Format::Bitstream)
41*480093f4SDimitry Andric .Default(Format::Unknown);
42*480093f4SDimitry Andric
43*480093f4SDimitry Andric if (Result == Format::Unknown)
44*480093f4SDimitry Andric return createStringError(std::make_error_code(std::errc::invalid_argument),
45*480093f4SDimitry Andric "Unknown remark magic: '%s'", MagicStr.data());
46*480093f4SDimitry Andric return Result;
47*480093f4SDimitry Andric }
48