Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmMessenger.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmMessenger.h"
4
5
#include <algorithm>
6
#include <array>
7
#include <sstream>
8
#include <utility>
9
10
#include <cmext/string_view>
11
12
#include "cmDocumentationFormatter.h"
13
#include "cmMessageMetadata.h"
14
#include "cmMessageType.h"
15
#include "cmStateSnapshot.h"
16
#include "cmStdIoTerminal.h"
17
#include "cmStringAlgorithms.h"
18
#include "cmSystemTools.h"
19
20
#if !defined(CMAKE_BOOTSTRAP)
21
#  include "cmsys/SystemInformation.hxx"
22
#endif
23
24
#ifdef CMake_ENABLE_DEBUGGER
25
#  include "cmDebuggerAdapter.h"
26
#endif
27
28
namespace {
29
char const* getMessageTypeStr(MessageType t)
30
1
{
31
1
  switch (t) {
32
1
    case MessageType::FATAL_ERROR:
33
1
      return "Error";
34
0
    case MessageType::INTERNAL_ERROR:
35
0
      return "Internal Error (please report a bug)";
36
0
    case MessageType::LOG:
37
0
      return "Debug Log";
38
0
    default:
39
0
      break;
40
1
  }
41
0
  return "Warning";
42
1
}
43
44
std::string getDiagnosticCategoryStr(cmDiagnosticCategory category)
45
0
{
46
0
  std::string out = cmSystemTools::LowerCase(
47
0
    cmDiagnostics::GetCategoryString(category).substr(4));
48
0
  std::replace(out.begin(), out.end(), '_', '-');
49
0
  return out;
50
0
}
51
52
cm::StdIo::TermAttr getMessageColor(MessageType t)
53
1
{
54
1
  switch (t) {
55
0
    case MessageType::INTERNAL_ERROR:
56
1
    case MessageType::FATAL_ERROR:
57
1
      return cm::StdIo::TermAttr::ForegroundRed;
58
0
    case MessageType::WARNING:
59
0
      return cm::StdIo::TermAttr::ForegroundYellow;
60
0
    default:
61
0
      return cm::StdIo::TermAttr::Normal;
62
1
  }
63
1
}
64
65
bool isAuthorDiagnostic(cmDiagnosticCategory category)
66
1
{
67
1
  while (category != cmDiagnostics::CMD_NONE) {
68
0
    if (category == cmDiagnostics::CMD_AUTHOR) {
69
0
      return true;
70
0
    }
71
0
    category = cmDiagnostics::CategoryInfo[category].Parent;
72
0
  }
73
1
  return false;
74
1
}
75
76
void printMessageText(std::ostream& msg, std::string const& text)
77
1
{
78
1
  msg << ":\n";
79
1
  cmDocumentationFormatter formatter;
80
1
  formatter.SetIndent(2u);
81
1
  formatter.PrintFormatted(msg, text);
82
1
}
83
84
void displayMessage(MessageType type, cmDiagnosticCategory category,
85
                    std::ostringstream& msg)
86
1
{
87
1
  if (isAuthorDiagnostic(category)) {
88
    // Add a note about warning suppression.
89
0
    std::ostringstream text;
90
0
    if (type == MessageType::WARNING) {
91
0
      text << "This warning is for project developers.  "
92
0
              "Use -Wno-author";
93
0
      if (category != cmDiagnostics::CMD_AUTHOR) {
94
0
        text << " or -Wno-" << getDiagnosticCategoryStr(category);
95
0
      }
96
0
    } else if (type == MessageType::FATAL_ERROR) {
97
0
      text << "This error is for project developers.  "
98
0
              "Use -Wno-error=author";
99
0
      if (category != cmDiagnostics::CMD_AUTHOR) {
100
0
        text << " or -Wno-error=" << getDiagnosticCategoryStr(category);
101
0
      }
102
0
    }
103
0
    text << " to suppress it.";
104
105
0
    cmDocumentationFormatter formatter;
106
0
    formatter.PrintFormatted(msg, text.str());
107
1
  } else {
108
    // Add a terminating blank line.
109
1
    msg << '\n';
110
1
  }
111
112
1
#if !defined(CMAKE_BOOTSTRAP)
113
  // Add a C++ stack trace to internal errors.
114
1
  if (type == MessageType::INTERNAL_ERROR) {
115
0
    std::string stack = cmsys::SystemInformation::GetProgramStack(0, 0);
116
0
    if (!stack.empty()) {
117
0
      if (cmHasLiteralPrefix(stack, "WARNING:")) {
118
0
        stack = "Note:" + stack.substr(8);
119
0
      }
120
0
      msg << stack << '\n';
121
0
    }
122
0
  }
123
1
#endif
124
125
  // Output the message.
126
1
  cmMessageMetadata md;
127
1
  md.attrs = getMessageColor(type);
128
1
  if (type == MessageType::FATAL_ERROR ||
129
1
      type == MessageType::INTERNAL_ERROR) {
130
1
    cmSystemTools::SetErrorOccurred();
131
1
    md.title = "Error";
132
1
  } else {
133
0
    md.title = "Warning";
134
0
  }
135
1
  cmSystemTools::Message(msg.str(), md);
136
1
}
137
138
void PrintCallStack(std::ostream& out, cmListFileBacktrace bt,
139
                    cm::optional<std::string> const& topSource)
140
1
{
141
  // The call stack exists only if we have at least two calls on top
142
  // of the bottom.
143
1
  if (bt.Empty()) {
144
0
    return;
145
0
  }
146
1
  std::string lastFilePath = bt.Top().FilePath;
147
1
  bt = bt.Pop();
148
1
  if (bt.Empty()) {
149
0
    return;
150
0
  }
151
152
1
  bool first = true;
153
2
  for (; !bt.Empty(); bt = bt.Pop()) {
154
1
    cmListFileContext lfc = bt.Top();
155
1
    if (lfc.Name.empty() &&
156
1
        lfc.Line != cmListFileContext::DeferPlaceholderLine &&
157
1
        lfc.FilePath == lastFilePath) {
158
      // An entry with no function name is frequently preceded (in the stack)
159
      // by a more specific entry.  When this happens (as verified by the
160
      // preceding entry referencing the same file path), skip the less
161
      // specific entry, as we have already printed the more specific one.
162
1
      continue;
163
1
    }
164
0
    if (first) {
165
0
      first = false;
166
0
      out << "Call Stack (most recent call first):\n";
167
0
    }
168
0
    lastFilePath = lfc.FilePath;
169
0
    if (topSource) {
170
0
      lfc.FilePath = cmSystemTools::RelativeIfUnder(*topSource, lfc.FilePath);
171
0
    }
172
0
    out << "  " << lfc << '\n';
173
0
  }
174
1
}
175
176
} // anonymous namespace
177
178
void cmMessenger::IssueMessage(MessageType t, std::string const& text,
179
                               cmListFileBacktrace const& backtrace)
180
1
{
181
1
  this->DisplayMessage(t, cmDiagnostics::CMD_NONE, text, backtrace);
182
1
}
183
184
void cmMessenger::IssueDiagnostic(cmDiagnosticCategory category,
185
                                  std::string const& text,
186
                                  cmStateSnapshot const& fallbackContext,
187
                                  cmDiagnosticContext const& context)
188
0
{
189
0
  cmDiagnosticAction const action = [&] {
190
0
    if (context.HasState) {
191
0
      cmDiagnosticAction const ca = context.DiagnosticState[category];
192
0
      if (ca != cmDiagnostics::Undefined) {
193
0
        return ca;
194
0
      }
195
196
      // If the context has recorded states, but not the state we want, this
197
      // implies that we had the opportunity to record the state and failed to
198
      // do so. Ask users to report this.
199
0
      std::string msg =
200
0
        cmStrCat("Stored diagnostic context did not record state for "_s,
201
0
                 cmDiagnostics::GetCategoryString(category),
202
0
                 ".  Please report this as a bug.\n"_s);
203
0
      this->IssueMessage(MessageType::LOG, msg, context.GetBacktrace());
204
0
    }
205
206
0
    return fallbackContext.GetDiagnostic(category);
207
0
  }();
208
209
0
  switch (action) {
210
0
    case cmDiagnostics::FatalError:
211
0
      cmSystemTools::SetFatalErrorOccurred();
212
0
      CM_FALLTHROUGH;
213
0
    case cmDiagnostics::SendError:
214
0
      cmSystemTools::SetErrorOccurred();
215
0
      this->DisplayMessage(MessageType::FATAL_ERROR, category, text,
216
0
                           context.GetBacktrace());
217
0
      break;
218
0
    case cmDiagnostics::Warn:
219
0
      this->DisplayMessage(MessageType::WARNING, category, text,
220
0
                           context.GetBacktrace());
221
0
      break;
222
0
    default:
223
0
      return;
224
0
  }
225
0
}
226
227
void cmMessenger::DisplayMessage(MessageType type,
228
                                 cmDiagnosticCategory category,
229
                                 std::string const& text,
230
                                 cmListFileBacktrace const& backtrace)
231
1
{
232
1
  std::ostringstream msg;
233
234
  // Print the message preamble.
235
1
  msg << "CMake " << getMessageTypeStr(type);
236
1
  if (category != cmDiagnostics::CMD_NONE) {
237
0
    msg << " (" << getDiagnosticCategoryStr(category) << ')';
238
0
  }
239
240
  // Add the immediate context.
241
1
  this->PrintBacktraceTitle(msg, backtrace);
242
243
1
  printMessageText(msg, text);
244
245
  // Add the rest of the context.
246
1
  PrintCallStack(msg, backtrace, this->TopSource);
247
248
1
  displayMessage(type, category, msg);
249
250
  // Add message to logs
251
1
  this->DisplayedMessages.emplace_back(
252
1
    Message{ type, category, backtrace, text });
253
254
1
#ifdef CMake_ENABLE_DEBUGGER
255
1
  if (DebuggerAdapter) {
256
0
    DebuggerAdapter->OnMessageOutput(type, msg.str());
257
0
  }
258
1
#endif
259
1
}
260
261
void cmMessenger::PrintBacktraceTitle(std::ostream& out,
262
                                      cmListFileBacktrace const& bt) const
263
1
{
264
  // The title exists only if we have a call on top of the bottom.
265
1
  if (bt.Empty()) {
266
0
    return;
267
0
  }
268
1
  cmListFileContext lfc = bt.Top();
269
1
  if (this->TopSource) {
270
1
    lfc.FilePath =
271
1
      cmSystemTools::RelativeIfUnder(*this->TopSource, lfc.FilePath);
272
1
  }
273
1
  out << (lfc.Line ? " at " : " in ") << lfc;
274
1
}
275
276
void cmMessenger::SetTopSource(cm::optional<std::string> topSource)
277
24
{
278
24
  this->TopSource = std::move(topSource);
279
24
}