]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
2012-09-14 Richard Guenther <rguenther@suse.de>
authorrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 14 Sep 2012 08:50:01 +0000 (08:50 +0000)
committerrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 14 Sep 2012 08:50:01 +0000 (08:50 +0000)
* builtin-types.def (BT_FN_CONST_STRING): Add.
* builtins.def (BUILT_IN_FILE, BUILT_IN_FUNCTION,
BUILT_IN_LINE): New builtins.
* gimplify.c (gimplify_call_expr): Expand them.
* doc/extend.texi (__builtin_LINE, __builtin_FUNCTION,
__builtin_FILE): Document.

* g++.dg/torture/builtin-location.C: New testcase.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@191290 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/builtin-types.def
gcc/builtins.def
gcc/doc/extend.texi
gcc/gimplify.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/torture/builtin-location.C [new file with mode: 0644]

index 2af6b3df1cfcf67f86325b9c143edc190c49ee72..91a9033ba1749691119e2b17aacfdc20a040b604 100644 (file)
@@ -1,3 +1,12 @@
+2012-09-14  Richard Guenther  <rguenther@suse.de>
+
+       * builtin-types.def (BT_FN_CONST_STRING): Add.
+       * builtins.def (BUILT_IN_FILE, BUILT_IN_FUNCTION,
+       BUILT_IN_LINE): New builtins.
+       * gimplify.c (gimplify_call_expr): Expand them.
+       * doc/extend.texi (__builtin_LINE, __builtin_FUNCTION,
+       __builtin_FILE): Document.
+
 2012-09-13  Anthony Green  <green@moxielogic.com>
 
        * config/moxie/moxie.h (LINK_SPEC): Add bi-endian support.
index d8e9e566694709919b3cc4e5a1633ebc4cf84f19..660300dea785dad8a6bb40916d7d610ef6252e03 100644 (file)
@@ -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 de45724998eaeb97907ebfbeb11fcd3810791398..8493ca41b345ae626a7f85b188acff8ab9455ef9 100644 (file)
@@ -801,6 +801,11 @@ DEF_BUILTIN_STUB (BUILT_IN_EH_POINTER, "__builtin_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_FILE, "FILE", BT_FN_CONST_STRING, ATTR_NOTHROW_LEAF_LIST)
+DEF_GCC_BUILTIN (BUILT_IN_FUNCTION, "FUNCTION", BT_FN_CONST_STRING, ATTR_NOTHROW_LEAF_LIST)
+DEF_GCC_BUILTIN (BUILT_IN_LINE, "LINE", BT_FN_INT, ATTR_NOTHROW_LEAF_LIST)
+
 /* Synchronization Primitives.  */
 #include "sync-builtins.def"
 
index cea5ddabd281f97f0a66cf17acdb020c3a0f2eca..e850266de878bad76497182469df7bc8d17772c3 100644 (file)
@@ -8324,6 +8324,21 @@ means that the compiler can assume for x, set to arg, that
 (char *) x - 8 is 32 byte aligned.
 @end deftypefn
 
+@deftypefn {Built-in Function} int __builtin_LINE ()
+This function is the equivalent to the preprocessor @code{__LINE__}
+macro and returns the line number of the invocation of the built-in.
+@end deftypefn
+
+@deftypefn {Built-in Function} int __builtin_FUNCTION ()
+This function is the equivalent to the preprocessor @code{__FUNCTION__}
+macro and returns the function name the invocation of the built-in is in.
+@end deftypefn
+
+@deftypefn {Built-in Function} int __builtin_FILE ()
+This function is the equivalent to the preprocessor @code{__FILE__}
+macro and returns the file name the invocation of the built-in is in.
+@end deftypefn
+
 @deftypefn {Built-in Function} void __builtin___clear_cache (char *@var{begin}, char *@var{end})
 This function is used to flush the processor's instruction cache for
 the region of memory between @var{begin} inclusive and @var{end}
index 03973537ee6470faf310e1b6949d6cc1df0cee4b..f73ac1e83ea1d0aa4fbaab035ff8f259fb27b216 100644 (file)
@@ -2498,21 +2498,11 @@ gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
      transform all calls in the same manner as the expanders do, but
      we do transform most of them.  */
   fndecl = get_callee_fndecl (*expr_p);
-  if (fndecl && DECL_BUILT_IN (fndecl))
-    {
-      tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
-
-      if (new_tree && new_tree != *expr_p)
-       {
-         /* There was a transformation of this call which computes the
-            same value, but in a more efficient way.  Return and try
-            again.  */
-         *expr_p = new_tree;
-         return GS_OK;
-       }
-
-      if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
-         && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_VA_START)
+  if (fndecl
+      && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
+    switch (DECL_FUNCTION_CODE (fndecl))
+      {
+      case BUILT_IN_VA_START:
         {
          builtin_va_start_p = TRUE;
          if (call_expr_nargs (*expr_p) < 2)
@@ -2527,6 +2517,40 @@ gimplify_call_expr (tree *expr_p, gimple_seq *pre_p, bool want_value)
              *expr_p = build_empty_stmt (EXPR_LOCATION (*expr_p));
              return GS_OK;
            }
+         break;
+       }
+      case BUILT_IN_LINE:
+       {
+         expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
+         *expr_p = build_int_cst (TREE_TYPE (*expr_p), loc.line);
+         return GS_OK;
+       }
+      case BUILT_IN_FILE:
+       {
+         expanded_location loc = expand_location (EXPR_LOCATION (*expr_p));
+         *expr_p = build_string_literal (strlen (loc.file) + 1, loc.file);
+         return GS_OK;
+       }
+      case BUILT_IN_FUNCTION:
+       {
+         const char *function;
+         function = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
+         *expr_p = build_string_literal (strlen (function) + 1, function);
+         return GS_OK;
+       }
+      default:
+        ;
+      }
+  if (fndecl && DECL_BUILT_IN (fndecl))
+    {
+      tree new_tree = fold_call_expr (input_location, *expr_p, !want_value);
+      if (new_tree && new_tree != *expr_p)
+       {
+         /* There was a transformation of this call which computes the
+            same value, but in a more efficient way.  Return and try
+            again.  */
+         *expr_p = new_tree;
+         return GS_OK;
        }
     }
 
index 54f9cf9942322c753a5b28dbb031aea295f9806f..4bd658eb03b88077910786f7e1d0d9863c18a116 100644 (file)
@@ -1,3 +1,7 @@
+2012-09-14  Richard Guenther  <rguenther@suse.de>
+
+       * g++.dg/torture/builtin-location.C: New testcase.
+
 2012-09-13  Paolo Carlini  <paolo.carlini@oracle.com>
            Manuel López-Ibáñez  <manu@gcc.gnu.org>
 
diff --git a/gcc/testsuite/g++.dg/torture/builtin-location.C b/gcc/testsuite/g++.dg/torture/builtin-location.C
new file mode 100644 (file)
index 0000000..cad53de
--- /dev/null
@@ -0,0 +1,31 @@
+// { dg-do run }
+
+#include <cstring>
+
+const char *gfile;
+const char *gfn;
+int gline;
+
+void bar (const char *file = __builtin_FILE (),
+         const char *function = __builtin_FUNCTION (),
+         int line = __builtin_LINE ())
+{
+  gfile = file;
+  gfn = function;
+  gline = line;
+}
+
+extern "C" void abort (void);
+
+int main()
+{
+  int here;
+  bar (); here = __LINE__;
+  if (std::strcmp (gfn, __FUNCTION__) != 0)
+    abort ();
+  if (std::strcmp (gfile, __FILE__) != 0)
+    abort ();
+  if (gline != here)
+    abort ();
+  return 0;
+}