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
6.1KB

  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 << range.file << ": ";
  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 DiagnosticReceiver
  98. {
  99. virtual ~DiagnosticReceiver() {}
  100. virtual void handleDiagnostic (const DiagnosticMessage&) = 0;
  101. virtual void handleRecoverableErrorPCH (const DiagnosticMessage& m, String pchFileName, String sourceFileName) = 0;
  102. };
  103. //==============================================================================
  104. struct DiagnosticList
  105. {
  106. // after some research, it seems that notes never come on their own
  107. // i.e. they always have a warning / error preceding them
  108. // so we can keep notes and their associated diagnostics
  109. // together by keeping track of the last message
  110. DiagnosticMessage lastMessage;
  111. ValueTree list { MessageTypes::DIAGNOSTIC_LIST };
  112. void clear()
  113. {
  114. list = ValueTree { MessageTypes::DIAGNOSTIC_LIST };
  115. lastMessage = DiagnosticMessage();
  116. }
  117. void add (DiagnosticMessage m)
  118. {
  119. if (m.isNote())
  120. {
  121. if (lastMessage.message.isEmpty())
  122. return; // seems to happen sometimes, but with seemingly duplicated messages (?)
  123. m.associatedDiagnostic = new DiagnosticMessage (lastMessage);
  124. }
  125. else
  126. {
  127. lastMessage = m;
  128. }
  129. list.addChild (m.toValueTree(), -1, nullptr);
  130. }
  131. void add (const DiagnosticList& l)
  132. {
  133. jassert (l.list != list);
  134. for (int i = 0; i < l.list.getNumChildren(); ++i)
  135. list.addChild (l.list.getChild(i).createCopy(), -1, nullptr);
  136. }
  137. void remove (DiagnosticMessage m)
  138. {
  139. auto n = m.toValueTree();
  140. for (int i = 0; i < list.getNumChildren(); ++i)
  141. {
  142. if (list.getChild (i).isEquivalentTo (n))
  143. {
  144. list.removeChild (i, nullptr);
  145. return;
  146. }
  147. }
  148. jassertfalse;
  149. }
  150. bool hasRecoveryWarning (DiagnosticMessage m) const
  151. {
  152. auto n = m.toValueTree();
  153. for (int i = 0; i < list.getNumChildren(); ++i)
  154. if (list.getChild (i).isEquivalentTo (n))
  155. return true;
  156. return false;
  157. }
  158. const ValueTree& toValueTree() const noexcept
  159. {
  160. return list;
  161. }
  162. void loadFromChildOfValueTree (ValueTree& parent)
  163. {
  164. list = parent.getChildWithName (MessageTypes::DIAGNOSTIC_LIST).createCopy();
  165. }
  166. };