Skip to content

Commit

Permalink
Part of adding an improved ODR checker.
Browse files Browse the repository at this point in the history
Reserve a spot for ODR hash in CXXRecordDecl and in its modules storage.
Default the hash value to 0 for all classes.

Differential Revision: https://reviews.llvm.org/D21675

llvm-svn: 295533
  • Loading branch information
Weverything committed Feb 18, 2017
1 parent 54e0682 commit b6adf54
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions clang/include/clang/AST/DeclCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ class CXXRecordDecl : public RecordDecl {
/// \brief Whether we are currently parsing base specifiers.
unsigned IsParsingBaseSpecifiers : 1;

/// \brief A hash of parts of the class to help in ODR checking.
unsigned ODRHash;

/// \brief The number of base class specifiers in Bases.
unsigned NumBases;

Expand Down Expand Up @@ -703,6 +706,9 @@ class CXXRecordDecl : public RecordDecl {
return data().IsParsingBaseSpecifiers;
}

void computeODRHash();
unsigned getODRHash() const { return data().ODRHash; }

/// \brief Sets the base classes of this struct or class.
void setBases(CXXBaseSpecifier const * const *Bases, unsigned NumBases);

Expand Down
6 changes: 4 additions & 2 deletions clang/lib/AST/DeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
ImplicitCopyAssignmentHasConstParam(true),
HasDeclaredCopyConstructorWithConstParam(false),
HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0), Bases(),
VBases(), Definition(D), FirstFriend() {}
IsParsingBaseSpecifiers(false), ODRHash(0), NumBases(0), NumVBases(0),
Bases(), VBases(), Definition(D), FirstFriend() {}

CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
return Bases.get(Definition->getASTContext().getExternalSource());
Expand Down Expand Up @@ -371,6 +371,8 @@ CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
data().IsParsingBaseSpecifiers = false;
}

void CXXRecordDecl::computeODRHash() {}

void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
// C++11 [class.copy]p11:
// A defaulted copy/move constructor for a class X is defined as
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13771,8 +13771,11 @@ void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD,
RD->completeDefinition();
}

if (isa<CXXRecordDecl>(Tag))
if (auto *RD = dyn_cast<CXXRecordDecl>(Tag)) {
FieldCollector->FinishClass();
if (Context.getLangOpts().Modules)
RD->computeODRHash();
}

// Exit this scope of this tag's definition.
PopDeclContext();
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,7 @@ void ASTDeclReader::ReadCXXDefinitionData(
Data.ImplicitCopyAssignmentHasConstParam = Record.readInt();
Data.HasDeclaredCopyConstructorWithConstParam = Record.readInt();
Data.HasDeclaredCopyAssignmentWithConstParam = Record.readInt();
Data.ODRHash = Record.readInt();

Data.NumBases = Record.readInt();
if (Data.NumBases)
Expand Down Expand Up @@ -1658,6 +1659,7 @@ void ASTDeclReader::MergeDefinitionData(
OR_FIELD(HasDeclaredCopyConstructorWithConstParam)
OR_FIELD(HasDeclaredCopyAssignmentWithConstParam)
MATCH_FIELD(IsLambda)
MATCH_FIELD(ODRHash)
#undef OR_FIELD
#undef MATCH_FIELD

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5707,6 +5707,7 @@ void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Record->push_back(Data.ODRHash);
// IsLambda bit is already saved.

Record->push_back(Data.NumBases);
Expand Down

0 comments on commit b6adf54

Please sign in to comment.