[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 14 13:47:29 PDT 2025
================
@@ -66,22 +89,45 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) {
hasParent(varDecl(isExternC())),
hasParent(fieldDecl(
hasParent(recordDecl(isExternCContext())))),
- hasAncestor(functionDecl(isExternC())))),
+ hasAncestor(functionDecl(isExternC())),
+ isWithinImplicitTemplateInstantiation())),
std::move(IgnoreStringArrayIfNeededMatcher))
.bind("typeloc"),
this);
+
+ Finder->addMatcher(templateArgumentLoc(hasTypeLoc(hasType(arrayType())))
+ .bind("template_arg_with_array_type_loc"),
+ this);
}
void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
- const auto *ArrayType = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
+ TypeLoc ArrayTypeLoc{};
+
+ if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs<TypeLoc>("typeloc");
+ MatchedTypeLoc != nullptr) {
+ ArrayTypeLoc = *MatchedTypeLoc;
+ }
+
+ if (const auto *TemplateArgLoc = Result.Nodes.getNodeAs<TemplateArgumentLoc>(
+ "template_arg_with_array_type_loc");
+ TemplateArgLoc != nullptr &&
+ TemplateArgLoc->getTypeSourceInfo() != nullptr) {
+ ArrayTypeLoc = TemplateArgLoc->getTypeSourceInfo()->getTypeLoc();
+ }
+
+ // check whether an actual array type got matched (see checks above)
+ if (ArrayTypeLoc.isNull()) {
+ return;
+ }
----------------
vbvictor wrote:
I think we don't expect `ArrayTypeLoc` to be null, so write `assert`.
https://llvm.org/docs/CodingStandards.html#assert-liberally
https://github.com/llvm/llvm-project/pull/132924
More information about the cfe-commits
mailing list