r357720 - Special case ObjCPropertyDecl for printing

David Goldman via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 4 13:13:22 PDT 2019


Author: dgoldman
Date: Thu Apr  4 13:13:22 2019
New Revision: 357720

URL: http://llvm.org/viewvc/llvm-project?rev=357720&view=rev
Log:
Special case ObjCPropertyDecl for printing

ObjCPropertyDecl should use the category interface as a context similar to what is done for methods.

Previously category methods would be printed as `::property`; now they are printed as `Class::property`.

Modified:
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/unittests/AST/NamedDeclPrinterTest.cpp

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=357720&r1=357719&r2=357720&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Thu Apr  4 13:13:22 2019
@@ -1531,10 +1531,16 @@ void NamedDecl::printQualifiedName(raw_o
                                    const PrintingPolicy &P) const {
   const DeclContext *Ctx = getDeclContext();
 
-  // For ObjC methods, look through categories and use the interface as context.
+  // For ObjC methods and properties, look through categories and use the
+  // interface as context.
   if (auto *MD = dyn_cast<ObjCMethodDecl>(this))
     if (auto *ID = MD->getClassInterface())
       Ctx = ID;
+  if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) {
+    if (auto *MD = PD->getGetterMethodDecl())
+      if (auto *ID = MD->getClassInterface())
+        Ctx = ID;
+  }
 
   if (Ctx->isFunctionOrMethod()) {
     printName(OS);

Modified: cfe/trunk/unittests/AST/NamedDeclPrinterTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/NamedDeclPrinterTest.cpp?rev=357720&r1=357719&r2=357720&view=diff
==============================================================================
--- cfe/trunk/unittests/AST/NamedDeclPrinterTest.cpp (original)
+++ cfe/trunk/unittests/AST/NamedDeclPrinterTest.cpp Thu Apr  4 13:13:22 2019
@@ -115,6 +115,18 @@ PrintedWrittenNamedDeclCXX11Matches(Stri
                                  "input.cc");
 }
 
+::testing::AssertionResult
+PrintedWrittenPropertyDeclObjCMatches(StringRef Code, StringRef DeclName,
+                                   StringRef ExpectedPrinted) {
+  std::vector<std::string> Args{"-std=c++11", "-xobjective-c++"};
+  return PrintedNamedDeclMatches(Code,
+                                 Args,
+                                 /*SuppressUnwrittenScope*/ true,
+                                 objcPropertyDecl(hasName(DeclName)).bind("id"),
+                                 ExpectedPrinted,
+                                 "input.m");
+}
+
 } // unnamed namespace
 
 TEST(NamedDeclPrinter, TestNamespace1) {
@@ -179,3 +191,31 @@ TEST(NamedDeclPrinter, TestLinkageInName
     "A",
     "X::A"));
 }
+
+TEST(NamedDeclPrinter, TestObjCClassExtension) {
+  ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches(
+    R"(
+      @interface Obj
+      @end
+
+      @interface Obj ()
+      @property(nonatomic) int property;
+      @end
+    )",
+    "property",
+    "Obj::property"));
+}
+
+TEST(NamedDeclPrinter, TestObjCClassExtensionWithGetter) {
+  ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches(
+    R"(
+      @interface Obj
+      @end
+
+      @interface Obj ()
+      @property(nonatomic, getter=myPropertyGetter) int property;
+      @end
+    )",
+    "property",
+    "Obj::property"));
+}




More information about the cfe-commits mailing list