Allow forward-declaration of elaborated types in type decls

This covers the common C pattern of declaring a typedef for a struct before the
struct itself, e.g.

   typedef struct foo foo_t;

   struct foo {
     int value;
     foo_t *next;
   };

Fixes issue #1065.
This commit is contained in:
Kim Gräsman 2022-07-09 23:29:39 +02:00
parent 0f7a7aba8c
commit d2d4d15d8d
2 changed files with 13 additions and 0 deletions

View File

@ -170,6 +170,7 @@ using clang::Decl;
using clang::DeclContext;
using clang::DeclRefExpr;
using clang::DeducedTemplateSpecializationType;
using clang::ElaboratedType;
using clang::EnumConstantDecl;
using clang::EnumDecl;
using clang::EnumType;
@ -2610,6 +2611,10 @@ class IwyuBaseAstVisitor : public BaseAstVisitor<Derived> {
}
parent_type = GetTypeOf(decl);
} else if (ast_node->IsA<ElaboratedType>()) {
// If it's not a ValueDecl, it must be a type decl. Elaborated types in
// type decls are forward-declarable.
return true;
}
}

View File

@ -28,6 +28,14 @@ int UseStruct(struct Struct* s);
struct ForwardDeclared;
void UseForwardDeclared(struct ForwardDeclared*);
// If a forward-declaration is seen before an actual struct declaration in the
// same file, no diagnostic is expected (see issue #1065).
typedef struct local_struct local_struct_t;
struct local_struct {
int x;
local_struct_t* next;
};
/**** IWYU_SUMMARY
tests/c/elaborated_struct.c should add these lines: