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
// 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())
"Obj::property"));
}
+TEST(NamedDeclPrinter, TestInstanceObjCClassExtension) {
+ const char *Code =
+R"(
+@interface ObjC
+@end
+@interface ObjC () {
+ char data; // legal with non-fragile ABI.
+}
+@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"(