aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorKaveh R. Ghazi <ghazi@caip.rutgers.edu>2000-09-10 03:41:50 +0000
committerKaveh Ghazi <ghazi@gcc.gnu.org>2000-09-10 03:41:50 +0000
commite1aa5140912178a5711762be987a0c10aad42e31 (patch)
tree7c23b23e1e31bfbc438ca968e64e66b8dfefe79b /gcc
parentafa1738b588b778082a43c3c933f8a4b412a831d (diff)
downloadgcc-e1aa5140912178a5711762be987a0c10aad42e31.zip
gcc-e1aa5140912178a5711762be987a0c10aad42e31.tar.gz
gcc-e1aa5140912178a5711762be987a0c10aad42e31.tar.bz2
cppmacro.c (check_trad_stringification): New function.
* cppmacro.c (check_trad_stringification): New function. (save_expansion): If -Wtraditional, warn about stringification of macro arguments. testsuite: * gcc.dg/cpp/tr-warn6.c: New test. From-SVN: r36285
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog6
-rw-r--r--gcc/cppmacro.c52
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/gcc.dg/cpp/tr-warn6.c27
4 files changed, 89 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 50a5643..f5e5062 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2000-09-09 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
+
+ * cppmacro.c (check_trad_stringification): New function.
+ (save_expansion): If -Wtraditional, warn about stringification of
+ macro arguments.
+
2000-09-11 Michael Hayes <mhayes@cygnus.com>
* loop.h (struct loop_mem_info): Move from loop.c
diff --git a/gcc/cppmacro.c b/gcc/cppmacro.c
index 3c9ffb9..2b16fd47 100644
--- a/gcc/cppmacro.c
+++ b/gcc/cppmacro.c
@@ -52,6 +52,9 @@ static const cpp_toklist * save_expansion PARAMS((cpp_reader *,
static unsigned int find_param PARAMS ((const cpp_token *,
const cpp_token *));
static cpp_toklist * alloc_macro PARAMS ((cpp_reader *, struct macro_info *));
+static void check_trad_stringification PARAMS ((cpp_reader *,
+ const struct macro_info *,
+ const cpp_string *));
/* These are all the tokens that can have something pasted after them.
Comma is included in the list only to support the GNU varargs extension
@@ -502,6 +505,12 @@ save_expansion (pfile, info)
continue;
break;
+ case CPP_STRING:
+ case CPP_CHAR:
+ if (CPP_WTRADITIONAL (pfile) && list->paramc > 0)
+ check_trad_stringification (pfile, info, &token->val.str);
+ break;
+
default:
break;
}
@@ -618,3 +627,46 @@ dump_macro_args (fp, list)
}
putc (')', fp);
}
+
+/* Warn if a token in `string' matches one of the function macro
+ arguments in `info'. This function assumes that the macro is a
+ function macro and not an object macro. */
+static void
+check_trad_stringification (pfile, info, string)
+ cpp_reader *pfile;
+ const struct macro_info *info;
+ const cpp_string *string;
+{
+ const U_CHAR *p, *q, *limit = string->text + string->len;
+
+ /* Loop over the string. */
+ for (p = string->text; p < limit; p = q)
+ {
+ const cpp_token *token;
+
+ /* Find the start of an identifier. */
+ while (!is_idstart (*p) && p < limit) p++;
+
+ /* Find the end of the identifier. */
+ q = p;
+ while (is_idchar (*q) && q < limit) q++;
+
+ /* Loop over the function macro arguments to see if the
+ identifier inside the string matches one of them. */
+ for (token = info->first_param; token < info->first; token++)
+ {
+ /* Skip the commas in between the arguments. */
+ if (token->type != CPP_NAME)
+ continue;
+
+ if (token->val.node->length == (q - p)
+ && !memcmp (p, token->val.node->name, (q - p)))
+ {
+ cpp_warning (pfile,
+ "macro arg \"%.*s\" would be stringified with -traditional.",
+ (int) (q - p), p);
+ break;
+ }
+ }
+ }
+}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index ff26a52..830af37 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2000-09-09 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
+
+ * gcc.dg/cpp/tr-warn6.c: New test
+
2000-09-07 Joseph S. Myers <jsm28@cam.ac.uk>
* gcc.dg/format-ext-3.c: Test %#b and %#h as formats that should
diff --git a/gcc/testsuite/gcc.dg/cpp/tr-warn6.c b/gcc/testsuite/gcc.dg/cpp/tr-warn6.c
new file mode 100644
index 0000000..4068cc5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/tr-warn6.c
@@ -0,0 +1,27 @@
+/* Test for -Wtraditional warnings for stringification of macro args.
+ Note, gcc should omit these warnings in system header files.
+ By Kaveh R. Ghazi <ghazi@caip.rutgers.edu> 9/8/2000. */
+/* { dg-do preprocess } */
+/* { dg-options "-Wtraditional -fno-show-column" } */
+
+#define foo1(h) sdf "h3" fds "h" /* { dg-warning "macro arg \"h\" would be stringified" "traditional stringification" } */
+#define foo2(h2) sdf "h2" fds "h3" /* { dg-warning "macro arg \"h2\" would be stringified" "traditional stringification" } */
+#define foo3(h3) sdf "h2" fds "h3" /* { dg-warning "macro arg \"h3\" would be stringified" "traditional stringification" } */
+#define foo4(h) sdf 'h3' fds 'h' /* { dg-warning "macro arg \"h\" would be stringified" "traditional stringification" } */
+#define foo5(h2) sdf 'h2' fds 'h3' /* { dg-warning "macro arg \"h2\" would be stringified" "traditional stringification" } */
+#define foo6(h3) sdf 'h2' fds 'h3' /* { dg-warning "macro arg \"h3\" would be stringified" "traditional stringification" } */
+#define foo7(AA, hello, world, EEE) sdf "A B hello C,world,DhelloE F" fds EEE /* { dg-warning "macro arg \"hello\" would be stringified" "traditional stringification" } */
+
+/* Catch the second warning from the above line. */
+/* { dg-warning "macro arg \"world\" would be stringified" "traditional stringification" { target *-*-* } 13 } */
+
+#line 19 "sys-header.h" 3
+/* We are in system headers now, no -Wtraditional warnings should issue. */
+
+#define bar1(h) sdf "h3" fds "h"
+#define bar2(h2) sdf "h2" fds "h3"
+#define bar3(h3) sdf "h2" fds "h3"
+#define bar4(h) sdf 'h3' fds 'h'
+#define bar5(h2) sdf 'h2' fds 'h3'
+#define bar6(h3) sdf 'h2' fds 'h3'
+#define bar7(AA, hello, world, EEE) sdf "A B hello C,world,DhelloE F" fds EEE