1 | //===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===// |
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 | /// \file |
10 | /// This file defines the structure of a YAML document for serializing |
11 | /// diagnostics. |
12 | /// |
13 | //===----------------------------------------------------------------------===// |
14 | |
15 | #ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H |
16 | #define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H |
17 | |
18 | #include "clang/Tooling/Core/Diagnostic.h" |
19 | #include "clang/Tooling/ReplacementsYaml.h" |
20 | #include "llvm/Support/YAMLTraits.h" |
21 | #include <string> |
22 | |
23 | LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic) |
24 | LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage) |
25 | |
26 | namespace llvm { |
27 | namespace yaml { |
28 | |
29 | template <> struct MappingTraits<clang::tooling::DiagnosticMessage> { |
30 | static void mapping(IO &Io, clang::tooling::DiagnosticMessage &M) { |
31 | Io.mapRequired("Message", M.Message); |
32 | Io.mapOptional("FilePath", M.FilePath); |
33 | Io.mapOptional("FileOffset", M.FileOffset); |
34 | } |
35 | }; |
36 | |
37 | template <> struct MappingTraits<clang::tooling::Diagnostic> { |
38 | /// Helper to (de)serialize a Diagnostic since we don't have direct |
39 | /// access to its data members. |
40 | class NormalizedDiagnostic { |
41 | public: |
42 | NormalizedDiagnostic(const IO &) |
43 | : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {} |
44 | |
45 | NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D) |
46 | : DiagnosticName(D.DiagnosticName), Message(D.Message), Fix(D.Fix), |
47 | Notes(D.Notes), DiagLevel(D.DiagLevel), |
48 | BuildDirectory(D.BuildDirectory) {} |
49 | |
50 | clang::tooling::Diagnostic denormalize(const IO &) { |
51 | return clang::tooling::Diagnostic(DiagnosticName, Message, Fix, Notes, |
52 | DiagLevel, BuildDirectory); |
53 | } |
54 | |
55 | std::string DiagnosticName; |
56 | clang::tooling::DiagnosticMessage Message; |
57 | llvm::StringMap<clang::tooling::Replacements> Fix; |
58 | SmallVector<clang::tooling::DiagnosticMessage, 1> Notes; |
59 | clang::tooling::Diagnostic::Level DiagLevel; |
60 | std::string BuildDirectory; |
61 | }; |
62 | |
63 | static void mapping(IO &Io, clang::tooling::Diagnostic &D) { |
64 | MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys( |
65 | Io, D); |
66 | Io.mapRequired("DiagnosticName", Keys->DiagnosticName); |
67 | Io.mapRequired("Message", Keys->Message.Message); |
68 | Io.mapRequired("FileOffset", Keys->Message.FileOffset); |
69 | Io.mapRequired("FilePath", Keys->Message.FilePath); |
70 | Io.mapOptional("Notes", Keys->Notes); |
71 | |
72 | // FIXME: Export properly all the different fields. |
73 | |
74 | std::vector<clang::tooling::Replacement> Fixes; |
75 | for (auto &Replacements : Keys->Fix) { |
76 | for (auto &Replacement : Replacements.second) { |
77 | Fixes.push_back(Replacement); |
78 | } |
79 | } |
80 | Io.mapRequired("Replacements", Fixes); |
81 | for (auto &Fix : Fixes) { |
82 | llvm::Error Err = Keys->Fix[Fix.getFilePath()].add(Fix); |
83 | if (Err) { |
84 | // FIXME: Implement better conflict handling. |
85 | llvm::errs() << "Fix conflicts with existing fix: " |
86 | << llvm::toString(std::move(Err)) << "\n"; |
87 | } |
88 | } |
89 | } |
90 | }; |
91 | |
92 | /// Specialized MappingTraits to describe how a |
93 | /// TranslationUnitDiagnostics is (de)serialized. |
94 | template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> { |
95 | static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) { |
96 | Io.mapRequired("MainSourceFile", Doc.MainSourceFile); |
97 | Io.mapRequired("Diagnostics", Doc.Diagnostics); |
98 | } |
99 | }; |
100 | } // end namespace yaml |
101 | } // end namespace llvm |
102 | |
103 | #endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H |
104 | |