2017-02-22 09:11:25 +08:00
|
|
|
//===-- ODRHash.cpp - Hashing to diagnose ODR failures ----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// This file implements the ODRHash class, which calculates a hash based
|
|
|
|
/// on AST nodes, which is stable across different runs.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ODRHash.h"
|
|
|
|
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/NestedNameSpecifier.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/AST/TypeVisitor.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2017-02-23 06:22:42 +08:00
|
|
|
void ODRHash::AddStmt(const Stmt *S) {
|
|
|
|
assert(S && "Expecting non-null pointer.");
|
|
|
|
S->ProcessODRHash(ID, *this);
|
|
|
|
}
|
2017-02-23 08:23:01 +08:00
|
|
|
|
|
|
|
void ODRHash::AddIdentifierInfo(const IdentifierInfo *II) {
|
|
|
|
assert(II && "Expecting non-null pointer.");
|
|
|
|
ID.AddString(II->getName());
|
|
|
|
}
|
|
|
|
|
2017-02-24 10:59:12 +08:00
|
|
|
void ODRHash::AddDeclarationName(DeclarationName Name) {
|
2018-02-22 13:32:25 +08:00
|
|
|
// Index all DeclarationName and use index numbers to refer to them.
|
|
|
|
auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size()));
|
|
|
|
ID.AddInteger(Result.first->second);
|
|
|
|
if (!Result.second) {
|
|
|
|
// If found in map, the the DeclarationName has previously been processed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First time processing each DeclarationName, also process its details.
|
2017-02-24 10:59:12 +08:00
|
|
|
AddBoolean(Name.isEmpty());
|
|
|
|
if (Name.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto Kind = Name.getNameKind();
|
|
|
|
ID.AddInteger(Kind);
|
|
|
|
switch (Kind) {
|
|
|
|
case DeclarationName::Identifier:
|
|
|
|
AddIdentifierInfo(Name.getAsIdentifierInfo());
|
|
|
|
break;
|
|
|
|
case DeclarationName::ObjCZeroArgSelector:
|
|
|
|
case DeclarationName::ObjCOneArgSelector:
|
|
|
|
case DeclarationName::ObjCMultiArgSelector: {
|
|
|
|
Selector S = Name.getObjCSelector();
|
|
|
|
AddBoolean(S.isNull());
|
|
|
|
AddBoolean(S.isKeywordSelector());
|
|
|
|
AddBoolean(S.isUnarySelector());
|
|
|
|
unsigned NumArgs = S.getNumArgs();
|
|
|
|
for (unsigned i = 0; i < NumArgs; ++i) {
|
|
|
|
AddIdentifierInfo(S.getIdentifierInfoForSlot(i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DeclarationName::CXXConstructorName:
|
|
|
|
case DeclarationName::CXXDestructorName:
|
|
|
|
AddQualType(Name.getCXXNameType());
|
|
|
|
break;
|
|
|
|
case DeclarationName::CXXOperatorName:
|
|
|
|
ID.AddInteger(Name.getCXXOverloadedOperator());
|
|
|
|
break;
|
|
|
|
case DeclarationName::CXXLiteralOperatorName:
|
|
|
|
AddIdentifierInfo(Name.getCXXLiteralIdentifier());
|
|
|
|
break;
|
|
|
|
case DeclarationName::CXXConversionFunctionName:
|
|
|
|
AddQualType(Name.getCXXNameType());
|
|
|
|
break;
|
|
|
|
case DeclarationName::CXXUsingDirective:
|
|
|
|
break;
|
|
|
|
case DeclarationName::CXXDeductionGuideName: {
|
|
|
|
auto *Template = Name.getCXXDeductionGuideTemplate();
|
|
|
|
AddBoolean(Template);
|
|
|
|
if (Template) {
|
|
|
|
AddDecl(Template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:23:35 +08:00
|
|
|
void ODRHash::AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
|
2017-07-01 10:00:05 +08:00
|
|
|
assert(NNS && "Expecting non-null pointer.");
|
|
|
|
const auto *Prefix = NNS->getPrefix();
|
|
|
|
AddBoolean(Prefix);
|
|
|
|
if (Prefix) {
|
|
|
|
AddNestedNameSpecifier(Prefix);
|
2017-05-17 11:23:35 +08:00
|
|
|
}
|
|
|
|
auto Kind = NNS->getKind();
|
|
|
|
ID.AddInteger(Kind);
|
|
|
|
switch (Kind) {
|
|
|
|
case NestedNameSpecifier::Identifier:
|
|
|
|
AddIdentifierInfo(NNS->getAsIdentifier());
|
|
|
|
break;
|
|
|
|
case NestedNameSpecifier::Namespace:
|
|
|
|
AddDecl(NNS->getAsNamespace());
|
|
|
|
break;
|
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
AddDecl(NNS->getAsNamespaceAlias());
|
|
|
|
break;
|
|
|
|
case NestedNameSpecifier::TypeSpec:
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate:
|
|
|
|
AddType(NNS->getAsType());
|
|
|
|
break;
|
|
|
|
case NestedNameSpecifier::Global:
|
|
|
|
case NestedNameSpecifier::Super:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:31:58 +08:00
|
|
|
void ODRHash::AddTemplateName(TemplateName Name) {
|
|
|
|
auto Kind = Name.getKind();
|
|
|
|
ID.AddInteger(Kind);
|
|
|
|
|
|
|
|
switch (Kind) {
|
|
|
|
case TemplateName::Template:
|
|
|
|
AddDecl(Name.getAsTemplateDecl());
|
|
|
|
break;
|
|
|
|
// TODO: Support these cases.
|
|
|
|
case TemplateName::OverloadedTemplate:
|
|
|
|
case TemplateName::QualifiedTemplate:
|
|
|
|
case TemplateName::DependentTemplate:
|
|
|
|
case TemplateName::SubstTemplateTemplateParm:
|
|
|
|
case TemplateName::SubstTemplateTemplateParmPack:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 06:21:18 +08:00
|
|
|
void ODRHash::AddTemplateArgument(TemplateArgument TA) {
|
|
|
|
const auto Kind = TA.getKind();
|
|
|
|
ID.AddInteger(Kind);
|
2017-06-14 09:28:00 +08:00
|
|
|
|
|
|
|
switch (Kind) {
|
|
|
|
case TemplateArgument::Null:
|
2017-07-01 06:40:33 +08:00
|
|
|
llvm_unreachable("Expected valid TemplateArgument");
|
2017-06-14 09:28:00 +08:00
|
|
|
case TemplateArgument::Type:
|
2017-07-01 06:40:33 +08:00
|
|
|
AddQualType(TA.getAsType());
|
|
|
|
break;
|
2017-06-14 09:28:00 +08:00
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
case TemplateArgument::NullPtr:
|
|
|
|
case TemplateArgument::Integral:
|
2017-06-14 11:17:26 +08:00
|
|
|
break;
|
2017-06-14 09:28:00 +08:00
|
|
|
case TemplateArgument::Template:
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
2017-06-14 11:17:26 +08:00
|
|
|
AddTemplateName(TA.getAsTemplateOrTemplatePattern());
|
2017-06-14 09:28:00 +08:00
|
|
|
break;
|
|
|
|
case TemplateArgument::Expression:
|
|
|
|
AddStmt(TA.getAsExpr());
|
|
|
|
break;
|
|
|
|
case TemplateArgument::Pack:
|
2017-06-15 09:35:06 +08:00
|
|
|
ID.AddInteger(TA.pack_size());
|
|
|
|
for (auto SubTA : TA.pack_elements()) {
|
|
|
|
AddTemplateArgument(SubTA);
|
|
|
|
}
|
2017-06-14 09:28:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-06-14 06:21:18 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 10:43:59 +08:00
|
|
|
void ODRHash::AddTemplateParameterList(const TemplateParameterList *TPL) {
|
|
|
|
assert(TPL && "Expecting non-null pointer.");
|
|
|
|
|
|
|
|
ID.AddInteger(TPL->size());
|
|
|
|
for (auto *ND : TPL->asArray()) {
|
|
|
|
AddSubDecl(ND);
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 09:11:25 +08:00
|
|
|
|
|
|
|
void ODRHash::clear() {
|
2018-02-22 13:32:25 +08:00
|
|
|
DeclNameMap.clear();
|
2017-02-22 09:11:25 +08:00
|
|
|
TypeMap.clear();
|
|
|
|
Bools.clear();
|
|
|
|
ID.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ODRHash::CalculateHash() {
|
|
|
|
// Append the bools to the end of the data segment backwards. This allows
|
|
|
|
// for the bools data to be compressed 32 times smaller compared to using
|
|
|
|
// ID.AddBoolean
|
|
|
|
const unsigned unsigned_bits = sizeof(unsigned) * CHAR_BIT;
|
|
|
|
const unsigned size = Bools.size();
|
|
|
|
const unsigned remainder = size % unsigned_bits;
|
|
|
|
const unsigned loops = size / unsigned_bits;
|
|
|
|
auto I = Bools.rbegin();
|
|
|
|
unsigned value = 0;
|
|
|
|
for (unsigned i = 0; i < remainder; ++i) {
|
|
|
|
value <<= 1;
|
|
|
|
value |= *I;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
ID.AddInteger(value);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < loops; ++i) {
|
|
|
|
value = 0;
|
|
|
|
for (unsigned j = 0; j < unsigned_bits; ++j) {
|
|
|
|
value <<= 1;
|
|
|
|
value |= *I;
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
ID.AddInteger(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(I == Bools.rend());
|
|
|
|
Bools.clear();
|
|
|
|
return ID.ComputeHash();
|
|
|
|
}
|
|
|
|
|
2017-08-20 21:02:57 +08:00
|
|
|
namespace {
|
2017-02-22 09:11:25 +08:00
|
|
|
// Process a Decl pointer. Add* methods call back into ODRHash while Visit*
|
|
|
|
// methods process the relevant parts of the Decl.
|
|
|
|
class ODRDeclVisitor : public ConstDeclVisitor<ODRDeclVisitor> {
|
|
|
|
typedef ConstDeclVisitor<ODRDeclVisitor> Inherited;
|
|
|
|
llvm::FoldingSetNodeID &ID;
|
2017-02-23 06:22:42 +08:00
|
|
|
ODRHash &Hash;
|
2017-02-22 09:11:25 +08:00
|
|
|
|
|
|
|
public:
|
2017-02-23 06:22:42 +08:00
|
|
|
ODRDeclVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
|
|
|
|
: ID(ID), Hash(Hash) {}
|
|
|
|
|
|
|
|
void AddStmt(const Stmt *S) {
|
|
|
|
Hash.AddBoolean(S);
|
|
|
|
if (S) {
|
|
|
|
Hash.AddStmt(S);
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 09:11:25 +08:00
|
|
|
|
2017-02-23 08:23:01 +08:00
|
|
|
void AddIdentifierInfo(const IdentifierInfo *II) {
|
|
|
|
Hash.AddBoolean(II);
|
|
|
|
if (II) {
|
|
|
|
Hash.AddIdentifierInfo(II);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:25:57 +08:00
|
|
|
void AddQualType(QualType T) {
|
|
|
|
Hash.AddQualType(T);
|
|
|
|
}
|
|
|
|
|
2017-07-08 10:04:42 +08:00
|
|
|
void AddDecl(const Decl *D) {
|
|
|
|
Hash.AddBoolean(D);
|
|
|
|
if (D) {
|
|
|
|
Hash.AddDecl(D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 10:43:59 +08:00
|
|
|
void AddTemplateArgument(TemplateArgument TA) {
|
|
|
|
Hash.AddTemplateArgument(TA);
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:11:25 +08:00
|
|
|
void Visit(const Decl *D) {
|
|
|
|
ID.AddInteger(D->getKind());
|
|
|
|
Inherited::Visit(D);
|
|
|
|
}
|
|
|
|
|
2017-02-23 08:23:01 +08:00
|
|
|
void VisitNamedDecl(const NamedDecl *D) {
|
2017-03-04 08:08:58 +08:00
|
|
|
Hash.AddDeclarationName(D->getDeclName());
|
2017-02-23 08:23:01 +08:00
|
|
|
Inherited::VisitNamedDecl(D);
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:25:57 +08:00
|
|
|
void VisitValueDecl(const ValueDecl *D) {
|
2017-07-14 09:36:41 +08:00
|
|
|
if (!isa<FunctionDecl>(D)) {
|
|
|
|
AddQualType(D->getType());
|
|
|
|
}
|
2017-02-23 11:25:57 +08:00
|
|
|
Inherited::VisitValueDecl(D);
|
|
|
|
}
|
|
|
|
|
2017-06-16 10:44:29 +08:00
|
|
|
void VisitVarDecl(const VarDecl *D) {
|
|
|
|
Hash.AddBoolean(D->isStaticLocal());
|
|
|
|
Hash.AddBoolean(D->isConstexpr());
|
|
|
|
const bool HasInit = D->hasInit();
|
|
|
|
Hash.AddBoolean(HasInit);
|
|
|
|
if (HasInit) {
|
|
|
|
AddStmt(D->getInit());
|
|
|
|
}
|
|
|
|
Inherited::VisitVarDecl(D);
|
|
|
|
}
|
|
|
|
|
2017-05-03 07:58:52 +08:00
|
|
|
void VisitParmVarDecl(const ParmVarDecl *D) {
|
|
|
|
// TODO: Handle default arguments.
|
|
|
|
Inherited::VisitParmVarDecl(D);
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:11:25 +08:00
|
|
|
void VisitAccessSpecDecl(const AccessSpecDecl *D) {
|
|
|
|
ID.AddInteger(D->getAccess());
|
|
|
|
Inherited::VisitAccessSpecDecl(D);
|
|
|
|
}
|
2017-02-23 06:22:42 +08:00
|
|
|
|
|
|
|
void VisitStaticAssertDecl(const StaticAssertDecl *D) {
|
|
|
|
AddStmt(D->getAssertExpr());
|
|
|
|
AddStmt(D->getMessage());
|
|
|
|
|
|
|
|
Inherited::VisitStaticAssertDecl(D);
|
|
|
|
}
|
2017-02-23 08:23:01 +08:00
|
|
|
|
|
|
|
void VisitFieldDecl(const FieldDecl *D) {
|
2017-02-25 04:59:28 +08:00
|
|
|
const bool IsBitfield = D->isBitField();
|
|
|
|
Hash.AddBoolean(IsBitfield);
|
|
|
|
|
|
|
|
if (IsBitfield) {
|
|
|
|
AddStmt(D->getBitWidth());
|
|
|
|
}
|
2017-02-25 07:35:37 +08:00
|
|
|
|
|
|
|
Hash.AddBoolean(D->isMutable());
|
|
|
|
AddStmt(D->getInClassInitializer());
|
2017-02-25 09:29:34 +08:00
|
|
|
|
|
|
|
Inherited::VisitFieldDecl(D);
|
2017-02-23 08:23:01 +08:00
|
|
|
}
|
2017-03-01 05:24:38 +08:00
|
|
|
|
|
|
|
void VisitFunctionDecl(const FunctionDecl *D) {
|
2017-03-04 08:08:58 +08:00
|
|
|
ID.AddInteger(D->getStorageClass());
|
|
|
|
Hash.AddBoolean(D->isInlineSpecified());
|
|
|
|
Hash.AddBoolean(D->isVirtualAsWritten());
|
|
|
|
Hash.AddBoolean(D->isPure());
|
|
|
|
Hash.AddBoolean(D->isDeletedAsWritten());
|
|
|
|
|
2017-05-03 07:58:52 +08:00
|
|
|
ID.AddInteger(D->param_size());
|
|
|
|
|
|
|
|
for (auto *Param : D->parameters()) {
|
|
|
|
Hash.AddSubDecl(Param);
|
|
|
|
}
|
|
|
|
|
2017-07-14 09:36:41 +08:00
|
|
|
AddQualType(D->getReturnType());
|
|
|
|
|
2017-03-01 05:24:38 +08:00
|
|
|
Inherited::VisitFunctionDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitCXXMethodDecl(const CXXMethodDecl *D) {
|
2017-03-04 08:08:58 +08:00
|
|
|
Hash.AddBoolean(D->isConst());
|
|
|
|
Hash.AddBoolean(D->isVolatile());
|
|
|
|
|
2017-03-01 05:24:38 +08:00
|
|
|
Inherited::VisitCXXMethodDecl(D);
|
|
|
|
}
|
2017-03-08 08:13:19 +08:00
|
|
|
|
|
|
|
void VisitTypedefNameDecl(const TypedefNameDecl *D) {
|
|
|
|
AddQualType(D->getUnderlyingType());
|
|
|
|
|
|
|
|
Inherited::VisitTypedefNameDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitTypedefDecl(const TypedefDecl *D) {
|
|
|
|
Inherited::VisitTypedefDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitTypeAliasDecl(const TypeAliasDecl *D) {
|
|
|
|
Inherited::VisitTypeAliasDecl(D);
|
|
|
|
}
|
2017-07-08 10:04:42 +08:00
|
|
|
|
|
|
|
void VisitFriendDecl(const FriendDecl *D) {
|
|
|
|
TypeSourceInfo *TSI = D->getFriendType();
|
|
|
|
Hash.AddBoolean(TSI);
|
|
|
|
if (TSI) {
|
|
|
|
AddQualType(TSI->getType());
|
|
|
|
} else {
|
|
|
|
AddDecl(D->getFriendDecl());
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 10:43:59 +08:00
|
|
|
|
|
|
|
void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
|
|
|
|
// Only care about default arguments as part of the definition.
|
|
|
|
const bool hasDefaultArgument =
|
|
|
|
D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
|
|
|
|
Hash.AddBoolean(hasDefaultArgument);
|
|
|
|
if (hasDefaultArgument) {
|
|
|
|
AddTemplateArgument(D->getDefaultArgument());
|
|
|
|
}
|
|
|
|
|
|
|
|
Inherited::VisitTemplateTypeParmDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D) {
|
|
|
|
// Only care about default arguments as part of the definition.
|
|
|
|
const bool hasDefaultArgument =
|
|
|
|
D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
|
|
|
|
Hash.AddBoolean(hasDefaultArgument);
|
|
|
|
if (hasDefaultArgument) {
|
|
|
|
AddStmt(D->getDefaultArgument());
|
|
|
|
}
|
|
|
|
|
|
|
|
Inherited::VisitNonTypeTemplateParmDecl(D);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D) {
|
|
|
|
// Only care about default arguments as part of the definition.
|
|
|
|
const bool hasDefaultArgument =
|
|
|
|
D->hasDefaultArgument() && !D->defaultArgumentWasInherited();
|
|
|
|
Hash.AddBoolean(hasDefaultArgument);
|
|
|
|
if (hasDefaultArgument) {
|
|
|
|
AddTemplateArgument(D->getDefaultArgument().getArgument());
|
|
|
|
}
|
|
|
|
|
|
|
|
Inherited::VisitTemplateTemplateParmDecl(D);
|
|
|
|
}
|
2017-02-22 09:11:25 +08:00
|
|
|
};
|
2017-08-20 21:02:57 +08:00
|
|
|
} // namespace
|
2017-02-22 09:11:25 +08:00
|
|
|
|
|
|
|
// Only allow a small portion of Decl's to be processed. Remove this once
|
|
|
|
// all Decl's can be handled.
|
|
|
|
bool ODRHash::isWhitelistedDecl(const Decl *D, const CXXRecordDecl *Parent) {
|
|
|
|
if (D->isImplicit()) return false;
|
|
|
|
if (D->getDeclContext() != Parent) return false;
|
|
|
|
|
|
|
|
switch (D->getKind()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case Decl::AccessSpec:
|
2017-07-15 10:55:13 +08:00
|
|
|
case Decl::CXXConstructor:
|
|
|
|
case Decl::CXXDestructor:
|
2017-03-01 05:24:38 +08:00
|
|
|
case Decl::CXXMethod:
|
2017-02-23 08:23:01 +08:00
|
|
|
case Decl::Field:
|
2017-07-08 10:04:42 +08:00
|
|
|
case Decl::Friend:
|
2017-02-23 06:22:42 +08:00
|
|
|
case Decl::StaticAssert:
|
2017-03-08 08:13:19 +08:00
|
|
|
case Decl::TypeAlias:
|
|
|
|
case Decl::Typedef:
|
2017-06-16 10:44:29 +08:00
|
|
|
case Decl::Var:
|
2017-02-22 09:11:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ODRHash::AddSubDecl(const Decl *D) {
|
|
|
|
assert(D && "Expecting non-null pointer.");
|
|
|
|
|
2017-02-23 06:22:42 +08:00
|
|
|
ODRDeclVisitor(ID, *this).Visit(D);
|
2017-02-22 09:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ODRHash::AddCXXRecordDecl(const CXXRecordDecl *Record) {
|
|
|
|
assert(Record && Record->hasDefinition() &&
|
|
|
|
"Expected non-null record to be a definition.");
|
2017-05-03 07:58:52 +08:00
|
|
|
|
2017-08-05 08:54:19 +08:00
|
|
|
const DeclContext *DC = Record;
|
|
|
|
while (DC) {
|
|
|
|
if (isa<ClassTemplateSpecializationDecl>(DC)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DC = DC->getParent();
|
2017-05-03 07:58:52 +08:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:11:25 +08:00
|
|
|
AddDecl(Record);
|
|
|
|
|
|
|
|
// Filter out sub-Decls which will not be processed in order to get an
|
|
|
|
// accurate count of Decl's.
|
|
|
|
llvm::SmallVector<const Decl *, 16> Decls;
|
|
|
|
for (const Decl *SubDecl : Record->decls()) {
|
|
|
|
if (isWhitelistedDecl(SubDecl, Record)) {
|
|
|
|
Decls.push_back(SubDecl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ID.AddInteger(Decls.size());
|
|
|
|
for (auto SubDecl : Decls) {
|
|
|
|
AddSubDecl(SubDecl);
|
|
|
|
}
|
2017-08-23 10:43:59 +08:00
|
|
|
|
|
|
|
const ClassTemplateDecl *TD = Record->getDescribedClassTemplate();
|
|
|
|
AddBoolean(TD);
|
|
|
|
if (TD) {
|
|
|
|
AddTemplateParameterList(TD->getTemplateParameters());
|
|
|
|
}
|
2017-09-30 10:19:17 +08:00
|
|
|
|
|
|
|
ID.AddInteger(Record->getNumBases());
|
|
|
|
auto Bases = Record->bases();
|
|
|
|
for (auto Base : Bases) {
|
|
|
|
AddQualType(Base.getType());
|
|
|
|
ID.AddInteger(Base.isVirtual());
|
|
|
|
ID.AddInteger(Base.getAccessSpecifierAsWritten());
|
|
|
|
}
|
2017-02-22 09:11:25 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 08:41:01 +08:00
|
|
|
void ODRHash::AddFunctionDecl(const FunctionDecl *Function) {
|
|
|
|
assert(Function && "Expecting non-null pointer.");
|
|
|
|
|
|
|
|
// Skip hashing these kinds of function.
|
|
|
|
if (Function->isImplicit()) return;
|
|
|
|
if (Function->isDefaulted()) return;
|
|
|
|
if (Function->isDeleted()) return;
|
|
|
|
if (!Function->hasBody()) return;
|
|
|
|
if (!Function->getBody()) return;
|
|
|
|
|
|
|
|
// Skip functions that are specializations or in specialization context.
|
|
|
|
const DeclContext *DC = Function;
|
|
|
|
while (DC) {
|
|
|
|
if (isa<ClassTemplateSpecializationDecl>(DC)) return;
|
|
|
|
if (auto *F = dyn_cast<FunctionDecl>(DC))
|
|
|
|
if (F->isFunctionTemplateSpecialization()) return;
|
|
|
|
DC = DC->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddDecl(Function);
|
|
|
|
|
|
|
|
AddQualType(Function->getReturnType());
|
|
|
|
|
|
|
|
ID.AddInteger(Function->param_size());
|
|
|
|
for (auto Param : Function->parameters())
|
|
|
|
AddSubDecl(Param);
|
|
|
|
|
|
|
|
AddStmt(Function->getBody());
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:11:25 +08:00
|
|
|
void ODRHash::AddDecl(const Decl *D) {
|
|
|
|
assert(D && "Expecting non-null pointer.");
|
2017-12-22 06:38:29 +08:00
|
|
|
D = D->getCanonicalDecl();
|
2017-02-24 10:59:12 +08:00
|
|
|
|
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
AddDeclarationName(ND->getDeclName());
|
2018-02-22 13:32:25 +08:00
|
|
|
return;
|
2017-02-24 10:59:12 +08:00
|
|
|
}
|
2018-02-22 13:32:25 +08:00
|
|
|
|
|
|
|
ID.AddInteger(D->getKind());
|
|
|
|
// TODO: Handle non-NamedDecl here.
|
2017-02-22 09:11:25 +08:00
|
|
|
}
|
|
|
|
|
2017-08-20 21:02:57 +08:00
|
|
|
namespace {
|
2017-02-23 11:25:57 +08:00
|
|
|
// Process a Type pointer. Add* methods call back into ODRHash while Visit*
|
|
|
|
// methods process the relevant parts of the Type.
|
|
|
|
class ODRTypeVisitor : public TypeVisitor<ODRTypeVisitor> {
|
|
|
|
typedef TypeVisitor<ODRTypeVisitor> Inherited;
|
|
|
|
llvm::FoldingSetNodeID &ID;
|
|
|
|
ODRHash &Hash;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ODRTypeVisitor(llvm::FoldingSetNodeID &ID, ODRHash &Hash)
|
|
|
|
: ID(ID), Hash(Hash) {}
|
|
|
|
|
|
|
|
void AddStmt(Stmt *S) {
|
|
|
|
Hash.AddBoolean(S);
|
|
|
|
if (S) {
|
|
|
|
Hash.AddStmt(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-24 10:59:12 +08:00
|
|
|
void AddDecl(Decl *D) {
|
|
|
|
Hash.AddBoolean(D);
|
|
|
|
if (D) {
|
|
|
|
Hash.AddDecl(D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 07:58:52 +08:00
|
|
|
void AddQualType(QualType T) {
|
|
|
|
Hash.AddQualType(T);
|
|
|
|
}
|
|
|
|
|
2017-06-30 06:53:04 +08:00
|
|
|
void AddType(const Type *T) {
|
|
|
|
Hash.AddBoolean(T);
|
|
|
|
if (T) {
|
|
|
|
Hash.AddType(T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 10:29:02 +08:00
|
|
|
void AddNestedNameSpecifier(const NestedNameSpecifier *NNS) {
|
2017-07-01 10:00:05 +08:00
|
|
|
Hash.AddBoolean(NNS);
|
|
|
|
if (NNS) {
|
|
|
|
Hash.AddNestedNameSpecifier(NNS);
|
|
|
|
}
|
2017-05-17 10:29:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddIdentifierInfo(const IdentifierInfo *II) {
|
|
|
|
Hash.AddBoolean(II);
|
|
|
|
if (II) {
|
|
|
|
Hash.AddIdentifierInfo(II);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 07:58:52 +08:00
|
|
|
void VisitQualifiers(Qualifiers Quals) {
|
|
|
|
ID.AddInteger(Quals.getAsOpaqueValue());
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:25:57 +08:00
|
|
|
void Visit(const Type *T) {
|
|
|
|
ID.AddInteger(T->getTypeClass());
|
|
|
|
Inherited::Visit(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitType(const Type *T) {}
|
|
|
|
|
2017-05-03 07:58:52 +08:00
|
|
|
void VisitAdjustedType(const AdjustedType *T) {
|
|
|
|
AddQualType(T->getOriginalType());
|
|
|
|
AddQualType(T->getAdjustedType());
|
|
|
|
VisitType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDecayedType(const DecayedType *T) {
|
|
|
|
AddQualType(T->getDecayedType());
|
|
|
|
AddQualType(T->getPointeeType());
|
|
|
|
VisitAdjustedType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitArrayType(const ArrayType *T) {
|
|
|
|
AddQualType(T->getElementType());
|
|
|
|
ID.AddInteger(T->getSizeModifier());
|
|
|
|
VisitQualifiers(T->getIndexTypeQualifiers());
|
|
|
|
VisitType(T);
|
|
|
|
}
|
|
|
|
void VisitConstantArrayType(const ConstantArrayType *T) {
|
|
|
|
T->getSize().Profile(ID);
|
|
|
|
VisitArrayType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
|
|
|
|
AddStmt(T->getSizeExpr());
|
|
|
|
VisitArrayType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitIncompleteArrayType(const IncompleteArrayType *T) {
|
|
|
|
VisitArrayType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitVariableArrayType(const VariableArrayType *T) {
|
|
|
|
AddStmt(T->getSizeExpr());
|
|
|
|
VisitArrayType(T);
|
|
|
|
}
|
|
|
|
|
2017-02-23 11:25:57 +08:00
|
|
|
void VisitBuiltinType(const BuiltinType *T) {
|
|
|
|
ID.AddInteger(T->getKind());
|
|
|
|
VisitType(T);
|
|
|
|
}
|
2017-02-24 10:59:12 +08:00
|
|
|
|
2017-05-03 07:58:52 +08:00
|
|
|
void VisitFunctionType(const FunctionType *T) {
|
|
|
|
AddQualType(T->getReturnType());
|
|
|
|
T->getExtInfo().Profile(ID);
|
|
|
|
Hash.AddBoolean(T->isConst());
|
|
|
|
Hash.AddBoolean(T->isVolatile());
|
|
|
|
Hash.AddBoolean(T->isRestrict());
|
|
|
|
VisitType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
|
|
|
|
VisitFunctionType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitFunctionProtoType(const FunctionProtoType *T) {
|
|
|
|
ID.AddInteger(T->getNumParams());
|
|
|
|
for (auto ParamType : T->getParamTypes())
|
|
|
|
AddQualType(ParamType);
|
|
|
|
|
|
|
|
VisitFunctionType(T);
|
|
|
|
}
|
|
|
|
|
2017-02-24 10:59:12 +08:00
|
|
|
void VisitTypedefType(const TypedefType *T) {
|
|
|
|
AddDecl(T->getDecl());
|
2017-06-30 06:53:04 +08:00
|
|
|
QualType UnderlyingType = T->getDecl()->getUnderlyingType();
|
|
|
|
VisitQualifiers(UnderlyingType.getQualifiers());
|
|
|
|
while (const TypedefType *Underlying =
|
|
|
|
dyn_cast<TypedefType>(UnderlyingType.getTypePtr())) {
|
|
|
|
UnderlyingType = Underlying->getDecl()->getUnderlyingType();
|
|
|
|
}
|
|
|
|
AddType(UnderlyingType.getTypePtr());
|
2017-02-24 10:59:12 +08:00
|
|
|
VisitType(T);
|
|
|
|
}
|
2017-05-17 10:29:02 +08:00
|
|
|
|
|
|
|
void VisitTagType(const TagType *T) {
|
|
|
|
AddDecl(T->getDecl());
|
|
|
|
VisitType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitRecordType(const RecordType *T) { VisitTagType(T); }
|
|
|
|
void VisitEnumType(const EnumType *T) { VisitTagType(T); }
|
|
|
|
|
|
|
|
void VisitTypeWithKeyword(const TypeWithKeyword *T) {
|
|
|
|
ID.AddInteger(T->getKeyword());
|
|
|
|
VisitType(T);
|
|
|
|
};
|
|
|
|
|
|
|
|
void VisitDependentNameType(const DependentNameType *T) {
|
|
|
|
AddNestedNameSpecifier(T->getQualifier());
|
|
|
|
AddIdentifierInfo(T->getIdentifier());
|
|
|
|
VisitTypeWithKeyword(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDependentTemplateSpecializationType(
|
|
|
|
const DependentTemplateSpecializationType *T) {
|
|
|
|
AddIdentifierInfo(T->getIdentifier());
|
|
|
|
AddNestedNameSpecifier(T->getQualifier());
|
|
|
|
ID.AddInteger(T->getNumArgs());
|
|
|
|
for (const auto &TA : T->template_arguments()) {
|
|
|
|
Hash.AddTemplateArgument(TA);
|
|
|
|
}
|
|
|
|
VisitTypeWithKeyword(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitElaboratedType(const ElaboratedType *T) {
|
|
|
|
AddNestedNameSpecifier(T->getQualifier());
|
|
|
|
AddQualType(T->getNamedType());
|
|
|
|
VisitTypeWithKeyword(T);
|
|
|
|
}
|
2017-05-31 08:31:58 +08:00
|
|
|
|
|
|
|
void VisitTemplateSpecializationType(const TemplateSpecializationType *T) {
|
|
|
|
ID.AddInteger(T->getNumArgs());
|
|
|
|
for (const auto &TA : T->template_arguments()) {
|
|
|
|
Hash.AddTemplateArgument(TA);
|
|
|
|
}
|
|
|
|
Hash.AddTemplateName(T->getTemplateName());
|
|
|
|
VisitType(T);
|
|
|
|
}
|
2017-06-15 09:35:06 +08:00
|
|
|
|
|
|
|
void VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
|
|
|
|
ID.AddInteger(T->getDepth());
|
|
|
|
ID.AddInteger(T->getIndex());
|
|
|
|
Hash.AddBoolean(T->isParameterPack());
|
|
|
|
AddDecl(T->getDecl());
|
|
|
|
}
|
2017-02-23 11:25:57 +08:00
|
|
|
};
|
2017-08-20 21:02:57 +08:00
|
|
|
} // namespace
|
2017-02-23 11:25:57 +08:00
|
|
|
|
|
|
|
void ODRHash::AddType(const Type *T) {
|
|
|
|
assert(T && "Expecting non-null pointer.");
|
|
|
|
auto Result = TypeMap.insert(std::make_pair(T, TypeMap.size()));
|
|
|
|
ID.AddInteger(Result.first->second);
|
|
|
|
// On first encounter of a Type pointer, process it. Every time afterwards,
|
|
|
|
// only the index value is needed.
|
|
|
|
if (!Result.second) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ODRTypeVisitor(ID, *this).Visit(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ODRHash::AddQualType(QualType T) {
|
|
|
|
AddBoolean(T.isNull());
|
|
|
|
if (T.isNull())
|
|
|
|
return;
|
|
|
|
SplitQualType split = T.split();
|
|
|
|
ID.AddInteger(split.Quals.getAsOpaqueValue());
|
|
|
|
AddType(split.Ty);
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:11:25 +08:00
|
|
|
void ODRHash::AddBoolean(bool Value) {
|
|
|
|
Bools.push_back(Value);
|
|
|
|
}
|