Skip to content

Commit f59ff19

Browse files
committed
c++: add -fdump-lang-tinst
This patch adds a dump with a trace of template instantiations, indented based on the depth of recursive instantiation. -lineno adds the location that triggered the instantiation, -details adds non-instantiation sbustitutions. The instantiate_pending_templates change is to avoid a bunch of entries for reopening tinst scopes that we then don't instantiate anything with; it also seems a bit cleaner this way. gcc/cp/ChangeLog: * cp-tree.h: Declare tinst_dump_id. * cp-objcp-common.cc (cp_register_dumps): Set it. * pt.cc (push_tinst_level_loc): Dump it. (reopen_tinst_level): Here too. (tinst_complete_p): New. (instantiate_pending_templates): Don't reopen_tinst_level for already-complete instantiations. gcc/ChangeLog: * doc/invoke.texi: Move C++ -fdump-lang to C++ section. Add -fdump-lang-tinst.
1 parent d424245 commit f59ff19

File tree

4 files changed

+145
-41
lines changed

4 files changed

+145
-41
lines changed

gcc/cp/cp-objcp-common.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ cp_register_dumps (gcc::dump_manager *dumps)
614614
(".raw", "lang-raw", "lang-raw", DK_lang, OPTGROUP_NONE, false);
615615
coro_dump_id = dumps->dump_register
616616
(".coro", "lang-coro", "lang-coro", DK_lang, OPTGROUP_NONE, false);
617+
tinst_dump_id = dumps->dump_register
618+
(".tinst", "lang-tinst", "lang-tinst", DK_lang, OPTGROUP_NONE, false);
617619
}
618620

619621
void

gcc/cp/cp-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6822,6 +6822,7 @@ extern int class_dump_id;
68226822
extern int module_dump_id;
68236823
extern int raw_dump_id;
68246824
extern int coro_dump_id;
6825+
extern int tinst_dump_id;
68256826

68266827
/* Whether the current context is manifestly constant-evaluated.
68276828
Used by the constexpr machinery to control folding of

gcc/cp/pt.cc

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11378,6 +11378,7 @@ limit_bad_template_recursion (tree decl)
1137811378
static int tinst_depth;
1137911379
extern int max_tinst_depth;
1138011380
int depth_reached;
11381+
int tinst_dump_id;
1138111382

1138211383
static GTY(()) struct tinst_level *last_error_tinst_level;
1138311384

@@ -11430,6 +11431,40 @@ push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
1143011431
set_refcount_ptr (new_level->next, current_tinst_level);
1143111432
set_refcount_ptr (current_tinst_level, new_level);
1143211433

11434+
if (cxx_dump_pretty_printer pp {tinst_dump_id})
11435+
{
11436+
#if __GNUC__ >= 10
11437+
#pragma GCC diagnostic push
11438+
#pragma GCC diagnostic ignored "-Wformat-diag"
11439+
#endif
11440+
bool list_p = new_level->list_p ();
11441+
if (list_p && !pp.has_flag (TDF_DETAILS))
11442+
/* Skip non-instantiations unless -details. */;
11443+
else
11444+
{
11445+
if (tinst_depth == 0)
11446+
pp_newline (&pp);
11447+
if (loc && pp.has_flag (TDF_LINENO))
11448+
{
11449+
for (int i = 0; i < tinst_depth; ++i)
11450+
pp_space (&pp);
11451+
const expanded_location el = expand_location (loc);
11452+
pp_printf (&pp, "%s:%d:%d", el.file, el.line, el.column);
11453+
pp_newline (&pp);
11454+
}
11455+
for (int i = 0; i < tinst_depth; ++i)
11456+
pp_space (&pp);
11457+
if (list_p)
11458+
pp_printf (&pp, "S %S", new_level->get_node ());
11459+
else
11460+
pp_printf (&pp, "I %D", tldcl);
11461+
pp_newline (&pp);
11462+
}
11463+
#if __GNUC__ >= 10
11464+
#pragma GCC diagnostic pop
11465+
#endif
11466+
}
11467+
1143311468
++tinst_depth;
1143411469
if (GATHER_STATISTICS && (tinst_depth > depth_reached))
1143511470
depth_reached = tinst_depth;
@@ -11481,6 +11516,20 @@ pop_tinst_level (void)
1148111516
--tinst_depth;
1148211517
}
1148311518

11519+
/* True if the instantiation represented by LEVEL is complete. */
11520+
11521+
static bool
11522+
tinst_complete_p (struct tinst_level *level)
11523+
{
11524+
gcc_assert (!level->list_p ());
11525+
tree node = level->get_node ();
11526+
if (TYPE_P (node))
11527+
return COMPLETE_TYPE_P (node);
11528+
else
11529+
return (DECL_TEMPLATE_INSTANTIATED (node)
11530+
|| DECL_TEMPLATE_SPECIALIZATION (node));
11531+
}
11532+
1148411533
/* We're instantiating a deferred template; restore the template
1148511534
instantiation context in which the instantiation was requested, which
1148611535
is one step out from LEVEL. Return the corresponding DECL or TYPE. */
@@ -11499,6 +11548,38 @@ reopen_tinst_level (struct tinst_level *level)
1149911548
if (current_tinst_level && !current_tinst_level->had_errors)
1150011549
current_tinst_level->errors = errorcount+sorrycount;
1150111550

11551+
if (cxx_dump_pretty_printer pp {tinst_dump_id})
11552+
{
11553+
#if __GNUC__ >= 10
11554+
#pragma GCC diagnostic push
11555+
#pragma GCC diagnostic ignored "-Wformat-diag"
11556+
#endif
11557+
/* Dump the reopened instantiation context. */
11558+
t = current_tinst_level;
11559+
if (!pp.has_flag (TDF_DETAILS))
11560+
/* Skip non-instantiations unless -details. */
11561+
while (t && t->list_p ())
11562+
t = t->next;
11563+
if (t)
11564+
{
11565+
static tree last_ctx = NULL_TREE;
11566+
tree ctx = t->get_node ();
11567+
if (ctx != last_ctx)
11568+
{
11569+
last_ctx = ctx;
11570+
pp_newline (&pp);
11571+
if (t->list_p ())
11572+
pp_printf (&pp, "RS %S", ctx);
11573+
else
11574+
pp_printf (&pp, "RI %D", ctx);
11575+
pp_newline (&pp);
11576+
}
11577+
}
11578+
#if __GNUC__ >= 10
11579+
#pragma GCC diagnostic pop
11580+
#endif
11581+
}
11582+
1150211583
tree decl = level->maybe_get_node ();
1150311584
if (decl && modules_p ())
1150411585
{
@@ -28068,14 +28149,16 @@ instantiate_pending_templates (int retries)
2806828149
reconsider = 0;
2806928150
while (*t)
2807028151
{
28071-
tree instantiation = reopen_tinst_level ((*t)->tinst);
28072-
bool complete = false;
28152+
struct tinst_level *tinst = (*t)->tinst;
28153+
bool complete = tinst_complete_p (tinst);
2807328154

28074-
if (limit_bad_template_recursion (instantiation))
28075-
/* Do nothing. */;
28076-
else if (TYPE_P (instantiation))
28155+
if (!complete)
2807728156
{
28078-
if (!COMPLETE_TYPE_P (instantiation))
28157+
tree instantiation = reopen_tinst_level (tinst);
28158+
28159+
if (limit_bad_template_recursion (instantiation))
28160+
/* Do nothing. */;
28161+
else if (TYPE_P (instantiation))
2807928162
{
2808028163
instantiate_class_template (instantiation);
2808128164
if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
@@ -28092,13 +28175,7 @@ instantiate_pending_templates (int retries)
2809228175
if (COMPLETE_TYPE_P (instantiation))
2809328176
reconsider = 1;
2809428177
}
28095-
28096-
complete = COMPLETE_TYPE_P (instantiation);
28097-
}
28098-
else
28099-
{
28100-
if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
28101-
&& !DECL_TEMPLATE_INSTANTIATED (instantiation))
28178+
else
2810228179
{
2810328180
instantiation
2810428181
= instantiate_decl (instantiation,
@@ -28108,8 +28185,10 @@ instantiate_pending_templates (int retries)
2810828185
reconsider = 1;
2810928186
}
2811028187

28111-
complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
28112-
|| DECL_TEMPLATE_INSTANTIATED (instantiation));
28188+
complete = tinst_complete_p (tinst);
28189+
28190+
tinst_depth = 0;
28191+
set_refcount_ptr (current_tinst_level);
2811328192
}
2811428193

2811528194
if (complete)
@@ -28126,8 +28205,6 @@ instantiate_pending_templates (int retries)
2812628205
last = *t;
2812728206
t = &(*t)->next;
2812828207
}
28129-
tinst_depth = 0;
28130-
set_refcount_ptr (current_tinst_level);
2813128208
}
2813228209
last_pending_template = last;
2813328210
}

gcc/doc/invoke.texi

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,6 +3297,50 @@ Enable support for the C++ coroutines extension (experimental).
32973297
Permit the C++ front end to note all candidates during overload resolution
32983298
failure, including when a deleted function is selected.
32993299

3300+
@item -fdump-lang-
3301+
@itemx -fdump-lang-@var{switch}
3302+
@itemx -fdump-lang-@var{switch}-@var{options}
3303+
@itemx -fdump-lang-@var{switch}-@var{options}=@var{filename}
3304+
Control the dumping of C++-specific information. The @var{options}
3305+
and @var{filename} portions behave as described in the
3306+
@option{-fdump-tree} option. The following @var{switch} values are
3307+
accepted:
3308+
3309+
@table @samp
3310+
@item all
3311+
Enable all of the below.
3312+
3313+
@opindex fdump-lang-class
3314+
@item class
3315+
Dump class hierarchy information. Virtual table information is emitted
3316+
unless '@option{slim}' is specified.
3317+
3318+
@opindex fdump-lang-module
3319+
@item module
3320+
Dump module information. Options @option{lineno} (locations),
3321+
@option{graph} (reachability), @option{blocks} (clusters),
3322+
@option{uid} (serialization), @option{alias} (mergeable),
3323+
@option{asmname} (Elrond), @option{eh} (mapper) & @option{vops}
3324+
(macros) may provide additional information.
3325+
3326+
@opindex fdump-lang-raw
3327+
@item raw
3328+
Dump the raw internal tree data.
3329+
3330+
@opindex fdump-lang-tinst
3331+
@item tinst
3332+
Dump the sequence of template instantiations, indented to show the
3333+
depth of recursion. The @option{lineno} option adds the source
3334+
location where the instantiation was triggered, and the
3335+
@option{details} option also dumps pre-instantiation substitutions
3336+
such as those performed during template argument deduction.
3337+
3338+
Lines in the .tinst dump start with @samp{I} for an instantiation,
3339+
@samp{S} for another substitution, and @samp{R[IS]} for the reopened
3340+
context of a deferred instantiation.
3341+
3342+
@end table
3343+
33003344
@opindex fno-elide-constructors
33013345
@opindex felide-constructors
33023346
@item -fno-elide-constructors
@@ -20891,30 +20935,10 @@ Dump language-specific information. The file name is made by appending
2089120935
@itemx -fdump-lang-@var{switch}-@var{options}=@var{filename}
2089220936
Control the dumping of language-specific information. The @var{options}
2089320937
and @var{filename} portions behave as described in the
20894-
@option{-fdump-tree} option. The following @var{switch} values are
20895-
accepted:
20896-
20897-
@table @samp
20898-
@item all
20899-
20900-
Enable all language-specific dumps.
20901-
20902-
@item class
20903-
Dump class hierarchy information. Virtual table information is emitted
20904-
unless '@option{slim}' is specified. This option is applicable to C++ only.
20905-
20906-
@item module
20907-
Dump module information. Options @option{lineno} (locations),
20908-
@option{graph} (reachability), @option{blocks} (clusters),
20909-
@option{uid} (serialization), @option{alias} (mergeable),
20910-
@option{asmname} (Elrond), @option{eh} (mapper) & @option{vops}
20911-
(macros) may provide additional information. This option is
20912-
applicable to C++ only.
20913-
20914-
@item raw
20915-
Dump the raw internal tree data. This option is applicable to C++ only.
20916-
20917-
@end table
20938+
@option{-fdump-tree} option. @option{-fdump-tree-all} enables all
20939+
language-specific dumps; other options vary with the language. For
20940+
instance, see @xref{C++ Dialect Options} for the @option{-fdump-lang}
20941+
flags supported by the C++ front-end.
2091820942

2091920943
@opindex fdump-passes
2092020944
@item -fdump-passes

0 commit comments

Comments
 (0)