-
Notifications
You must be signed in to change notification settings - Fork 10
Added check for scope in structural equivalence (experimental). #629
base: ctu-clang7
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,11 +102,103 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | |
| static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | ||
| const TemplateArgument &Arg1, | ||
| const TemplateArgument &Arg2); | ||
| static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | ||
| const TemplateArgumentList &ArgL1, | ||
| const TemplateArgumentList &ArgL2); | ||
| static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | ||
| NestedNameSpecifier *NNS1, | ||
| NestedNameSpecifier *NNS2); | ||
| static bool IsStructurallyEquivalent(const IdentifierInfo *Name1, | ||
| const IdentifierInfo *Name2); | ||
| static bool IsStructurallyEquivalent(const DeclarationName Name1, | ||
| const DeclarationName Name2); | ||
|
|
||
| using ContextVector = llvm::SmallVector<const DeclContext *, 4>; | ||
|
|
||
| static void GetContexts(ContextVector &Contexts, const NamedDecl *ND) { | ||
| const DeclContext *Ctx = ND->getDeclContext(); | ||
|
|
||
| // For ObjC methods, look through categories and use the interface as context. | ||
| if (auto *MD = dyn_cast<ObjCMethodDecl>(ND)) | ||
| if (auto *ID = MD->getClassInterface()) | ||
| Ctx = ID; | ||
|
|
||
| while (Ctx && Ctx->isFunctionOrMethod()) | ||
|
||
| Ctx = Ctx->getParent(); | ||
|
|
||
| // Collect named contexts. | ||
| while (Ctx) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems dangerous, what is the parent of a TranslationUnitDecl? Perhaps another guard is needed? |
||
| if (isa<NamedDecl>(Ctx)) | ||
| Contexts.push_back(Ctx); | ||
| Ctx = Ctx->getParent(); | ||
| } | ||
| } | ||
|
|
||
| static bool IsEquivalentContext(StructuralEquivalenceContext &Context, const ContextVector &Contexts1, const ContextVector &Contexts2) { | ||
|
||
| auto DC2I = Contexts2.begin(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a check before to see whether the sizes are equal and then an early return if not would make it simpler. |
||
| for (const DeclContext *DC1 : Contexts1) { | ||
| if (DC2I == Contexts2.end()) | ||
| return false; | ||
| const DeclContext *DC2 = *DC2I; | ||
| ++DC2I; | ||
|
|
||
| if (const auto *Spec1 = dyn_cast<ClassTemplateSpecializationDecl>(DC1)) { | ||
|
||
| const auto *Spec2 = dyn_cast<ClassTemplateSpecializationDecl>(DC2); | ||
| if (!Spec2) | ||
| return false; | ||
| if (!IsStructurallyEquivalent(Spec1->getDeclName(), Spec2->getDeclName())) | ||
| return false; | ||
| if (!IsStructurallyEquivalent(Context, Spec1->getTemplateArgs(), Spec2->getTemplateArgs())) | ||
| return false; | ||
| } else if (const auto *ND1 = dyn_cast<NamespaceDecl>(DC1)) { | ||
| const auto *ND2 = dyn_cast<NamespaceDecl>(DC2); | ||
| if (!ND2) | ||
| return false; | ||
| if (ND1->isAnonymousNamespace() != ND2->isAnonymousNamespace()) | ||
| return false; | ||
| if (ND1->isInline() != ND2->isInline()) | ||
| return false; | ||
| if (ND1->isAnonymousNamespace() || ND1->isInlineNamespace()) | ||
| continue; | ||
| if (!IsStructurallyEquivalent(ND1->getDeclName(), ND2->getDeclName())) | ||
| return false; | ||
| } else if (const auto *RD1 = dyn_cast<RecordDecl>(DC1)) { | ||
| const auto *RD2 = dyn_cast<RecordDecl>(DC2); | ||
| if (!RD2) | ||
| return false; | ||
| if (!IsStructurallyEquivalent(RD1->getDeclName(), RD2->getDeclName())) | ||
| return false; | ||
| } else if (const auto *FD1 = dyn_cast<FunctionDecl>(DC1)) { | ||
| const auto *FD2 = dyn_cast<FunctionDecl>(DC2); | ||
| if (!FD2) | ||
| return false; | ||
| if (!IsStructurallyEquivalent(Context, const_cast<FunctionDecl *>(FD1), const_cast<FunctionDecl *>(FD2))) | ||
| return false; | ||
| /*} else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { | ||
| // C++ [dcl.enum]p10: Each enum-name and each unscoped | ||
| // enumerator is declared in the scope that immediately contains | ||
| // the enum-specifier. Each scoped enumerator is declared in the | ||
| // scope of the enumeration. | ||
| // For the case of unscoped enumerator, do not include in the qualified | ||
| // name any information about its enum enclosing scope, as its visibility | ||
| // is global. | ||
| if (ED->isScoped()) | ||
| OS << *ED; | ||
| else | ||
| continue;*/ | ||
| } else if (const auto *ND1 = dyn_cast<NamedDecl>(DC1)) { | ||
|
||
| const auto *ND2 = cast<NamedDecl>(DC2); | ||
| if (!IsStructurallyEquivalent(ND1->getDeclName(), ND2->getDeclName())) | ||
| return false; | ||
| } else | ||
| llvm_unreachable("Context should be NamedDecl."); | ||
| } | ||
|
|
||
| if (DC2I != Contexts2.end()) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static bool IsStructurallyEquivalent(const DeclarationName Name1, | ||
| const DeclarationName Name2) { | ||
|
|
@@ -338,6 +430,23 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | |
| llvm_unreachable("Invalid template argument kind"); | ||
| } | ||
|
|
||
| /// Determine whether two template argument lists are equivalent. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this hunk related? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I see you use it when comparing the contexts.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function is used in |
||
| static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context, | ||
| const TemplateArgumentList &ArgL1, | ||
| const TemplateArgumentList &ArgL2) { | ||
| if (ArgL1.size() != ArgL2.size()) { | ||
| if (Context.Complain) { | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| for (unsigned I = 0, N = ArgL1.size(); I != N; ++I) | ||
| if (!IsStructurallyEquivalent(Context, ArgL1.get(I), ArgL2.get(I))) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// Determine structural equivalence for the common part of array | ||
| /// types. | ||
| static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context, | ||
|
|
@@ -1633,6 +1742,8 @@ unsigned StructuralEquivalenceContext::getApplicableDiagnostic( | |
| return diag::warn_odr_parameter_pack_non_pack; | ||
| case diag::err_odr_non_type_parameter_type_inconsistent: | ||
| return diag::warn_odr_non_type_parameter_type_inconsistent; | ||
| default: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably not related too.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to make a build warning go away (I have another one in ASTImporterTest.cpp, probably a new PR can be created to correct this). |
||
| llvm_unreachable("Unknown diagnostic type."); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1674,6 +1785,17 @@ bool StructuralEquivalenceContext::CheckCommonEquivalence(Decl *D1, Decl *D2) { | |
|
|
||
| // FIXME: Move check for identifier names into this function. | ||
|
|
||
| if (auto *ND1 = dyn_cast<NamedDecl>(D1)) { | ||
| auto *ND2 = dyn_cast<NamedDecl>(D2); | ||
| if (!ND2) | ||
| return false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if here we would just get the two DeclContexts from D1 and D2 and we would compare them by either NamedDecl::getQualifiedNameAsString or by index::generateUSRForDecl? How is it different than what we had previously in #624 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I am saying here is just an alternative solution to consider, not necessarily better ...
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to compare only scope, not the decl name itself (that is done already). Probably this was the reason for some test failures when the qualified name was used. |
||
| ContextVector Contexts1, Contexts2; | ||
| GetContexts(Contexts1, ND1); | ||
| GetContexts(Contexts2, ND2); | ||
| if (!IsEquivalentContext(*this, Contexts1, Contexts2)) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it handle transparent contexts correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How should these be handled? They can be skipped here.