[clang] 6f56599 - [AST] Fix the PrintQualifiedName for ObjC instance variable in class extension.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue May 19 06:17:55 PDT 2020


Author: Haojian Wu
Date: 2020-05-19T15:17:36+02:00
New Revision: 6f56599c14af0a4b7a4d4b2c4f6ae44bc2754c0c

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

LOG: [AST] Fix the PrintQualifiedName for ObjC instance variable in class extension.

Summary:
Similar to property, we print the containing interface decl as the
nested name specifier for ivar; otherwise we will get "::ivar_name".

this would fix an assertion crash in clangd: https://github.com/clangd/clangd/issues/365

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: ilya-biryukov, kadircet, usaxena95, cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang/lib/AST/Decl.cpp
    clang/unittests/AST/NamedDeclPrinterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 9c1b99d30e78..27b3ae3ef00e 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -1571,13 +1571,16 @@ void NamedDecl::printNestedNameSpecifier(raw_ostream &OS,
 
   // For ObjC methods and properties, look through categories and use the
   // interface as context.
-  if (auto *MD = dyn_cast<ObjCMethodDecl>(this))
+  if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) {
     if (auto *ID = MD->getClassInterface())
       Ctx = ID;
-  if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) {
+  } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) {
     if (auto *MD = PD->getGetterMethodDecl())
       if (auto *ID = MD->getClassInterface())
         Ctx = ID;
+  } else if (auto *ID = dyn_cast<ObjCIvarDecl>(this)) {
+    if (auto *CI = ID->getContainingInterface())
+      Ctx = CI;
   }
 
   if (Ctx->isFunctionOrMethod())

diff  --git a/clang/unittests/AST/NamedDeclPrinterTest.cpp b/clang/unittests/AST/NamedDeclPrinterTest.cpp
index a38b28bddaf4..1042312e8a73 100644
--- a/clang/unittests/AST/NamedDeclPrinterTest.cpp
+++ b/clang/unittests/AST/NamedDeclPrinterTest.cpp
@@ -230,6 +230,27 @@ R"(
     "Obj::property"));
 }
 
+TEST(NamedDeclPrinter, TestInstanceObjCClassExtension) {
+  const char *Code =
+R"(
+ at interface ObjC
+ at end
+ at interface ObjC () {
+  char data; // legal with non-fragile ABI.
+}
+ at end
+)";
+
+  std::vector<std::string> Args{
+      "-std=c++11", "-xobjective-c++",
+      "-fobjc-runtime=macosx" /*force to use non-fragile ABI*/};
+  ASSERT_TRUE(PrintedNamedDeclMatches(Code, Args,
+                                      /*SuppressUnwrittenScope*/ true,
+                                      namedDecl(hasName("data")).bind("id"),
+                                      // not "::data"
+                                      "ObjC::data", "input.mm"));
+}
+
 TEST(NamedDeclPrinter, TestObjCClassExtensionWithGetter) {
   const char *Code =
 R"(


        


More information about the cfe-commits mailing list