================
@@ -366,6 +389,101 @@ getFunctionSourceCode(const FunctionDecl *FD, const
DeclContext *TargetContext,
DelKeyword(tok::kw_explicit, {FD->getBeginLoc(), FD->getLocation()});
}
+ // Drop attributes that the out-of-line definition inherits from the
+ // declaration. Ones we can't safely locate are left alone.
+ // FIXME: Attributes coing from macros and attributes unknown to clang (which
+ // are not part of the AST) are not dropped.
+ for (Attr *A : FD->attrs()) {
----------------
ckandeler wrote:
**[LLM-generated]** Not all attributes can be dropped from the definition,
because some of them are part of the function's identity rather than just
inherited. Running the tweak on these inputs produces code that clang rejects
(checked with `-fsyntax-only`):
```cpp
struct A { int ^foo(int i) __attribute__((enable_if(i > 0, "x"))) { return 0; }
};
// -> int A::foo(int i) { return 0; }
// error: out-of-line definition of 'foo' does not match any declaration in 'A'
struct A { __attribute__((target("avx2"))) int ^foo() { return 1; }
__attribute__((target("default"))) int foo() { return 0; } };
// -> int A::foo() { return 1; }
// error: function declaration is missing 'target' attribute in a
multiversioned function
struct A { __attribute__((cpu_specific(atom))) int ^foo() { return 1; } };
// -> int A::foo() { return 1; }
// error: function declaration is missing 'cpu_specific' or 'cpu_dispatch'
attribute in a multiversioned function
```
Before this patch, all three compiled because the attributes were copied.
Checking `isInherited()`/`InheritableAttr` doesn't catch these (`EnableIfAttr`
is inheritable), so these attributes need to be skipped explicitly: `EnableIf`,
`Target`, `TargetVersion`, `TargetClones`, `CPUSpecific`, `CPUDispatch`,
possibly together with a `FD->isMultiVersion()` check. Test cases for these
would be good too.
https://github.com/llvm/llvm-project/pull/242635
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits