Closed marco-antognini-sonarsource closed 1 month ago
Now, I see at least two possible ways to solve this.
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 112cf3d08182..a328da15d722 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -381,7 +381,18 @@ static void handleGslAnnotatedTypes(IndirectLocalPath &Path, Expr *Call,
if (auto *CCE = dyn_cast<CXXConstructExpr>(Call)) {
const auto *Ctor = CCE->getConstructor();
const CXXRecordDecl *RD = Ctor->getParent();
- if (CCE->getNumArgs() > 0 && RD->hasAttr<PointerAttr>())
+ auto const hasPointerAttr = [&] {
+ auto const *Record = RD;
+ if (auto const *Spec =
+ dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
+ Record = Spec->getSpecializedTemplate()
+ ->getTemplatedDecl()
+ ->getCanonicalDecl();
+ }
+
+ return Record->hasAttr<PointerAttr>();
+ };
+ if (CCE->getNumArgs() > 0 && hasPointerAttr())
VisitPointerArg(Ctor->getParamDecl(0), CCE->getArgs()[0], true);
}
}
i.e., look at the "main" decl for std::span
here when we have a ClassTemplateSpecializationDecl
.
But this is inaccurate for non-inferred types (addGslOwnerPointerAttributeIfNotExisting
) as I imagine users could, theoretically, add an [[attribute]]
in their code for some specialization but not others (although unlikely).
Happy to hear people opinion and guidance.
Tagging @hokein.
Thanks for the report and for catching this!
It seems to be a bug or perhaps an oversight in the implementation. The current implementation only updates the primary template declaration, but we should do the same thing for explicit template specializations as well.
That was quick, thanks @hokein!
I'm checking the behavior of Clang 19 vs 18 and I noticed that @hokein's https://github.com/llvm/llvm-project/pull/99622 introduces some nice warnings thanks to the added attributes on
std::span
.However, it does not work with libc++.
Here is an example:
https://godbolt.org/z/Yfe78nsdb
IIUC, the problem boils down to the fact
std::span
is defined with a template specialization for dynamic extent and theClassTemplatePartialSpecializationDecl
does not get thePointerAttr
:https://godbolt.org/z/nh1j4a7xE