[PATCH] D108556: [clangd] Don't highlight ObjC `id` and `instancetype`
David Goldman via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 3 09:40:47 PDT 2021
dgoldman updated this revision to Diff 370609.
dgoldman added a comment.
Swap to ignore in FindTarget
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D108556/new/
https://reviews.llvm.org/D108556
Files:
clang-tools-extra/clangd/FindTarget.cpp
clang-tools-extra/clangd/unittests/FindTargetTests.cpp
clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -661,13 +661,15 @@
@interface $Class_decl[[Foo]]
@end
@interface $Class_decl[[Bar]] : $Class[[Foo]]
- -($Class[[id]]) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]];
+ -(id) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]];
+ +(instancetype)$StaticMethod_decl_static[[sharedInstance]];
+(void) $StaticMethod_decl_static[[explode]];
@end
@implementation $Class_decl[[Bar]]
- -($Class[[id]]) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]] {
+ -(id) $Method_decl[[x]]:(int)$Parameter_decl[[a]] $Method_decl[[y]]:(int)$Parameter_decl[[b]] {
return self;
}
+ +(instancetype)$StaticMethod_decl_static[[sharedInstance]] { return 0; }
+(void) $StaticMethod_decl_static[[explode]] {}
@end
Index: clang-tools-extra/clangd/unittests/FindTargetTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -995,6 +995,20 @@
}
)cpp";
EXPECT_DECLS("ObjCPropertyRefExpr", "+ (id)sharedInstance");
+
+ Code = R"cpp(
+ @interface Foo
+ + ([[id]])sharedInstance;
+ @end
+ )cpp";
+ EXPECT_DECLS("TypedefTypeLoc");
+
+ Code = R"cpp(
+ @interface Foo
+ + ([[instancetype]])sharedInstance;
+ @end
+ )cpp";
+ EXPECT_DECLS("TypedefTypeLoc");
}
class FindExplicitReferencesTest : public ::testing::Test {
Index: clang-tools-extra/clangd/FindTarget.cpp
===================================================================
--- clang-tools-extra/clangd/FindTarget.cpp
+++ clang-tools-extra/clangd/FindTarget.cpp
@@ -94,6 +94,17 @@
return nullptr;
}
+// Returns true if the `TypedefNameDecl` should not be reported.
+bool shouldSkipTypedef(const TypedefNameDecl *TD) {
+ // Even though ObjC `id` and `instancetype` are *implemented* via typedefs, we
+ // don't want to treat them like typedefs - instead let the editor treat
+ // them like keywords.
+ if (TD == TD->getASTContext().getObjCInstanceTypeDecl() ||
+ TD == TD->getASTContext().getObjCIdDecl())
+ return true;
+ return false;
+}
+
// TargetFinder locates the entities that an AST node refers to.
//
// Typically this is (possibly) one declaration and (possibly) one type, but
@@ -174,6 +185,8 @@
D = UDD->getNominatedNamespaceAsWritten();
if (const TypedefNameDecl *TND = dyn_cast<TypedefNameDecl>(D)) {
+ if (shouldSkipTypedef(TND))
+ return;
add(TND->getUnderlyingType(), Flags | Rel::Underlying);
Flags |= Rel::Alias; // continue with the alias.
} else if (const UsingDecl *UD = dyn_cast<UsingDecl>(D)) {
@@ -395,6 +408,8 @@
}
}
void VisitTypedefType(const TypedefType *TT) {
+ if (shouldSkipTypedef(TT->getDecl()))
+ return;
Outer.add(TT->getDecl(), Flags);
}
void
@@ -903,6 +918,8 @@
}
void VisitTypedefTypeLoc(TypedefTypeLoc L) {
+ if (shouldSkipTypedef(L.getTypedefNameDecl()))
+ return;
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
L.getNameLoc(),
/*IsDecl=*/false,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108556.370609.patch
Type: text/x-patch
Size: 3658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210903/cd71821f/attachment.bin>
More information about the cfe-commits
mailing list