public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped
@ 2014-03-18 20:13 seb.grindle at gmail dot com
  2014-03-18 20:17 ` [Bug preprocessor/60570] " schwab@linux-m68k.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: seb.grindle at gmail dot com @ 2014-03-18 20:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

            Bug ID: 60570
           Summary: expression in 'elif' directive mis-diagnosed as error
                    when group will be skipped
           Product: gcc
           Version: 4.7.3
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: preprocessor
          Assignee: unassigned at gcc dot gnu.org
          Reporter: seb.grindle at gmail dot com

Consider the following C99 program:

#if 1
int i;
#elif 1/0
#endif

With gcc 4.7.3:

   gcc -c test.c

we get

   test.c:3:8: error: division by zero in #if

According to 6.10.1p5, any sequence of tokens between the #elif and the newline
is valid syntax since the group for which this line is the controlling
condition will be skipped, yet gcc emits a divide by zero error.

(I don't know if this bug is present in later versions as 4.7.3 is the latest I
have tested on.)

Seb


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
@ 2014-03-18 20:17 ` schwab@linux-m68k.org
  2014-03-18 20:29 ` seb.grindle at gmail dot com
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: schwab@linux-m68k.org @ 2014-03-18 20:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> ---
Dup of 19040?


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
  2014-03-18 20:17 ` [Bug preprocessor/60570] " schwab@linux-m68k.org
@ 2014-03-18 20:29 ` seb.grindle at gmail dot com
  2014-03-18 22:12 ` joseph at codesourcery dot com
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: seb.grindle at gmail dot com @ 2014-03-18 20:29 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #2 from seb.grindle at gmail dot com ---
19040 was sort of the opposite. Here we shouldn't get a error but do; there the
report was of the form "this should produce a diagostic but doesn't"


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
  2014-03-18 20:17 ` [Bug preprocessor/60570] " schwab@linux-m68k.org
  2014-03-18 20:29 ` seb.grindle at gmail dot com
@ 2014-03-18 22:12 ` joseph at codesourcery dot com
  2015-01-22  2:58 ` moshansky at hotmail dot com
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: joseph at codesourcery dot com @ 2014-03-18 22:12 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #3 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_412.htm gives an 
approved change (presumably to appear in C11 TC2) to stop #elif 
conditionals from being evaluated when they don't need to be, so it looks 
like we should in fact change this (for all standard versions, given it's 
being deemed a defect).


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (2 preceding siblings ...)
  2014-03-18 22:12 ` joseph at codesourcery dot com
@ 2015-01-22  2:58 ` moshansky at hotmail dot com
  2015-01-22  9:37 ` mpolacek at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: moshansky at hotmail dot com @ 2015-01-22  2:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

moshansky at hotmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |moshansky at hotmail dot com

--- Comment #4 from moshansky at hotmail dot com ---
Still present as of gcc version 4.8.4 20140526 (release)
[ARM/embedded-4_8-branch revision 211358] (GNU Tools for ARM Embedded
Processors).

Fails:
#if(1)
    #pragma message "This Should Work"
#elif(1/0)
    #pragma message "1/0 should not be attempted"
#endif

Workaround:
#if(1)
    #pragma message "This Should Work"
#else
    #elif(1/0)
        #pragma message "1/0 should is not attempted"
    #endif
#endif


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (3 preceding siblings ...)
  2015-01-22  2:58 ` moshansky at hotmail dot com
@ 2015-01-22  9:37 ` mpolacek at gcc dot gnu.org
  2015-01-22  9:53 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-01-22  9:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
It would appear so, in which case the following (untested, only quick hack)
should work:

diff --git a/gcc/testsuite/gcc.dg/cpp/pr36320.c
b/gcc/testsuite/gcc.dg/cpp/pr36320.c
index d136a69..ebd5191 100644
--- a/gcc/testsuite/gcc.dg/cpp/pr36320.c
+++ b/gcc/testsuite/gcc.dg/cpp/pr36320.c
@@ -4,5 +4,5 @@

 int z;
 #if 1
-#elif   /* { dg-error "with no expression" } */
+#elif
 #endif
diff --git a/libcpp/directives.c b/libcpp/directives.c
index ab4f15c..37cd109 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.  */

together with a test like the following

#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 0
# elif 1/0    /* { dg-error "division by zero" } */
# endif
#endif


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (4 preceding siblings ...)
  2015-01-22  9:37 ` mpolacek at gcc dot gnu.org
@ 2015-01-22  9:53 ` jakub at gcc dot gnu.org
  2015-01-22 13:31 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2015-01-22  9:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
-#elif   /* { dg-error "with no expression" } */
+#elif

Perhaps turn it into dg-bogus instead?


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (5 preceding siblings ...)
  2015-01-22  9:53 ` jakub at gcc dot gnu.org
@ 2015-01-22 13:31 ` mpolacek at gcc dot gnu.org
  2015-01-22 14:10 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-01-22 13:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
dg-bogus is better, yet.  FWIW, the patch has been successfully
regtested/bootstrapped now.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (6 preceding siblings ...)
  2015-01-22 13:31 ` mpolacek at gcc dot gnu.org
@ 2015-01-22 14:10 ` mpolacek at gcc dot gnu.org
  2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-01-22 14:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2015-01-22
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
   Target Milestone|---                         |5.0
     Ever confirmed|0                           |1


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (8 preceding siblings ...)
  2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
@ 2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
  2024-04-02 21:40 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-01-23 11:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

--- Comment #9 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Author: mpolacek
Date: Fri Jan 23 11:57:43 2015
New Revision: 220035

URL: https://gcc.gnu.org/viewcvs?rev=220035&root=gcc&view=rev
Log:
    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.

Added:
    trunk/gcc/testsuite/gcc.dg/cpp/pr60570.c
Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/cpp/pr36320.c
    trunk/libcpp/ChangeLog
    trunk/libcpp/directives.c


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (7 preceding siblings ...)
  2015-01-22 14:10 ` mpolacek at gcc dot gnu.org
@ 2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
  2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
  2024-04-02 21:40 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-01-23 11:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #10 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed for GCC 5.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug preprocessor/60570] expression in 'elif' directive mis-diagnosed as error when group will be skipped
  2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
                   ` (9 preceding siblings ...)
  2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
@ 2024-04-02 21:40 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-02 21:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60570

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mueller at gcc dot gnu.org

--- Comment #11 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 36453 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-04-02 21:40 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-03-18 20:13 [Bug preprocessor/60570] New: expression in 'elif' directive mis-diagnosed as error when group will be skipped seb.grindle at gmail dot com
2014-03-18 20:17 ` [Bug preprocessor/60570] " schwab@linux-m68k.org
2014-03-18 20:29 ` seb.grindle at gmail dot com
2014-03-18 22:12 ` joseph at codesourcery dot com
2015-01-22  2:58 ` moshansky at hotmail dot com
2015-01-22  9:37 ` mpolacek at gcc dot gnu.org
2015-01-22  9:53 ` jakub at gcc dot gnu.org
2015-01-22 13:31 ` mpolacek at gcc dot gnu.org
2015-01-22 14:10 ` mpolacek at gcc dot gnu.org
2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
2015-01-23 11:58 ` mpolacek at gcc dot gnu.org
2024-04-02 21:40 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).