[PATCH] D56924: Handle ObjCCategoryDecl class extensions for print
David Goldman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 5 10:07:35 PST 2019
dgoldman updated this revision to Diff 185341.
dgoldman added a comment.
Herald added a project: clang.
- Output class scope for ObjCPropertyDecl
Repository:
rC Clang
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D56924/new/
https://reviews.llvm.org/D56924
Files:
lib/AST/Decl.cpp
unittests/AST/NamedDeclPrinterTest.cpp
Index: unittests/AST/NamedDeclPrinterTest.cpp
===================================================================
--- unittests/AST/NamedDeclPrinterTest.cpp
+++ unittests/AST/NamedDeclPrinterTest.cpp
@@ -115,6 +115,18 @@
"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,17 @@
"A",
"X::A"));
}
+
+TEST(NamedDeclPrinter, TestObjCClassExtension) {
+ ASSERT_TRUE(PrintedWrittenPropertyDeclObjCMatches(
+ R"(
+ @interface Obj
+ @end
+
+ @interface Obj ()
+ @property(nonatomic) int property;
+ @end
+ )",
+ "property",
+ "Obj::property"));
+}
Index: lib/AST/Decl.cpp
===================================================================
--- lib/AST/Decl.cpp
+++ lib/AST/Decl.cpp
@@ -1531,10 +1531,19 @@
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 (auto *MD = PD->getSetterMethodDecl())
+ if (auto *ID = MD->getClassInterface())
+ Ctx = ID;
+ }
if (Ctx->isFunctionOrMethod()) {
printName(OS);
@@ -1604,6 +1613,15 @@
OS << *ED;
else
continue;
+ } else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(DC)) {
+ if (CD->IsClassExtension()) {
+ if (P.SuppressUnwrittenScope)
+ continue;
+ else
+ OS << "(class extension)";
+ } else {
+ OS << *CD;
+ }
} else {
OS << *cast<NamedDecl>(DC);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56924.185341.patch
Type: text/x-patch
Size: 2545 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190205/bfbff2e7/attachment.bin>
More information about the cfe-commits
mailing list