[clang] [C23] Implement WG14 N3037 (PR #132939)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 27 05:16:22 PDT 2025
================
@@ -450,6 +453,116 @@ class StmtComparer {
};
} // namespace
+static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
+ const Attr *Attr1, const Attr *Attr2) {
+ // Two attributes are structurally equivalent if they are the same kind
+ // of attribute, spelled with the same spelling kind, and have the same
+ // arguments. This means that [[noreturn]] and __attribute__((noreturn)) are
+ // not structurally equivalent, nor are [[nodiscard("foo")]] and
+ // [[nodiscard("bar")]].
+ if (Attr1->getKind() != Attr2->getKind())
+ return false;
+
+ if (Attr1->getSyntax() != Attr2->getSyntax())
+ return false;
+
+ if (Attr1->getSpellingListIndex() != Attr2->getSpellingListIndex())
+ return false;
+
+ auto GetAttrName = [](const Attr *A) {
+ if (const IdentifierInfo *II = A->getAttrName())
+ return II->getName();
+ return StringRef{};
+ };
+
+ if (GetAttrName(Attr1) != GetAttrName(Attr2))
+ return false;
+
+ // FIXME: check the attribute arguments. Attr does not track the arguments on
+ // the base class, which makes this awkward. We may want to tablegen a
+ // comparison function for attributes? In the meantime, we're doing this the
+ // cheap way by pretty printing the attributes and checking they produce
----------------
AaronBallman wrote:
> I don't think this applies here.
I think it does, but I think WG14 has some serious problems around their interpretation of attributes as the feature was intended when I standardized it, at least as they relate to types. I'm not surprised we come down differently here. :-)
> Sounds reasonable, but I would exempt standard attributes, because I believe this is what the standard requires.
We make no distinction between standard and vendor attributes in our AST, they're both represented by an `Attr` subclass. And given that we have to handle things like differences between `[[noreturn]]` and `__attribute__((noreturn))`, I don't think we should exempt standard attributes.
https://github.com/llvm/llvm-project/pull/132939
More information about the cfe-commits
mailing list