+/* The type for callback data for iterating the splay tree in
+ macro_for_each and macro_for_each_in_scope. Only the latter uses
+ the FILE and LINE fields. */
+struct macro_for_each_data
+{
+ gdb::function_view<macro_callback_fn> fn;
+ struct macro_source_file *file;
+ int line;
+};
+
+/* Helper function for macro_for_each. */
+static int
+foreach_macro (splay_tree_node node, void *arg)
+{
+ struct macro_for_each_data *datum = (struct macro_for_each_data *) arg;
+ struct macro_key *key = (struct macro_key *) node->key;
+ struct macro_definition *def;
+
+ std::string key_fullname = macro_source_fullname (key->start_file);
+ def = fixup_definition (key_fullname.c_str (), key->start_line,
+ (struct macro_definition *) node->value);
+
+ datum->fn (key->name, def, key->start_file, key->start_line);
+ return 0;
+}
+
+/* Call FN for every macro in TABLE. */
+void
+macro_for_each (struct macro_table *table,
+ gdb::function_view<macro_callback_fn> fn)
+{
+ struct macro_for_each_data datum;
+
+ datum.fn = fn;
+ datum.file = NULL;
+ datum.line = 0;
+ splay_tree_foreach (table->definitions, foreach_macro, &datum);
+}
+
+static int
+foreach_macro_in_scope (splay_tree_node node, void *info)
+{
+ struct macro_for_each_data *datum = (struct macro_for_each_data *) info;
+ struct macro_key *key = (struct macro_key *) node->key;
+ struct macro_definition *def;
+
+ std::string datum_fullname = macro_source_fullname (datum->file);
+ def = fixup_definition (datum_fullname.c_str (), datum->line,
+ (struct macro_definition *) node->value);
+
+ /* See if this macro is defined before the passed-in line, and
+ extends past that line. */
+ if (compare_locations (key->start_file, key->start_line,
+ datum->file, datum->line) < 0
+ && (!key->end_file
+ || compare_locations (key->end_file, key->end_line,
+ datum->file, datum->line) >= 0))
+ datum->fn (key->name, def, key->start_file, key->start_line);
+ return 0;
+}
+
+/* Call FN for every macro is visible in SCOPE. */
+void
+macro_for_each_in_scope (struct macro_source_file *file, int line,
+ gdb::function_view<macro_callback_fn> fn)
+{
+ struct macro_for_each_data datum;
+
+ datum.fn = fn;
+ datum.file = file;
+ datum.line = line;
+ splay_tree_foreach (file->table->definitions,
+ foreach_macro_in_scope, &datum);
+}
+
+