aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorDodji Seketeli <dodji@redhat.com>2012-05-29 09:36:29 +0000
committerDodji Seketeli <dodji@gcc.gnu.org>2012-05-29 11:36:29 +0200
commit828a7f76c76bc700ca12bf374cb535140007bd1c (patch)
tree4d1a41dbd3a96306cc1ff6a62d9d8839ec0f16ff /gcc
parent0de9dab58d9ea8f1464c3b47be6634f6d507c7f6 (diff)
downloadgcc-828a7f76c76bc700ca12bf374cb535140007bd1c.zip
gcc-828a7f76c76bc700ca12bf374cb535140007bd1c.tar.gz
gcc-828a7f76c76bc700ca12bf374cb535140007bd1c.tar.bz2
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
Diffstat (limited to 'gcc')
-rw-r--r--gcc/testsuite/ChangeLog18
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste12-2.c11
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste12.c5
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste13.c5
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste14-2.c11
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste14.c5
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste18.c16
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste6.c5
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste8-2.c15
-rw-r--r--gcc/testsuite/gcc.dg/cpp/paste8.c2
10 files changed, 88 insertions, 5 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index b08556c..1c1b28e 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,21 @@
+2012-05-29 Dodji Seketeli <dodji@redhat.com>
+
+ 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.
+
2012-05-29 Hans-Peter Nilsson <hp@axis.com>
* gcc.target/cris/torture/trap-1.c,
diff --git a/gcc/testsuite/gcc.dg/cpp/paste12-2.c b/gcc/testsuite/gcc.dg/cpp/paste12-2.c
new file mode 100644
index 0000000..6e2e4f1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/paste12-2.c
@@ -0,0 +1,11 @@
+/*
+ { dg-options "-ftrack-macro-expansion=2" }
+ { dg-do preprocess }
+ */
+
+/* Test correct diagnostics when pasting in #include.
+ Source: PR preprocessor/6780. */
+
+#define inc2(a,b) <##a.b> /* { dg-error "pasting \"<\" and \"stdio\" does not" } */
+#define INC(X) inc2(X,h)
+#include INC(stdio)
diff --git a/gcc/testsuite/gcc.dg/cpp/paste12.c b/gcc/testsuite/gcc.dg/cpp/paste12.c
index e61ec514..3e0f7b9 100644
--- a/gcc/testsuite/gcc.dg/cpp/paste12.c
+++ b/gcc/testsuite/gcc.dg/cpp/paste12.c
@@ -1,4 +1,7 @@
-/* { dg-do preprocess } */
+/*
+ { dg-options "-ftrack-macro-expansion=0" }
+ { dg-do preprocess }
+*/
/* Test correct diagnostics when pasting in #include.
Source: PR preprocessor/6780. */
diff --git a/gcc/testsuite/gcc.dg/cpp/paste13.c b/gcc/testsuite/gcc.dg/cpp/paste13.c
index 62c72d4..f0f4fd8 100644
--- a/gcc/testsuite/gcc.dg/cpp/paste13.c
+++ b/gcc/testsuite/gcc.dg/cpp/paste13.c
@@ -1,6 +1,9 @@
/* Copyright (C) 2000 Free Software Foundation, Inc. */
-/* { dg-do preprocess } */
+/*
+ { dg-options "-ftrack-macro-expansion=0" }
+ { dg-do preprocess }
+*/
/* This used to be recognized as a comment when lexing after pasting
spellings. Neil Booth, 9 Oct 2002. */
diff --git a/gcc/testsuite/gcc.dg/cpp/paste14-2.c b/gcc/testsuite/gcc.dg/cpp/paste14-2.c
new file mode 100644
index 0000000..3b23ada
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/paste14-2.c
@@ -0,0 +1,11 @@
+/* PR preprocessor/28709 */
+/*
+ { dg-options "-ftrack-macro-expansion=2" }
+ { dg-do preprocess }
+*/
+
+#define foo - ## >> /* { dg-error "pasting \"-\" and \">>\"" } */
+foo
+#define bar = ## == /* { dg-error "pasting \"=\" and \"==\"" } */
+bar
+
diff --git a/gcc/testsuite/gcc.dg/cpp/paste14.c b/gcc/testsuite/gcc.dg/cpp/paste14.c
index ec243c2..043d5e5 100644
--- a/gcc/testsuite/gcc.dg/cpp/paste14.c
+++ b/gcc/testsuite/gcc.dg/cpp/paste14.c
@@ -1,5 +1,8 @@
/* PR preprocessor/28709 */
-/* { dg-do preprocess } */
+/*
+ { dg-options "-ftrack-macro-expansion=0" }
+ { dg-do preprocess }
+*/
#define foo - ## >>
foo /* { dg-error "pasting \"-\" and \">>\"" } */
diff --git a/gcc/testsuite/gcc.dg/cpp/paste18.c b/gcc/testsuite/gcc.dg/cpp/paste18.c
new file mode 100644
index 0000000..2888144
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/paste18.c
@@ -0,0 +1,16 @@
+/*
+ { dg-options "-ftrack-macro-expansion=2" }
+ { dg-do compile }
+ */
+
+struct x {
+ int i;
+};
+struct x x;
+
+#define TEST(X) x.##X /* { dg-error "pasting\[^\n\r\]*does not give\[^\n\r\]*token" } */
+
+void foo (void)
+{
+ TEST(i) = 0;
+}
diff --git a/gcc/testsuite/gcc.dg/cpp/paste6.c b/gcc/testsuite/gcc.dg/cpp/paste6.c
index ac9ae39..a4e70e4 100644
--- a/gcc/testsuite/gcc.dg/cpp/paste6.c
+++ b/gcc/testsuite/gcc.dg/cpp/paste6.c
@@ -2,7 +2,10 @@
actual arguments. Original bug exposed by Linux kernel. Problem
reported by Jakub Jelinek <jakub@redhat.com>. */
-/* { dg-do compile } */
+/*
+ { dg-options "-ftrack-macro-expansion=0" }
+ { dg-do compile }
+*/
extern int foo(int x);
diff --git a/gcc/testsuite/gcc.dg/cpp/paste8-2.c b/gcc/testsuite/gcc.dg/cpp/paste8-2.c
new file mode 100644
index 0000000..c037e99
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/paste8-2.c
@@ -0,0 +1,15 @@
+/* { dg-do preprocess } */
+/* { dg-options "-ftrack-macro-expansion=2" } */
+
+int foo(int, ...);
+
+#define a(x, y...) foo(x, ##y)
+a(1)
+a(1, 2, 3)
+#define b(x, y, z...) foo(x, ##y) /* { dg-error "valid preprocessing token" } */
+b(1, 2, 3)
+#define c(x, y, z...) foo(x, ##z)
+c(1, 2)
+c(1, 2, 3)
+#define d(x) fo(##x) /* { dg-error "valid preprocessing token" } */
+d(1)
diff --git a/gcc/testsuite/gcc.dg/cpp/paste8.c b/gcc/testsuite/gcc.dg/cpp/paste8.c
index ab01779..db1416c 100644
--- a/gcc/testsuite/gcc.dg/cpp/paste8.c
+++ b/gcc/testsuite/gcc.dg/cpp/paste8.c
@@ -1,5 +1,5 @@
/* { dg-do preprocess } */
-/* { dg-options "" } */
+/* { dg-options "-ftrack-macro-expansion=0" } */
int foo(int, ...);