cmMessenger.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMessenger.h"
  4. #include "cmDocumentationFormatter.h"
  5. #include "cmMessageMetadata.h"
  6. #include "cmStringAlgorithms.h"
  7. #include "cmSystemTools.h"
  8. #if !defined(CMAKE_BOOTSTRAP)
  9. # include "cmsys/SystemInformation.hxx"
  10. #endif
  11. #include <sstream>
  12. #include <utility>
  13. #include "cmsys/Terminal.h"
  14. #ifdef CMake_ENABLE_DEBUGGER
  15. # include "cmDebuggerAdapter.h"
  16. #endif
  17. MessageType cmMessenger::ConvertMessageType(MessageType t) const
  18. {
  19. if (t == MessageType::AUTHOR_WARNING || t == MessageType::AUTHOR_ERROR) {
  20. if (this->GetDevWarningsAsErrors()) {
  21. return MessageType::AUTHOR_ERROR;
  22. }
  23. return MessageType::AUTHOR_WARNING;
  24. }
  25. if (t == MessageType::DEPRECATION_WARNING ||
  26. t == MessageType::DEPRECATION_ERROR) {
  27. if (this->GetDeprecatedWarningsAsErrors()) {
  28. return MessageType::DEPRECATION_ERROR;
  29. }
  30. return MessageType::DEPRECATION_WARNING;
  31. }
  32. return t;
  33. }
  34. bool cmMessenger::IsMessageTypeVisible(MessageType t) const
  35. {
  36. if (t == MessageType::DEPRECATION_ERROR) {
  37. return this->GetDeprecatedWarningsAsErrors();
  38. }
  39. if (t == MessageType::DEPRECATION_WARNING) {
  40. return !this->GetSuppressDeprecatedWarnings();
  41. }
  42. if (t == MessageType::AUTHOR_ERROR) {
  43. return this->GetDevWarningsAsErrors();
  44. }
  45. if (t == MessageType::AUTHOR_WARNING) {
  46. return !this->GetSuppressDevWarnings();
  47. }
  48. return true;
  49. }
  50. static bool printMessagePreamble(MessageType t, std::ostream& msg)
  51. {
  52. // Construct the message header.
  53. if (t == MessageType::FATAL_ERROR) {
  54. msg << "CMake Error";
  55. } else if (t == MessageType::INTERNAL_ERROR) {
  56. msg << "CMake Internal Error (please report a bug)";
  57. } else if (t == MessageType::LOG) {
  58. msg << "CMake Debug Log";
  59. } else if (t == MessageType::DEPRECATION_ERROR) {
  60. msg << "CMake Deprecation Error";
  61. } else if (t == MessageType::DEPRECATION_WARNING) {
  62. msg << "CMake Deprecation Warning";
  63. } else if (t == MessageType::AUTHOR_WARNING) {
  64. msg << "CMake Warning (dev)";
  65. } else if (t == MessageType::AUTHOR_ERROR) {
  66. msg << "CMake Error (dev)";
  67. } else {
  68. msg << "CMake Warning";
  69. }
  70. return true;
  71. }
  72. static int getMessageColor(MessageType t)
  73. {
  74. switch (t) {
  75. case MessageType::INTERNAL_ERROR:
  76. case MessageType::FATAL_ERROR:
  77. case MessageType::AUTHOR_ERROR:
  78. return cmsysTerminal_Color_ForegroundRed;
  79. case MessageType::AUTHOR_WARNING:
  80. case MessageType::WARNING:
  81. return cmsysTerminal_Color_ForegroundYellow;
  82. default:
  83. return cmsysTerminal_Color_Normal;
  84. }
  85. }
  86. static void printMessageText(std::ostream& msg, std::string const& text)
  87. {
  88. msg << ":\n";
  89. cmDocumentationFormatter formatter;
  90. formatter.SetIndent(2u);
  91. formatter.PrintFormatted(msg, text);
  92. }
  93. static void displayMessage(MessageType t, std::ostringstream& msg)
  94. {
  95. // Add a note about warning suppression.
  96. if (t == MessageType::AUTHOR_WARNING) {
  97. msg << "This warning is for project developers. Use -Wno-dev to suppress "
  98. "it.";
  99. } else if (t == MessageType::AUTHOR_ERROR) {
  100. msg << "This error is for project developers. Use -Wno-error=dev to "
  101. "suppress it.";
  102. }
  103. // Add a terminating blank line.
  104. msg << "\n";
  105. #if !defined(CMAKE_BOOTSTRAP)
  106. // Add a C++ stack trace to internal errors.
  107. if (t == MessageType::INTERNAL_ERROR) {
  108. std::string stack = cmsys::SystemInformation::GetProgramStack(0, 0);
  109. if (!stack.empty()) {
  110. if (cmHasLiteralPrefix(stack, "WARNING:")) {
  111. stack = "Note:" + stack.substr(8);
  112. }
  113. msg << stack << "\n";
  114. }
  115. }
  116. #endif
  117. // Output the message.
  118. cmMessageMetadata md;
  119. md.desiredColor = getMessageColor(t);
  120. if (t == MessageType::FATAL_ERROR || t == MessageType::INTERNAL_ERROR ||
  121. t == MessageType::DEPRECATION_ERROR || t == MessageType::AUTHOR_ERROR) {
  122. cmSystemTools::SetErrorOccurred();
  123. md.title = "Error";
  124. cmSystemTools::Message(msg.str(), md);
  125. } else {
  126. md.title = "Warning";
  127. cmSystemTools::Message(msg.str(), md);
  128. }
  129. }
  130. namespace {
  131. void PrintCallStack(std::ostream& out, cmListFileBacktrace bt,
  132. cm::optional<std::string> const& topSource)
  133. {
  134. // The call stack exists only if we have at least two calls on top
  135. // of the bottom.
  136. if (bt.Empty()) {
  137. return;
  138. }
  139. bt = bt.Pop();
  140. if (bt.Empty()) {
  141. return;
  142. }
  143. bool first = true;
  144. for (; !bt.Empty(); bt = bt.Pop()) {
  145. cmListFileContext lfc = bt.Top();
  146. if (lfc.Name.empty() &&
  147. lfc.Line != cmListFileContext::DeferPlaceholderLine) {
  148. // Skip this whole-file scope. When we get here we already will
  149. // have printed a more-specific context within the file.
  150. continue;
  151. }
  152. if (first) {
  153. first = false;
  154. out << "Call Stack (most recent call first):\n";
  155. }
  156. if (topSource) {
  157. lfc.FilePath = cmSystemTools::RelativeIfUnder(*topSource, lfc.FilePath);
  158. }
  159. out << " " << lfc << "\n";
  160. }
  161. }
  162. }
  163. void cmMessenger::IssueMessage(MessageType t, const std::string& text,
  164. const cmListFileBacktrace& backtrace) const
  165. {
  166. bool force = false;
  167. // override the message type, if needed, for warnings and errors
  168. MessageType override = this->ConvertMessageType(t);
  169. if (override != t) {
  170. t = override;
  171. force = true;
  172. }
  173. if (force || this->IsMessageTypeVisible(t)) {
  174. this->DisplayMessage(t, text, backtrace);
  175. }
  176. }
  177. void cmMessenger::DisplayMessage(MessageType t, const std::string& text,
  178. const cmListFileBacktrace& backtrace) const
  179. {
  180. std::ostringstream msg;
  181. if (!printMessagePreamble(t, msg)) {
  182. return;
  183. }
  184. // Add the immediate context.
  185. this->PrintBacktraceTitle(msg, backtrace);
  186. printMessageText(msg, text);
  187. // Add the rest of the context.
  188. PrintCallStack(msg, backtrace, this->TopSource);
  189. displayMessage(t, msg);
  190. #ifdef CMake_ENABLE_DEBUGGER
  191. if (DebuggerAdapter != nullptr) {
  192. DebuggerAdapter->OnMessageOutput(t, msg.str());
  193. }
  194. #endif
  195. }
  196. void cmMessenger::PrintBacktraceTitle(std::ostream& out,
  197. cmListFileBacktrace const& bt) const
  198. {
  199. // The title exists only if we have a call on top of the bottom.
  200. if (bt.Empty()) {
  201. return;
  202. }
  203. cmListFileContext lfc = bt.Top();
  204. if (this->TopSource) {
  205. lfc.FilePath =
  206. cmSystemTools::RelativeIfUnder(*this->TopSource, lfc.FilePath);
  207. }
  208. out << (lfc.Line ? " at " : " in ") << lfc;
  209. }
  210. void cmMessenger::SetTopSource(cm::optional<std::string> topSource)
  211. {
  212. this->TopSource = std::move(topSource);
  213. }