[clang] 164e0fc - [ASTImporter] Implicitly declare parameters for imported ObjCMethodDecls

Raphael Isemann via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 6 09:51:10 PST 2019


Author: Raphael Isemann
Date: 2019-12-06T18:50:32+01:00
New Revision: 164e0fc5c7f782b174db5c87b37725ea0e174853

URL: https://github.com/llvm/llvm-project/commit/164e0fc5c7f782b174db5c87b37725ea0e174853
DIFF: https://github.com/llvm/llvm-project/commit/164e0fc5c7f782b174db5c87b37725ea0e174853.diff

LOG: [ASTImporter] Implicitly declare parameters for imported ObjCMethodDecls

Summary:
When Sema encounters a ObjCMethodDecl definition it declares the implicit parameters for the ObjCMethodDecl.
When importing such a method with the ASTImporter we need to do the same for the imported method
otherwise we will crash when generating code (where CodeGen expects that this was called by Sema).

Note I had to implement Objective-C[++] support in Language.cpp as this is the first test for Objective-C and this
would otherwise just hit this 'not implemented' assert when running the unit test.

Reviewers: martong, a.sidorin, shafik

Reviewed By: martong

Subscribers: rnkovacs, cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang/lib/AST/ASTImporter.cpp
    clang/unittests/AST/ASTImporterTest.cpp
    clang/unittests/AST/Language.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index ff844f98bfb2..7d71a4a143cc 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4010,6 +4010,14 @@ ExpectedDecl ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
 
   ToMethod->setLexicalDeclContext(LexicalDC);
   LexicalDC->addDeclInternal(ToMethod);
+
+  // Implicit params are declared when Sema encounters the definition but this
+  // never happens when the method is imported. Manually declare the implicit
+  // params now that the MethodDecl knows its class interface.
+  if (D->getSelfDecl())
+    ToMethod->createImplicitParams(Importer.getToContext(),
+                                   ToMethod->getClassInterface());
+
   return ToMethod;
 }
 

diff  --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index ba2feff5fca6..abf29966e92f 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -5599,6 +5599,30 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportDefaultConstructibleLambdas) {
             2u);
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, ImplicitlyDeclareSelf) {
+  Decl *FromTU = getTuDecl(R"(
+                           __attribute__((objc_root_class))
+                           @interface Root
+                           @end
+                           @interface C : Root
+                             -(void)method;
+                           @end
+                           @implementation C
+                             -(void)method {}
+                           @end
+                           )",
+                           Lang_OBJCXX, "input.mm");
+  auto *FromMethod = LastDeclMatcher<ObjCMethodDecl>().match(
+      FromTU, namedDecl(hasName("method")));
+  ASSERT_TRUE(FromMethod);
+  auto ToMethod = Import(FromMethod, Lang_OBJCXX);
+  ASSERT_TRUE(ToMethod);
+
+  // Both methods should have their implicit parameters.
+  EXPECT_TRUE(FromMethod->getSelfDecl() != nullptr);
+  EXPECT_TRUE(ToMethod->getSelfDecl() != nullptr);
+}
+
 INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest,
                         DefaultTestValuesForRunOptions, );
 

diff  --git a/clang/unittests/AST/Language.cpp b/clang/unittests/AST/Language.cpp
index 68b78a3179d6..47993cbb99ec 100644
--- a/clang/unittests/AST/Language.cpp
+++ b/clang/unittests/AST/Language.cpp
@@ -37,8 +37,10 @@ ArgVector getBasicRunOptionsForLanguage(Language Lang) {
   case Lang_CXX2a:
     BasicArgs = {"-std=c++2a", "-frtti"};
     break;
-  case Lang_OpenCL:
   case Lang_OBJCXX:
+    BasicArgs = {"-x", "objective-c++", "-frtti"};
+    break;
+  case Lang_OpenCL:
     llvm_unreachable("Not implemented yet!");
   }
   return BasicArgs;


        


More information about the cfe-commits mailing list