DiagnosticsYaml.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- DiagnosticsYaml.h -- Serialiazation for Diagnosticss ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the structure of a YAML document for serializing
  16. /// diagnostics.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
  20. #define LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
  21. #include "clang/Tooling/Core/Diagnostic.h"
  22. #include "clang/Tooling/ReplacementsYaml.h"
  23. #include "llvm/Support/YAMLTraits.h"
  24. #include <string>
  25. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Diagnostic)
  26. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::DiagnosticMessage)
  27. LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::FileByteRange)
  28. namespace llvm {
  29. namespace yaml {
  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. template <> struct MappingTraits<clang::tooling::DiagnosticMessage> {
  38. static void mapping(IO &Io, clang::tooling::DiagnosticMessage &M) {
  39. Io.mapRequired("Message", M.Message);
  40. Io.mapOptional("FilePath", M.FilePath);
  41. Io.mapOptional("FileOffset", M.FileOffset);
  42. std::vector<clang::tooling::Replacement> Fixes;
  43. for (auto &Replacements : M.Fix) {
  44. llvm::append_range(Fixes, Replacements.second);
  45. }
  46. Io.mapRequired("Replacements", Fixes);
  47. for (auto &Fix : Fixes) {
  48. llvm::Error Err = M.Fix[Fix.getFilePath()].add(Fix);
  49. if (Err) {
  50. // FIXME: Implement better conflict handling.
  51. llvm::errs() << "Fix conflicts with existing fix: "
  52. << llvm::toString(std::move(Err)) << "\n";
  53. }
  54. }
  55. Io.mapOptional("Ranges", M.Ranges);
  56. }
  57. };
  58. template <> struct MappingTraits<clang::tooling::Diagnostic> {
  59. /// Helper to (de)serialize a Diagnostic since we don't have direct
  60. /// access to its data members.
  61. class NormalizedDiagnostic {
  62. public:
  63. NormalizedDiagnostic(const IO &)
  64. : DiagLevel(clang::tooling::Diagnostic::Level::Warning) {}
  65. NormalizedDiagnostic(const IO &, const clang::tooling::Diagnostic &D)
  66. : DiagnosticName(D.DiagnosticName), Message(D.Message), Notes(D.Notes),
  67. DiagLevel(D.DiagLevel), BuildDirectory(D.BuildDirectory) {}
  68. clang::tooling::Diagnostic denormalize(const IO &) {
  69. return clang::tooling::Diagnostic(DiagnosticName, Message, Notes,
  70. DiagLevel, BuildDirectory);
  71. }
  72. std::string DiagnosticName;
  73. clang::tooling::DiagnosticMessage Message;
  74. SmallVector<clang::tooling::DiagnosticMessage, 1> Notes;
  75. clang::tooling::Diagnostic::Level DiagLevel;
  76. std::string BuildDirectory;
  77. };
  78. static void mapping(IO &Io, clang::tooling::Diagnostic &D) {
  79. MappingNormalization<NormalizedDiagnostic, clang::tooling::Diagnostic> Keys(
  80. Io, D);
  81. Io.mapRequired("DiagnosticName", Keys->DiagnosticName);
  82. Io.mapRequired("DiagnosticMessage", Keys->Message);
  83. Io.mapOptional("Notes", Keys->Notes);
  84. Io.mapOptional("Level", Keys->DiagLevel);
  85. Io.mapOptional("BuildDirectory", Keys->BuildDirectory);
  86. }
  87. };
  88. /// Specialized MappingTraits to describe how a
  89. /// TranslationUnitDiagnostics is (de)serialized.
  90. template <> struct MappingTraits<clang::tooling::TranslationUnitDiagnostics> {
  91. static void mapping(IO &Io, clang::tooling::TranslationUnitDiagnostics &Doc) {
  92. Io.mapRequired("MainSourceFile", Doc.MainSourceFile);
  93. Io.mapRequired("Diagnostics", Doc.Diagnostics);
  94. }
  95. };
  96. template <> struct ScalarEnumerationTraits<clang::tooling::Diagnostic::Level> {
  97. static void enumeration(IO &IO, clang::tooling::Diagnostic::Level &Value) {
  98. IO.enumCase(Value, "Warning", clang::tooling::Diagnostic::Warning);
  99. IO.enumCase(Value, "Error", clang::tooling::Diagnostic::Error);
  100. IO.enumCase(Value, "Remark", clang::tooling::Diagnostic::Remark);
  101. }
  102. };
  103. } // end namespace yaml
  104. } // end namespace llvm
  105. #endif // LLVM_CLANG_TOOLING_DIAGNOSTICSYAML_H
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif