Description
I was kicking the tires of 3C's rewriting of multi-decls containing structs a bit in preparation for working on #531, and I discovered that the current code doesn't handle forward declarations of structs properly. The following file:
struct fwd *p;
rewrites to the following syntatically invalid code, with or without -alltypes
:
struct }_Ptr<struct fwd> p = ((void *)0);
My understanding of Clang's AST parsing behavior is that the first mention of struct fwd
in any context in a translation unit is reported as a RecordDecl
whether or not it has a body. If it has a body, it is the definition, otherwise it is a forward declaration; we can use RecordDecl::isCompleteDefinition
to distinguish between these cases. Subsequent occurrences of struct fwd;
by itself are reported as forward declarations, but subsequent mentions of struct fwd
as part of other constructs are not reported as RecordDecl
s at all.
AFAIK, "forward declarations" of structs as part of other constructs have no significance in C, because the first mention of a struct, wherever it appears, will be treated as a forward declaration. (There is one edge case in which a standalone forward declaration does make a difference, but 3C won't modify those anyway.) So probably what we want to do is have all 3C code that handles struct definitions in multi-decls just ignore RecordDecl
s for which isCompleteDefinition
returns false. I prototyped this change and it seemed to have the desired effect in the example above; I'll probably submit a PR later.