From 10ef8f287e92cad2f6963a9ee25d954c87567629 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Fri, 23 Jan 2015 11:57:43 +0000 Subject: [PATCH] re PR preprocessor/60570 (expression in 'elif' directive mis-diagnosed as error when group will be skipped) DR#412 PR preprocessor/60570 * directives.c (do_elif): Don't evaluate #elif conditionals when they don't need to be. * gcc.dg/cpp/pr36320.c: Turn dg-error into dg-bogus. * gcc.dg/cpp/pr60570.c: New test. From-SVN: r220035 --- gcc/testsuite/ChangeLog | 6 ++++ gcc/testsuite/gcc.dg/cpp/pr36320.c | 4 +-- gcc/testsuite/gcc.dg/cpp/pr60570.c | 48 ++++++++++++++++++++++++++++++ libcpp/ChangeLog | 7 +++++ libcpp/directives.c | 25 ++++++---------- 5 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/cpp/pr60570.c diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 86753d6fc1d..cf9b478268b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2015-01-23 Marek Polacek + + PR preprocessor/60570 + * gcc.dg/cpp/pr36320.c: Turn dg-error into dg-bogus. + * gcc.dg/cpp/pr60570.c: New test. + 2015-01-23 Richard Biener PR testsuite/63439 diff --git a/gcc/testsuite/gcc.dg/cpp/pr36320.c b/gcc/testsuite/gcc.dg/cpp/pr36320.c index d136a69b6b2..cc2baa73e1f 100644 --- a/gcc/testsuite/gcc.dg/cpp/pr36320.c +++ b/gcc/testsuite/gcc.dg/cpp/pr36320.c @@ -1,8 +1,8 @@ /* PR 36320 - #elif still requires valid expression. */ - +/* DR#412: #elif doesn't have to be valid expression (PR60570). */ /* { dg-do preprocess } */ int z; #if 1 -#elif /* { dg-error "with no expression" } */ +#elif /* { dg-bogus "with no expression" } */ #endif diff --git a/gcc/testsuite/gcc.dg/cpp/pr60570.c b/gcc/testsuite/gcc.dg/cpp/pr60570.c new file mode 100644 index 00000000000..4755206c500 --- /dev/null +++ b/gcc/testsuite/gcc.dg/cpp/pr60570.c @@ -0,0 +1,48 @@ +/* PR preprocessor/60570 */ +/* { dg-do preprocess } */ + +#if 1 +int i; +#elif 1/0 +#endif + +#if 1 +int j; +#elif +#endif + +#if 0 +#elif 1/0 /* { dg-error "division by zero" } */ +int k; +#endif + +#if 0 +#elif /* { dg-error "with no expression" } */ +int n; +#endif + +#if 1 +# if 1 +int l; +# elif 1/0 +# endif +#endif + +#if 1 +# if 1 +int l; +# elif +# endif +#endif + +#if 1 +# if 0 +# elif 1/0 /* { dg-error "division by zero" } */ +# endif +#endif + +#if 1 +# if 0 +# elif /* { dg-error "with no expression" } */ +# endif +#endif diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 23251619472..41aa56fa9c3 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,10 @@ +2015-01-23 Marek Polacek + + DR#412 + PR preprocessor/60570 + * directives.c (do_elif): Don't evaluate #elif conditionals + when they don't need to be. + 2015-01-16 Jakub Jelinek * expr.c (cpp_classify_number): Add N_() around ?: string diff --git a/libcpp/directives.c b/libcpp/directives.c index ab4f15c5bd7..37cd109ed8a 100644 --- a/libcpp/directives.c +++ b/libcpp/directives.c @@ -2036,23 +2036,16 @@ do_elif (cpp_reader *pfile) } ifs->type = T_ELIF; - if (! ifs->was_skipping) + /* See DR#412: "Only the first group whose control condition + evaluates to true (nonzero) is processed; any following groups + are skipped and their controlling directives are processed as + if they were in a group that is skipped." */ + if (ifs->skip_elses) + pfile->state.skipping = 1; + else { - bool value; - /* The standard mandates that the expression be parsed even - if we are skipping elses at this point -- the lexical - restrictions on #elif only apply to skipped groups, but - this group is not being skipped. Temporarily set - skipping to false to get lexer warnings. */ - pfile->state.skipping = 0; - value = _cpp_parse_expr (pfile, false); - if (ifs->skip_elses) - pfile->state.skipping = 1; - else - { - pfile->state.skipping = ! value; - ifs->skip_elses = value; - } + pfile->state.skipping = ! _cpp_parse_expr (pfile, false); + ifs->skip_elses = ! pfile->state.skipping; } /* Invalidate any controlling macro. */ -- 2.30.2