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`.

llvm-svn: 357720
This commit is contained in:
David Goldman 2019-04-04 20:13:22 +00:00
parent e028de43cd
commit 19d21854e9
2 changed files with 47 additions and 1 deletions

View File

@ -1531,10 +1531,16 @@ void NamedDecl::printQualifiedName(raw_ostream &OS,
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);

View File

@ -115,6 +115,18 @@ PrintedWrittenNamedDeclCXX11Matches(StringRef Code, StringRef DeclName,
"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, TestLinkageInNamespace) {
"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"));
}