llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.3k stars 11.69k forks source link

Wrong AST generated in presence of `__attribute__((noreturn))` #7331

Open Abramo-Bagnara opened 14 years ago

Abramo-Bagnara commented 14 years ago
Bugzilla Link 6959
Version trunk
OS Linux

Extended Description

The typescript below shows that the declaration of f1 is altered by AST builder and use of typedef fun is inappropriately expanded.

$ cat d.c
typedef void (*fun)();
fun f1 __attribute__ ((noreturn));
fun f2;
$ clang -cc1 -ast-dump d.c
typedef __int128_t __int128_t;
typedef __uint128_t __uint128_t;
struct __va_list_tag {
    unsigned int gp_offset;
    unsigned int fp_offset;
    void *overflow_arg_area;
    void *reg_save_area;
};
typedef struct __va_list_tag __va_list_tag;
typedef __va_list_tag __builtin_va_list[1];
typedef void (*fun)();
void (*f1)() __attribute__((noreturn));
fun f2;
JOE1994 commented 7 months ago

clang -cc1 -ast-dump d.c generates the following output (llvm trunk 056d62be38c5db3d8332ac300c4ff29214126697):

TranslationUnitDecl 0x55a2d7a24008 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x55a2d7a24838 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
| `-BuiltinType 0x55a2d7a245d0 '__int128'
|-TypedefDecl 0x55a2d7a248a8 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
| `-BuiltinType 0x55a2d7a245f0 'unsigned __int128'
|-TypedefDecl 0x55a2d7a24bb0 <<invalid sloc>> <invalid sloc> implicit __NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x55a2d7a24980 'struct __NSConstantString_tag'
|   `-Record 0x55a2d7a24900 '__NSConstantString_tag'
|-TypedefDecl 0x55a2d7a24c58 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'
| `-PointerType 0x55a2d7a24c10 'char *'
|   `-BuiltinType 0x55a2d7a240b0 'char'
|-TypedefDecl 0x55a2d7a24f50 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list 'struct __va_list_tag[1]'
| `-ConstantArrayType 0x55a2d7a24ef0 'struct __va_list_tag[1]' 1 
|   `-RecordType 0x55a2d7a24d30 'struct __va_list_tag'
|     `-Record 0x55a2d7a24cb0 '__va_list_tag'
|-TypedefDecl 0x55a2d7a768c0 <d.c:1:1, col:21> col:16 referenced fun 'void (*)()'
| `-PointerType 0x55a2d7a76860 'void (*)()'
|   `-ParenType 0x55a2d7a76800 'void ()' sugar
|     `-FunctionNoProtoType 0x55a2d7a24fb0 'void ()' cdecl
|       `-BuiltinType 0x55a2d7a24070 'void'
|-VarDecl 0x55a2d7a76a70 <line:2:1> col:5 f1 'void (*)() __attribute__((noreturn))'
`-VarDecl 0x55a2d7a76b30 <line:3:1, col:5> col:5 f2 'fun':'void (*)()'

declaration of f1 is altered by AST builder and use of typedef fun is inappropriately expanded.

Clang seems to simply treat function types of f1 and f2 as distinct types, as they differ in terms of __attribute__((noreturn)).

I'm not sure what "inappropriately expanded" means in this context. Maybe a non-problem?

Endilll commented 7 months ago

I'm not sure what "inappropriately expanded" means in this context.

My guess is that author is not happy that -ast-dump (now -ast-print) didn't resugar type of f1 back into typedef + attribute. The issue is still there in 17 and post-18 trunk: https://godbolt.org/z/EK8bnTx5v, but this might be a wontfix, because we don't care about dumping AST as source code too much these days.

CC @AaronBallman

AaronBallman commented 7 months ago

It's a valid enhancement request but I don't consider this to be a bug. AST pretty printing is a best-effort feature and the printed code is equivalent to the original code and still compiles.