From 750aa8e568e1a00217f9a82a96e98c28e2c52834 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 2 Oct 2025 11:49:32 -0400 Subject: [PATCH 26/30] FIXME: C AST --- gcc/c/c-parser.cc | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 9816a9425ce..9acbfd396b5 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -36,6 +36,7 @@ along with GCC; see the file COPYING3. If not see location rather than implicitly using input_location. */ #include "config.h" +#define INCLUDE_LIST #define INCLUDE_STRING #include "system.h" #include "coretypes.h" @@ -77,6 +78,9 @@ along with GCC; see the file COPYING3. If not see #include "asan.h" #include "c-family/c-ubsan.h" #include "gcc-urlifier.h" +#include "channels.h" +#include "topics/ast-events.h" + /* We need to walk over decls with incomplete struct/union/enum types after parsing the whole translation unit. @@ -275,6 +279,10 @@ struct GTY(()) c_parser { Managed by c_parser_omp_metadirective. */ struct omp_metadirective_parse_data * GTY((skip)) omp_metadirective_state; + + // FIXME + const pub_sub::channel * GTY((skip)) + ast_events_channel; }; /* Holds data needed to restore the token stream to its previous state @@ -299,6 +307,52 @@ vec *omp_begin_declare_variant_map; static tree omp_start_variant_function (c_declarator *, tree); static void omp_finish_variant_function (tree, tree, tree); +class auto_notify_ast_node +{ +public: + auto_notify_ast_node (c_parser *parser, const char *kind) + : m_parser (parser), + m_kind (kind) + { + // FIXME: unlikely + // gengtype doesn't understand [[unlikely]] , and is it legal + // in c++14 anyway? + if (parser->ast_events_channel) + parser->ast_events_channel->publish + (gcc::topics::ast_events::begin_node {kind, get_next_loc ()}); + } + + ~auto_notify_ast_node () + { + // FIXME: unlikely +#if 0 + if (parser->ast_events_channel) + parser->ast_events_channel->publish + (gcc::topics::ast_events::end_node {m_kind, get_prev_loc ()}); +#endif + } + + location_t + get_next_loc () const + { + return c_parser_peek_token (m_parser)->location; + } + +#if 0 + location_t + get_prev_loc () const + { + if (const cp_token *tok = cp_lexer_safe_previous_token (m_parser->lexer)) + return tok->location; + return UNKNOWN_LOCATION; + } +#endif + +private: + c_parser *const m_parser; + const char *const m_kind; +}; + /* Return a pointer to the Nth token in PARSERs tokens_buf. */ c_token * @@ -2074,6 +2128,8 @@ private: static void c_parser_translation_unit (c_parser *parser) { + auto_notify_ast_node (parser, "translation-unit"); + if (c_parser_next_token_is (parser, CPP_EOF)) { pedwarn (c_parser_peek_token (parser)->location, OPT_Wpedantic, @@ -2168,6 +2224,8 @@ c_parser_translation_unit (c_parser *parser) static void c_parser_external_declaration (c_parser *parser) { + auto_notify_ast_node (parser, "external-declaration"); + int ext; switch (c_parser_peek_token (parser)->type) { @@ -4111,6 +4169,8 @@ report_bad_enum_name (c_parser *parser) static struct c_typespec c_parser_enum_specifier (c_parser *parser) { + auto_notify_ast_node (parser, "enum-specifier"); + struct c_typespec ret; bool have_std_attrs; bool potential_nesting_p = false; @@ -4382,6 +4442,7 @@ c_parser_enum_specifier (c_parser *parser) static struct c_typespec c_parser_struct_or_union_specifier (c_parser *parser) { + auto_notify_ast_node (parser, "struct-or-union-specifier"); struct c_typespec ret; bool have_std_attrs; tree std_attrs = NULL_TREE; @@ -4981,6 +5042,8 @@ struct c_declarator * c_parser_declarator (c_parser *parser, bool type_seen_p, c_dtr_syn kind, bool *seen_id) { + auto_notify_ast_node (parser, "declarator"); + /* Parse any initial pointer part. */ if (c_parser_next_token_is (parser, CPP_MULT)) { @@ -31291,6 +31354,9 @@ c_parse_file (void) if (tparser.tokens == &tparser.tokens_buf[0]) the_parser->tokens = &the_parser->tokens_buf[0]; + the_parser->ast_events_channel + = g->get_channels ().ast_events_channel.get_if_active (); + /* Initialize EH, if we've been told to do so. */ if (flag_exceptions) using_eh_for_cleanups (); -- 2.49.0