Skip to content

Test both new TagDecl flag and old miscompilation fix #3878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3483,6 +3483,23 @@ class TagDecl : public TypeDecl,
/// parameters.
bool isDependentType() const { return isDependentContext(); }

/// Whether this declaration was a definition in some module but was forced
/// to be a declaration.
///
/// Useful for clients checking if a module has a definition of a specific
/// symbol and not interested in the final AST with deduplicated definitions.
bool isThisDeclarationADemotedDefinition() const {
return TagDeclBits.IsThisDeclarationADemotedDefinition;
}

/// Mark a definition as a declaration and maintain information it _was_
/// a definition.
void demoteThisDefinitionToDeclaration() {
assert(isCompleteDefinition() && "Not a definition!");
setCompleteDefinition(false);
TagDeclBits.IsThisDeclarationADemotedDefinition = true;
}

/// Starts the definition of this tag declaration.
///
/// This method should be invoked at the beginning of the definition
Expand Down
6 changes: 5 additions & 1 deletion clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1435,10 +1435,14 @@ class DeclContext {
/// Has the full definition of this type been required by a use somewhere in
/// the TU.
uint64_t IsCompleteDefinitionRequired : 1;

/// Whether this tag is a definition which was demoted due to
/// a module merge.
uint64_t IsThisDeclarationADemotedDefinition : 1;
};

/// Number of non-inherited bits in TagDeclBitfields.
enum { NumTagDeclBits = 9 };
enum { NumTagDeclBits = 10 };

/// Stores the bits used by EnumDecl.
/// If modified NumEnumDeclBit and the accessor
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,10 @@ class ASTReader
/// definitions. Only populated when using modules in C++.
llvm::DenseMap<EnumDecl *, EnumDecl *> EnumDefinitions;

/// A mapping from canonical declarations of records to their canonical
/// definitions. Doesn't cover CXXRecordDecl.
llvm::DenseMap<RecordDecl *, RecordDecl *> RecordDefinitions;

/// When reading a Stmt tree, Stmt operands are placed in this stack.
SmallVector<Stmt *, 16> StmtStack;

Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4293,6 +4293,7 @@ TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC,
setEmbeddedInDeclarator(false);
setFreeStanding(false);
setCompleteDefinitionRequired(false);
TagDeclBits.IsThisDeclarationADemotedDefinition = false;
}

SourceLocation TagDecl::getOuterLocStart() const {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
// Otherwise, we only care about anonymous class members / block-scope decls.
// FIXME: We need to handle lambdas and blocks within inline / templated
// variables too.
if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
if (D->getDeclName() || !isa<RecordDecl>(D->getLexicalDeclContext()))
return false;
return isa<TagDecl>(D) || isa<FieldDecl>(D);
}
40 changes: 37 additions & 3 deletions clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ namespace clang {
RedeclarableResult VisitTagDecl(TagDecl *TD);
void VisitEnumDecl(EnumDecl *ED);
RedeclarableResult VisitRecordDeclImpl(RecordDecl *RD);
void VisitRecordDecl(RecordDecl *RD) { VisitRecordDeclImpl(RD); }
void VisitRecordDecl(RecordDecl *RD);
RedeclarableResult VisitCXXRecordDeclImpl(CXXRecordDecl *D);
void VisitCXXRecordDecl(CXXRecordDecl *D) { VisitCXXRecordDeclImpl(D); }
RedeclarableResult VisitClassTemplateSpecializationDeclImpl(
Expand Down Expand Up @@ -773,7 +773,7 @@ void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
}
if (OldDef) {
Reader.MergedDeclContexts.insert(std::make_pair(ED, OldDef));
ED->setCompleteDefinition(false);
ED->demoteThisDefinitionToDeclaration();
Reader.mergeDefinitionVisibility(OldDef, ED);
if (OldDef->getODRHash() != ED->getODRHash())
Reader.PendingEnumOdrMergeFailures[OldDef].push_back(ED);
Expand Down Expand Up @@ -808,6 +808,34 @@ ASTDeclReader::VisitRecordDeclImpl(RecordDecl *RD) {
return Redecl;
}

void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
VisitRecordDeclImpl(RD);

// Maintain the invariant of a redeclaration chain containing only
// a single definition.
if (RD->isCompleteDefinition()) {
RecordDecl *Canon = static_cast<RecordDecl *>(RD->getCanonicalDecl());
RecordDecl *&OldDef = Reader.RecordDefinitions[Canon];
if (!OldDef) {
// This is the first time we've seen an imported definition. Look for a
// local definition before deciding that we are the first definition.
for (auto *D : merged_redecls(Canon)) {
if (!D->isFromASTFile() && D->isCompleteDefinition()) {
OldDef = D;
break;
}
}
}
if (OldDef) {
Reader.MergedDeclContexts.insert(std::make_pair(RD, OldDef));
RD->demoteThisDefinitionToDeclaration();
Reader.mergeDefinitionVisibility(OldDef, RD);
} else {
OldDef = RD;
}
}
}

void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
VisitNamedDecl(VD);
// For function declarations, defer reading the type in case the function has
Expand Down Expand Up @@ -2661,7 +2689,7 @@ static bool allowODRLikeMergeInC(NamedDecl *ND) {
if (!ND)
return false;
// TODO: implement merge for other necessary decls.
if (isa<EnumConstantDecl>(ND))
if (isa<EnumConstantDecl, FieldDecl, IndirectFieldDecl>(ND))
return true;
return false;
}
Expand Down Expand Up @@ -3333,6 +3361,9 @@ DeclContext *ASTDeclReader::getPrimaryContextForMerging(ASTReader &Reader,
return DD->Definition;
}

if (auto *RD = dyn_cast<RecordDecl>(DC))
return RD->getDefinition();

if (auto *ED = dyn_cast<EnumDecl>(DC))
return ED->getASTContext().getLangOpts().CPlusPlus? ED->getDefinition()
: nullptr;
Expand Down Expand Up @@ -3419,6 +3450,9 @@ ASTDeclReader::getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC) {
if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
if (MD->isThisDeclarationADefinition())
return MD;
if (auto *RD = dyn_cast<RecordDecl>(D))
if (RD->isThisDeclarationADefinition())
return RD;
}

// No merged definition yet.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// It is important to have a definition *after* non-definition declaration.
typedef struct _Buffer Buffer;
struct _Buffer {
int a;
int b;
int c;
};

typedef struct _AnonymousStruct AnonymousStruct;
struct _AnonymousStruct {
struct {
int x;
int y;
};
};

typedef union _UnionRecord UnionRecord;
union _UnionRecord {
int u: 2;
int v: 4;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework module RecordDef {
header "RecordDef.h"
export *
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// It is important to have a definition *after* non-definition declaration.
typedef struct _Buffer Buffer;
struct _Buffer {
int a;
int b;
int c;
};

typedef struct _AnonymousStruct AnonymousStruct;
struct _AnonymousStruct {
struct {
int x;
int y;
};
};

typedef union _UnionRecord UnionRecord;
union _UnionRecord {
int u: 2;
int v: 4;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework module RecordDefCopy {
header "RecordDefCopy.h"
export *
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// It is important to have a definition *after* non-definition declaration.
typedef struct _Buffer Buffer;
struct _Buffer {
int a;
int b;
int c;
};

typedef struct _AnonymousStruct AnonymousStruct;
struct _AnonymousStruct {
struct {
int x;
int y;
};
};

typedef union _UnionRecord UnionRecord;
union _UnionRecord {
int u: 2;
int v: 4;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty header to create a module.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
framework module RecordDefHidden {
header "Visible.h"
export *

explicit module Hidden {
header "Hidden.h"
export *
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import <RecordDef/RecordDef.h>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework module RecordDefIncluder {
header "RecordDefIncluder.h"
export *
}
39 changes: 39 additions & 0 deletions clang/test/Modules/merge-record-definition-nonmodular.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// UNSUPPORTED: -zos, -aix
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: %clang_cc1 -emit-llvm -o %t/test.bc -F%S/Inputs/merge-record-definition %s \
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache -fmodule-name=RecordDef
// RUN: %clang_cc1 -emit-llvm -o %t/test.bc -F%S/Inputs/merge-record-definition %s -DMODULAR_BEFORE_TEXTUAL \
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache -fmodule-name=RecordDef

// Test a case when a struct definition once is included from a textual header and once from a module.

#ifdef MODULAR_BEFORE_TEXTUAL
#import <RecordDefIncluder/RecordDefIncluder.h>
#else
#import <RecordDef/RecordDef.h>
#endif

void bibi(void) {
Buffer buf;
buf.b = 1;
AnonymousStruct strct;
strct.x = 1;
UnionRecord rec;
rec.u = 1;
}

#ifdef MODULAR_BEFORE_TEXTUAL
#import <RecordDef/RecordDef.h>
#else
#import <RecordDefIncluder/RecordDefIncluder.h>
#endif

void mbap(void) {
Buffer buf;
buf.c = 2;
AnonymousStruct strct;
strct.y = 2;
UnionRecord rec;
rec.v = 2;
}
19 changes: 19 additions & 0 deletions clang/test/Modules/merge-record-definition-visibility.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// UNSUPPORTED: -zos, -aix
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: %clang_cc1 -emit-llvm -o %t/test.bc -F%S/Inputs/merge-record-definition %s \
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache

// Test a case when a struct definition is first imported as invisible and then as visible.

#import <RecordDefHidden/Visible.h>
#import <RecordDef/RecordDef.h>

void bibi(void) {
Buffer buf;
buf.b = 1;
AnonymousStruct strct;
strct.y = 1;
UnionRecord rec;
rec.u = 1;
}
29 changes: 29 additions & 0 deletions clang/test/Modules/merge-record-definition.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// UNSUPPORTED: -zos, -aix
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: %clang_cc1 -emit-llvm -o %t/test.bc -F%S/Inputs/merge-record-definition %s \
// RUN: -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/modules.cache

// Test a case when a struct definition is present in two different modules.

#import <RecordDef/RecordDef.h>

void bibi(void) {
Buffer buf;
buf.b = 1;
AnonymousStruct strct;
strct.x = 1;
UnionRecord rec;
rec.u = 1;
}

#import <RecordDefCopy/RecordDefCopy.h>

void mbap(void) {
Buffer buf;
buf.c = 2;
AnonymousStruct strct;
strct.y = 2;
UnionRecord rec;
rec.v = 2;
}