[clang] [APINotes] Support nested tags (PR #99655)
Saleem Abdulrasool via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 22 07:36:38 PDT 2024
================
@@ -783,51 +783,76 @@ static void ProcessVersionedAPINotes(
}
}
+static std::optional<api_notes::Context>
+UnwindNamespaceContext(DeclContext *DC, api_notes::APINotesManager &APINotes) {
+ if (auto NamespaceContext = dyn_cast<NamespaceDecl>(DC)) {
+ for (auto Reader : APINotes.findAPINotes(NamespaceContext->getLocation())) {
+ // Retrieve the context ID for the parent namespace of the decl.
+ std::stack<NamespaceDecl *> NamespaceStack;
+ {
+ for (auto CurrentNamespace = NamespaceContext; CurrentNamespace;
+ CurrentNamespace =
+ dyn_cast<NamespaceDecl>(CurrentNamespace->getParent())) {
+ if (!CurrentNamespace->isInlineNamespace())
+ NamespaceStack.push(CurrentNamespace);
+ }
+ }
+ std::optional<api_notes::ContextID> NamespaceID;
+ while (!NamespaceStack.empty()) {
+ auto CurrentNamespace = NamespaceStack.top();
+ NamespaceStack.pop();
+ NamespaceID =
+ Reader->lookupNamespaceID(CurrentNamespace->getName(), NamespaceID);
+ if (!NamespaceID)
+ return std::nullopt;
+ }
+ if (NamespaceID)
+ return api_notes::Context(*NamespaceID,
+ api_notes::ContextKind::Namespace);
+ }
+ }
+ return std::nullopt;
+}
+
+static std::optional<api_notes::Context>
+UnwindTagContext(TagDecl *DC, api_notes::APINotesManager &APINotes) {
+ for (auto Reader : APINotes.findAPINotes(DC->getLocation())) {
+ // Retrieve the context ID for the parent tag of the decl.
+ std::stack<TagDecl *> TagStack;
+ {
+ for (auto CurrentTag = DC; CurrentTag;
+ CurrentTag = dyn_cast<TagDecl>(CurrentTag->getParent()))
+ TagStack.push(CurrentTag);
+ }
+ assert(!TagStack.empty());
----------------
compnerd wrote:
Right, but that also implies that the loop above also executed. That means the loop invariant also must hold. The tag stack can never be empty because it is initialized with the tag as the decl context. But you can also work backwards and assert that DC is not null and hoist the assertion. You can also remove the assertion by taking a reference to the DC.
https://github.com/llvm/llvm-project/pull/99655
More information about the cfe-commits
mailing list