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
23LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
24LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage)
25LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::FileByteRange)
26
27namespace llvm {
28namespace yaml {
29
30template <> struct MappingTraits<clang::tooling::FileByteRange> {
31 static void mapping(IO &Io, clang::tooling::FileByteRange &R) {
32 Io.mapRequired(Key: "FilePath", Val&: R.FilePath);
33 Io.mapRequired(Key: "FileOffset", Val&: R.FileOffset);
34 Io.mapRequired(Key: "Length", Val&: R.Length);
35 }
36};
37
38template <> struct MappingTraits<clang::tooling::DiagnosticMessage> {
39 static void mapping(IO &Io, clang::tooling::DiagnosticMessage &M) {
40 Io.mapRequired(Key: "Message", Val&: M.Message);
41 Io.mapOptional(Key: "FilePath", Val&: M.FilePath);
42 Io.mapOptional(Key: "FileOffset", Val&: M.FileOffset);
43 std::vector<clang::tooling::Replacement> Fixes;
44 for (auto &Replacements : M.Fix) {
45 llvm::append_range(C&: Fixes, R&: Replacements.second);
46 }
47 Io.mapRequired(Key: "Replacements", Val&: Fixes);
48 for (auto &Fix : Fixes) {
49 llvm::Error Err = M.Fix[Fix.getFilePath()].add(R: Fix);
50 if (Err) {
51 // FIXME: Implement better conflict handling.
52 llvm::errs() << "Fix conflicts with existing fix: "
53 << llvm::toString(E: std::move(Err)) << "\n";
54 }
55 }
56 Io.mapOptional(Key: "Ranges", Val&: M.Ranges);
57 }
58};
59
60template <> struct MappingTraits<clang::tooling::Diagnostic> {
61 /// Helper to (de)serialize a Diagnostic since we don't have direct
62 /// access to its data members.
63 class NormalizedDiagnostic {
64 public:
65 NormalizedDiagnostic(const IO &)
66 : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
67
68 NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
69 : DiagnosticName(D.DiagnosticName), Message(D.Message), Notes(D.Notes),
70 DiagLevel(D.DiagLevel), BuildDirectory(D.BuildDirectory) {}
71
72 clang::tooling::Diagnostic denormalize(const IO &) {
73 return clang::tooling::Diagnostic(DiagnosticName, Message, Notes,
74 DiagLevel, BuildDirectory);
75 }
76
77 std::string DiagnosticName;
78 clang::tooling::DiagnosticMessage Message;
79 SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
80 clang::tooling::Diagnostic::Level DiagLevel;
81 std::string BuildDirectory;
82 };
83
84 static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
85 MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
86 Io, D);
87 Io.mapRequired(Key: "DiagnosticName", Val&: Keys->DiagnosticName);
88 Io.mapRequired(Key: "DiagnosticMessage", Val&: Keys->Message);
89 Io.mapOptional(Key: "Notes", Val&: Keys->Notes);
90 Io.mapOptional(Key: "Level", Val&: Keys->DiagLevel);
91 Io.mapOptional(Key: "BuildDirectory", Val&: Keys->BuildDirectory);
92 }
93};
94
95/// Specialized MappingTraits to describe how a
96/// TranslationUnitDiagnostics is (de)serialized.
97template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
98 static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
99 Io.mapRequired(Key: "MainSourceFile", Val&: Doc.MainSourceFile);
100 Io.mapRequired(Key: "Diagnostics", Val&: Doc.Diagnostics);
101 }
102};
103
104template <> struct ScalarEnumerationTraits<clang::tooling::Diagnostic::Level> {
105 static void enumeration(IO &IO, clang::tooling::Diagnostic::Level &Value) {
106 IO.enumCase(Val&: Value, Str: "Warning", ConstVal: clang::tooling::Diagnostic::Warning);
107 IO.enumCase(Val&: Value, Str: "Error", ConstVal: clang::tooling::Diagnostic::Error);
108 IO.enumCase(Val&: Value, Str: "Remark", ConstVal: clang::tooling::Diagnostic::Remark);
109 }
110};
111
112} // end namespace yaml
113} // end namespace llvm
114
115#endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
116

source code of clang/include/clang/Tooling/DiagnosticsYaml.h