From 847c76c86abab21f77c80bf224c35869f6a12736 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Sat, 22 Jun 2002 11:08:20 +0000 Subject: [PATCH] cpptrad.c (struct fun_macro): Add line number. * cpptrad.c (struct fun_macro): Add line number. (scan_out_logical_line): Set it, and use it to report unterminated macro invocations. testsuite: * gcc.dg/cpp/trad/comment-2.c. gcc.dg/cpp/trad/funlike-2.c, gcc.dg/cpp/trad/funlike.c, gcc.dg/cpp/trad/funlike-3.c, gcc.dg/cpp/trad/literals-1.c, gcc.dg/cpp/trad/literals-2.c, gcc.dg/cpp/trad/macroargs.c, gcc.dg/cpp/trad/quote.c: New tests. From-SVN: r54907 --- gcc/ChangeLog | 6 +++++ gcc/cpptrad.c | 10 +++++--- gcc/testsuite/ChangeLog | 7 ++++++ gcc/testsuite/gcc.dg/cpp/trad/comment-2.c | 11 +++++++++ gcc/testsuite/gcc.dg/cpp/trad/funlike-2.c | 23 ++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/trad/funlike-3.c | 12 ++++++++++ gcc/testsuite/gcc.dg/cpp/trad/funlike.c | 25 +++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/trad/literals-1.c | 28 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/trad/literals-2.c | 8 +++++++ gcc/testsuite/gcc.dg/cpp/trad/macroargs.c | 24 +++++++++++++++++++ gcc/testsuite/gcc.dg/cpp/trad/quote.c | 6 +++++ 11 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/comment-2.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/funlike-2.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/funlike-3.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/funlike.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/literals-1.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/literals-2.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/macroargs.c create mode 100644 gcc/testsuite/gcc.dg/cpp/trad/quote.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index ea2f1e5028d..0d3a098ca7e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2002-06-22 Neil Booth + + * cpptrad.c (struct fun_macro): Add line number. + (scan_out_logical_line): Set it, and use it to report unterminated + macro invocations. + 2002-06-21 Kaveh R. Ghazi * genautomata.c (copy_node, VLA_PTR_CREATE, VLA_PTR_EXPAND, diff --git a/gcc/cpptrad.c b/gcc/cpptrad.c index b3413d031f0..22320c49edc 100644 --- a/gcc/cpptrad.c +++ b/gcc/cpptrad.c @@ -59,6 +59,9 @@ struct fun_macro /* The offset of the macro name in the output buffer. */ size_t offset; + /* The line the macro name appeared on. */ + unsigned int line; + /* Zero-based index of argument being currently lexed. */ unsigned int argc; }; @@ -586,6 +589,7 @@ scan_out_logical_line (pfile, macro) { maybe_start_funlike (pfile, node, out_start, &fmacro); lex_state = ls_fun_open; + fmacro.line = pfile->line; continue; } else @@ -721,9 +725,9 @@ scan_out_logical_line (pfile, macro) _cpp_release_buff (pfile, fmacro.buff); if (lex_state == ls_fun_close) - cpp_error (pfile, DL_ERROR, - "unterminated argument list invoking macro \"%s\"", - NODE_NAME (fmacro.node)); + cpp_error_with_line (pfile, DL_ERROR, fmacro.line, 0, + "unterminated argument list invoking macro \"%s\"", + NODE_NAME (fmacro.node)); } /* Push a context holding the replacement text of the macro NODE on diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 75b2189769a..52b8c9b9989 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2002-06-22 Neil Booth + + * gcc.dg/cpp/trad/comment-2.c. gcc.dg/cpp/trad/funlike-2.c, + gcc.dg/cpp/trad/funlike.c, gcc.dg/cpp/trad/funlike-3.c, + gcc.dg/cpp/trad/literals-1.c, gcc.dg/cpp/trad/literals-2.c, + gcc.dg/cpp/trad/macroargs.c, gcc.dg/cpp/trad/quote.c: New tests. + 2002-06-21 Neil Booth * gcc.dg/cpp/trad: New directory with traditional tests copied diff --git a/gcc/testsuite/gcc.dg/cpp/trad/comment-2.c b/gcc/testsuite/gcc.dg/cpp/trad/comment-2.c new file mode 100644 index 00000000000..8d54e3a0f32 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/comment-2.c @@ -0,0 +1,11 @@ +/* Test for warning of nested comments. */ + +/* { dg-do preprocess } */ + +/* { dg-options "-traditional-cpp -Wcomments" } + +/* /* */ /* { dg-warning "within comment" } */ + +/* + + /* { dg-warning "within comment" } */ diff --git a/gcc/testsuite/gcc.dg/cpp/trad/funlike-2.c b/gcc/testsuite/gcc.dg/cpp/trad/funlike-2.c new file mode 100644 index 00000000000..edcf2abf200 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/funlike-2.c @@ -0,0 +1,23 @@ +/* Test that nested commas and parentheses in macro arguments are + OK. */ + +/* { dg-do preprocess } */ + +#define f(x) x +#define g(x, y) x y + +#if f((1)) != 1 +# error /* { dg-bogus "error" "nested parens 1" } */ +#endif + +#if f((1, 2)) != 2 +# error /* { dg-bogus "error" "nested comma 1" } */ +#endif + +#if g(, (1)) != 1 +# error /* { dg-bogus "error" "nested parens 2" } */ +#endif + +#if g((1, 2), + 3) != 5 +# error /* { dg-bogus "error" "nested comma 2" } */ +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/trad/funlike-3.c b/gcc/testsuite/gcc.dg/cpp/trad/funlike-3.c new file mode 100644 index 00000000000..5300afba708 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/funlike-3.c @@ -0,0 +1,12 @@ +/* Test that function-like macros are restricted to directives, and + that unterminated ones are warned about. */ + +/* { dg-do preprocess } */ + +#define f(x) x + +#if 2 f(/* { dg-error "unterminated" "unterminated macro in directive" } */ +) +#endif + +f( /* { dg-error "unterminated" "unterminated macro" } */ diff --git a/gcc/testsuite/gcc.dg/cpp/trad/funlike.c b/gcc/testsuite/gcc.dg/cpp/trad/funlike.c new file mode 100644 index 00000000000..db550d53c76 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/funlike.c @@ -0,0 +1,25 @@ +/* Test that undefined names evaluate to zero, that macros after a + funlike macro are expanded, and that if it is a '(' the funlike + macro is not treated as such. */ + +/* { dg-do preprocess } */ + +#define f(x) x +#define h != 0 +#define i +#define paren ( + +#if f != 0 +# error /* { dg-bogus "error" "undefined name" } */ +#endif + +#if f h +# error /* { dg-bogus "error" "h not expanded" } */ +#endif + +#if f i +# error /* { dg-bogus "error" "empty macro" } */ +#endif + +#if f paren 6) /* { dg-error "missing binary" "macro-expanded parenthesis" } */ +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/trad/literals-1.c b/gcc/testsuite/gcc.dg/cpp/trad/literals-1.c new file mode 100644 index 00000000000..7fde0d5422e --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/literals-1.c @@ -0,0 +1,28 @@ +/* Test that (what looks like) comments are not recognised in literals + and that quotes within quotes do not confused the preprocessor. */ + +/* { dg-do run } */ + +extern void abort (void); + +int main () +{ + const char *str1 = "/*"; + const char *str2 = "'"; + + if (str1[0] != '/' || str1[1] != '*' || str1[2] != '\0') + abort (); + + if (str2[0] != '\'' || str2[1] != '\0') + abort (); + +#if '"' != '\"' +# error /* { dg-bogus "error" "double quote in charconst" } */ +#endif + +#if !'\'' +# error quote /* { dg-bogus "quote" "quote in charconst" } */ +#endif + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/trad/literals-2.c b/gcc/testsuite/gcc.dg/cpp/trad/literals-2.c new file mode 100644 index 00000000000..807bb0db0a8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/literals-2.c @@ -0,0 +1,8 @@ +/* Test that unterminated quotes in CPP expressions are + recognized. */ + +/* { dg-do preprocess } */ + +/* { dg-error "missing terminating" "bad charconst" { target *-*-* } 7 } */ +#if 'x +#endif diff --git a/gcc/testsuite/gcc.dg/cpp/trad/macroargs.c b/gcc/testsuite/gcc.dg/cpp/trad/macroargs.c new file mode 100644 index 00000000000..12effa75004 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/macroargs.c @@ -0,0 +1,24 @@ +/* Test that whitespace in arguments is preserved, and that each + newline in macro arguments become a space. */ + +/* { dg-do run } */ + +#define f(x, y) "x y" + +extern void abort (void); + +int main () +{ + const char *str1 = f( foo ,bar); + const char *str2 = f( +foo +,bar); + + if (strcmp (str1, " foo bar")) + abort (); + + if (strcmp (str1, str2)) + abort (); + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/trad/quote.c b/gcc/testsuite/gcc.dg/cpp/trad/quote.c new file mode 100644 index 00000000000..f8e61700fd7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/trad/quote.c @@ -0,0 +1,6 @@ +/* Test that unterminated quotes are OK when only preprocessing. */ + +/* { dg-do preprocess } */ + +/* { dg-bogus "unterminated" } */ 'x +/* { dg-bogus "unterminated" } */ "x -- 2.30.2