[clang-tools-extra] [clang-doc] Add a breadcrumb navigation bar (PR #173297)
Paul Kirth via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 5 13:05:03 PST 2026
================
@@ -697,10 +697,39 @@ 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) {
+ if (DeclKind == Decl::Kind::Record || DeclKind == Decl::Kind::CXXRecord ||
+ DeclKind == Decl::Kind::Namespace ||
+ DeclKind == Decl::Kind::ClassTemplateSpecialization ||
+ DeclKind == Decl::Kind::ClassTemplatePartialSpecialization)
+ return true;
+ return false;
+}
+
+template <typename T> static void findParent(Info &I, const T *D) {
+ // Only walk up contexts if D is a record or namespace.
+ if (isSupportedContext(D->getKind())) {
+ 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:
```suggestion
assert(D && "invalid Decl");
if (!isSupportedContext(D->getKind()))
return;
// Only walk up contexts if D is a record or namespace.
const DeclContext *ParentCtx = dyn_cast<DeclContext>(D)->getLexicalParent();
while (ParentCtx) {
if (isSupportedContext(ParentCtx->getDeclKind())) {
// Break when we reach the first valid parent (record or namespace).
I.ParentUSR = getUSRForDecl(dyn_cast<Decl>(ParentCtx));
break;
}
ParentCtx = ParentCtx->getParent();
}
```
You can use an early return to simplify. We can also just assert D isn't `nullptr` as a sanity check.
https://github.com/llvm/llvm-project/pull/173297
More information about the cfe-commits
mailing list