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     std::vector<clang::tooling::Replacement> Fixes;
35     for (auto &Replacements : M.Fix) {
36       for (auto &Replacement : Replacements.second)
37         Fixes.push_back(Replacement);
38     }
39     Io.mapRequired("Replacements", Fixes);
40     for (auto &Fix : Fixes) {
41       llvm::Error Err = M.Fix[Fix.getFilePath()].add(Fix);
42       if (Err) {
43         // FIXME: Implement better conflict handling.
44         llvm::errs() << "Fix conflicts with existing fix: "
45                      << llvm::toString(std::move(Err)) << "\n";
46       }
47     }
48   }
49 };
50 
51 template <> struct MappingTraits<clang::tooling::Diagnostic> {
52   /// Helper to (de)serialize a Diagnostic since we don't have direct
53   /// access to its data members.
54   class NormalizedDiagnostic {
55   public:
56     NormalizedDiagnostic(const IO &)
57         : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
58 
59     NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
60         : DiagnosticName(D.DiagnosticName), Message(D.Message), Notes(D.Notes),
61           DiagLevel(D.DiagLevel), BuildDirectory(D.BuildDirectory) {}
62 
63     clang::tooling::Diagnostic denormalize(const IO &) {
64       return clang::tooling::Diagnostic(DiagnosticName, Message, Notes,
65                                         DiagLevel, BuildDirectory);
66     }
67 
68     std::string DiagnosticName;
69     clang::tooling::DiagnosticMessage Message;
70     llvm::StringMap<clang::tooling::Replacements> Fix;
71     SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
72     clang::tooling::Diagnostic::Level DiagLevel;
73     std::string BuildDirectory;
74   };
75 
76   static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
77     MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
78         Io, D);
79     Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
80     Io.mapRequired("DiagnosticMessage", Keys->Message);
81     Io.mapOptional("Notes", Keys->Notes);
82 
83     // FIXME: Export properly all the different fields.
84   }
85 };
86 
87 /// Specialized MappingTraits to describe how a
88 /// TranslationUnitDiagnostics is (de)serialized.
89 template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
90   static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
91     Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
92     Io.mapRequired("Diagnostics", Doc.Diagnostics);
93   }
94 };
95 } // end namespace yaml
96 } // end namespace llvm
97 
98 #endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
99