[clang] fbb499e - [AST] Fix crashes caused by redeclarations in hidden prototypes

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 8 19:50:36 PDT 2020


Author: Ben Barham
Date: 2020-10-08T19:48:36-07:00
New Revision: fbb499ef255b77c5a3300543de88956b13e706b7

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

LOG: [AST] Fix crashes caused by redeclarations in hidden prototypes

ObjCContainerDecl.getMethod returns a nullptr by default when the
container is a hidden prototype. Callsites where the method is being
looked up on the redeclaration's own container should skip this check
since they (rightly) expect a valid method to be found.

Resolves rdar://69444243

Reviewed By: akyrtzi

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

Added: 
    clang/test/Index/Inputs/hidden-redecls-sub.h
    clang/test/Index/Inputs/hidden-redecls.h
    clang/test/Index/hidden-redecls.m

Modified: 
    clang/lib/AST/DeclObjC.cpp
    clang/test/Index/Inputs/module.map

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/DeclObjC.cpp b/clang/lib/AST/DeclObjC.cpp
index b6f8227b157a..961230fb54ce 100644
--- a/clang/lib/AST/DeclObjC.cpp
+++ b/clang/lib/AST/DeclObjC.cpp
@@ -950,7 +950,8 @@ ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
   if (!Redecl && isRedeclaration()) {
     // This is the last redeclaration, go back to the first method.
     return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
-                                                    isInstanceMethod());
+                                                    isInstanceMethod(),
+                                                    /*AllowHidden=*/true);
   }
 
   return Redecl ? Redecl : this;
@@ -983,7 +984,8 @@ ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
   if (isRedeclaration()) {
     // It is possible that we have not done deserializing the ObjCMethod yet.
     ObjCMethodDecl *MD =
-        cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod());
+        cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod(),
+                                                 /*AllowHidden=*/true);
     return MD ? MD : this;
   }
 
@@ -1308,8 +1310,9 @@ void ObjCMethodDecl::getOverriddenMethods(
   const ObjCMethodDecl *Method = this;
 
   if (Method->isRedeclaration()) {
-    Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
-                   getMethod(Method->getSelector(), Method->isInstanceMethod());
+    Method = cast<ObjCContainerDecl>(Method->getDeclContext())
+                 ->getMethod(Method->getSelector(), Method->isInstanceMethod(),
+                             /*AllowHidden=*/true);
   }
 
   if (Method->isOverriding()) {

diff  --git a/clang/test/Index/Inputs/hidden-redecls-sub.h b/clang/test/Index/Inputs/hidden-redecls-sub.h
new file mode 100644
index 000000000000..f5a77977cfba
--- /dev/null
+++ b/clang/test/Index/Inputs/hidden-redecls-sub.h
@@ -0,0 +1,7 @@
+ at protocol P1
+- (void)p1_method;
+- (void)p1_method;
+ at end
+
+ at interface Foo (SubP1) <P1>
+ at end

diff  --git a/clang/test/Index/Inputs/hidden-redecls.h b/clang/test/Index/Inputs/hidden-redecls.h
new file mode 100644
index 000000000000..c5558cf0ab18
--- /dev/null
+++ b/clang/test/Index/Inputs/hidden-redecls.h
@@ -0,0 +1,3 @@
+ at interface Foo
+- (void)parent_method;
+ at end

diff  --git a/clang/test/Index/Inputs/module.map b/clang/test/Index/Inputs/module.map
index 10712accb1c2..cd5bcb467032 100644
--- a/clang/test/Index/Inputs/module.map
+++ b/clang/test/Index/Inputs/module.map
@@ -20,3 +20,11 @@ module PreambleWithImplicitImport {
     export *
   }
 }
+
+module hidden_redecls {
+  header "hidden-redecls.h"
+
+  explicit module sub {
+    header "hidden-redecls-sub.h"
+  }
+}

diff  --git a/clang/test/Index/hidden-redecls.m b/clang/test/Index/hidden-redecls.m
new file mode 100644
index 000000000000..1735c0b5e184
--- /dev/null
+++ b/clang/test/Index/hidden-redecls.m
@@ -0,0 +1,12 @@
+ at import hidden_redecls;
+
+ at interface Foo (Top)
+- (void)top_method;
+ at end
+
+// p1_method in protocol P1 is hidden since module_redecls.sub hasn't been
+// imported yet. Check it is still indexed.
+
+// RUN: c-index-test -index-file-full %s -isystem %S/Inputs -fmodules -target x86_64-apple-macosx10.7 | FileCheck %s
+// CHECK: [indexDeclaration]: kind: objc-instance-method | name: p1_method | {{.*}} | loc: {{.*}}hidden-redecls-sub.h:2:9 | {{.*}} | isRedecl: 0
+// CHECK: [indexDeclaration]: kind: objc-instance-method | name: p1_method | {{.*}} | loc: {{.*}}hidden-redecls-sub.h:3:9 | {{.*}} | isRedecl: 1


        


More information about the cfe-commits mailing list