Issue Details (XML | Word | Printable)

Key: PCC-386
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Anders Magnusson
Reporter: Iain Hibbert
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
pcc

enum declaration before definition causes compiler error

Created: 16/Mar/12 06:39 PM   Updated: 18/Mar/12 01:27 PM
Component/s: C frontend
Affects Version/s: None
Fix Version/s: None

Environment: NetBSD/i386


 Description  « Hide
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 6.7.2.3 note 3 is not allowed), but the following code
snippet

  void foo(enum bar);

  enum bar { A, B, C };

  void main(void)
  {
      foo(B);
  }

when compiled results in

  test.c: line 7: compiler error: strmemb

rather than a useful warning or error.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Anders Magnusson added a comment - 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.

Anders Magnusson made changes - 17/Mar/12 05:52 PM
Field Original Value New Value
Assignee Anders Magnusson [ ragge ]
Anders Magnusson added a comment - 17/Mar/12 05:54 PM
Added error if an enum argument to a prototype is undeclared.
This will avoid other odd errors later.

Anders Magnusson made changes - 17/Mar/12 05:54 PM
Iain Hibbert added a comment - 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)

Anders Magnusson added a comment - 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.