Bug 48248 - [4.5 Regression] Wrong error message location when compiling preprocessed code
Summary: [4.5 Regression] Wrong error message location when compiling preprocessed code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: 4.5.2
: P2 normal
Target Milestone: 4.5.3
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-23 08:47 UTC by Jörg Richter
Modified: 2011-04-26 10:14 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.5.2
Last reconfirmed: 2011-03-23 09:41:27


Attachments
gcc46-pr48248.patch (808 bytes, patch)
2011-03-28 18:49 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jörg Richter 2011-03-23 08:47:37 UTC
$ cat > foobar.h << EOF
enum Foo
{
  BAR
};
#define BAR(x) 
#define BARBAR BAR
EOF

$ cat > main.cc << EOF
#include "foobar.h"



void func()
{
  (void)BARBAR;
  aaa;
}
EOF

$ g++ -c main.cc
main.cc: In function 'void func()':
main.cc:8:3: error: 'aaa' was not declared in this scope

$ g++ -E main.cc > main.ii
$ g++ -c main.ii
foobar.h: In function 'void func()':
foobar.h:8:3: error: 'aaa' was not declared in this scope

Please notice the wrong filename when compiling the preprocessed source.

GCC 4.4 doesn't have this problem.
Comment 1 Richard Biener 2011-03-23 09:41:27 UTC
Confirmed.

> g++-4.5 -S foo.c -save-temps
foobar.h: In function ‘void func()’:
foobar.h:8:3: error: ‘aaa’ was not declared in this scope

and preprocessed source looks like

# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "foo.c"
# 1 "foobar.h" 1
enum Foo
{
    BAR
};
# 2 "foo.c" 2



void func()
{
  (void)
# 6 "foobar.h"
               BAR
              ;
  aaa;
}

we miss to switch back to foo.c.  4.4 does not switch to foobar.h at all.
Comment 2 Jakub Jelinek 2011-03-28 13:47:05 UTC
Can't reproduce this, neither with g++ 4.5, nor trunk.
Comment 3 Jörg Richter 2011-03-28 13:51:55 UTC
(In reply to comment #2)
> Can't reproduce this, neither with g++ 4.5, nor trunk.

Did you delete the empty lines?
Comment 4 Jakub Jelinek 2011-03-28 16:15:52 UTC
Ah, I can reproduce with those lines.
Apparently caused by my PR41445 fix.

To fix this, I think we should remember not just src_line, but also filename
in print variable in c-ppoutput.c.  And, either we should avoid calling do_line_change for avoid_paste resp. PREV_WHITE in scan_translation_unit if file is different, or maybe_print_line should do the
if (src_line >= print.src_line && src_line < print.src_line + 8)
optimization only if it is the same file.

As PR41445 has been in already in 4.5 and nobody complained about e.g.
# 1 "pr48248-2.C"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "pr48248-2.C"
# 1 "pr48248.h" 1
enum E { B };
# 2 "pr48248-2.C" 2
# 17 "pr48248-2.C"
void
foo ()
{
  (void)
# 3 "pr48248.h"
           B
# 20 "pr48248-2.C"
          ;
  a;
}

(where # 3 "pr48248.h" doesn't say that the header is being entered, just
temporarily jumps to it), I'd probably prefer the latter choice, as it gives more correct locus info.
Comment 5 Jakub Jelinek 2011-03-28 18:49:06 UTC
Created attachment 23792 [details]
gcc46-pr48248.patch

Untested fix (the second alternative).
Comment 6 Jakub Jelinek 2011-03-29 20:25:32 UTC
Author: jakub
Date: Tue Mar 29 20:25:27 2011
New Revision: 171693

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171693
Log:
	PR preprocessor/48248
	* c-ppoutput.c (print): Add src_file field.
	(init_pp_output): Initialize it.
	(maybe_print_line): Don't optimize by adding up to 8 newlines
	if map->to_file and print.src_file are different file.
	(print_line): Update print.src_file.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-family/c-ppoutput.c
Comment 7 Jakub Jelinek 2011-03-29 20:30:14 UTC
Author: jakub
Date: Tue Mar 29 20:30:06 2011
New Revision: 171697

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=171697
Log:
	PR preprocessor/48248
	* c-ppoutput.c (print): Add src_file field.
	(init_pp_output): Initialize it.
	(maybe_print_line): Don't optimize by adding up to 8 newlines
	if map->to_file and print.src_file are different file.
	(print_line): Update print.src_file.

Modified:
    branches/gcc-4_6-branch/gcc/c-family/ChangeLog
    branches/gcc-4_6-branch/gcc/c-family/c-ppoutput.c
Comment 8 Jakub Jelinek 2011-03-29 20:42:32 UTC
Fixed for 4.6.1+ so far.
Comment 9 Richard Biener 2011-04-19 09:14:08 UTC
Author: rguenth
Date: Tue Apr 19 09:14:05 2011
New Revision: 172701

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172701
Log:
2011-04-18  Richard Guenther  <rguenther@suse.de>

	PR preprocessor/48248
	* c-ppoutput.c (maybe_print_line): Avoid changing -P behavior.

Modified:
    branches/gcc-4_6-branch/gcc/c-family/ChangeLog
    branches/gcc-4_6-branch/gcc/c-family/c-ppoutput.c
Comment 10 Richard Biener 2011-04-19 09:19:35 UTC
Author: rguenth
Date: Tue Apr 19 09:19:33 2011
New Revision: 172703

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172703
Log:
2011-04-19  Richard Guenther  <rguenther@suse.de>

	Backported from 4.6 branch
	2011-03-29  Jakub Jelinek  <jakub@redhat.com>

	PR preprocessor/48248
	* c-ppoutput.c (print): Add src_file field.
	(init_pp_output): Initialize it.
	(maybe_print_line): Don't optimize by adding up to 8 newlines
	if map->to_file and print.src_file are different file.
	(print_line): Update print.src_file.

	2011-04-18  Richard Guenther  <rguenther@suse.de>

	PR preprocessor/48248
	* c-ppoutput.c (maybe_print_line): Avoid changing -P behavior.

Modified:
    branches/gcc-4_5-branch/gcc/ChangeLog
    branches/gcc-4_5-branch/gcc/c-ppoutput.c
Comment 11 Richard Biener 2011-04-19 09:36:01 UTC
Fixed.
Comment 12 Richard Biener 2011-04-26 10:14:37 UTC
Author: rguenth
Date: Tue Apr 26 10:14:34 2011
New Revision: 172959

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172959
Log:
2011-04-26  Richard Guenther  <rguenther@suse.de>

	PR preprocessor/48248
	* c-ppoutput.c (maybe_print_line): Always optimize newlines
	for output size with -P.

Modified:
    trunk/gcc/c-family/ChangeLog
    trunk/gcc/c-family/c-ppoutput.c