1 //===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines the structure of a YAML document for serializing
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
17 #define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
19 #include "clang/Tooling/Core/Diagnostic.h"
20 #include "clang/Tooling/ReplacementsYaml.h"
21 #include "llvm/Support/YAMLTraits.h"
24 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
29 template <> struct MappingTraits<clang::tooling::Diagnostic> {
30 /// Helper to (de)serialize a Diagnostic since we don't have direct
31 /// access to its data members.
32 class NormalizedDiagnostic {
34 NormalizedDiagnostic(const IO &)
35 : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
37 NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
38 : DiagnosticName(D.DiagnosticName), Message(D.Message), Fix(D.Fix),
39 Notes(D.Notes), DiagLevel(D.DiagLevel),
40 BuildDirectory(D.BuildDirectory) {}
42 clang::tooling::Diagnostic denormalize(const IO &) {
43 return clang::tooling::Diagnostic(DiagnosticName, Message, Fix, Notes,
44 DiagLevel, BuildDirectory);
47 std::string DiagnosticName;
48 clang::tooling::DiagnosticMessage Message;
49 llvm::StringMap<clang::tooling::Replacements> Fix;
50 SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
51 clang::tooling::Diagnostic::Level DiagLevel;
52 std::string BuildDirectory;
55 static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
56 MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
58 Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
59 Io.mapRequired("Message", Keys->Message.Message);
60 Io.mapRequired("FileOffset", Keys->Message.FileOffset);
61 Io.mapRequired("FilePath", Keys->Message.FilePath);
63 // FIXME: Export properly all the different fields.
65 std::vector<clang::tooling::Replacement> Fixes;
66 for (auto &Replacements : Keys->Fix) {
67 for (auto &Replacement : Replacements.second) {
68 Fixes.push_back(Replacement);
71 Io.mapRequired("Replacements", Fixes);
72 for (auto &Fix : Fixes) {
73 llvm::Error Err = Keys->Fix[Fix.getFilePath()].add(Fix);
75 // FIXME: Implement better conflict handling.
76 llvm::errs() << "Fix conflicts with existing fix: "
77 << llvm::toString(std::move(Err)) << "\n";
83 /// Specialized MappingTraits to describe how a
84 /// TranslationUnitDiagnostics is (de)serialized.
85 template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
86 static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
87 Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
88 Io.mapRequired("Diagnostics", Doc.Diagnostics);
91 } // end namespace yaml
92 } // end namespace llvm
94 #endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H