Files
cmake/Source/cmMessenger.h
T
Matthew Woehlke 42c7a97446 Diagnostics: Add class to record state
Create new cmDiagnosticContext used to capture the diagnostic state for
instances when the issuing of a diagnostic might happen at a point far
separated from processing the CMake command ultimately responsible for
the diagnostic. (This is particularly the case for diagnostics that get
emitted during the generation phase.) In such cases, it is preferred
that we record the diagnostic state that pertained when the ultimately
instigating command was processed, so that users can control the
diagnostic in a natural manner.  Copying the entire cmStateSnapshot is
unreasonably expensive; this will provide a lighter-weight mechanism for
capturing the relevant diagnostic state for later use.

Since it is almost universal that the points where we would capture this
state also capture a stack trace (which is likewise used to issue
diagnostics), the new class contains a stack trace, and replaces the
stack trace in some diagnostic calls. (In some instances, an overload
which implicitly converts a stack trace to the new context class is
retained for convenience. These will behave the same, since capturing
diagnostic state is an optional function of the new class.)
2026-05-07 17:10:46 -04:00

73 lines
1.8 KiB
C++

/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <iosfwd>
#include <memory>
#include <string>
#include <cm/optional>
#include "cmDiagnosticContext.h"
#include "cmDiagnostics.h"
#include "cmListFileCache.h"
#include "cmMessageType.h" // IWYU pragma: keep
#ifndef CMAKE_BOOTSTRAP
# include "cmSarifLog.h"
#endif
class cmStateSnapshot;
#ifdef CMake_ENABLE_DEBUGGER
namespace cmDebugger {
class cmDebuggerAdapter;
}
#endif
class cmMessenger
{
public:
void IssueMessage(
MessageType type, std::string const& text,
cmListFileBacktrace const& backtrace = cmListFileBacktrace()) const;
void IssueDiagnostic(cmDiagnosticCategory category, std::string const& text,
cmStateSnapshot const& fallbackContext,
cmDiagnosticContext const& context = {}) const;
void DisplayMessage(MessageType type, cmDiagnosticCategory category,
std::string const& text,
cmListFileBacktrace const& backtrace) const;
void SetTopSource(cm::optional<std::string> topSource);
#ifndef CMAKE_BOOTSTRAP
cmSarif::ResultsLog const& GetSarifResultsLog() const { return SarifLog; }
#endif
// Print the top of a backtrace.
void PrintBacktraceTitle(std::ostream& out,
cmListFileBacktrace const& bt) const;
#ifdef CMake_ENABLE_DEBUGGER
void SetDebuggerAdapter(
std::shared_ptr<cmDebugger::cmDebuggerAdapter> const& debuggerAdapter)
{
DebuggerAdapter = debuggerAdapter;
}
#endif
private:
cm::optional<std::string> TopSource;
#ifndef CMAKE_BOOTSTRAP
cmSarif::ResultsLog SarifLog;
#endif
#ifdef CMake_ENABLE_DEBUGGER
std::shared_ptr<cmDebugger::cmDebuggerAdapter> DebuggerAdapter;
#endif
};