[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
a.sidorin added a comment. Hello Kareem, While the functionality of this patch is already implemented in my already merged patch, I added your tests into the repo. Thank you! https://reviews.llvm.org/D27181 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
This revision was automatically updated to reflect the committed changes. Closed by commit rL319632: [ASTImporter] Add unit tests for UsingDecl and UsingShadowDecl (authored by a.sidorin). Changed prior to commit: https://reviews.llvm.org/D27181?vs=79488=125290#toc Repository: rL LLVM https://reviews.llvm.org/D27181 Files: cfe/trunk/unittests/AST/ASTImporterTest.cpp Index: cfe/trunk/unittests/AST/ASTImporterTest.cpp === --- cfe/trunk/unittests/AST/ASTImporterTest.cpp +++ cfe/trunk/unittests/AST/ASTImporterTest.cpp @@ -583,5 +583,46 @@ callExpr(has(cxxPseudoDestructorExpr(); } +TEST(ImportDecl, ImportUsingDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( +testImport( + "namespace foo { int bar; }" + "int declToImport(){ using foo::bar; }", + Lang_CXX, "", Lang_CXX, Verifier, + functionDecl( +has( + compoundStmt( +has( + declStmt( +has( + usingDecl(); +} + +/// \brief Matches shadow declarations introduced into a scope by a +///(resolved) using declaration. +/// +/// Given +/// \code +/// namespace n { int f; } +/// namespace declToImport { using n::f; } +/// \endcode +/// usingShadowDecl() +/// matches \code f \endcode +const internal::VariadicDynCastAllOfMatcherusingShadowDecl; + +TEST(ImportDecl, ImportUsingShadowDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( +testImport( + "namespace foo { int bar; }" + "namespace declToImport { using foo::bar; }", + Lang_CXX, "", Lang_CXX, Verifier, + namespaceDecl( +has( + usingShadowDecl(); +} + } // end namespace ast_matchers } // end namespace clang Index: cfe/trunk/unittests/AST/ASTImporterTest.cpp === --- cfe/trunk/unittests/AST/ASTImporterTest.cpp +++ cfe/trunk/unittests/AST/ASTImporterTest.cpp @@ -583,5 +583,46 @@ callExpr(has(cxxPseudoDestructorExpr(); } +TEST(ImportDecl, ImportUsingDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( +testImport( + "namespace foo { int bar; }" + "int declToImport(){ using foo::bar; }", + Lang_CXX, "", Lang_CXX, Verifier, + functionDecl( +has( + compoundStmt( +has( + declStmt( +has( + usingDecl(); +} + +/// \brief Matches shadow declarations introduced into a scope by a +///(resolved) using declaration. +/// +/// Given +/// \code +/// namespace n { int f; } +/// namespace declToImport { using n::f; } +/// \endcode +/// usingShadowDecl() +/// matches \code f \endcode +const internal::VariadicDynCastAllOfMatcher usingShadowDecl; + +TEST(ImportDecl, ImportUsingShadowDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( +testImport( + "namespace foo { int bar; }" + "namespace declToImport { using foo::bar; }", + Lang_CXX, "", Lang_CXX, Verifier, + namespaceDecl( +has( + usingShadowDecl(); +} + } // end namespace ast_matchers } // end namespace clang ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
a.sidorin added inline comments. Comment at: lib/AST/ASTImporter.cpp:4305 + DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc())); + ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); + spyffe wrote: > I've seen this pattern before, in [[ https://reviews.llvm.org/D27033 | D20733 > ]], at ASTImporter.cpp:6488: > ``` > DeclarationNameInfo NameInfo(E->getName(), E->getNameLoc()); > ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); > ``` > That code didn't do the `Import` during the initialization of the > `DeclarationNameInfo`. Is either of these incorrect? This may come from our published code but it looks like an issue that needs to be fixed. Thank you for spotting this. https://reviews.llvm.org/D27181 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
spyffe added a comment. Looks good, but I have a concern about the underlying branch's apparently inconsistent initialization of `DeclarationNameInfo`. Aleksei, could you clarify how that code works? Should we have a helper function so we don't have to carefully repeat this pattern everywhere? Comment at: lib/AST/ASTImporter.cpp:4305 + DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc())); + ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); + I've seen this pattern before, in [[ https://reviews.llvm.org/D27033 | D20733 ]], at ASTImporter.cpp:6488: ``` DeclarationNameInfo NameInfo(E->getName(), E->getNameLoc()); ImportDeclarationNameLoc(E->getNameInfo(), NameInfo); ``` That code didn't do the `Import` during the initialization of the `DeclarationNameInfo`. Is either of these incorrect? https://reviews.llvm.org/D27181 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
a.sidorin added a comment. Thank you Kareem, It looks mostly good, but I'd like to have some functional tests in ASTMerge for this patch. Comment at: lib/AST/ASTImporter.cpp:4299 + if (ImportDeclParts(D, DC, LexicalDC, Name, AlreadyImported, Loc)) +return NULL; + assert(DC && "Null DeclContext after importing decl parts"); nullptr Comment at: lib/AST/ASTImporter.cpp:4323 + for (UsingShadowDecl *I : D->shadows()) { +UsingShadowDecl *SD = cast(Importer.Import(I)); +ToD->addShadowDecl(SD); This will assert if import fails. We need to use `cast_or_null` and check for null returns. (If this is the result of my misleading code, sorry for this.) Comment at: lib/AST/ASTImporter.cpp:4335 + if (ImportDeclParts(D, DC, LexicalDC, Name, AlreadyImported, Loc)) +return NULL; + assert(DC && "Null DeclContext after importing decl parts"); nullptr Comment at: lib/AST/ASTImporter.cpp:4342 +Importer.getToContext(), DC, Loc, +cast(Importer.Import(D->getUsingDecl())), +cast_or_null(Importer.Import(D->getTargetDecl(; This will assert if import fails. We need to use cast_or_null and check for null returns. https://reviews.llvm.org/D27181 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
klimek added a comment. Can you add a test to ASTMatchersNodeTests.cpp? Otherwise LG for the matcher parts. https://reviews.llvm.org/D27181 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D27181: [ASTImporter] Support for importing UsingDecl and UsingShadowDecl
khazem created this revision. khazem added reviewers: spyffe, a.sidorin. khazem added subscribers: cfe-commits, phosek, seanklein, klimek. Some of this patch comes from Aleksei's branch [1], with minor revisions. I've added unit tests and AST Matcher support. Copying in Manuel in case there is no need for a dedicated usingShadowDecl() matcher, in which case I could define it only in the test suite. [1] https://github.com/haoNoQ/clang/blob/summary-ipa-draft/lib/AST/ASTImporter.cpp#L3594 https://reviews.llvm.org/D27181 Files: include/clang/AST/ASTImporter.h include/clang/ASTMatchers/ASTMatchers.h lib/AST/ASTImporter.cpp lib/ASTMatchers/Dynamic/Registry.cpp unittests/AST/ASTImporterTest.cpp Index: unittests/AST/ASTImporterTest.cpp === --- unittests/AST/ASTImporterTest.cpp +++ unittests/AST/ASTImporterTest.cpp @@ -558,5 +558,35 @@ } +TEST(ImportDecl, ImportUsingDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( +testImport( + "namespace foo { int bar; }" + "int declToImport(){ using foo::bar; }", + Lang_CXX, "", Lang_CXX, Verifier, + functionDecl( +has( + compoundStmt( +has( + declStmt( +has( + usingDecl(); +} + + +TEST(ImportDecl, ImportUsingShadowDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( +testImport( + "namespace foo { int bar; }" + "namespace declToImport { using foo::bar; }", + Lang_CXX, "", Lang_CXX, Verifier, + namespaceDecl( +has( + usingShadowDecl(); +} + + } // end namespace ast_matchers } // end namespace clang Index: lib/ASTMatchers/Dynamic/Registry.cpp === --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -420,6 +420,7 @@ REGISTER_MATCHER(unresolvedUsingValueDecl); REGISTER_MATCHER(userDefinedLiteral); REGISTER_MATCHER(usingDecl); + REGISTER_MATCHER(usingShadowDecl); REGISTER_MATCHER(usingDirectiveDecl); REGISTER_MATCHER(valueDecl); REGISTER_MATCHER(varDecl); Index: lib/AST/ASTImporter.cpp === --- lib/AST/ASTImporter.cpp +++ lib/AST/ASTImporter.cpp @@ -175,6 +175,8 @@ Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D); Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D); Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D); +Decl *VisitUsingDecl(UsingDecl *D); +Decl *VisitUsingShadowDecl(UsingShadowDecl *D); ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list); Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); @@ -4288,6 +4290,68 @@ return ToLinkageSpec; } +Decl *ASTNodeImporter::VisitUsingDecl(UsingDecl *D) { + DeclContext *DC, *LexicalDC; + DeclarationName Name; + SourceLocation Loc; + NamedDecl *AlreadyImported; + if (ImportDeclParts(D, DC, LexicalDC, Name, AlreadyImported, Loc)) +return NULL; + assert(DC && "Null DeclContext after importing decl parts"); + if (AlreadyImported) +return AlreadyImported; + + DeclarationNameInfo NameInfo(Name, Importer.Import(D->getNameInfo().getLoc())); + ImportDeclarationNameLoc(D->getNameInfo(), NameInfo); + + // If a UsingShadowDecl has a pointer to its UsingDecl, then we may + // already have imported this UsingDecl. That should have been caught + // above when checking if ToD is non-null. + assert(!Importer.GetImported(D) && + "Decl imported but not assigned to AlreadyImported"); + + UsingDecl *ToD = UsingDecl::Create(Importer.getToContext(), DC, + Importer.Import(D->getUsingLoc()), + Importer.Import(D->getQualifierLoc()), + NameInfo, D->hasTypename()); + ImportAttributes(D, ToD); + ToD->setLexicalDeclContext(LexicalDC); + LexicalDC->addDeclInternal(ToD); + Importer.Imported(D, ToD); + + for (UsingShadowDecl *I : D->shadows()) { +UsingShadowDecl *SD = cast(Importer.Import(I)); +ToD->addShadowDecl(SD); + } + return ToD; +} + +Decl *ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { + DeclContext *DC, *LexicalDC; + DeclarationName Name; + SourceLocation Loc; + NamedDecl *AlreadyImported; + if (ImportDeclParts(D, DC, LexicalDC, Name, AlreadyImported, Loc)) +return NULL; + assert(DC && "Null DeclContext after importing decl parts"); + if (AlreadyImported) +return AlreadyImported; + + UsingShadowDecl *ToD = UsingShadowDecl::Create( +Importer.getToContext(), DC, Loc, +cast(Importer.Import(D->getUsingDecl())), +cast_or_null(Importer.Import(D->getTargetDecl(; + + ImportAttributes(D, ToD); + ToD->setAccess(D->getAccess()); + ToD->setLexicalDeclContext(LexicalDC); + Importer.Imported(D, ToD); + +