cmMessenger.cxx 6.7 KB

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