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.

210 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. default: break;
  65. };
  66. res << mainFile << ": ";
  67. res << message << "\n";
  68. return res;
  69. }
  70. ValueTree toValueTree() const
  71. {
  72. ValueTree v (MessageTypes::DIAGNOSTIC);
  73. v.setProperty (Ids::text, message, nullptr);
  74. v.setProperty (Ids::file, mainFile, nullptr);
  75. v.setProperty (Ids::range, range.toString(), nullptr);
  76. v.setProperty (Ids::type, (int) type, nullptr);
  77. if (associatedDiagnostic != nullptr)
  78. v.addChild (associatedDiagnostic->toValueTree(), 0, nullptr);
  79. return v;
  80. }
  81. static DiagnosticMessage fromValueTree (const ValueTree& v)
  82. {
  83. DiagnosticMessage d;
  84. d.message = v[Ids::text];
  85. d.mainFile = v[Ids::file];
  86. d.range = SourceCodeRange (v [Ids::range]);
  87. d.type = (Type) static_cast<int> (v[Ids::type]);
  88. auto associated = v.getChild (0);
  89. if (associated.isValid())
  90. d.associatedDiagnostic.reset (new DiagnosticMessage (fromValueTree (associated)));
  91. return d;
  92. }
  93. bool operator== (const DiagnosticMessage& other) const noexcept
  94. {
  95. return range == other.range
  96. && message == other.message
  97. && mainFile == other.mainFile;
  98. }
  99. bool operator!= (const DiagnosticMessage& other) const noexcept { return ! operator== (other); }
  100. };
  101. //==============================================================================
  102. struct DiagnosticList
  103. {
  104. // after some research, it seems that notes never come on their own
  105. // i.e. they always have a warning / error preceding them
  106. // so we can keep notes and their associated diagnostics
  107. // together by keeping track of the last message
  108. DiagnosticMessage lastMessage;
  109. ValueTree list { MessageTypes::DIAGNOSTIC_LIST };
  110. void clear()
  111. {
  112. list = ValueTree { MessageTypes::DIAGNOSTIC_LIST };
  113. lastMessage = DiagnosticMessage();
  114. }
  115. void add (DiagnosticMessage m)
  116. {
  117. if (m.isNote())
  118. {
  119. if (lastMessage.message.isEmpty())
  120. return; // seems to happen sometimes, but with seemingly duplicated messages (?)
  121. m.associatedDiagnostic.reset (new DiagnosticMessage (lastMessage));
  122. }
  123. else
  124. {
  125. lastMessage = m;
  126. }
  127. list.appendChild (m.toValueTree(), nullptr);
  128. }
  129. void add (const DiagnosticList& l)
  130. {
  131. jassert (l.list != list);
  132. for (int i = 0; i < l.list.getNumChildren(); ++i)
  133. list.appendChild (l.list.getChild(i).createCopy(), nullptr);
  134. }
  135. void remove (DiagnosticMessage m)
  136. {
  137. auto n = m.toValueTree();
  138. for (int i = 0; i < list.getNumChildren(); ++i)
  139. {
  140. if (list.getChild (i).isEquivalentTo (n))
  141. {
  142. list.removeChild (i, nullptr);
  143. return;
  144. }
  145. }
  146. jassertfalse;
  147. }
  148. bool hasRecoveryWarning (DiagnosticMessage m) const
  149. {
  150. auto n = m.toValueTree();
  151. for (int i = 0; i < list.getNumChildren(); ++i)
  152. if (list.getChild (i).isEquivalentTo (n))
  153. return true;
  154. return false;
  155. }
  156. const ValueTree& toValueTree() const noexcept
  157. {
  158. return list;
  159. }
  160. void loadFromChildOfValueTree (ValueTree& parent)
  161. {
  162. list = parent.getChildWithName (MessageTypes::DIAGNOSTIC_LIST).createCopy();
  163. }
  164. };