This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C PATCH] Diagnose predefined identifiers in pedantic mode


On Tue, Aug 12, 2014 at 05:46:09PM +0000, Joseph S. Myers wrote:
> On Sun, 10 Aug 2014, Marek Polacek wrote:
> 
> > We weren't properly diagnosing neither the __func__ (introduced in C99),
> > nor the __FUNCTION__/__PRETTY_FUNCTION__ (GNU extension) predefined
> > identifiers.  I believe we should; the compiler ought to have a
> > compile-time switch for turning off extensions.
> > 
> > Bootstrapped/regtested on x86_64-linux, ok for trunk?
> 
> OK presuming uses of these in macros defined in system headers, such as 
> assert(), do not result in -pedantic diagnostics even when those macros do 
> not use __extension__.  (Tests verifying that would be a good idea.)

Correct, we don't pedwarn in such a scenario (verified even with
-Wc90-c99-compat).  I added a test for that.

Applying the following to trunk.

2014-08-12  Marek Polacek  <polacek@redhat.com>

	* c-parser.c (c_parser_postfix_expression) <case RID_FUNCTION_NAME>:
	Add pedwarn.
	(c_parser_postfix_expression) <case RID_PRETTY_FUNCTION_NAME>:
	Likewise.
	(c_parser_postfix_expression) <case RID_C99_FUNCTION_NAME>: Likewise.

	* gcc.dg/concat.c: Add dg-options.
	* gcc.dg/func-outside-2.c: Add __extension__.
	* gcc.dg/pr19967.c: Use -std=c99.
	* gcc.dg/pr22458-1.c: Add dg-options.
	* gcc.dg/pr33676.c: Add dg-options.
	* gcc.dg/gnu-predef-1.c: New test.
	* gcc.dg/c90-func-1.c: New test.
	* gcc.dg/c90-func-2.c: New test.
	* gcc.dg/c90-func-2.h: New test.

diff --git gcc/c/c-parser.c gcc/c/c-parser.c
index ca8577c..5f23379 100644
--- gcc/c/c-parser.c
+++ gcc/c/c-parser.c
@@ -7136,8 +7136,24 @@ c_parser_postfix_expression (c_parser *parser)
       switch (c_parser_peek_token (parser)->keyword)
 	{
 	case RID_FUNCTION_NAME:
+	  pedwarn (loc, OPT_Wpedantic,  "ISO C does not support "
+		   "%<__FUNCTION__%> predefined identifier");
+	  expr.value = fname_decl (loc,
+				   c_parser_peek_token (parser)->keyword,
+				   c_parser_peek_token (parser)->value);
+	  c_parser_consume_token (parser);
+	  break;
 	case RID_PRETTY_FUNCTION_NAME:
+	  pedwarn (loc, OPT_Wpedantic,  "ISO C does not support "
+		   "%<__PRETTY_FUNCTION__%> predefined identifier");
+	  expr.value = fname_decl (loc,
+				   c_parser_peek_token (parser)->keyword,
+				   c_parser_peek_token (parser)->value);
+	  c_parser_consume_token (parser);
+	  break;
 	case RID_C99_FUNCTION_NAME:
+	  pedwarn_c90 (loc, OPT_Wpedantic,  "ISO C90 does not support "
+		   "%<__func__%> predefined identifier");
 	  expr.value = fname_decl (loc,
 				   c_parser_peek_token (parser)->keyword,
 				   c_parser_peek_token (parser)->value);
diff --git gcc/testsuite/gcc.dg/c90-func-1.c gcc/testsuite/gcc.dg/c90-func-1.c
index e69de29..5f171d3 100644
--- gcc/testsuite/gcc.dg/c90-func-1.c
+++ gcc/testsuite/gcc.dg/c90-func-1.c
@@ -0,0 +1,10 @@
+/* Test that we diagnose the __func__ predefined identifier in
+   C90 pedantic mode.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+void
+foo (void)
+{
+  const char *s = __func__; /* { dg-error " ISO C90 does not support .__func__. predefined identifier" } */
+}
diff --git gcc/testsuite/gcc.dg/c90-func-2.c gcc/testsuite/gcc.dg/c90-func-2.c
index e69de29..5185e35 100644
--- gcc/testsuite/gcc.dg/c90-func-2.c
+++ gcc/testsuite/gcc.dg/c90-func-2.c
@@ -0,0 +1,12 @@
+/* Test that we don't pedwarn about __func__ predefined identifier in
+   a system header in C90 pedantic mode.  */
+/* { dg-do compile } */
+/* { dg-options "-std=iso9899:1990 -pedantic-errors" } */
+
+#include "c90-func-2.h"
+
+void
+foo (void)
+{
+  const char *s = FN;
+}
diff --git gcc/testsuite/gcc.dg/c90-func-2.h gcc/testsuite/gcc.dg/c90-func-2.h
index e69de29..2bad1bd 100644
--- gcc/testsuite/gcc.dg/c90-func-2.h
+++ gcc/testsuite/gcc.dg/c90-func-2.h
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+#define FN __func__
diff --git gcc/testsuite/gcc.dg/concat.c gcc/testsuite/gcc.dg/concat.c
index 0b9d6f6..e3bfd46 100644
--- gcc/testsuite/gcc.dg/concat.c
+++ gcc/testsuite/gcc.dg/concat.c
@@ -1,6 +1,7 @@
 /* Copyright (C) 2001 Free Software Foundation, Inc.  */
 
 /* { dg-do compile } */
+/* { dg-options "" } */
 
 /* Test we output an error for concatenation of artificial strings.
 
diff --git gcc/testsuite/gcc.dg/func-outside-2.c gcc/testsuite/gcc.dg/func-outside-2.c
index be3b099..28ef6bc 100644
--- gcc/testsuite/gcc.dg/func-outside-2.c
+++ gcc/testsuite/gcc.dg/func-outside-2.c
@@ -4,6 +4,6 @@
 /* { dg-do compile } */
 /* { dg-options "-pedantic-errors" } */
 
-const char *a = __func__; /* { dg-error "'__func__' is not defined outside of function scope" "undef" } */
-const char *b = __FUNCTION__;
-const char *c = __PRETTY_FUNCTION__;
+__extension__ const char *a = __func__; /* { dg-error "'__func__' is not defined outside of function scope" "undef" } */
+__extension__ const char *b = __FUNCTION__;
+__extension__ const char *c = __PRETTY_FUNCTION__;
diff --git gcc/testsuite/gcc.dg/gnu-predef-1.c gcc/testsuite/gcc.dg/gnu-predef-1.c
index e69de29..9c7eebf 100644
--- gcc/testsuite/gcc.dg/gnu-predef-1.c
+++ gcc/testsuite/gcc.dg/gnu-predef-1.c
@@ -0,0 +1,14 @@
+/* Test that we diagnose the __FUNCTION__ and the __PRETTY_FUNCTION__
+   predefined identifiers in pedantic mode.  */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11 -pedantic" } */
+
+void
+foo (void)
+{
+  const char *s;
+  s = __FUNCTION__; /* { dg-warning " ISO C does not support .__FUNCTION__. predefined identifier" } */
+  s = __PRETTY_FUNCTION__; /* { dg-warning " ISO C does not support .__PRETTY_FUNCTION__. predefined identifier" } */
+  s = __extension__ __FUNCTION__;
+  s = __extension__ __PRETTY_FUNCTION__;
+}
diff --git gcc/testsuite/gcc.dg/pr19967.c gcc/testsuite/gcc.dg/pr19967.c
index 85afeaf..68c7e1c 100644
--- gcc/testsuite/gcc.dg/pr19967.c
+++ gcc/testsuite/gcc.dg/pr19967.c
@@ -4,7 +4,7 @@
    be const char *.  */
 
 /* { dg-do compile } */
-/* { dg-options "-pedantic" } */
+/* { dg-options "-pedantic -std=c99" } */
 
 char *strchr(const char *, int);
 char *strrchr(const char *, int);
diff --git gcc/testsuite/gcc.dg/pr22458-1.c gcc/testsuite/gcc.dg/pr22458-1.c
index 8b8032c..023fb21 100644
--- gcc/testsuite/gcc.dg/pr22458-1.c
+++ gcc/testsuite/gcc.dg/pr22458-1.c
@@ -1,4 +1,6 @@
 /* { dg-error "expected declaration or statement" "" { target *-*-* } 0 } */
+/* { dg-options "" } */
+
 void foo()
 {
      __PRETTY_FUNCTION__;
diff --git gcc/testsuite/gcc.dg/pr33676.c gcc/testsuite/gcc.dg/pr33676.c
index 79c830e..c234470 100644
--- gcc/testsuite/gcc.dg/pr33676.c
+++ gcc/testsuite/gcc.dg/pr33676.c
@@ -1,4 +1,5 @@
 /* { dg-do run } */
+/* { dg-options "" } */
 /* { dg-options "-O0 -mtune=i386 -fomit-frame-pointer" { target { { i?86-*-* x86_64-*-* } && ia32 } } } */
 
 __attribute__((noreturn,noinline)) void abrt (const char *fi, const char *fu)

	Marek


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]