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]

[c++] pedwarn about invalid pure specifiers (e.g "= 0L")


Hi,

this patch adds the mentioned warning for invalid pure specifiers like

struct A {
  virtual int e() = 00;
  virtual int f() = 0L;
  virtual int g() = 0x0;
};

Initially I wanted to extend the interface of c_lex_with_flags to return 
the internal lexer flags (with CPP_N_OCTAL and such), then realized that 
this wouldn't catch "00", hence I anyway need the length of the token.  
But once I have this, it's also enough to identify the single valid form.  

Additionally returning the full cpp flags would create the question of 
where to store them in the cp_token stream, plus the length.  So I simply 
decided to handle just one case specially, and additionally to reuse a 
flag (STRINGIFY_ARG), which only is used in the pre-processor, and hence 
never seen by the C++ frontend to encode that special case.

Third I only made it a pedwarn, because it's obvious what the programmer 
meant, when he wrote one of the above cases.  In fact I've only seen the 
"0L" form, probably from people who somehow mix "null pointer", 
"unspecified" and "null function" and "abstract function" in their head ;)

Before giving it a full test I wanted to ask if I'm climbing up the wrong 
ark, or if something like this is okay, therefore the verbose description 
above.  (The real patch would obviously include the appropriate changes in 
the comments)


Ciao,
Michael.
-- 
Index: c-lex.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-lex.c,v
retrieving revision 1.219
diff -u -p -r1.219 c-lex.c
--- c-lex.c	11 Feb 2004 15:29:23 -0000	1.219
+++ c-lex.c	25 Feb 2004 03:25:32 -0000
@@ -424,7 +424,16 @@ c_lex_with_flags (tree *value, unsigned 
     }
 
   if (cpp_flags)
-    *cpp_flags = tok->flags;
+    {
+      *cpp_flags = tok->flags;
+      /* We should be the only one setting this, and the bit
+         this flag overlays should not be set already.  */
+      if (tok->flags & SINGLE_DIGIT)
+        abort ();
+      if (tok->type == CPP_NUMBER
+          && tok->val.str.len == 1)
+	*cpp_flags |= SINGLE_DIGIT;
+    }
   return tok->type;
 }
 
Index: cpplib.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cpplib.h,v
retrieving revision 1.278
diff -u -p -r1.278 cpplib.h
--- cpplib.h	19 Feb 2004 22:18:49 -0000	1.278
+++ cpplib.h	25 Feb 2004 03:25:32 -0000
@@ -170,6 +170,8 @@ struct cpp_string
 #define NO_EXPAND	(1 << 5) /* Do not macro-expand this token.  */
 #define BOL		(1 << 6) /* Token at beginning of line.  */
 
+#define SINGLE_DIGIT	STRINGIFY_ARG /* If this token was a single digit.  */
+
 /* A preprocessing token.  This has been carefully packed and should
    occupy 16 bytes on 32-bit hosts and 24 bytes on 64-bit hosts.  */
 struct cpp_token
Index: cp/parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.174
diff -u -p -r1.174 parser.c
--- cp/parser.c	19 Feb 2004 03:06:09 -0000	1.174
+++ cp/parser.c	25 Feb 2004 03:25:52 -0000
@@ -12718,6 +12718,8 @@ cp_parser_pure_specifier (cp_parser* par
   if (!token || !integer_zerop (token->value))
     return error_mark_node;
 
+  if (pedantic && !(token->flags & SINGLE_DIGIT))
+    pedwarn ("ISO C++ requires a literal `0' here");
   return integer_zero_node;
 }
 


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