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.

204 lines
5.7KB

  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. struct DiagnosticMessage
  20. {
  21. DiagnosticMessage() = default;
  22. DiagnosticMessage (const DiagnosticMessage& other)
  23. : associatedDiagnostic (createCopyIfNotNull (other.associatedDiagnostic.get())),
  24. message (other.message),
  25. mainFile (other.mainFile),
  26. range (other.range),
  27. type (other.type)
  28. {
  29. }
  30. DiagnosticMessage& operator= (const DiagnosticMessage& other)
  31. {
  32. associatedDiagnostic = createCopyIfNotNull (other.associatedDiagnostic.get());
  33. message = other.message;
  34. mainFile = other.mainFile;
  35. range = other.range;
  36. type = other.type;
  37. return *this;
  38. }
  39. enum Type
  40. {
  41. error = 0,
  42. warning = 1,
  43. note = 2
  44. };
  45. ScopedPointer<DiagnosticMessage> associatedDiagnostic;
  46. String message;
  47. String mainFile;
  48. SourceCodeRange range;
  49. Type type;
  50. bool isError() const noexcept { return type == error; }
  51. bool isWarning() const noexcept { return type == warning; }
  52. bool isNote() const noexcept { return type == note; }
  53. String toString() const
  54. {
  55. // todo: copy recursively from root
  56. String res;
  57. switch (type)
  58. {
  59. case error: res << "error: "; break;
  60. case warning: res << "warning: "; break;
  61. case note: res << "note: "; break;
  62. };
  63. res << mainFile << ": ";
  64. res << message << "\n";
  65. return res;
  66. }
  67. ValueTree toValueTree() const
  68. {
  69. ValueTree v (MessageTypes::DIAGNOSTIC);
  70. v.setProperty (Ids::text, message, nullptr);
  71. v.setProperty (Ids::file, mainFile, nullptr);
  72. v.setProperty (Ids::range, range.toString(), nullptr);
  73. v.setProperty (Ids::type, (int) type, nullptr);
  74. if (associatedDiagnostic != nullptr)
  75. v.addChild (associatedDiagnostic->toValueTree(), 0, nullptr);
  76. return v;
  77. }
  78. static DiagnosticMessage fromValueTree (const ValueTree& v)
  79. {
  80. DiagnosticMessage d;
  81. d.message = v[Ids::text];
  82. d.mainFile = v[Ids::file];
  83. d.range = SourceCodeRange (v [Ids::range]);
  84. d.type = (Type) static_cast<int> (v[Ids::type]);
  85. auto associated = v.getChild (0);
  86. if (associated.isValid())
  87. d.associatedDiagnostic = new DiagnosticMessage (fromValueTree (associated));
  88. return d;
  89. }
  90. bool operator== (const DiagnosticMessage& other) const noexcept
  91. {
  92. return range == other.range
  93. && message == other.message
  94. && mainFile == other.mainFile;
  95. }
  96. bool operator!= (const DiagnosticMessage& other) const noexcept { return ! operator== (other); }
  97. };
  98. //==============================================================================
  99. struct DiagnosticList
  100. {
  101. // after some research, it seems that notes never come on their own
  102. // i.e. they always have a warning / error preceding them
  103. // so we can keep notes and their associated diagnostics
  104. // together by keeping track of the last message
  105. DiagnosticMessage lastMessage;
  106. ValueTree list { MessageTypes::DIAGNOSTIC_LIST };
  107. void clear()
  108. {
  109. list = ValueTree { MessageTypes::DIAGNOSTIC_LIST };
  110. lastMessage = DiagnosticMessage();
  111. }
  112. void add (DiagnosticMessage m)
  113. {
  114. if (m.isNote())
  115. {
  116. if (lastMessage.message.isEmpty())
  117. return; // seems to happen sometimes, but with seemingly duplicated messages (?)
  118. m.associatedDiagnostic = new DiagnosticMessage (lastMessage);
  119. }
  120. else
  121. {
  122. lastMessage = m;
  123. }
  124. list.addChild (m.toValueTree(), -1, nullptr);
  125. }
  126. void add (const DiagnosticList& l)
  127. {
  128. jassert (l.list != list);
  129. for (int i = 0; i < l.list.getNumChildren(); ++i)
  130. list.addChild (l.list.getChild(i).createCopy(), -1, nullptr);
  131. }
  132. void remove (DiagnosticMessage m)
  133. {
  134. auto n = m.toValueTree();
  135. for (int i = 0; i < list.getNumChildren(); ++i)
  136. {
  137. if (list.getChild (i).isEquivalentTo (n))
  138. {
  139. list.removeChild (i, nullptr);
  140. return;
  141. }
  142. }
  143. jassertfalse;
  144. }
  145. bool hasRecoveryWarning (DiagnosticMessage m) const
  146. {
  147. auto n = m.toValueTree();
  148. for (int i = 0; i < list.getNumChildren(); ++i)
  149. if (list.getChild (i).isEquivalentTo (n))
  150. return true;
  151. return false;
  152. }
  153. const ValueTree& toValueTree() const noexcept
  154. {
  155. return list;
  156. }
  157. void loadFromChildOfValueTree (ValueTree& parent)
  158. {
  159. list = parent.getChildWithName (MessageTypes::DIAGNOSTIC_LIST).createCopy();
  160. }
  161. };