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