llvm-project
llvm-project copied to clipboard
Clang handles variable declaration with unknown type poorly
For a given code in foo.c file
int foo() {
some_type *bar;
}
clang gives a cascade of confusing diagnostics:
foo.c:2:3: error: use of undeclared identifier 'some_type'
some_type *bar;
^
foo.c:2:12: error: use of undeclared identifier 'bar'
some_type *bar;
and AST does not have a node with corresponding invalid VarDecl. So clang parser does not handle this code as variable declaration.
gcc gives far better diagnostics:
foo.c: In function 'foo':
foo.c:2:3: error: unknown type name 'some_type'
2 | some_type *bar;
| ^~~~~~~~~
In case of foo.cpp, clang works pretty well:
foo.c: In function 'foo':
foo.c:2:3: error: unknown type name 'some_type'
2 | some_type *bar;
| ^~~~~~~~~
So some_type is handled as unknown type and this code is handled as although invalid but variable declaration.
The above behavior remains for variable declaration without * declarator.
Suggestion is to make clang handle this case consistently for C and C++ code. It will give better diagnostics for users and AST will have invalid declaration nodes and declaration reference nodes to this invalid declarations under RecoveryExpr.