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.

209 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. struct DiagnosticMessage
  22. {
  23. DiagnosticMessage() = default;
  24. DiagnosticMessage (const DiagnosticMessage& other)
  25. : associatedDiagnostic (createCopyIfNotNull (other.associatedDiagnostic.get())),
  26. message (other.message),
  27. mainFile (other.mainFile),
  28. range (other.range),
  29. type (other.type)
  30. {
  31. }
  32. DiagnosticMessage& operator= (const DiagnosticMessage& other)
  33. {
  34. associatedDiagnostic.reset (createCopyIfNotNull (other.associatedDiagnostic.get()));
  35. message = other.message;
  36. mainFile = other.mainFile;
  37. range = other.range;
  38. type = other.type;
  39. return *this;
  40. }
  41. enum Type
  42. {
  43. error = 0,
  44. warning = 1,
  45. note = 2
  46. };
  47. std::unique_ptr<DiagnosticMessage> associatedDiagnostic;
  48. String message;
  49. String mainFile;
  50. SourceCodeRange range;
  51. Type type;
  52. bool isError() const noexcept { return type == error; }
  53. bool isWarning() const noexcept { return type == warning; }
  54. bool isNote() const noexcept { return type == note; }
  55. String toString() const
  56. {
  57. // todo: copy recursively from root
  58. String res;
  59. switch (type)
  60. {
  61. case error: res << "error: "; break;
  62. case warning: res << "warning: "; break;
  63. case note: res << "note: "; break;
  64. };
  65. res << mainFile << ": ";
  66. res << message << "\n";
  67. return res;
  68. }
  69. ValueTree toValueTree() const
  70. {
  71. ValueTree v (MessageTypes::DIAGNOSTIC);
  72. v.setProperty (Ids::text, message, nullptr);
  73. v.setProperty (Ids::file, mainFile, nullptr);
  74. v.setProperty (Ids::range, range.toString(), nullptr);
  75. v.setProperty (Ids::type, (int) type, nullptr);
  76. if (associatedDiagnostic != nullptr)
  77. v.addChild (associatedDiagnostic->toValueTree(), 0, nullptr);
  78. return v;
  79. }
  80. static DiagnosticMessage fromValueTree (const ValueTree& v)
  81. {
  82. DiagnosticMessage d;
  83. d.message = v[Ids::text];
  84. d.mainFile = v[Ids::file];
  85. d.range = SourceCodeRange (v [Ids::range]);
  86. d.type = (Type) static_cast<int> (v[Ids::type]);
  87. auto associated = v.getChild (0);
  88. if (associated.isValid())
  89. d.associatedDiagnostic.reset (new DiagnosticMessage (fromValueTree (associated)));
  90. return d;
  91. }
  92. bool operator== (const DiagnosticMessage& other) const noexcept
  93. {
  94. return range == other.range
  95. && message == other.message
  96. && mainFile == other.mainFile;
  97. }
  98. bool operator!= (const DiagnosticMessage& other) const noexcept { return ! operator== (other); }
  99. };
  100. //==============================================================================
  101. struct DiagnosticList
  102. {
  103. // after some research, it seems that notes never come on their own
  104. // i.e. they always have a warning / error preceding them
  105. // so we can keep notes and their associated diagnostics
  106. // together by keeping track of the last message
  107. DiagnosticMessage lastMessage;
  108. ValueTree list { MessageTypes::DIAGNOSTIC_LIST };
  109. void clear()
  110. {
  111. list = ValueTree { MessageTypes::DIAGNOSTIC_LIST };
  112. lastMessage = DiagnosticMessage();
  113. }
  114. void add (DiagnosticMessage m)
  115. {
  116. if (m.isNote())
  117. {
  118. if (lastMessage.message.isEmpty())
  119. return; // seems to happen sometimes, but with seemingly duplicated messages (?)
  120. m.associatedDiagnostic.reset (new DiagnosticMessage (lastMessage));
  121. }
  122. else
  123. {
  124. lastMessage = m;
  125. }
  126. list.appendChild (m.toValueTree(), nullptr);
  127. }
  128. void add (const DiagnosticList& l)
  129. {
  130. jassert (l.list != list);
  131. for (int i = 0; i < l.list.getNumChildren(); ++i)
  132. list.appendChild (l.list.getChild(i).createCopy(), nullptr);
  133. }
  134. void remove (DiagnosticMessage m)
  135. {
  136. auto n = m.toValueTree();
  137. for (int i = 0; i < list.getNumChildren(); ++i)
  138. {
  139. if (list.getChild (i).isEquivalentTo (n))
  140. {
  141. list.removeChild (i, nullptr);
  142. return;
  143. }
  144. }
  145. jassertfalse;
  146. }
  147. bool hasRecoveryWarning (DiagnosticMessage m) const
  148. {
  149. auto n = m.toValueTree();
  150. for (int i = 0; i < list.getNumChildren(); ++i)
  151. if (list.getChild (i).isEquivalentTo (n))
  152. return true;
  153. return false;
  154. }
  155. const ValueTree& toValueTree() const noexcept
  156. {
  157. return list;
  158. }
  159. void loadFromChildOfValueTree (ValueTree& parent)
  160. {
  161. list = parent.getChildWithName (MessageTypes::DIAGNOSTIC_LIST).createCopy();
  162. }
  163. };