<< Back to previous view

[PCC-386] enum declaration before definition causes compiler error Created: 16/Mar/12  Updated: 18/Mar/12

Status: Resolved
Project: pcc
Component/s: C frontend
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Iain Hibbert Assignee: Anders Magnusson
Resolution: Fixed Votes: 0
Environment: NetBSD/i386

While attempting to compile GDB 7.3.1 I discovered an error

/var/cvs/NetBSD-current/src/external/gpl3/gdb/dist/gdb/i386-tdep.c, line 7187: warning: implicit conversion of argument 2 due to prototype
/var/cvs/NetBSD-current/src/external/gpl3/gdb/dist/gdb/i386-tdep.c, line 7187: compiler error: strmemb

examining this in detail showed that include file ordering means that
an enum is being referenced in a function prototype before its definition
(which according to C99 note 3 is not allowed), but the following code

  void foo(enum bar);

  enum bar { A, B, C };

  void main(void)

when compiled results in

  test.c: line 7: compiler error: strmemb

rather than a useful warning or error.

Comment by Anders Magnusson [ 17/Mar/12 03:47 PM ]
Hm, I'm wondering how this should be handled. gcc (4.2.1) also fails on this example, but nevertheless people tend to write such code.
Which gcc versions accept it? What I'm thinking about is if something more intelligent needs to be done here.
Comment by Anders Magnusson [ 17/Mar/12 05:54 PM ]
Added error if an enum argument to a prototype is undeclared.
This will avoid other odd errors later.
Comment by Iain Hibbert [ 18/Mar/12 09:23 AM ]
sorry, the gcc version was 4.5.3 but it seems I reduced the code too much and it was also giving a warning

the actual code is too big to fit in the margin here, but a better example is

typedef enum bar foo_t (void);

void foo (foo_t);

enum bar { A, B, C };

enum bar func (void);

void main (void)
    foo (func);

which gcc accepts but pcc still fails, with

enum.c, line 11: warning: implicit conversion of argument 1 due to prototype
enum.c, line 11: compiler error: strmemb

(I think as mentioned previously, the code is wrong to do that, it is an include file out of order problem)
Comment by Anders Magnusson [ 18/Mar/12 01:27 PM ]
Just adding "enum bar;" first in the first example shows the problem.
I think that pcc should delay the prototype content check until the function is referenced, but that requires some internal rewriting.
Generated at Thu May 24 00:26:50 CEST 2018 using JIRA Enterprise Edition, Version: 3.13.1-#333.