2015-12-23 8 views
6

considerare:Tipo incompatibile con se stesso in GCC 5 quando si utilizza -Wwrite-stringhe

char f(const char (*x)[4]); 

void foo(void) { 
    typeof(*"FOO") x[4]; 
    f(&x); 
} 

Compilare con -Wwrite-strings:

gcc-5 -c gcc5.c -Wwrite-strings 

Si ottiene:

gcc5.c: In function ‘foo’: 
gcc5.c:5:7: warning: passing argument 1 of ‘f’ from incompatible pointer type [-Wincompatible-pointer-types] 
    f(&x); 
    ^
gcc5.c:1:6: note: expected ‘const char (*)[4]’ 
    but argument is of type ‘const char (*)[4]’ 
char f(const char (*x)[4]); 
    ^

Sembra un insetto in gcc, a meno che mi manchi qualcosa?

Nota: -Wwrite-strings cambia il tipo di stringhe letterali:

Quando si compila C, dare costanti stringa del tipo "const char [lunghezza]"

+1

In C, il tipo di "*" FOO "' è 'char'. '-Wwrite-stringhe 'dovrebbe cambiare questo (rendendo il compilatore non standard)?Se non lo è, il bug mostra il tipo di '& x' come' const char (*) [4] ': non c'è' const'. –

+1

Sembra un insetto. Prendi in considerazione la possibilità di presentare una segnalazione di bug. – fuz

+1

@PascalCuoq: Anche così, un 'char (*) [4]' è convertibile in 'const char (*) [4]' corretto? Perché dovrebbe essere un "tipo di puntatore incompatibile"? –

risposta

1

Per me è davvero un bug in gcc5.

gccdocumentation dice

-Wwrite-stringhe

Quando si compila C, dare costanti stringa il char tipo const [lunghezza] in modo che la copia l'indirizzo di uno in un puntatore non-const char * produce un avvertimento.

Quindi, con questa dichiarazione:

typeof(*"FOO") x[4]; 

poi &x è di tipo const char (*)[4] quando è presente -Wwrite-strings. In Standard C, &x è di tipo char (*)[4].

Questa piccola funzione:

void foo(void) { 
    typeof(*"FOO") x[4]; 
    printf("%d\n", __builtin_types_compatible_p(typeof(&x), const char (*)[4])); 
    printf("%d\n", __builtin_types_compatible_p(typeof(&x), char (*)[4])); 
} 

stampe:

1 
0 

con gcc5.3 e -Wwrite-strings. Quindi possiamo vedere che gcc5.3 identifica correttamente &x a partire dal tipo const char (*)[4] con -Wwrite-strings.

gcc deve quindi accettare l'argomento &x quando si chiama una funzione che ha un parametro const char (*)[4]. Avviso per tipo incompatibile è IMHO quindi un bug in gcc.

(Questo bug, probabilmente non ha mostrato nelle precedenti versioni di gcc semplicemente perché gcc falliti (un altro bug) per identificare &x come const char (*)[4] con -Wwrite-strings in gcc versioni precedenti. Ho provato con gcc4.9.2 e gcc-6-20151206.)

Problemi correlati