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 | |
13 | #include "llvm/Remarks/RemarkFormat.h" |
14 | #include "llvm/ADT/StringSwitch.h" |
15 | #include "llvm/Remarks/BitstreamRemarkContainer.h" |
16 | |
17 | using namespace llvm; |
18 | using namespace llvm::remarks; |
19 | |
20 | Expected<Format> llvm::remarks::(StringRef FormatStr) { |
21 | auto Result = StringSwitch<Format>(FormatStr) |
22 | .Cases(S0: "" , S1: "yaml" , Value: Format::YAML) |
23 | .Case(S: "bitstream" , Value: Format::Bitstream) |
24 | .Default(Value: Format::Unknown); |
25 | |
26 | if (Result == Format::Unknown) |
27 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
28 | Fmt: "Unknown remark format: '%s'" , |
29 | Vals: FormatStr.data()); |
30 | |
31 | return Result; |
32 | } |
33 | |
34 | Expected<Format> llvm::remarks::(StringRef MagicStr) { |
35 | auto Result = |
36 | StringSwitch<Format>(MagicStr) |
37 | .StartsWith(S: "--- " , Value: Format::YAML) // This is only an assumption. |
38 | .StartsWith(S: remarks::Magic, |
39 | Value: Format::YAML) // Needed for remark meta section |
40 | .StartsWith(S: remarks::ContainerMagic, Value: Format::Bitstream) |
41 | .Default(Value: Format::Unknown); |
42 | |
43 | if (Result == Format::Unknown) |
44 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
45 | Fmt: "Automatic detection of remark format failed. " |
46 | "Unknown magic number: '%.4s'" , |
47 | Vals: MagicStr.data()); |
48 | return Result; |
49 | } |
50 | |
51 | Expected<Format> llvm::remarks::(Format Selected, |
52 | StringRef MagicStr) { |
53 | if (Selected == Format::Unknown) |
54 | return createStringError(EC: std::make_error_code(e: std::errc::invalid_argument), |
55 | S: "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 | } |
64 | |