Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file defines the structure of a YAML document for serializing
0011 /// diagnostics.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
0016 #define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
0017 
0018 #include "clang/Tooling/Core/Diagnostic.h"
0019 #include "clang/Tooling/ReplacementsYaml.h"
0020 #include "llvm/Support/YAMLTraits.h"
0021 #include <string>
0022 
0023 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
0024 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage)
0025 LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::FileByteRange)
0026 
0027 namespace llvm {
0028 namespace yaml {
0029 
0030 template <> struct MappingTraits<clang::tooling::FileByteRange> {
0031   static void mapping(IO &Io, clang::tooling::FileByteRange &R) {
0032     Io.mapRequired("FilePath", R.FilePath);
0033     Io.mapRequired("FileOffset", R.FileOffset);
0034     Io.mapRequired("Length", R.Length);
0035   }
0036 };
0037 
0038 template <> struct MappingTraits<clang::tooling::DiagnosticMessage> {
0039   static void mapping(IO &Io, clang::tooling::DiagnosticMessage &M) {
0040     Io.mapRequired("Message", M.Message);
0041     Io.mapOptional("FilePath", M.FilePath);
0042     Io.mapOptional("FileOffset", M.FileOffset);
0043     std::vector<clang::tooling::Replacement> Fixes;
0044     for (auto &Replacements : M.Fix) {
0045       llvm::append_range(Fixes, Replacements.second);
0046     }
0047     Io.mapRequired("Replacements", Fixes);
0048     for (auto &Fix : Fixes) {
0049       llvm::Error Err = M.Fix[Fix.getFilePath()].add(Fix);
0050       if (Err) {
0051         // FIXME: Implement better conflict handling.
0052         llvm::errs() << "Fix conflicts with existing fix: "
0053                      << llvm::toString(std::move(Err)) << "\n";
0054       }
0055     }
0056     Io.mapOptional("Ranges", M.Ranges);
0057   }
0058 };
0059 
0060 template <> struct MappingTraits<clang::tooling::Diagnostic> {
0061   /// Helper to (de)serialize a Diagnostic since we don't have direct
0062   /// access to its data members.
0063   class NormalizedDiagnostic {
0064   public:
0065     NormalizedDiagnostic(const IO &)
0066         : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
0067 
0068     NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
0069         : DiagnosticName(D.DiagnosticName), Message(D.Message), Notes(D.Notes),
0070           DiagLevel(D.DiagLevel), BuildDirectory(D.BuildDirectory) {}
0071 
0072     clang::tooling::Diagnostic denormalize(const IO &) {
0073       return clang::tooling::Diagnostic(DiagnosticName, Message, Notes,
0074                                         DiagLevel, BuildDirectory);
0075     }
0076 
0077     std::string DiagnosticName;
0078     clang::tooling::DiagnosticMessage Message;
0079     SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
0080     clang::tooling::Diagnostic::Level DiagLevel;
0081     std::string BuildDirectory;
0082   };
0083 
0084   static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
0085     MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
0086         Io, D);
0087     Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
0088     Io.mapRequired("DiagnosticMessage", Keys->Message);
0089     Io.mapOptional("Notes", Keys->Notes);
0090     Io.mapOptional("Level", Keys->DiagLevel);
0091     Io.mapOptional("BuildDirectory", Keys->BuildDirectory);
0092   }
0093 };
0094 
0095 /// Specialized MappingTraits to describe how a
0096 /// TranslationUnitDiagnostics is (de)serialized.
0097 template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
0098   static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
0099     Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
0100     Io.mapRequired("Diagnostics", Doc.Diagnostics);
0101   }
0102 };
0103 
0104 template <> struct ScalarEnumerationTraits<clang::tooling::Diagnostic::Level> {
0105   static void enumeration(IO &IO, clang::tooling::Diagnostic::Level &Value) {
0106     IO.enumCase(Value, "Warning", clang::tooling::Diagnostic::Warning);
0107     IO.enumCase(Value, "Error", clang::tooling::Diagnostic::Error);
0108     IO.enumCase(Value, "Remark", clang::tooling::Diagnostic::Remark);
0109   }
0110 };
0111 
0112 } // end namespace yaml
0113 } // end namespace llvm
0114 
0115 #endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H