The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. struct DiagnosticMessage
  18. {
  19. DiagnosticMessage() = default;
  20. DiagnosticMessage (const DiagnosticMessage& other)
  21. : associatedDiagnostic (createCopyIfNotNull (other.associatedDiagnostic.get())),
  22. message (other.message),
  23. mainFile (other.mainFile),
  24. range (other.range),
  25. type (other.type)
  26. {
  27. }
  28. DiagnosticMessage& operator= (const DiagnosticMessage& other)
  29. {
  30. associatedDiagnostic = createCopyIfNotNull (other.associatedDiagnostic.get());
  31. message = other.message;
  32. mainFile = other.mainFile;
  33. range = other.range;
  34. type = other.type;
  35. return *this;
  36. }
  37. enum Type
  38. {
  39. error = 0,
  40. warning = 1,
  41. note = 2
  42. };
  43. ScopedPointer<DiagnosticMessage> associatedDiagnostic;
  44. String message;
  45. String mainFile;
  46. SourceCodeRange range;
  47. Type type;
  48. bool isError() const noexcept { return type == error; }
  49. bool isWarning() const noexcept { return type == warning; }
  50. bool isNote() const noexcept { return type == note; }
  51. ValueTree toValueTree() const
  52. {
  53. ValueTree v (MessageTypes::DIAGNOSTIC);
  54. v.setProperty (Ids::text, message, nullptr);
  55. v.setProperty (Ids::file, mainFile, nullptr);
  56. v.setProperty (Ids::range, range.toString(), nullptr);
  57. v.setProperty (Ids::type, (int) type, nullptr);
  58. if (associatedDiagnostic != nullptr)
  59. v.addChild (associatedDiagnostic->toValueTree(), 0, nullptr);
  60. return v;
  61. }
  62. static DiagnosticMessage fromValueTree (const ValueTree& v)
  63. {
  64. DiagnosticMessage d;
  65. d.message = v[Ids::text];
  66. d.mainFile = v[Ids::file];
  67. d.range = SourceCodeRange (v [Ids::range]);
  68. d.type = (Type) static_cast<int> (v[Ids::type]);
  69. auto associated = v.getChild (0);
  70. if (associated.isValid())
  71. d.associatedDiagnostic = new DiagnosticMessage (fromValueTree (associated));
  72. return d;
  73. }
  74. bool operator== (const DiagnosticMessage& other) const noexcept
  75. {
  76. return range == other.range
  77. && message == other.message
  78. && mainFile == other.mainFile;
  79. }
  80. bool operator!= (const DiagnosticMessage& other) const noexcept { return ! operator== (other); }
  81. };
  82. //==============================================================================
  83. struct DiagnosticReceiver
  84. {
  85. virtual ~DiagnosticReceiver() {}
  86. virtual void handleDiagnostic (const DiagnosticMessage&) = 0;
  87. };
  88. //==============================================================================
  89. struct DiagnosticList
  90. {
  91. // after some research, it seems that notes never come on their own
  92. // i.e. they always have a warning / error preceding them
  93. // so we can use this to keep notes and their associated notes together
  94. // by keeping track of the last message
  95. DiagnosticMessage lastMessage;
  96. ValueTree list { MessageTypes::DIAGNOSTIC_LIST };
  97. void clear()
  98. {
  99. list = ValueTree { MessageTypes::DIAGNOSTIC_LIST };
  100. lastMessage = DiagnosticMessage();
  101. }
  102. void add (DiagnosticMessage m)
  103. {
  104. if (m.isNote())
  105. {
  106. if (lastMessage.message.isEmpty())
  107. return; // seems to happen sometimes, but with seemingly duplicated messages (?)
  108. m.associatedDiagnostic = new DiagnosticMessage (lastMessage);
  109. }
  110. else
  111. {
  112. lastMessage = m;
  113. }
  114. list.addChild (m.toValueTree(), -1, nullptr);
  115. }
  116. void add (const DiagnosticList& l)
  117. {
  118. jassert (l.list != list);
  119. for (int i = 0; i < l.list.getNumChildren(); ++i)
  120. list.addChild (l.list.getChild(i).createCopy(), -1, nullptr);
  121. }
  122. const ValueTree& toValueTree() const noexcept
  123. {
  124. return list;
  125. }
  126. void loadFromChildOfValueTree (ValueTree& parent)
  127. {
  128. list = parent.getChildWithName (MessageTypes::DIAGNOSTIC_LIST).createCopy();
  129. }
  130. };