From 909eb89ca5b48b9a5645358e1cb5c949ec3f1edb Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 17 Sep 2014 21:49:46 +0000 Subject: [PATCH] re PR c/61854 (Warning single-line comment for -std=c89?) PR c/61854 libcpp/ * init.c (struct lang_flags): Remove cplusplus_comments. (cpp_set_lang): Likewise. (post_options): Likewise. * lex.c (_cpp_lex_direct): Disallow C++ style comments in C90/C94. testsuite/ * gcc.dg/cpp/pr61854-1.c: New test. * gcc.dg/cpp/pr61854-2.c: New test. * gcc.dg/cpp/pr61854-3.c: New test. * gcc.dg/cpp/pr61854-3.h: New test. * gcc.dg/cpp/pr61854-4.c: New test. * gcc.dg/cpp/pr61854-5.c: New test. * gcc.dg/cpp/pr61854-6.c: New test. * gcc.dg/cpp/pr61854-7.c: New test. * gcc.dg/cpp/pr61854-c90.c: New test. * gcc.dg/cpp/pr61854-c94.c: New test. From-SVN: r215339 --- gcc/testsuite/ChangeLog | 14 ++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-1.c | 15 ++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-2.c | 15 ++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-3.c | 6 ++++ gcc/testsuite/gcc.dg/cpp/pr61854-3.h | 4 +++ gcc/testsuite/gcc.dg/cpp/pr61854-4.c | 16 +++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-5.c | 15 ++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-6.c | 19 +++++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-7.c | 19 +++++++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-c90.c | 13 +++++++++ gcc/testsuite/gcc.dg/cpp/pr61854-c94.c | 13 +++++++++ libcpp/ChangeLog | 8 ++++++ libcpp/init.c | 38 ++++++++++++-------------- libcpp/lex.c | 37 +++++++++++++++++++++---- 14 files changed, 206 insertions(+), 26 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-1.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-2.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-3.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-3.h create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-4.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-5.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-6.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-7.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-c90.c create mode 100644 gcc/testsuite/gcc.dg/cpp/pr61854-c94.c diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0362744727b..28157924c47 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,17 @@ +2014-09-17 Marek Polacek + + PR c/61854 + * gcc.dg/cpp/pr61854-1.c: New test. + * gcc.dg/cpp/pr61854-2.c: New test. + * gcc.dg/cpp/pr61854-3.c: New test. + * gcc.dg/cpp/pr61854-3.h: New test. + * gcc.dg/cpp/pr61854-4.c: New test. + * gcc.dg/cpp/pr61854-5.c: New test. + * gcc.dg/cpp/pr61854-6.c: New test. + * gcc.dg/cpp/pr61854-7.c: New test. + * gcc.dg/cpp/pr61854-c90.c: New test. + * gcc.dg/cpp/pr61854-c94.c: New test. + 2014-09-17 Jakub Jelinek PR debug/63284 diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-1.c b/gcc/testsuite/gcc.dg/cpp/pr61854-1.c new file mode 100644 index 00000000000..364a5112e33 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-1.c @@ -0,0 +1,15 @@ +/* PR c/61854 */ +/* { dg-do run } */ +/* { dg-options "-std=c89" } */ + +int +main (void) +{ + int i = 1 //**/ 2 + ; + int j = 1 //**/ 2 + ; + if (i != 0 || j != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-2.c b/gcc/testsuite/gcc.dg/cpp/pr61854-2.c new file mode 100644 index 00000000000..883db21b005 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-2.c @@ -0,0 +1,15 @@ +/* PR c/61854 */ +/* { dg-do run } */ +/* { dg-options "-std=gnu89" } */ + +int +main (void) +{ + int i = 1 //**/ 2 + ; + int j = 1 //**/ 2 + ; + if (i != 1 || j != 1) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-3.c b/gcc/testsuite/gcc.dg/cpp/pr61854-3.c new file mode 100644 index 00000000000..916c12ea3cf --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-3.c @@ -0,0 +1,6 @@ +/* PR c/61854 */ +/* { dg-do preprocess } */ +/* { dg-options "-std=c89" } */ + +#include "pr61854-3.h" +int i; diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-3.h b/gcc/testsuite/gcc.dg/cpp/pr61854-3.h new file mode 100644 index 00000000000..fd798bd8184 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-3.h @@ -0,0 +1,4 @@ +#pragma GCC system_header +// X +// Y +// Z diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-4.c b/gcc/testsuite/gcc.dg/cpp/pr61854-4.c new file mode 100644 index 00000000000..5cfa5a74923 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-4.c @@ -0,0 +1,16 @@ +/* PR c/61854 */ +/* { dg-do preprocess } */ +/* { dg-options "-std=c89" } */ + +void +foo (void) +{ +#if 0 + // Do not error here. +#endif +#if 1 + // But error here. +#endif + /* { dg-error "C\\+\\+ style comments are not allowed in ISO C90" "comments" { target *-*-*} 12 } */ + /* { dg-error "reported only once" "" { target *-*-*} 12 } */ +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-5.c b/gcc/testsuite/gcc.dg/cpp/pr61854-5.c new file mode 100644 index 00000000000..a7628dc6ea4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-5.c @@ -0,0 +1,15 @@ +/* PR c/61854 */ +/* { dg-do run } */ +/* { dg-options "-std=c89" } */ + +#define h(x) #x +#define s(x) h(x) +#define foo // + +int +main (void) +{ + if (__builtin_memcmp (s(foo), "//", 3) != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-6.c b/gcc/testsuite/gcc.dg/cpp/pr61854-6.c new file mode 100644 index 00000000000..1b60cc37fb1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-6.c @@ -0,0 +1,19 @@ +/* PR c/61854 */ +/* { dg-do run } */ +/* { dg-options "-std=gnu89" } */ + +int +main (void) +{ + int i = 0 +#if 0 +// /* +#else +// */ ++1 +#endif +; + if (i != 1) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-7.c b/gcc/testsuite/gcc.dg/cpp/pr61854-7.c new file mode 100644 index 00000000000..8bf479bbaae --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-7.c @@ -0,0 +1,19 @@ +/* PR c/61854 */ +/* { dg-do run } */ +/* { dg-options "-std=c89" } */ + +int +main (void) +{ + int i = 0 +#if 0 +// /* +#else +// */ ++1 +#endif +; + if (i != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-c90.c b/gcc/testsuite/gcc.dg/cpp/pr61854-c90.c new file mode 100644 index 00000000000..37eecbe23fb --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-c90.c @@ -0,0 +1,13 @@ +/* PR c/61854 */ +/* { dg-do preprocess } */ +/* { dg-options "-std=iso9899:1990" } */ + +void +foo (void) +{ + // 1st + /* { dg-error "C\\+\\+ style comments are not allowed in ISO C90" "comments" { target *-*-*} 8 } */ + /* { dg-error "reported only once" "" { target *-*-*} 8 } */ + // 2nd + // 3rd +} diff --git a/gcc/testsuite/gcc.dg/cpp/pr61854-c94.c b/gcc/testsuite/gcc.dg/cpp/pr61854-c94.c new file mode 100644 index 00000000000..64f1e1845c7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr61854-c94.c @@ -0,0 +1,13 @@ +/* PR c/61854 */ +/* { dg-do preprocess } */ +/* { dg-options "-std=iso9899:199409" } */ + +void +foo (void) +{ + // 1st + /* { dg-error "C\\+\\+ style comments are not allowed in ISO C90" "comments" { target *-*-*} 8 } */ + /* { dg-error "reported only once" "" { target *-*-*} 8 } */ + // 2nd + // 3rd +} diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index d98611925d2..77713476d4c 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,11 @@ +2014-09-17 Marek Polacek + + PR c/61854 + * init.c (struct lang_flags): Remove cplusplus_comments. + (cpp_set_lang): Likewise. + (post_options): Likewise. + * lex.c (_cpp_lex_direct): Disallow C++ style comments in C90/C94. + 2014-09-09 Manuel López-Ibáñez * include/cpplib.h (struct cpp_options): Declare warn_normalize as diff --git a/libcpp/init.c b/libcpp/init.c index d61237490a4..1121962148b 100644 --- a/libcpp/init.c +++ b/libcpp/init.c @@ -83,7 +83,6 @@ struct lang_flags char extended_identifiers; char c11_identifiers; char std; - char cplusplus_comments; char digraphs; char uliterals; char rliterals; @@ -94,23 +93,23 @@ struct lang_flags }; static const struct lang_flags lang_defaults[] = -{ /* c99 c++ xnum xid c11 std // digr ulit rlit udlit bincst digsep trig */ - /* GNUC89 */ { 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, - /* GNUC99 */ { 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 }, - /* GNUC11 */ { 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0 }, - /* STDC89 */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 }, - /* STDC94 */ { 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 }, - /* STDC99 */ { 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 }, - /* STDC11 */ { 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 }, - /* GNUCXX */ { 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, - /* CXX98 */ { 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1 }, - /* GNUCXX11 */ { 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0 }, - /* CXX11 */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1 }, - /* GNUCXX14 */ { 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0 }, - /* CXX14 */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, - /* GNUCXX1Z */ { 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0 }, - /* CXX1Z */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, - /* ASM */ { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } +{ /* c99 c++ xnum xid c11 std digr ulit rlit udlit bincst digsep trig */ + /* GNUC89 */ { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, + /* GNUC99 */ { 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 }, + /* GNUC11 */ { 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0 }, + /* STDC89 */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 }, + /* STDC94 */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, + /* STDC99 */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, + /* STDC11 */ { 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 }, + /* GNUCXX */ { 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, + /* CXX98 */ { 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, + /* GNUCXX11 */ { 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0 }, + /* CXX11 */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1 }, + /* GNUCXX14 */ { 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0 }, + /* CXX14 */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + /* GNUCXX1Z */ { 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0 }, + /* CXX1Z */ { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, + /* ASM */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } /* xid should be 1 for GNUC99, STDC99, GNUCXX, CXX98, GNUCXX11, CXX11, GNUCXX14, and CXX14 when no longer experimental (when all uses of identifiers in the compiler have been audited for correct handling @@ -131,7 +130,6 @@ cpp_set_lang (cpp_reader *pfile, enum c_lang lang) CPP_OPTION (pfile, extended_identifiers) = l->extended_identifiers; CPP_OPTION (pfile, c11_identifiers) = l->c11_identifiers; CPP_OPTION (pfile, std) = l->std; - CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments; CPP_OPTION (pfile, digraphs) = l->digraphs; CPP_OPTION (pfile, uliterals) = l->uliterals; CPP_OPTION (pfile, rliterals) = l->rliterals; @@ -775,8 +773,6 @@ post_options (cpp_reader *pfile) if (CPP_OPTION (pfile, traditional)) { - CPP_OPTION (pfile, cplusplus_comments) = 0; - CPP_OPTION (pfile, trigraphs) = 0; CPP_OPTION (pfile, warn_trigraphs) = 0; } diff --git a/libcpp/lex.c b/libcpp/lex.c index 5366dad9e44..bdaa0705bc7 100644 --- a/libcpp/lex.c +++ b/libcpp/lex.c @@ -2322,13 +2322,16 @@ _cpp_lex_direct (cpp_reader *pfile) if (_cpp_skip_block_comment (pfile)) cpp_error (pfile, CPP_DL_ERROR, "unterminated comment"); } - else if (c == '/' && (CPP_OPTION (pfile, cplusplus_comments) - || cpp_in_system_header (pfile))) + else if (c == '/' && ! CPP_OPTION (pfile, traditional)) { + /* Don't warn for system headers. */ + if (cpp_in_system_header (pfile)) + ; /* Warn about comments if pedantically GNUC89, and not in system headers. */ - if (CPP_OPTION (pfile, lang) == CLK_GNUC89 && CPP_PEDANTIC (pfile) - && ! buffer->warned_cplusplus_comments) + else if (CPP_OPTION (pfile, lang) == CLK_GNUC89 + && CPP_PEDANTIC (pfile) + && ! buffer->warned_cplusplus_comments) { cpp_error (pfile, CPP_DL_PEDWARN, "C++ style comments are not allowed in ISO C90"); @@ -2347,7 +2350,31 @@ _cpp_lex_direct (cpp_reader *pfile) "(this will be reported only once per input file)"); buffer->warned_cplusplus_comments = 1; } - + /* In C89/C94, C++ style comments are forbidden. */ + else if ((CPP_OPTION (pfile, lang) == CLK_STDC89 + || CPP_OPTION (pfile, lang) == CLK_STDC94)) + { + /* But don't be confused about valid code such as + - // immediately followed by *, + - // in a preprocessing directive, + - // in an #if 0 block. */ + if (buffer->cur[1] == '*' + || pfile->state.in_directive + || pfile->state.skipping) + { + result->type = CPP_DIV; + break; + } + else if (! buffer->warned_cplusplus_comments) + { + cpp_error (pfile, CPP_DL_ERROR, + "C++ style comments are not allowed in ISO C90"); + cpp_error (pfile, CPP_DL_ERROR, + "(this will be reported only once per input " + "file)"); + buffer->warned_cplusplus_comments = 1; + } + } if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments)) cpp_warning (pfile, CPP_W_COMMENTS, "multi-line comment"); }