Files
gcc-reflection/gcc/context.cc
David Malcolm 4a2e1c3115 Add publish/subscribe topics and channel: pass_events
This patch adds a new "struct compiler_channels" to hold channels
relating to the compiler that plugins (or diagnostic sinks) might want
to subscribe to events for, accessed from the global gcc::context
object, along with a new gcc/topics/ source subdirectory to hold
strongly-typed publish/subscribe topics relating to the compiler.

For now, there is just one: pass_events_channel, which, if there are any
subscribers, issues notifications about passes starting/stopping on
particular functions, using topics::pass_events, declared in
topics/pass-events.h, but followup patches add more kinds of
notification channel.

A toy plugin in the testsuite shows how this could be used to build a
progress notification UI for the compiler, and a followup patch uses the
channel to (optionally) capture CFG information at each stage of
optimization in machine-readable form into a SARIF sink.

gcc/ChangeLog:
	* channels.h: New file.
	* context.cc: Define INCLUDE_LIST.  Include "channels.h".
	(gcc::context::context): Create m_channels.
	(gcc::context::~context): Delete it.
	* context.h (struct compiler_channels): New forward decl.
	(gcc::context::get_channels): New accessor.
	(gcc::context::m_channels): New field.
	* passes.cc: Define INCLUDE_LIST.  Include "topics/pass-events.h"
	and "channels.h".
	(execute_one_pass): If the global context's pass_events_channel
	has subscribers, publish before_pass and after_pass events to it.
	* topics/pass-events.h: New file.

gcc/testsuite/ChangeLog:
	* gcc.dg/plugin/plugin.exp: Add progress_notifications_plugin.cc.
	* gcc.dg/plugin/progress_notifications_plugin.cc: New test plugin.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
2026-01-09 15:54:15 -05:00

50 lines
1.3 KiB
C++

/* context.cc - Holder for global state
Copyright (C) 2013-2026 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#define INCLUDE_LIST
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "context.h"
#include "pass_manager.h"
#include "dumpfile.h"
#include "realmpfr.h"
#include "channels.h"
/* The singleton holder of global state: */
gcc::context *g;
gcc::context::context ()
: m_passes (NULL),
m_dumps (new gcc::dump_manager ()),
m_channels (new gcc::compiler_channels ())
{
have_offload = false;
}
gcc::context::~context ()
{
delete m_passes;
delete m_dumps;
delete m_channels;
/* Release MPFR caches to avoid Valgrind leak reports. */
mpfr_free_cache ();
}