================
@@ -697,10 +697,46 @@ static TemplateParamInfo convertTemplateArgToInfo(const
clang::Decl *D,
return TemplateParamInfo(Str);
}
+// Check if the DeclKind is one for which we support contextual relationships.
+// There might be other ContextDecls, like blocks, that we currently don't
+// handle at all.
+static bool isSupportedContext(Decl::Kind DeclKind) {
+ switch (DeclKind) {
+ case Decl::Kind::Record:
+ case Decl::Kind::CXXRecord:
+ case Decl::Kind::ClassTemplateSpecialization:
+ case Decl::Kind::ClassTemplatePartialSpecialization:
+ case Decl::Kind::Namespace:
+ return true;
+ default:
+ return false;
+ }
+}
+
+template <typename T> static void findParent(Info &I, const T *D) {
+ assert(D && "Invalid Decl");
+
+ // Only walk up contexts if D is a record or namespace.
+ if (!isSupportedContext(D->getKind()))
+ return;
+
+ const DeclContext *ParentCtx = dyn_cast<DeclContext>(D)->getLexicalParent();
+ while (ParentCtx) {
+ if (isSupportedContext(ParentCtx->getDeclKind())) {
+ // Break when we reach the first record or namespace.
+ I.ParentUSR = getUSRForDecl(dyn_cast<Decl>(ParentCtx));
+ break;
+ }
+ ParentCtx = ParentCtx->getParent();
----------------
ilovepi wrote:
I cant recall if getLexicalParent() can return a `nullptr` or if that's
structurally impossible. We can either add a check, or assert that its non-null.
https://github.com/llvm/llvm-project/pull/173297
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits