DiagnosticEngine.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2024, Dan Klishch <danilklishch@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/FlyString.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/String.h>
  11. #include <LibXML/DOM/Node.h>
  12. namespace JSSpecCompiler {
  13. struct LogicalLocation : RefCounted<LogicalLocation> {
  14. String section;
  15. String step;
  16. };
  17. struct Location {
  18. StringView filename;
  19. size_t line = 0;
  20. size_t column = 0;
  21. RefPtr<LogicalLocation> logical_location;
  22. static Location global_scope() { return {}; }
  23. };
  24. class DiagnosticEngine {
  25. AK_MAKE_NONCOPYABLE(DiagnosticEngine);
  26. AK_MAKE_NONMOVABLE(DiagnosticEngine);
  27. public:
  28. DiagnosticEngine() = default;
  29. #define DEFINE_DIAGNOSTIC_FUNCTION(name_, level_) \
  30. template<typename... Parameters> \
  31. void name_(Location const& location, AK::CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters) \
  32. { \
  33. add_diagnostic({ \
  34. .location = location, \
  35. .level = DiagnosticLevel::level_, \
  36. .message = MUST(String::formatted(move(fmtstr), parameters...)), \
  37. }); \
  38. }
  39. DEFINE_DIAGNOSTIC_FUNCTION(note, Note)
  40. DEFINE_DIAGNOSTIC_FUNCTION(warn, Warning)
  41. DEFINE_DIAGNOSTIC_FUNCTION(error, Error)
  42. DEFINE_DIAGNOSTIC_FUNCTION(fatal_error, FatalError)
  43. #undef DEFINE_DIAGNOSTIC_FUNCTION
  44. bool has_fatal_errors() const;
  45. void print_diagnostics();
  46. private:
  47. enum class DiagnosticLevel {
  48. Note,
  49. Warning,
  50. Error,
  51. FatalError,
  52. };
  53. enum class UseColor {
  54. No,
  55. Yes,
  56. };
  57. struct Diagnostic {
  58. Location location;
  59. DiagnosticLevel level;
  60. String message;
  61. Vector<Diagnostic> notes;
  62. bool operator<(Diagnostic const& other) const;
  63. void format_into(StringBuilder& builder, UseColor) const;
  64. };
  65. void add_diagnostic(Diagnostic&& diagnostic);
  66. Vector<Diagnostic> m_diagnostics;
  67. bool m_has_fatal_errors = false;
  68. };
  69. }