Files
gcc-reflection/gcc/context.h
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

82 lines
1.9 KiB
C++

/* context.h - 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/>. */
#ifndef GCC_CONTEXT_H
#define GCC_CONTEXT_H
namespace gcc {
class pass_manager;
class dump_manager;
struct compiler_channels;
/* GCC's internal state can be divided into zero or more
"parallel universe" of state; an instance of this class is one such
context of state. */
class context
{
public:
context ();
~context ();
/* The flag shows if there are symbols to be streamed for offloading. */
bool have_offload;
/* Pass-management. */
void set_passes (pass_manager *m)
{
gcc_assert (!m_passes);
m_passes = m;
}
pass_manager *get_passes () { gcc_assert (m_passes); return m_passes; }
/* Handling dump files. */
dump_manager *get_dumps () {gcc_assert (m_dumps); return m_dumps; }
/* Publish/subscribe channels for events
on various compiler-specific topics. */
compiler_channels &
get_channels () const
{
gcc_assert (m_channels);
return *m_channels;
}
private:
/* Pass-management. */
pass_manager *m_passes;
/* Dump files. */
dump_manager *m_dumps;
compiler_channels *m_channels;
}; // class context
} // namespace gcc
/* The global singleton context aka "g".
(the name is chosen to be easy to type in a debugger). */
extern gcc::context *g;
#endif /* ! GCC_CONTEXT_H */