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.

192 lines
5.7KB

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