cmMessenger.cxx 6.5 KB

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