public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [libcpp PATCH] Fix up location of builtin macros (PR c/61861)
@ 2014-07-23 13:20 Marek Polacek
  2014-07-23 15:44 ` Marek Polacek
  2014-07-25 21:21 ` Jeff Law
  0 siblings, 2 replies; 4+ messages in thread
From: Marek Polacek @ 2014-07-23 13:20 UTC (permalink / raw)
  To: GCC Patches; +Cc: Tom Tromey, Joseph S. Myers

Bultin macros like __FILE__, __DATE__, etc. had wrong locus - always
column 1.  This patch fixes it by giving those macros location
of the expansion point, i.e, the location, where builtin macro is used.
It now also does the correct thing if we do e.g.
#define F __FILE__.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2014-07-23  Marek Polacek  <polacek@redhat.com>

	PR c/61861
	* macro.c (builtin_macro): Add location parameter.  Set
	location of builtin macro to the expansion point.
	(enter_macro_context): Pass location to builtin_macro.

	* gcc.dg/pr61861.c: New test.

diff --git gcc/gcc/testsuite/gcc.dg/pr61861.c gcc/gcc/testsuite/gcc.dg/pr61861.c
index e69de29..d902868 100644
--- gcc/gcc/testsuite/gcc.dg/pr61861.c
+++ gcc/gcc/testsuite/gcc.dg/pr61861.c
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-prune-output "expected" } */
+
+extern void foo (int);
+extern void bar (int, char *);
+
+#define F __FILE__ /* { dg-error "11:passing argument" } */
+#define T __TIME__ /* { dg-error "11:passing argument" } */
+#define D __DATE__ /* { dg-error "11:passing argument" } */
+#define L __LINE__ /* { dg-error "11:passing argument" } */
+
+#define F2 "foo" /* { dg-error "12:passing argument" } */
+#define T2 "foo" /* { dg-error "12:passing argument" } */
+#define D2 "foo" /* { dg-error "12:passing argument" } */
+#define L2 42 /* { dg-error "12:passing argument" } */
+
+void
+f (void)
+{
+  foo (__FILE__); /* { dg-error "8:passing argument" } */
+  foo (__BASE_FILE__); /* { dg-error "8:passing argument" } */
+  foo (__TIME__); /* { dg-error "8:passing argument" } */
+  foo (__DATE__); /* { dg-error "8:passing argument" } */
+  foo (__TIMESTAMP__); /* { dg-error "8:passing argument" } */
+  bar (1, __LINE__); /* { dg-error "11:passing argument" } */
+  bar (__COUNTER__, __COUNTER__); /* { dg-error "21:passing argument" } */
+
+  foo (F); /* { dg-message "8:in expansion of" } */
+  foo (T); /* { dg-message "8:in expansion of" } */
+  foo (D); /* { dg-message "8:in expansion of" } */
+  bar (1, L); /* { dg-message "11:in expansion of" } */
+
+  foo (F2); /* { dg-message "8:in expansion of" } */
+  foo (T2); /* { dg-message "8:in expansion of" } */
+  foo (D2); /* { dg-message "8:in expansion of" } */
+  bar (1, L2); /* { dg-message "11:in expansion of" } */
+}
diff --git gcc/libcpp/macro.c gcc/libcpp/macro.c
index 3b8fa40..556628b 100644
--- gcc/libcpp/macro.c
+++ gcc/libcpp/macro.c
@@ -84,7 +84,7 @@ struct macro_arg_token_iter
 
 static int enter_macro_context (cpp_reader *, cpp_hashnode *,
 				const cpp_token *, source_location);
-static int builtin_macro (cpp_reader *, cpp_hashnode *);
+static int builtin_macro (cpp_reader *, cpp_hashnode *, source_location);
 static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
 				 const cpp_token **, unsigned int);
 static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
@@ -399,9 +399,10 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
 /* Convert builtin macros like __FILE__ to a token and push it on the
    context stack.  Also handles _Pragma, for which a new token may not
    be created.  Returns 1 if it generates a new token context, 0 to
-   return the token to the caller.  */
+   return the token to the caller.  LOC is the location of the expansion
+   point of the macro.  */
 static int
-builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
+builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
 {
   const uchar *buf;
   size_t len;
@@ -429,6 +430,8 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
   /* Set pfile->cur_token as required by _cpp_lex_direct.  */
   pfile->cur_token = _cpp_temp_token (pfile);
   cpp_token *token = _cpp_lex_direct (pfile);
+  /* We should point to the expansion point of the builtin macro.  */
+  token->src_loc = loc;
   if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
     {
       /* We are tracking tokens resulting from macro expansion.
@@ -1212,7 +1215,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
 
   pfile->about_to_expand_macro_p = false;
   /* Handle built-in macros and the _Pragma operator.  */
-  return builtin_macro (pfile, node);
+  return builtin_macro (pfile, node, location);
 }
 
 /* De-allocate the memory used by BUFF which is an array of instances

	Marek

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [libcpp PATCH] Fix up location of builtin macros (PR c/61861)
  2014-07-23 13:20 [libcpp PATCH] Fix up location of builtin macros (PR c/61861) Marek Polacek
@ 2014-07-23 15:44 ` Marek Polacek
  2014-07-23 15:59   ` Marek Polacek
  2014-07-25 21:21 ` Jeff Law
  1 sibling, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2014-07-23 15:44 UTC (permalink / raw)
  To: GCC Patches; +Cc: Tom Tromey, Joseph S. Myers, Dodji Seketeli

CCing Dodji, please, can you have a look?  (I don't believe it is
caused by yours
https://gcc.gnu.org/ml/gcc-patches/2014-07/msg01063.html though,
this was wrong even in 4.8 and maybe earlier.)

On Wed, Jul 23, 2014 at 03:15:53PM +0200, Marek Polacek wrote:
> Bultin macros like __FILE__, __DATE__, etc. had wrong locus - always
> column 1.  This patch fixes it by giving those macros location
> of the expansion point, i.e, the location, where builtin macro is used.
> It now also does the correct thing if we do e.g.
> #define F __FILE__.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
> 
> 2014-07-23  Marek Polacek  <polacek@redhat.com>
> 
> 	PR c/61861
> 	* macro.c (builtin_macro): Add location parameter.  Set
> 	location of builtin macro to the expansion point.
> 	(enter_macro_context): Pass location to builtin_macro.
> 
> 	* gcc.dg/pr61861.c: New test.
> 
> diff --git gcc/gcc/testsuite/gcc.dg/pr61861.c gcc/gcc/testsuite/gcc.dg/pr61861.c
> index e69de29..d902868 100644
> --- gcc/gcc/testsuite/gcc.dg/pr61861.c
> +++ gcc/gcc/testsuite/gcc.dg/pr61861.c
> @@ -0,0 +1,37 @@
> +/* { dg-do compile } */
> +/* { dg-prune-output "expected" } */
> +
> +extern void foo (int);
> +extern void bar (int, char *);
> +
> +#define F __FILE__ /* { dg-error "11:passing argument" } */
> +#define T __TIME__ /* { dg-error "11:passing argument" } */
> +#define D __DATE__ /* { dg-error "11:passing argument" } */
> +#define L __LINE__ /* { dg-error "11:passing argument" } */
> +
> +#define F2 "foo" /* { dg-error "12:passing argument" } */
> +#define T2 "foo" /* { dg-error "12:passing argument" } */
> +#define D2 "foo" /* { dg-error "12:passing argument" } */
> +#define L2 42 /* { dg-error "12:passing argument" } */
> +
> +void
> +f (void)
> +{
> +  foo (__FILE__); /* { dg-error "8:passing argument" } */
> +  foo (__BASE_FILE__); /* { dg-error "8:passing argument" } */
> +  foo (__TIME__); /* { dg-error "8:passing argument" } */
> +  foo (__DATE__); /* { dg-error "8:passing argument" } */
> +  foo (__TIMESTAMP__); /* { dg-error "8:passing argument" } */
> +  bar (1, __LINE__); /* { dg-error "11:passing argument" } */
> +  bar (__COUNTER__, __COUNTER__); /* { dg-error "21:passing argument" } */
> +
> +  foo (F); /* { dg-message "8:in expansion of" } */
> +  foo (T); /* { dg-message "8:in expansion of" } */
> +  foo (D); /* { dg-message "8:in expansion of" } */
> +  bar (1, L); /* { dg-message "11:in expansion of" } */
> +
> +  foo (F2); /* { dg-message "8:in expansion of" } */
> +  foo (T2); /* { dg-message "8:in expansion of" } */
> +  foo (D2); /* { dg-message "8:in expansion of" } */
> +  bar (1, L2); /* { dg-message "11:in expansion of" } */
> +}
> diff --git gcc/libcpp/macro.c gcc/libcpp/macro.c
> index 3b8fa40..556628b 100644
> --- gcc/libcpp/macro.c
> +++ gcc/libcpp/macro.c
> @@ -84,7 +84,7 @@ struct macro_arg_token_iter
>  
>  static int enter_macro_context (cpp_reader *, cpp_hashnode *,
>  				const cpp_token *, source_location);
> -static int builtin_macro (cpp_reader *, cpp_hashnode *);
> +static int builtin_macro (cpp_reader *, cpp_hashnode *, source_location);
>  static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
>  				 const cpp_token **, unsigned int);
>  static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
> @@ -399,9 +399,10 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
>  /* Convert builtin macros like __FILE__ to a token and push it on the
>     context stack.  Also handles _Pragma, for which a new token may not
>     be created.  Returns 1 if it generates a new token context, 0 to
> -   return the token to the caller.  */
> +   return the token to the caller.  LOC is the location of the expansion
> +   point of the macro.  */
>  static int
> -builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
> +builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
>  {
>    const uchar *buf;
>    size_t len;
> @@ -429,6 +430,8 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
>    /* Set pfile->cur_token as required by _cpp_lex_direct.  */
>    pfile->cur_token = _cpp_temp_token (pfile);
>    cpp_token *token = _cpp_lex_direct (pfile);
> +  /* We should point to the expansion point of the builtin macro.  */
> +  token->src_loc = loc;
>    if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
>      {
>        /* We are tracking tokens resulting from macro expansion.
> @@ -1212,7 +1215,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
>  
>    pfile->about_to_expand_macro_p = false;
>    /* Handle built-in macros and the _Pragma operator.  */
> -  return builtin_macro (pfile, node);
> +  return builtin_macro (pfile, node, location);
>  }
>  
>  /* De-allocate the memory used by BUFF which is an array of instances
> 
> 	Marek

	Marek

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [libcpp PATCH] Fix up location of builtin macros (PR c/61861)
  2014-07-23 15:44 ` Marek Polacek
@ 2014-07-23 15:59   ` Marek Polacek
  0 siblings, 0 replies; 4+ messages in thread
From: Marek Polacek @ 2014-07-23 15:59 UTC (permalink / raw)
  To: GCC Patches; +Cc: Tom Tromey, Joseph S. Myers, Dodji Seketeli

[Though Dodji is on a vacation for next two weeks, so if anyone else
can review this patch, it would be appreciated.]

On Wed, Jul 23, 2014 at 05:39:51PM +0200, Marek Polacek wrote:
> CCing Dodji, please, can you have a look?  (I don't believe it is
> caused by yours
> https://gcc.gnu.org/ml/gcc-patches/2014-07/msg01063.html though,
> this was wrong even in 4.8 and maybe earlier.)
> 
> On Wed, Jul 23, 2014 at 03:15:53PM +0200, Marek Polacek wrote:
> > Bultin macros like __FILE__, __DATE__, etc. had wrong locus - always
> > column 1.  This patch fixes it by giving those macros location
> > of the expansion point, i.e, the location, where builtin macro is used.
> > It now also does the correct thing if we do e.g.
> > #define F __FILE__.
> > 
> > Bootstrapped/regtested on x86_64-linux, ok for trunk?
> > 
> > 2014-07-23  Marek Polacek  <polacek@redhat.com>
> > 
> > 	PR c/61861
> > 	* macro.c (builtin_macro): Add location parameter.  Set
> > 	location of builtin macro to the expansion point.
> > 	(enter_macro_context): Pass location to builtin_macro.
> > 
> > 	* gcc.dg/pr61861.c: New test.
> > 
> > diff --git gcc/gcc/testsuite/gcc.dg/pr61861.c gcc/gcc/testsuite/gcc.dg/pr61861.c
> > index e69de29..d902868 100644
> > --- gcc/gcc/testsuite/gcc.dg/pr61861.c
> > +++ gcc/gcc/testsuite/gcc.dg/pr61861.c
> > @@ -0,0 +1,37 @@
> > +/* { dg-do compile } */
> > +/* { dg-prune-output "expected" } */
> > +
> > +extern void foo (int);
> > +extern void bar (int, char *);
> > +
> > +#define F __FILE__ /* { dg-error "11:passing argument" } */
> > +#define T __TIME__ /* { dg-error "11:passing argument" } */
> > +#define D __DATE__ /* { dg-error "11:passing argument" } */
> > +#define L __LINE__ /* { dg-error "11:passing argument" } */
> > +
> > +#define F2 "foo" /* { dg-error "12:passing argument" } */
> > +#define T2 "foo" /* { dg-error "12:passing argument" } */
> > +#define D2 "foo" /* { dg-error "12:passing argument" } */
> > +#define L2 42 /* { dg-error "12:passing argument" } */
> > +
> > +void
> > +f (void)
> > +{
> > +  foo (__FILE__); /* { dg-error "8:passing argument" } */
> > +  foo (__BASE_FILE__); /* { dg-error "8:passing argument" } */
> > +  foo (__TIME__); /* { dg-error "8:passing argument" } */
> > +  foo (__DATE__); /* { dg-error "8:passing argument" } */
> > +  foo (__TIMESTAMP__); /* { dg-error "8:passing argument" } */
> > +  bar (1, __LINE__); /* { dg-error "11:passing argument" } */
> > +  bar (__COUNTER__, __COUNTER__); /* { dg-error "21:passing argument" } */
> > +
> > +  foo (F); /* { dg-message "8:in expansion of" } */
> > +  foo (T); /* { dg-message "8:in expansion of" } */
> > +  foo (D); /* { dg-message "8:in expansion of" } */
> > +  bar (1, L); /* { dg-message "11:in expansion of" } */
> > +
> > +  foo (F2); /* { dg-message "8:in expansion of" } */
> > +  foo (T2); /* { dg-message "8:in expansion of" } */
> > +  foo (D2); /* { dg-message "8:in expansion of" } */
> > +  bar (1, L2); /* { dg-message "11:in expansion of" } */
> > +}
> > diff --git gcc/libcpp/macro.c gcc/libcpp/macro.c
> > index 3b8fa40..556628b 100644
> > --- gcc/libcpp/macro.c
> > +++ gcc/libcpp/macro.c
> > @@ -84,7 +84,7 @@ struct macro_arg_token_iter
> >  
> >  static int enter_macro_context (cpp_reader *, cpp_hashnode *,
> >  				const cpp_token *, source_location);
> > -static int builtin_macro (cpp_reader *, cpp_hashnode *);
> > +static int builtin_macro (cpp_reader *, cpp_hashnode *, source_location);
> >  static void push_ptoken_context (cpp_reader *, cpp_hashnode *, _cpp_buff *,
> >  				 const cpp_token **, unsigned int);
> >  static void push_extended_tokens_context (cpp_reader *, cpp_hashnode *,
> > @@ -399,9 +399,10 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node)
> >  /* Convert builtin macros like __FILE__ to a token and push it on the
> >     context stack.  Also handles _Pragma, for which a new token may not
> >     be created.  Returns 1 if it generates a new token context, 0 to
> > -   return the token to the caller.  */
> > +   return the token to the caller.  LOC is the location of the expansion
> > +   point of the macro.  */
> >  static int
> > -builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
> > +builtin_macro (cpp_reader *pfile, cpp_hashnode *node, source_location loc)
> >  {
> >    const uchar *buf;
> >    size_t len;
> > @@ -429,6 +430,8 @@ builtin_macro (cpp_reader *pfile, cpp_hashnode *node)
> >    /* Set pfile->cur_token as required by _cpp_lex_direct.  */
> >    pfile->cur_token = _cpp_temp_token (pfile);
> >    cpp_token *token = _cpp_lex_direct (pfile);
> > +  /* We should point to the expansion point of the builtin macro.  */
> > +  token->src_loc = loc;
> >    if (pfile->context->tokens_kind == TOKENS_KIND_EXTENDED)
> >      {
> >        /* We are tracking tokens resulting from macro expansion.
> > @@ -1212,7 +1215,7 @@ enter_macro_context (cpp_reader *pfile, cpp_hashnode *node,
> >  
> >    pfile->about_to_expand_macro_p = false;
> >    /* Handle built-in macros and the _Pragma operator.  */
> > -  return builtin_macro (pfile, node);
> > +  return builtin_macro (pfile, node, location);
> >  }
> >  
> >  /* De-allocate the memory used by BUFF which is an array of instances
> > 
> > 	Marek
> 
> 	Marek

	Marek

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [libcpp PATCH] Fix up location of builtin macros (PR c/61861)
  2014-07-23 13:20 [libcpp PATCH] Fix up location of builtin macros (PR c/61861) Marek Polacek
  2014-07-23 15:44 ` Marek Polacek
@ 2014-07-25 21:21 ` Jeff Law
  1 sibling, 0 replies; 4+ messages in thread
From: Jeff Law @ 2014-07-25 21:21 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches; +Cc: Tom Tromey, Joseph S. Myers

On 07/23/14 07:15, Marek Polacek wrote:
> Bultin macros like __FILE__, __DATE__, etc. had wrong locus - always
> column 1.  This patch fixes it by giving those macros location
> of the expansion point, i.e, the location, where builtin macro is used.
> It now also does the correct thing if we do e.g.
> #define F __FILE__.
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2014-07-23  Marek Polacek  <polacek@redhat.com>
>
> 	PR c/61861
> 	* macro.c (builtin_macro): Add location parameter.  Set
> 	location of builtin macro to the expansion point.
> 	(enter_macro_context): Pass location to builtin_macro.
>
> 	* gcc.dg/pr61861.c: New test.
This is fine for the trunk.

Thanks,
jeff

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-07-25 21:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-23 13:20 [libcpp PATCH] Fix up location of builtin macros (PR c/61861) Marek Polacek
2014-07-23 15:44 ` Marek Polacek
2014-07-23 15:59   ` Marek Polacek
2014-07-25 21:21 ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).