cmMessenger.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. MessageType cmMessenger::ConvertMessageType(MessageType t) const
  13. {
  14. bool warningsAsErrors;
  15. if (t == MessageType::AUTHOR_WARNING || t == MessageType::AUTHOR_ERROR) {
  16. warningsAsErrors = this->GetDevWarningsAsErrors();
  17. if (warningsAsErrors && t == MessageType::AUTHOR_WARNING) {
  18. t = MessageType::AUTHOR_ERROR;
  19. } else if (!warningsAsErrors && t == MessageType::AUTHOR_ERROR) {
  20. t = MessageType::AUTHOR_WARNING;
  21. }
  22. } else if (t == MessageType::DEPRECATION_WARNING ||
  23. t == MessageType::DEPRECATION_ERROR) {
  24. warningsAsErrors = this->GetDeprecatedWarningsAsErrors();
  25. if (warningsAsErrors && t == MessageType::DEPRECATION_WARNING) {
  26. t = MessageType::DEPRECATION_ERROR;
  27. } else if (!warningsAsErrors && t == MessageType::DEPRECATION_ERROR) {
  28. t = MessageType::DEPRECATION_WARNING;
  29. }
  30. }
  31. return t;
  32. }
  33. bool cmMessenger::IsMessageTypeVisible(MessageType t) const
  34. {
  35. bool isVisible = true;
  36. if (t == MessageType::DEPRECATION_ERROR) {
  37. if (!this->GetDeprecatedWarningsAsErrors()) {
  38. isVisible = false;
  39. }
  40. } else if (t == MessageType::DEPRECATION_WARNING) {
  41. if (this->GetSuppressDeprecatedWarnings()) {
  42. isVisible = false;
  43. }
  44. } else if (t == MessageType::AUTHOR_ERROR) {
  45. if (!this->GetDevWarningsAsErrors()) {
  46. isVisible = false;
  47. }
  48. } else if (t == MessageType::AUTHOR_WARNING) {
  49. if (this->GetSuppressDevWarnings()) {
  50. isVisible = false;
  51. }
  52. }
  53. return isVisible;
  54. }
  55. static bool printMessagePreamble(MessageType t, std::ostream& msg)
  56. {
  57. // Construct the message header.
  58. if (t == MessageType::FATAL_ERROR) {
  59. msg << "CMake Error";
  60. } else if (t == MessageType::INTERNAL_ERROR) {
  61. msg << "CMake Internal Error (please report a bug)";
  62. } else if (t == MessageType::LOG) {
  63. msg << "CMake Debug Log";
  64. } else if (t == MessageType::DEPRECATION_ERROR) {
  65. msg << "CMake Deprecation Error";
  66. } else if (t == MessageType::DEPRECATION_WARNING) {
  67. msg << "CMake Deprecation Warning";
  68. } else if (t == MessageType::AUTHOR_WARNING) {
  69. msg << "CMake Warning (dev)";
  70. } else if (t == MessageType::AUTHOR_ERROR) {
  71. msg << "CMake Error (dev)";
  72. } else {
  73. msg << "CMake Warning";
  74. }
  75. return true;
  76. }
  77. void printMessageText(std::ostream& msg, std::string const& text)
  78. {
  79. msg << ":\n";
  80. cmDocumentationFormatter formatter;
  81. formatter.SetIndent(" ");
  82. formatter.PrintFormatted(msg, text.c_str());
  83. }
  84. void displayMessage(MessageType t, std::ostringstream& msg)
  85. {
  86. // Add a note about warning suppression.
  87. if (t == MessageType::AUTHOR_WARNING) {
  88. msg << "This warning is for project developers. Use -Wno-dev to suppress "
  89. "it.";
  90. } else if (t == MessageType::AUTHOR_ERROR) {
  91. msg << "This error is for project developers. Use -Wno-error=dev to "
  92. "suppress it.";
  93. }
  94. // Add a terminating blank line.
  95. msg << "\n";
  96. #if !defined(CMAKE_BOOTSTRAP)
  97. // Add a C++ stack trace to internal errors.
  98. if (t == MessageType::INTERNAL_ERROR) {
  99. std::string stack = cmsys::SystemInformation::GetProgramStack(0, 0);
  100. if (!stack.empty()) {
  101. if (cmHasLiteralPrefix(stack, "WARNING:")) {
  102. stack = "Note:" + stack.substr(8);
  103. }
  104. msg << stack << "\n";
  105. }
  106. }
  107. #endif
  108. // Output the message.
  109. cmMessageMetadata md;
  110. if (t == MessageType::FATAL_ERROR || t == MessageType::INTERNAL_ERROR ||
  111. t == MessageType::DEPRECATION_ERROR || t == MessageType::AUTHOR_ERROR) {
  112. cmSystemTools::SetErrorOccured();
  113. md.title = "Error";
  114. cmSystemTools::Message(msg.str(), md);
  115. } else {
  116. md.title = "Warning";
  117. cmSystemTools::Message(msg.str(), md);
  118. }
  119. }
  120. void cmMessenger::IssueMessage(MessageType t, const std::string& text,
  121. const cmListFileBacktrace& backtrace) const
  122. {
  123. bool force = false;
  124. if (!force) {
  125. // override the message type, if needed, for warnings and errors
  126. MessageType override = this->ConvertMessageType(t);
  127. if (override != t) {
  128. t = override;
  129. force = true;
  130. }
  131. }
  132. if (!force && !this->IsMessageTypeVisible(t)) {
  133. return;
  134. }
  135. this->DisplayMessage(t, text, backtrace);
  136. }
  137. void cmMessenger::DisplayMessage(MessageType t, const std::string& text,
  138. const cmListFileBacktrace& backtrace) const
  139. {
  140. std::ostringstream msg;
  141. if (!printMessagePreamble(t, msg)) {
  142. return;
  143. }
  144. // Add the immediate context.
  145. backtrace.PrintTitle(msg);
  146. printMessageText(msg, text);
  147. // Add the rest of the context.
  148. backtrace.PrintCallStack(msg);
  149. displayMessage(t, msg);
  150. }