On Wed, 15 Aug 2012, Gabriel Dos Reis wrote: > On Wed, Aug 15, 2012 at 6:01 AM, Richard Guenther <rguent...@suse.de> wrote: > > On Wed, 15 Aug 2012, Gabriel Dos Reis wrote: > > > >> On Wed, Aug 15, 2012 at 5:11 AM, Florian Weimer <fwei...@redhat.com> wrote: > >> > On 08/15/2012 12:07 PM, Gabriel Dos Reis wrote: > >> > > >> >> You might try to encode/package information with the > >> >> parameters T and A, but essentially you will hit the wall > >> >> that __FILE__, __LINE__, and __FUNCTION__ are CPP artifacts. > >> > > >> > > >> > GNAT has built-in functions which return this information. > >> > >> This is a very sensible thing to do. > >> > >> > When they are > >> > used in default argument expressions and these arguments are omitted, the > >> > functions are evaluated in the context of the caller and return the file > >> > position at that point. Perhaps that would be an approach for C++ as > >> > well? > >> > >> C++ already has the rule that default arguments are evaluated at the call > >> site, so this is definitely something to consider for stage 2 and higher > >> builds. > >> For stage 1 builds, I suppose one would have to fake it and leave > >> with the imprecision that is bothering Richard? > >> > >> > (This assumes that gather-detailed-mem-stats is not needed in the first > >> > stage compiler.) > >> > >> it is built into stage 1 compiler, but I doubt that stage 1 compiler is > >> really useful to the kind of detailed statistics that would require this. > > > > The configury for --enable-gather-detailed-mem-stats is re-done for > > each stage, so if we add a proper feature check we can simply disable > > it in stage1 when the host compiler does not support this feature. > > Yes, agreed. > > > > > We can add __builtin__FILE__ and friends in generic code, for example > > "folding" them during gimplification using location information on > > the CALL_EXPR and the current function context. I think at least > > __FUNCTION__ is already sort-of a "builtin" as the preprocessor > > has no way of expanding it. > > Yes, you are right. > (We already have to treat __PRETTY_FUNCTION__ as a special case > so that it works reliably for template instances and reports template > arguments properly.)
Prototype below - fire away on bikeshedding names. #include <stdio.h> int main() { printf ("%s (%s): %d\n", __builtin_loc_file (), __builtin_loc_function (), __builtin_loc_line ()); return 0; } prints > ./t t.c (main): 4 No fancy trying to share constants for FILE / FUNCTION yet. Richard. Index: gcc/builtin-types.def =================================================================== --- gcc/builtin-types.def (revision 190407) +++ gcc/builtin-types.def (working copy) @@ -140,6 +140,7 @@ DEF_POINTER_TYPE (BT_PTR_PTR, BT_PTR) DEF_FUNCTION_TYPE_0 (BT_FN_VOID, BT_VOID) DEF_FUNCTION_TYPE_0 (BT_FN_BOOL, BT_BOOL) DEF_FUNCTION_TYPE_0 (BT_FN_PTR, BT_PTR) +DEF_FUNCTION_TYPE_0 (BT_FN_CONST_STRING, BT_CONST_STRING) DEF_FUNCTION_TYPE_0 (BT_FN_PID, BT_PID) DEF_FUNCTION_TYPE_0 (BT_FN_INT, BT_INT) DEF_FUNCTION_TYPE_0 (BT_FN_UINT, BT_UINT) Index: gcc/builtins.def =================================================================== --- gcc/builtins.def (revision 190407) +++ gcc/builtins.def (working copy) @@ -801,6 +801,11 @@ DEF_BUILTIN_STUB (BUILT_IN_EH_POINTER, " DEF_BUILTIN_STUB (BUILT_IN_EH_FILTER, "__builtin_eh_filter") DEF_BUILTIN_STUB (BUILT_IN_EH_COPY_VALUES, "__builtin_eh_copy_values") +/* __FILE__, __LINE__, __FUNCTION__ as builtins. */ +DEF_GCC_BUILTIN (BUILT_IN_LOC_FILE, "loc_file", BT_FN_CONST_STRING, ATTR_CONST_NOTHROW_LEAF_LIST) +DEF_GCC_BUILTIN (BUILT_IN_LOC_FUNCTION, "loc_function", BT_FN_CONST_STRING, ATTR_CONST_NOTHROW_LEAF_LIST) +DEF_GCC_BUILTIN (BUILT_IN_LOC_LINE, "loc_line", BT_FN_INT, ATTR_CONST_NOTHROW_LEAF_LIST) + /* Synchronization Primitives. */ #include "sync-builtins.def" Index: gcc/gimplify.c =================================================================== --- gcc/gimplify.c (revision 190407) +++ gcc/gimplify.c (working copy) @@ -2528,6 +2528,28 @@ gimplify_call_expr (tree *expr_p, gimple return GS_OK; } } + else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL + && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LOC_LINE) + { + expanded_location loc = expand_location (EXPR_LOCATION (*expr_p)); + *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line); + return GS_OK; + } + else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL + && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LOC_FILE) + { + expanded_location loc = expand_location (EXPR_LOCATION (*expr_p)); + *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file); + return GS_OK; + } + else if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL + && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_LOC_FUNCTION) + { + const char *function; + function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl)); + *expr_p = build_string_literal (strlen (function) + 1, function); + return GS_OK; + } } /* Remember the original function pointer type. */