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.

202 lines
5.8KB

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