const FunctionDecl *Defn = nullptr;
if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
FD->setLazyBody(PB->second);
- } else
- mergeDefinitionVisibility(const_cast<FunctionDecl*>(Defn), FD);
+ } else {
+ auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
+ mergeDefinitionVisibility(NonConstDefn, FD);
+
+ if (!FD->isLateTemplateParsed() &&
+ !NonConstDefn->isLateTemplateParsed() &&
+ FD->getODRHash() != NonConstDefn->getODRHash()) {
+ PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
+ }
+ }
continue;
}
}
void ASTReader::diagnoseOdrViolations() {
- if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty())
+ if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
+ PendingFunctionOdrMergeFailures.empty())
return;
// Trigger the import of the full definition of each class that had any
}
}
+ // Trigger the import of functions.
+ auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
+ PendingFunctionOdrMergeFailures.clear();
+ for (auto &Merge : FunctionOdrMergeFailures) {
+ Merge.first->buildLookup();
+ Merge.first->decls_begin();
+ Merge.first->getBody();
+ for (auto &FD : Merge.second) {
+ FD->buildLookup();
+ FD->decls_begin();
+ FD->getBody();
+ }
+ }
+
// For each declaration from a merged context, check that the canonical
// definition of that context also contains a declaration of the same
// entity.
}
}
- if (OdrMergeFailures.empty())
+ if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty())
return;
// Ensure we don't accidentally recursively enter deserialization while
// we're producing our diagnostics.
Deserializing RecursionGuard(this);
+ // Common code for hashing helpers.
+ ODRHash Hash;
+ auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
+ Hash.clear();
+ Hash.AddQualType(Ty);
+ return Hash.CalculateHash();
+ };
+
+ auto ComputeODRHash = [&Hash](const Stmt *S) {
+ assert(S);
+ Hash.clear();
+ Hash.AddStmt(S);
+ return Hash.CalculateHash();
+ };
+
+ auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
+ assert(D);
+ Hash.clear();
+ Hash.AddSubDecl(D);
+ return Hash.CalculateHash();
+ };
+
// Issue any pending ODR-failure diagnostics.
for (auto &Merge : OdrMergeFailures) {
// If we've already pointed out a specific problem with this class, don't
<< SecondModule << Range << DiffType;
};
- ODRHash Hash;
- auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
- Hash.clear();
- Hash.AddQualType(Ty);
- return Hash.CalculateHash();
- };
-
unsigned FirstNumBases = FirstDD->NumBases;
unsigned FirstNumVBases = FirstDD->NumVBases;
unsigned SecondNumBases = SecondDD->NumBases;
if (FirstTemplate && SecondTemplate) {
DeclHashes FirstTemplateHashes;
DeclHashes SecondTemplateHashes;
- ODRHash Hash;
auto PopulateTemplateParameterHashs =
- [&Hash](DeclHashes &Hashes, const ClassTemplateDecl *TD) {
+ [&ComputeSubDeclODRHash](DeclHashes &Hashes,
+ const ClassTemplateDecl *TD) {
for (auto *D : TD->getTemplateParameters()->asArray()) {
- Hash.clear();
- Hash.AddSubDecl(D);
- Hashes.emplace_back(D, Hash.CalculateHash());
+ Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
}
};
DeclHashes FirstHashes;
DeclHashes SecondHashes;
- ODRHash Hash;
- auto PopulateHashes = [&Hash, FirstRecord](DeclHashes &Hashes,
- CXXRecordDecl *Record) {
+ auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord](
+ DeclHashes &Hashes, CXXRecordDecl *Record) {
for (auto *D : Record->decls()) {
// Due to decl merging, the first CXXRecordDecl is the parent of
// Decls in both records.
if (!ODRHash::isWhitelistedDecl(D, FirstRecord))
continue;
- Hash.clear();
- Hash.AddSubDecl(D);
- Hashes.emplace_back(D, Hash.CalculateHash());
+ Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
}
};
PopulateHashes(FirstHashes, FirstRecord);
<< SecondModule << Range << DiffType;
};
- auto ComputeODRHash = [&Hash](const Stmt* S) {
- assert(S);
- Hash.clear();
- Hash.AddStmt(S);
- return Hash.CalculateHash();
- };
-
- auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
- Hash.clear();
- Hash.AddQualType(Ty);
- return Hash.CalculateHash();
- };
-
switch (FirstDiffType) {
case Other:
case EndOfClass:
<< Merge.first;
}
}
+
+ // Issue ODR failures diagnostics for functions.
+ for (auto &Merge : FunctionOdrMergeFailures) {
+ enum ODRFunctionDifference {
+ ReturnType,
+ ParameterName,
+ ParameterType,
+ ParameterSingleDefaultArgument,
+ ParameterDifferentDefaultArgument,
+ FunctionBody,
+ };
+
+ FunctionDecl *FirstFunction = Merge.first;
+ std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
+
+ bool Diagnosed = false;
+ for (auto &SecondFunction : Merge.second) {
+
+ if (FirstFunction == SecondFunction)
+ continue;
+
+ std::string SecondModule =
+ getOwningModuleNameForDiagnostic(SecondFunction);
+
+ auto ODRDiagError = [FirstFunction, &FirstModule,
+ this](SourceLocation Loc, SourceRange Range,
+ ODRFunctionDifference DiffType) {
+ return Diag(Loc, diag::err_module_odr_violation_function)
+ << FirstFunction << FirstModule.empty() << FirstModule << Range
+ << DiffType;
+ };
+ auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
+ SourceRange Range,
+ ODRFunctionDifference DiffType) {
+ return Diag(Loc, diag::note_module_odr_violation_function)
+ << SecondModule << Range << DiffType;
+ };
+
+ if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
+ ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
+ ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
+ FirstFunction->getReturnTypeSourceRange(), ReturnType)
+ << FirstFunction->getReturnType();
+ ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
+ SecondFunction->getReturnTypeSourceRange(), ReturnType)
+ << SecondFunction->getReturnType();
+ Diagnosed = true;
+ break;
+ }
+
+ assert(FirstFunction->param_size() == SecondFunction->param_size() &&
+ "Merged functions with different number of parameters");
+
+ auto ParamSize = FirstFunction->param_size();
+ bool ParameterMismatch = false;
+ for (unsigned I = 0; I < ParamSize; ++I) {
+ auto *FirstParam = FirstFunction->getParamDecl(I);
+ auto *SecondParam = SecondFunction->getParamDecl(I);
+
+ assert(getContext().hasSameType(FirstParam->getType(),
+ SecondParam->getType()) &&
+ "Merged function has different parameter types.");
+
+ if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
+ ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
+ ParameterName)
+ << I + 1 << FirstParam->getDeclName();
+ ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
+ ParameterName)
+ << I + 1 << SecondParam->getDeclName();
+ ParameterMismatch = true;
+ break;
+ };
+
+ QualType FirstParamType = FirstParam->getType();
+ QualType SecondParamType = SecondParam->getType();
+ if (FirstParamType != SecondParamType &&
+ ComputeQualTypeODRHash(FirstParamType) !=
+ ComputeQualTypeODRHash(SecondParamType)) {
+ if (const DecayedType *ParamDecayedType =
+ FirstParamType->getAs<DecayedType>()) {
+ ODRDiagError(FirstParam->getLocation(),
+ FirstParam->getSourceRange(), ParameterType)
+ << (I + 1) << FirstParamType << true
+ << ParamDecayedType->getOriginalType();
+ } else {
+ ODRDiagError(FirstParam->getLocation(),
+ FirstParam->getSourceRange(), ParameterType)
+ << (I + 1) << FirstParamType << false;
+ }
+
+ if (const DecayedType *ParamDecayedType =
+ SecondParamType->getAs<DecayedType>()) {
+ ODRDiagNote(SecondParam->getLocation(),
+ SecondParam->getSourceRange(), ParameterType)
+ << (I + 1) << SecondParamType << true
+ << ParamDecayedType->getOriginalType();
+ } else {
+ ODRDiagNote(SecondParam->getLocation(),
+ SecondParam->getSourceRange(), ParameterType)
+ << (I + 1) << SecondParamType << false;
+ }
+ ParameterMismatch = true;
+ break;
+ }
+
+ const Expr *FirstInit = FirstParam->getInit();
+ const Expr *SecondInit = SecondParam->getInit();
+ if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
+ ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
+ ParameterSingleDefaultArgument)
+ << (I + 1) << (FirstInit == nullptr)
+ << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
+ ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
+ ParameterSingleDefaultArgument)
+ << (I + 1) << (SecondInit == nullptr)
+ << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
+ ParameterMismatch = true;
+ break;
+ }
+
+ if (FirstInit && SecondInit &&
+ ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
+ ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
+ ParameterDifferentDefaultArgument)
+ << (I + 1) << FirstInit->getSourceRange();
+ ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
+ ParameterDifferentDefaultArgument)
+ << (I + 1) << SecondInit->getSourceRange();
+ ParameterMismatch = true;
+ break;
+ }
+
+ assert(ComputeSubDeclODRHash(FirstParam) ==
+ ComputeSubDeclODRHash(SecondParam) &&
+ "Undiagnosed parameter difference.");
+ }
+
+ if (ParameterMismatch) {
+ Diagnosed = true;
+ break;
+ }
+
+ // If no error has been generated before now, assume the problem is in
+ // the body and generate a message.
+ ODRDiagError(FirstFunction->getLocation(),
+ FirstFunction->getSourceRange(), FunctionBody);
+ ODRDiagNote(SecondFunction->getLocation(),
+ SecondFunction->getSourceRange(), FunctionBody);
+ Diagnosed = true;
+ break;
+ }
+ assert(Diagnosed && "Unable to emit ODR diagnostic.");
+ }
}
void ASTReader::StartedDeserializing() {
#if defined(FIRST)
struct S11 {
- void A(int x) {}
+ void A(int x);
};
#elif defined(SECOND)
struct S11 {
- void A(int y) {}
+ void A(int y);
};
#else
S11 s11;
#if defined(FIRST)
struct S12 {
- void A(int x) {}
+ void A(int x);
};
#elif defined(SECOND)
struct S12 {
- void A(int x = 1) {}
+ void A(int x = 1);
};
#else
S12 s12;
#if defined(FIRST)
struct S13 {
- void A(int x = 1 + 0) {}
+ void A(int x = 1 + 0);
};
#elif defined(SECOND)
struct S13 {
- void A(int x = 1) {}
+ void A(int x = 1);
};
#else
S13 s13;
#if defined(FIRST)
struct S14 {
- void A(int x[2]) {}
+ void A(int x[2]);
};
#elif defined(SECOND)
struct S14 {
- void A(int x[3]) {}
+ void A(int x[3]);
};
#else
S14 s14;
template <typename T>
struct S {
struct R {
- void foo(T x = 0) {}
+ void foo(T x = 0);
};
};
#elif defined(SECOND)
template <typename T>
struct S {
struct R {
- void foo(T x = 1) {}
+ void foo(T x = 1);
};
};
#else
#if defined(FIRST)
template <typename alpha> struct Bravo {
- void charlie(bool delta = false) {}
+ void charlie(bool delta = false);
};
typedef Bravo<char> echo;
echo foxtrot;
#elif defined(SECOND)
template <typename alpha> struct Bravo {
- void charlie(bool delta = (false)) {}
+ void charlie(bool delta = (false));
};
typedef Bravo<char> echo;
echo foxtrot;
#endif
} // namespace DefaultArguments
+namespace FunctionDecl {
+#if defined(FIRST)
+struct S1 {};
+S1 s1a;
+#elif defined(SECOND)
+struct S1 {};
+#else
+S1 s1;
+#endif
+
+#if defined(FIRST)
+struct S2 {
+ S2() = default;
+};
+S2 s2a = S2();
+#elif defined(SECOND)
+struct S2 {
+ S2() = default;
+};
+#else
+S2 s2;
+#endif
+
+#if defined(FIRST)
+struct S3 {
+ S3() = delete;
+};
+S3* s3c;
+#elif defined(SECOND)
+struct S3 {
+ S3() = delete;
+};
+#else
+S3* s3;
+#endif
+
+#if defined(FIRST) || defined(SECOND)
+int F1(int x, float y = 2.7) { return 1; }
+#else
+int I1 = F1(1);
+#endif
+
+#if defined(FIRST)
+int F2() { return 1; }
+#elif defined(SECOND)
+double F2() { return 1; }
+#else
+int I2 = F2();
+// expected-error@-1 {{call to 'F2' is ambiguous}}
+// expected-note@first.h:* {{candidate function}}
+// expected-note@second.h:* {{candidate function}}
+#endif
+
+#if defined(FIRST)
+int F3(float) { return 1; }
+#elif defined(SECOND)
+int F3(double) { return 1; }
+#else
+int I3 = F3(1);
+// expected-error@-1 {{call to 'F3' is ambiguous}}
+// expected-note@first.h:* {{candidate function}}
+// expected-note@second.h:* {{candidate function}}
+#endif
+
+#if defined(FIRST)
+int F4(int x) { return 1; }
+#elif defined(SECOND)
+int F4(int y) { return 1; }
+#else
+int I4 = F4(1);
+// expected-error@second.h:* {{'FunctionDecl::F4' has different definitions in different modules; definition in module 'SecondModule' first difference is 1st parameter with name 'y'}}
+// expected-note@first.h:* {{but in 'FirstModule' found 1st parameter with name 'x'}}
+#endif
+
+#if defined(FIRST)
+int F5(int x) { return 1; }
+#elif defined(SECOND)
+int F5(int x = 1) { return 1; }
+#else
+int I5 = F6(1);
+// expected-error@second.h:* {{'FunctionDecl::F5' has different definitions in different modules; definition in module 'SecondModule' first difference is 1st parameter without a default argument}}
+// expected-note@first.h:* {{but in 'FirstModule' found 1st parameter with a default argument}}
+#endif
+
+#if defined(FIRST)
+int F6(int x = 2) { return 1; }
+#elif defined(SECOND)
+int F6(int x = 1) { return 1; }
+#else
+int I6 = F6(1);
+// expected-error@second.h:* {{'FunctionDecl::F6' has different definitions in different modules; definition in module 'SecondModule' first difference is 1st parameter with a default argument}}
+// expected-note@first.h:* {{but in 'FirstModule' found 1st parameter with a different default argument}}
+#endif
+
+using I = int;
+#if defined(FIRST)
+I F7() { return 0; }
+#elif defined(SECOND)
+int F7() { return 0; }
+#else
+int I7 = F7();
+// expected-error@second.h:* {{'FunctionDecl::F7' has different definitions in different modules; definition in module 'SecondModule' first difference is return type is 'int'}}
+// expected-note@first.h:* {{but in 'FirstModule' found different return type 'FunctionDecl::I' (aka 'int')}}
+#endif
+
+#if defined(FIRST)
+int F8(int) { return 0; }
+#elif defined(SECOND)
+int F8(I) { return 0; }
+#else
+int I8 = F8(1);
+// expected-error@second.h:* {{'FunctionDecl::F8' has different definitions in different modules; definition in module 'SecondModule' first difference is 1st parameter with type 'FunctionDecl::I' (aka 'int')}}
+// expected-note@first.h:* {{but in 'FirstModule' found 1st parameter with type 'int'}}
+#endif
+
+#if defined(FIRST)
+int F9(int[1]) { return 0; }
+#elif defined(SECOND)
+int F9(int[2]) { return 0; }
+#else
+int I9 = F9(nullptr);
+// expected-error@second.h:* {{'FunctionDecl::F9' has different definitions in different modules; definition in module 'SecondModule' first difference is 1st parameter with type 'int *' decayed from 'int [2]'}}
+// expected-note@first.h:* {{but in 'FirstModule' found 1st parameter with type 'int *' decayed from 'int [1]'}}
+#endif
+
+#if defined(FIRST)
+int F10() { return 1; }
+#elif defined(SECOND)
+int F10() { return 2; }
+#else
+int I10 = F10();
+#endif
+// expected-error@second.h:* {{'FunctionDecl::F10' has different definitions in different modules; definition in module 'SecondModule' first difference is function body}}
+// expected-note@first.h:* {{but in 'FirstModule' found a different body}}
+} // namespace FunctionDecl
+
// Keep macros contained to one file.
#ifdef FIRST
#undef FIRST