LLVM 22.0.0git
RemarkFormat.cpp
Go to the documentation of this file.
1//===- RemarkFormat.cpp --------------------------------------------------===//
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// Implementation of utilities to handle the different remark formats.
10//
11//===----------------------------------------------------------------------===//
12
16
17using namespace llvm;
18using namespace llvm::remarks;
19
21 auto Result = StringSwitch<Format>(FormatStr)
22 .Cases("", "yaml", Format::YAML)
23 .Case("bitstream", Format::Bitstream)
25
26 if (Result == Format::Unknown)
27 return createStringError(std::make_error_code(std::errc::invalid_argument),
28 "Unknown remark format: '%s'",
29 FormatStr.data());
30
31 return Result;
32}
33
35 auto Result =
36 StringSwitch<Format>(MagicStr)
37 .StartsWith("--- ", Format::YAML) // This is only an assumption.
39 Format::YAML) // Needed for remark meta section
42
43 if (Result == Format::Unknown)
44 return createStringError(std::make_error_code(std::errc::invalid_argument),
45 "Automatic detection of remark format failed. "
46 "Unknown magic number: '%.4s'",
47 MagicStr.data());
48 return Result;
49}
50
52 StringRef MagicStr) {
53 if (Selected == Format::Unknown)
54 return createStringError(std::make_error_code(std::errc::invalid_argument),
55 "Unknown remark parser format.");
56 if (Selected != Format::Auto)
57 return Selected;
58
59 // Empty files are valid bitstream files
60 if (MagicStr.empty())
61 return Format::Bitstream;
62 return magicToFormat(MagicStr);
63}
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:151
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:148
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
LLVM_ABI Expected< Format > magicToFormat(StringRef Magic)
Parse and validate a magic number to a remark format.
constexpr StringLiteral Magic("REMARKS")
LLVM_ABI Expected< Format > parseFormat(StringRef FormatStr)
Parse and validate a string for the remark format.
Format
The format used for serializing/deserializing remarks.
LLVM_ABI Expected< Format > detectFormat(Format Selected, StringRef Magic)
Detect format based on selected format and magic number.
constexpr StringLiteral ContainerMagic("RMRK")
The magic number used for identifying remark blocks.
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305