Find a file
Dodji Seketeli 828a7f76c7 PR preprocessor/53229 - Fix diagnostics location when pasting tokens
As stated in the audit trail of this problem report, consider this
test case:

    $ cat test.c
	 1	struct x {
	 2	  int i;
	 3	};
	 4	struct x x;
	 5
	 6	#define TEST(X) x.##X
	 7
	 8	void foo (void)
	 9	{
	10	  TEST(i) = 0;
	11	}
    $

    $ cc1 -quiet test.c
    test.c: In function 'foo':
    test.c:10:1: error: pasting "." and "i" does not give a valid preprocessing token
       TEST(i) = 0;
     ^
    $

So, when pasting tokens, the error diagnostic uses the global and
imprecise input_location variable, leading to an imprecise output.

To properly fix this, I think libcpp should keep the token of the
pasting operator '##', instead of representing it with flag on the LHS
operand's token.  That way, it could use its location.  Doing that
would be quite intrusive though.  So this patch just uses the location
of the LHS of the pasting operator, for now.  It's IMHO better than
the current situation.

The patch makes paste_tokens take a location parameter that is used in
the diagnostics.  This change can still be useful later when we can
use the location of the pasting operator, because paste_tokens will
just be passed the new, more precise location.

Incidentally, it appeared that when getting tokens from within
preprocessor directives (like what is done in gcc.dg/cpp/paste12.c),
with -ftrack-macro-expansion disabled, the location of the expansion
point of macros was being lost because
cpp_reader::set_invocation_location wasn't being properly set.  It's
because when cpp_get_token_1 calls enter_macro_context, there is a
little period of time between the beginning of that later function and
when the macro is really pushed (and thus when the macro is really
expanded) where we wrongly consider that we are not expanding the
macro because macro_of_context is still NULL.  In that period of time,
in the occurrences of indirect recursive calls to cpp_get_token_1,
this later function wrongly sets cpp_reader::invocation_location
because cpp_reader::set_invocation_location is not being properly set.

To avoid that confusion the patch does away with
cpp_reader::set_invocation_location and introduces a new flag
cpp_reader::about_to_expand_macro_p that is set in the small time
interval exposed earlier.  A new in_macro_expansion_p is introduced as
well, so that cpp_get_token_1 can now accurately detect when we are in
the process of expanding a macro, and thus correctly collect the
location of the expansion point.

People seem to like screenshots.

Thus, after the patch, we now have:

    $ cc1 -quiet test.c
    test.c: In function 'foo':
    test.c:6:18: error: pasting "." and "i" does not give a valid preprocessing token
     #define TEST(X) x.##X
		      ^
    test.c:10:3: note: in expansion of macro 'TEST'
       TEST(i) = 0;
       ^
    $

Bootstrapped and tested on x86_64-unknown-linux-gnu against trunk.

libcpp/

	PR preprocessor/53229
	* internal.h (cpp_reader::set_invocation_location): Remove.
	(cpp_reader::about_to_expand_macro_p): New member flag.
	* directives.c (do_pragma):  Remove Kludge as
	pfile->set_invocation_location is no more.
	* macro.c (cpp_get_token_1): Do away with the use of
	cpp_reader::set_invocation_location.  Just collect the macro
	expansion point when we are about to expand the top-most macro.
	Do not override cpp_reader::about_to_expand_macro_p.
	This fixes gcc.dg/cpp/paste12.c by making get_token_no_padding
	properly handle locations of expansion points.
	(cpp_get_token_with_location): Adjust, as
	cpp_reader::set_invocation_location is no more.
	(paste_tokens): Take a virtual location parameter for
	the LHS of the pasting operator.  Use it in diagnostics.  Update
	comments.
	(paste_all_tokens): Tighten the assert.  Propagate the location of
	the expansion point when no virtual locations are available.
	Pass the virtual location to paste_tokens.
	(in_macro_expansion_p): New static function.
	(enter_macro_context): Set the cpp_reader::about_to_expand_macro_p
	flag until we really start expanding the macro.

gcc/testsuite/

	PR preprocessor/53229
	* gcc.dg/cpp/paste6.c: Force to run without
	-ftrack-macro-expansion.
	* gcc.dg/cpp/paste8.c: Likewise.
	* gcc.dg/cpp/paste8-2.c: New test, like paste8.c but run with
	-ftrack-macro-expansion.
	* gcc.dg/cpp/paste12.c: Force to run without
	-ftrack-macro-expansion.
	* gcc.dg/cpp/paste12-2.c: New test, like paste12.c but run with
	-ftrack-macro-expansion.
	* gcc.dg/cpp/paste13.c: Likewise.
	* gcc.dg/cpp/paste14.c: Likewise.
	* gcc.dg/cpp/paste14-2.c: New test, like paste14.c but run with
	-ftrack-macro-expansion.
	* gcc.dg/cpp/paste18.c: New test.

From-SVN: r187945
2012-05-29 11:36:29 +02:00
boehm-gc Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
config mh-ppc-aix (LDFLAGS): Quote $(CC). 2012-05-03 15:39:07 +00:00
contrib Remove any .comment sections if the first cmp failed 2012-05-25 04:47:05 -07:00
fixincludes fixincl.c (fix_with_system): Add missing specifier. 2012-04-24 09:24:55 +00:00
gcc PR preprocessor/53229 - Fix diagnostics location when pasting tokens 2012-05-29 11:36:29 +02:00
gnattools Remove obsolete IRIX 6.5 support 2012-03-14 16:33:37 +00:00
include leb128.h: #include stdint.h, inttypes.h. 2012-05-23 23:42:25 +00:00
INSTALL
intl
libada Adjust. 2012-05-06 09:24:21 +00:00
libatomic re PR other/53231 (libatomic/tas_n.c💯10: error: 'ret' undeclared (first use in this function)) 2012-05-22 23:54:32 +00:00
libcpp PR preprocessor/53229 - Fix diagnostics location when pasting tokens 2012-05-29 11:36:29 +02:00
libdecnumber warnings.m4 (ACX_PROG_CC_WARNING_OPTS): Avoid leading dash in expr call. 2011-12-20 16:54:12 +00:00
libffi Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
libgcc morestack.S (__morestack_non_split): Check whether caller is varargs and needs %bp to hold the stack frame on return. 2012-05-25 20:48:19 +00:00
libgfortran Minor timing cleanups. 2012-05-27 10:09:15 +03:00
libgo runtime: More efficient implementation of trampolines. 2012-05-25 21:51:39 +00:00
libgomp Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
libiberty demangle-expected: Add regression test. 2012-05-22 13:55:02 +00:00
libitm eh_cpp.cc: Fix __cxa_end_catch declaration. 2012-05-21 22:48:00 +00:00
libjava re PR bootstrap/52700 (lib* configure fails on --enable-symvers=gnu-versioned-namespace.) 2012-05-21 17:34:25 +00:00
libmudflap Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
libobjc Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
libquadmath Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
libssp Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
libstdc++-v3 re PR c++/53503 ([C++0x] unexpected AST of kind ltgt_expr) 2012-05-28 12:09:01 +00:00
lto-plugin Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
maintainer-scripts crontab: Enable snapshots from gcc-4_7-branch. 2012-03-22 09:25:49 +00:00
zlib Regenerate configure files for libtool.m4 change 2012-05-16 15:11:18 -07:00
ABOUT-NLS
ChangeLog Makefile.tpl (gcc-no-fixedincludes): Rename into ... 2012-05-16 13:37:14 +00:00
ChangeLog.tree-ssa
compile
config-ml.in MAINTAINERS (crx port, [...]): Remove. 2011-03-22 19:58:18 +00:00
config.guess oops - omitted from previous delta. 2011-06-06 10:34:35 +00:00
config.rpath Remove freebsd1 from libtool.m4 macros and config.rpath. 2011-02-13 11:45:53 +00:00
config.sub Update config.sub to 2012-04-18 version from official repo. 2012-04-25 15:48:28 +00:00
configure re PR bootstrap/50461 (mpfr.h found in mpfr-3.1.0/src instead of mpfr-3.0.1/. as previously) 2012-05-09 16:20:17 +00:00
configure.ac re PR bootstrap/50461 (mpfr.h found in mpfr-3.1.0/src instead of mpfr-3.0.1/. as previously) 2012-05-09 16:20:17 +00:00
COPYING
COPYING.LIB
COPYING.RUNTIME
COPYING3
COPYING3.LIB
depcomp
install-sh
libtool-ldflags
libtool.m4 Add x32 support to libtool.m4 2012-05-15 09:07:28 -07:00
ltgcc.m4
ltmain.sh backport: ltmain.sh (relink): Use absolute path when hardcoding with -L. 2011-01-13 18:51:10 +00:00
ltoptions.m4
ltsugar.m4
ltversion.m4
lt~obsolete.m4
MAINTAINERS MAINTAINERS (Write After Approval): Add myself. 2012-05-14 20:28:00 +00:00
Makefile.def Add libatomic as a target library. 2012-05-01 08:48:28 -07:00
Makefile.in Makefile.tpl (gcc-no-fixedincludes): Rename into ... 2012-05-16 13:37:14 +00:00
Makefile.tpl Makefile.tpl (gcc-no-fixedincludes): Rename into ... 2012-05-16 13:37:14 +00:00
missing
mkdep
mkinstalldirs
move-if-change Import move-if-change script from gnulib. 2011-02-12 15:48:30 +00:00
README
symlink-tree
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.