1 //===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file defines the structure of a YAML document for serializing
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
16 #define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
18 #include "clang/Tooling/Core/Diagnostic.h"
19 #include "clang/Tooling/ReplacementsYaml.h"
20 #include "llvm/Support/YAMLTraits.h"
23 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
24 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage)
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);
39 Io.mapRequired("Replacements", Fixes);
40 for (auto &Fix : Fixes) {
41 llvm::Error Err = M.Fix[Fix.getFilePath()].add(Fix);
43 // FIXME: Implement better conflict handling.
44 llvm::errs() << "Fix conflicts with existing fix: "
45 << llvm::toString(std::move(Err)) << "\n";
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 {
56 NormalizedDiagnostic(const IO &)
57 : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
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) {}
63 clang::tooling::Diagnostic denormalize(const IO &) {
64 return clang::tooling::Diagnostic(DiagnosticName, Message, Notes,
65 DiagLevel, BuildDirectory);
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;
76 static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
77 MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
79 Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
80 Io.mapRequired("DiagnosticMessage", Keys->Message);
81 Io.mapOptional("Notes", Keys->Notes);
83 // FIXME: Export properly all the different fields.
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);
95 } // end namespace yaml
96 } // end namespace llvm
98 #endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H