[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench updated https://github.com/llvm/llvm-project/pull/132924 >From 823475c91f9cdf64c816e9498a42f5ee9b2080be Mon Sep 17 00:00:00 2001 From: stmuench Date: Tue, 25 Mar 2025 12:38:53 +0100 Subject: [PATCH] [clang-tidy] do not diagn. array types in implicit templ. instantiations So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. --- .../modernize/AvoidCArraysCheck.cpp | 61 ++- .../checkers/modernize/avoid-c-arrays.cpp | 159 ++ 2 files changed, 216 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp index 0804aa76d953c..7adfcc19bfb99 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp @@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *ArrayType, + ASTContext *Context) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Context->getParents(*ArrayType); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; +if (IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get())) { + return true; +} +ParentNodes = Context->getParents(ParentNode); + } + + return false; +} + } // namespace AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context) @@ -70,18 +94,46 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) { std::move(IgnoreStringArrayIfNeededMatcher)) .bind("typeloc"), this); + + // TODO: check allow string arrays in template args + Finder->addMatcher(templateArgumentLoc(hasTypeLoc( + loc(arrayType()).bind("typeloc_in_template_arg"))), + this); } void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { - const auto *ArrayType = Result.Nodes.getNodeAs("typeloc"); + clang::TypeLoc ArrayTypeLoc{}; + + if (const auto *ArrayType = Result.Nodes.getNodeAs("typeloc"); + ArrayType != nullptr && + not(isWithinImplicitTemplateInstantiation(ArrayType, Result.Context))) { +ArrayTypeLoc = *ArrayType; + } + + if (const auto *ArrayTypeInTemplateArg = + Result.Nodes.getNodeAs("typeloc_in_template_arg"); + ArrayTypeInTemplateArg != nullptr) { +if (ArrayTypeInTemplateArg->getSourceRange() != +ArrayTypeInTemplateArg->getLocalSourceRange()) { + // only in case the above condition is fulfilled, we matched a written + // array type and not a template type parameter which got deduced to one + ArrayTypeLoc = *ArrayTypeInTemplateArg; +} + } + + // check whether the match result is a real array type (based on above checks) + if (ArrayTypeLoc.isNull()) { +return; + } + const bool IsInParam = Result.Nodes.getNodeAs("param_decl") != nullptr; - const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType(); + const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType(); enum class RecommendType { Array, Vector, Span }; llvm::SmallVector RecommendTypes{}; if (IsVLA) { RecommendTypes.push_back("'std::vector'"); - } else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) { + } else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) { // in function parameter, we also don't know the size of // IncompleteArrayType. if (Result.Context->getLangOpts().CPlusPlus20) @@ -93,7 +145,8 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { } else { RecommendTypes.push_back("'std::array'"); } - diag(ArrayType->getBeginLoc(), + + diag(ArrayTypeLoc.getBeginLoc
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench updated https://github.com/llvm/llvm-project/pull/132924 >From 8b720d23c2c2252a709d9fd94a2f2fffe8d3fd87 Mon Sep 17 00:00:00 2001 From: stmuench Date: Tue, 25 Mar 2025 12:38:53 +0100 Subject: [PATCH] [clang-tidy] do not diagn. array types in implicit templ. instantiations So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. --- .../modernize/AvoidCArraysCheck.cpp | 60 ++- .../avoid-c-arrays-ignores-strings.cpp| 12 ++ .../checkers/modernize/avoid-c-arrays.cpp | 159 ++ 3 files changed, 227 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp index 0804aa76d953c..e26b8cf885ea3 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp @@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *ArrayType, + ASTContext *Context) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Context->getParents(*ArrayType); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; +if (IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get())) { + return true; +} +ParentNodes = Context->getParents(ParentNode); + } + + return false; +} + } // namespace AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context) @@ -70,18 +94,45 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) { std::move(IgnoreStringArrayIfNeededMatcher)) .bind("typeloc"), this); + + Finder->addMatcher(templateArgumentLoc(hasTypeLoc( + loc(arrayType()).bind("typeloc_in_template_arg"))), + this); } void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { - const auto *ArrayType = Result.Nodes.getNodeAs("typeloc"); + clang::TypeLoc ArrayTypeLoc{}; + + if (const auto *ArrayType = Result.Nodes.getNodeAs("typeloc"); + ArrayType != nullptr && + not(isWithinImplicitTemplateInstantiation(ArrayType, Result.Context))) { +ArrayTypeLoc = *ArrayType; + } + + if (const auto *ArrayTypeInTemplateArg = + Result.Nodes.getNodeAs("typeloc_in_template_arg"); + ArrayTypeInTemplateArg != nullptr) { +if (ArrayTypeInTemplateArg->getSourceRange() != +ArrayTypeInTemplateArg->getLocalSourceRange()) { + // only in case the above condition is fulfilled, we matched a written + // array type and not a template type parameter which got deduced to one + ArrayTypeLoc = *ArrayTypeInTemplateArg; +} + } + + // check whether the match result is a real array type (based on above checks) + if (ArrayTypeLoc.isNull()) { +return; + } + const bool IsInParam = Result.Nodes.getNodeAs("param_decl") != nullptr; - const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType(); + const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType(); enum class RecommendType { Array, Vector, Span }; llvm::SmallVector RecommendTypes{}; if (IsVLA) { RecommendTypes.push_back("'std::vector'"); - } else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) { + } else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) { // in function parameter, we also don't know the size of // IncompleteArrayType. if (Result.Context->getLangOpts().CPlusPlus20) @@ -93,7 +144,8 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { } else { RecommendTypes.push_back("'std::array'"); } - diag(ArrayType->getBeginLoc(), + + diag(ArrayTypeLoc.getBeginLo
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench ready_for_review https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench edited https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -92,3 +92,162 @@ const char name[] = "Some string"; void takeCharArray(const char name[]); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] + +namespace std { + template + struct is_same { constexpr static bool value{false}; }; + + template + struct is_same { constexpr static bool value{true}; }; + + template + constexpr bool is_same_v = is_same::value; + + template struct remove_const { typedef T type; }; + template struct remove_const { typedef T type; }; + + template + using remove_const_t = typename remove_const::type; + + template struct enable_if {}; + template struct enable_if { typedef T type; }; + + template< bool B, class T = void > + using enable_if_t = typename enable_if::type; +} + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +class MyClassTemplate { + public: + // here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly + template , +// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +bool = std::is_same::value, +// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +std::enable_if_t, int[]>) && not(std::is_same_v::type, char[10]>), bool> = true, +// CHECK-MESSAGES: :[[@LINE-1]]:73: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +// CHECK-MESSAGES: :[[@LINE-2]]:140: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = typename std::remove_const::type, +// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = std::remove_const_t> +// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +class MyInnerClassTemplate { + public: + MyInnerClassTemplate(const U&) {} + private: + U field[3]; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +MyClassTemplate(const T&) {} + + private: +T field[7]; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +// an explicit instantiation +template +class MyClassTemplate; +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +using MyArrayType = int[3]; +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +// another explicit instantiation +template +class MyClassTemplate; + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +void func(const T& param) { + int array1[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T array2[2]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T value; +} + +// here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly +template , +
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -92,3 +92,162 @@ const char name[] = "Some string"; void takeCharArray(const char name[]); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] + +namespace std { + template + struct is_same { constexpr static bool value{false}; }; + + template + struct is_same { constexpr static bool value{true}; }; + + template + constexpr bool is_same_v = is_same::value; + + template struct remove_const { typedef T type; }; + template struct remove_const { typedef T type; }; + + template + using remove_const_t = typename remove_const::type; + + template struct enable_if {}; + template struct enable_if { typedef T type; }; + + template< bool B, class T = void > + using enable_if_t = typename enable_if::type; +} + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +class MyClassTemplate { + public: + // here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly + template , +// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +bool = std::is_same::value, +// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +std::enable_if_t, int[]>) && not(std::is_same_v::type, char[10]>), bool> = true, +// CHECK-MESSAGES: :[[@LINE-1]]:73: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +// CHECK-MESSAGES: :[[@LINE-2]]:140: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = typename std::remove_const::type, +// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = std::remove_const_t> +// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +class MyInnerClassTemplate { + public: + MyInnerClassTemplate(const U&) {} + private: + U field[3]; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +MyClassTemplate(const T&) {} + + private: +T field[7]; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +// an explicit instantiation +template +class MyClassTemplate; +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +using MyArrayType = int[3]; +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +// another explicit instantiation +template +class MyClassTemplate; + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +void func(const T& param) { + int array1[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T array2[2]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T value; +} + +// here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly +template , +
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench edited https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *MatchedTypeLoc, stmuench wrote: I tried to implement it this way at first but unfortunately the `hasAncestor` matcher seems to not work correctly for matched `TypeLoc`s. The reason for that is not clear to me at all. That's why I added a manual implementation as workaround. https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *MatchedTypeLoc, stmuench wrote: > Could you please elaborate what is this function for? I deleted its call in > `not(isWithinImplicitTemplateInstantiation(...))` and all tests passed. I > suggest we can delete this function or add tests cases to cover added > behavior. If I remove this function on my side, the unit tests start to fail since diagnostics get emitted then for implicit template instantiations. https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench updated https://github.com/llvm/llvm-project/pull/132924 >From fb9f0c3db8609645588692eecc294407f48c1c34 Mon Sep 17 00:00:00 2001 From: stmuench Date: Tue, 25 Mar 2025 12:38:53 +0100 Subject: [PATCH] [clang-tidy] do not diagn. array types in implicit templ. instantiations So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. --- .../modernize/AvoidCArraysCheck.cpp | 60 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 + .../avoid-c-arrays-ignores-strings.cpp| 12 ++ .../checkers/modernize/avoid-c-arrays.cpp | 192 ++ 4 files changed, 262 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp index 0804aa76d953c..57cbba87f4c02 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp @@ -39,6 +39,29 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +AST_MATCHER(clang::TypeLoc, isInImplicitTemplateInstantiation) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Finder->getASTContext().getParents(Node); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; +if (IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get())) { + return true; +} +ParentNodes = Finder->getASTContext().getParents(ParentNode); + } + + return false; +} + } // namespace AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context) @@ -66,22 +89,45 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) { hasParent(varDecl(isExternC())), hasParent(fieldDecl( hasParent(recordDecl(isExternCContext(), - hasAncestor(functionDecl(isExternC(), + hasAncestor(functionDecl(isExternC())), + isInImplicitTemplateInstantiation())), 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 ArrayTypeLoc{}; + + if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs("typeloc"); + MatchedTypeLoc != nullptr) { +ArrayTypeLoc = *MatchedTypeLoc; + } + + if (const auto *TemplateArgLoc = Result.Nodes.getNodeAs( + "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; + } + const bool IsInParam = Result.Nodes.getNodeAs("param_decl") != nullptr; - const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType(); + const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType(); enum class RecommendType { Array, Vector, Span }; llvm::SmallVector RecommendTypes{}; if (IsVLA) { RecommendTypes.push_back("'std::vector'"); - } else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) { + } else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) { // in function parameter, we also don't know the size of // IncompleteArrayType. if (Result.Context->getLangOpts().CPlusPlus20) @@ -93,9 +139,11 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) {
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
stmuench wrote: > Please add release notes in this patch. I think the idea is good, but you can > look into my review after PiotrZSL's comments. @vbvictor many thanks for your very valuable comments. I incorporated them as far as possible. https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *MatchedTypeLoc, stmuench wrote: Precisely, there were no template types so far in the existing unit tests which got deduced to array types. That's why I added some. https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench edited https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -39,6 +39,29 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +AST_MATCHER(clang::TypeLoc, isInImplicitTemplateInstantiation) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Finder->getASTContext().getParents(Node); stmuench wrote: done https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -39,6 +39,29 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +AST_MATCHER(clang::TypeLoc, isInImplicitTemplateInstantiation) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Finder->getASTContext().getParents(Node); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; stmuench wrote: done https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench updated https://github.com/llvm/llvm-project/pull/132924 Rate limit · GitHub body { background-color: #f6f8fa; color: #24292e; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol; font-size: 14px; line-height: 1.5; margin: 0; } .container { margin: 50px auto; max-width: 600px; text-align: center; padding: 0 24px; } a { color: #0366d6; text-decoration: none; } a:hover { text-decoration: underline; } h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; text-shadow: 0 1px 0 #fff; } p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; } ul { list-style: none; margin: 25px 0; padding: 0; } li { display: table-cell; font-weight: bold; width: 1%; } .logo { display: inline-block; margin-top: 35px; } .logo-img-2x { display: none; } @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and ( min--moz-device-pixel-ratio: 2), only screen and ( -o-min-device-pixel-ratio: 2/1), only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) { .logo-img-1x { display: none; } .logo-img-2x { display: inline-block; } } #suggestions { margin-top: 35px; color: #ccc; } #suggestions a { color: #66; font-weight: 200; font-size: 14px; margin: 0 10px; } Whoa there! You have exceeded a secondary rate limit. Please wait a few minutes before you try again; in some cases this may take up to an hour. https://support.github.com/contact";>Contact Support — https://githubstatus.com";>GitHub Status — https://twitter.com/githubstatus";>@githubstatus ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
stmuench wrote: @PiotrZSL would have any further remarks for this PR? https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench updated https://github.com/llvm/llvm-project/pull/132924 >From 29cfb29e064abf21c01a60aca380a58a4e5adb69 Mon Sep 17 00:00:00 2001 From: stmuench Date: Tue, 25 Mar 2025 12:38:53 +0100 Subject: [PATCH] [clang-tidy] do not diagn. array types in implicit templ. instantiations So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. --- .../modernize/AvoidCArraysCheck.cpp | 59 +- .../avoid-c-arrays-ignores-strings.cpp| 12 ++ .../checkers/modernize/avoid-c-arrays.cpp | 177 ++ 3 files changed, 243 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp index 0804aa76d953c..6fbb2f899fc68 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp @@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *MatchedTypeLoc, + ASTContext *Context) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Context->getParents(*MatchedTypeLoc); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; +if (IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get())) { + return true; +} +ParentNodes = Context->getParents(ParentNode); + } + + return false; +} + } // namespace AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context) @@ -70,18 +94,41 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) { 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 ArrayTypeLoc{}; + + if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs("typeloc"); + MatchedTypeLoc != nullptr && not(isWithinImplicitTemplateInstantiation( + MatchedTypeLoc, Result.Context))) { +ArrayTypeLoc = *MatchedTypeLoc; + } + + if (const auto *TemplateArgLoc = Result.Nodes.getNodeAs( + "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; + } + const bool IsInParam = Result.Nodes.getNodeAs("param_decl") != nullptr; - const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType(); + const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType(); enum class RecommendType { Array, Vector, Span }; llvm::SmallVector RecommendTypes{}; if (IsVLA) { RecommendTypes.push_back("'std::vector'"); - } else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) { + } else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) { // in function parameter, we also don't know the size of // IncompleteArrayType. if (Result.Context->getLangOpts().CPlusPlus20) @@ -93,9 +140,11 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { } else { RecommendTypes.push_back("'std::array'"); } - diag(ArrayType->getBeginLoc(), + + diag(ArrayTypeLoc.getBeginLoc(), "do not declare %select{C-style|C VLA}0 arrays, use %1 instead") - << IsVLA << llvm::join(RecommendTypes, " or "); + <<
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -92,3 +92,162 @@ const char name[] = "Some string"; void takeCharArray(const char name[]); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] + +namespace std { + template + struct is_same { constexpr static bool value{false}; }; + + template + struct is_same { constexpr static bool value{true}; }; + + template + constexpr bool is_same_v = is_same::value; + + template struct remove_const { typedef T type; }; + template struct remove_const { typedef T type; }; + + template + using remove_const_t = typename remove_const::type; + + template struct enable_if {}; + template struct enable_if { typedef T type; }; + + template< bool B, class T = void > + using enable_if_t = typename enable_if::type; +} + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +class MyClassTemplate { + public: + // here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly + template , +// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +bool = std::is_same::value, +// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +std::enable_if_t, int[]>) && not(std::is_same_v::type, char[10]>), bool> = true, +// CHECK-MESSAGES: :[[@LINE-1]]:73: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +// CHECK-MESSAGES: :[[@LINE-2]]:140: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = typename std::remove_const::type, +// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = std::remove_const_t> +// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +class MyInnerClassTemplate { + public: + MyInnerClassTemplate(const U&) {} + private: + U field[3]; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +MyClassTemplate(const T&) {} + + private: +T field[7]; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +// an explicit instantiation +template +class MyClassTemplate; +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +using MyArrayType = int[3]; +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +// another explicit instantiation +template +class MyClassTemplate; + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +void func(const T& param) { + int array1[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T array2[2]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T value; +} + +// here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly +template , +
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench updated https://github.com/llvm/llvm-project/pull/132924 >From 7f621bc7e169cf34d95ba34b0895c9fa75adb712 Mon Sep 17 00:00:00 2001 From: stmuench Date: Tue, 25 Mar 2025 12:38:53 +0100 Subject: [PATCH] [clang-tidy] do not diagn. array types in implicit templ. instantiations So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. --- .../modernize/AvoidCArraysCheck.cpp | 59 +- .../avoid-c-arrays-ignores-strings.cpp| 12 ++ .../checkers/modernize/avoid-c-arrays.cpp | 192 ++ 3 files changed, 258 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp index 0804aa76d953c..6fbb2f899fc68 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp @@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *MatchedTypeLoc, + ASTContext *Context) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Context->getParents(*MatchedTypeLoc); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; +if (IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get())) { + return true; +} +ParentNodes = Context->getParents(ParentNode); + } + + return false; +} + } // namespace AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context) @@ -70,18 +94,41 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) { 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 ArrayTypeLoc{}; + + if (const auto *MatchedTypeLoc = Result.Nodes.getNodeAs("typeloc"); + MatchedTypeLoc != nullptr && not(isWithinImplicitTemplateInstantiation( + MatchedTypeLoc, Result.Context))) { +ArrayTypeLoc = *MatchedTypeLoc; + } + + if (const auto *TemplateArgLoc = Result.Nodes.getNodeAs( + "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; + } + const bool IsInParam = Result.Nodes.getNodeAs("param_decl") != nullptr; - const bool IsVLA = ArrayType->getTypePtr()->isVariableArrayType(); + const bool IsVLA = ArrayTypeLoc.getTypePtr()->isVariableArrayType(); enum class RecommendType { Array, Vector, Span }; llvm::SmallVector RecommendTypes{}; if (IsVLA) { RecommendTypes.push_back("'std::vector'"); - } else if (ArrayType->getTypePtr()->isIncompleteArrayType() && IsInParam) { + } else if (ArrayTypeLoc.getTypePtr()->isIncompleteArrayType() && IsInParam) { // in function parameter, we also don't know the size of // IncompleteArrayType. if (Result.Context->getLangOpts().CPlusPlus20) @@ -93,9 +140,11 @@ void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { } else { RecommendTypes.push_back("'std::array'"); } - diag(ArrayType->getBeginLoc(), + + diag(ArrayTypeLoc.getBeginLoc(), "do not declare %select{C-style|C VLA}0 arrays, use %1 instead") - << IsVLA << llvm::join(RecommendTypes, " or "); + <<
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
@@ -92,3 +92,162 @@ const char name[] = "Some string"; void takeCharArray(const char name[]); // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] + +namespace std { + template + struct is_same { constexpr static bool value{false}; }; + + template + struct is_same { constexpr static bool value{true}; }; + + template + constexpr bool is_same_v = is_same::value; + + template struct remove_const { typedef T type; }; + template struct remove_const { typedef T type; }; + + template + using remove_const_t = typename remove_const::type; + + template struct enable_if {}; + template struct enable_if { typedef T type; }; + + template< bool B, class T = void > + using enable_if_t = typename enable_if::type; +} + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +class MyClassTemplate { + public: + // here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly + template , +// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +bool = std::is_same::value, +// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +std::enable_if_t, int[]>) && not(std::is_same_v::type, char[10]>), bool> = true, +// CHECK-MESSAGES: :[[@LINE-1]]:73: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +// CHECK-MESSAGES: :[[@LINE-2]]:140: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = typename std::remove_const::type, +// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +typename = std::remove_const_t> +// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +class MyInnerClassTemplate { + public: + MyInnerClassTemplate(const U&) {} + private: + U field[3]; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +MyClassTemplate(const T&) {} + + private: +T field[7]; +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] +}; + +// an explicit instantiation +template +class MyClassTemplate; +// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +using MyArrayType = int[3]; +// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + +// another explicit instantiation +template +class MyClassTemplate; + +// below, no array type findings are expected within the template parameter declarations since no array type gets written explicitly +template , + bool = std::is_same::value, + bool = std::is_same_v, int>, + bool = std::is_same, int>::value, + bool = std::is_same_v::type, int>, + bool = std::is_same::type, int>::value, + std::enable_if_t, int>) && not(std::is_same_v::type, char>), bool> = true, + typename std::enable_if, int>) && not(std::is_same_v::type, char>), bool>::type = true, + typename = std::enable_if_t, int>) && not(std::is_same_v::type, char>)>, + typename = typename std::remove_const::type, + typename = std::remove_const_t> +void func(const T& param) { + int array1[1]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T array2[2]; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] + + T value; +} + +// here, plenty of array type findings are expected for below template parameter declarations since array types get written explicitly +template , +
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
stmuench wrote: @PiotrZSL if it suits your convenience, could you maybe have a look at this PR? Many thanks in advance. https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
stmuench wrote: Ping https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench created https://github.com/llvm/llvm-project/pull/132924 So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. >From 8b2eb4e7f102e85c5074d9069b19335a0ded7ba3 Mon Sep 17 00:00:00 2001 From: stmuench Date: Tue, 25 Mar 2025 12:38:53 +0100 Subject: [PATCH] [clang-tidy] do not diagn. array types in implicit templ. instantiations So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed array types for type template parameters even though no actual array type got written there but it got deduced to one. In such case, there is nothing a developer can do at that location to fix the diagnostic. Since actually, the location where the template got instantiated would have to be adjusted. And this is in most cases some totally distant code where implementers of a template do not have access to. Also adding suppressions to the declaration of the template is not an option since that would clutter the code unnecessarily and is in many cases also simply not possible. Hence, we propose to not diagnose any occurrence of an array type in an implicit instantiation of a template but rather at the point where template arguments involve array types. --- .../modernize/AvoidCArraysCheck.cpp | 64 ++- .../checkers/modernize/avoid-c-arrays.cpp | 159 ++ 2 files changed, 219 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp index 0804aa76d953c..2f8a2e56dbd3d 100644 --- a/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/AvoidCArraysCheck.cpp @@ -39,6 +39,30 @@ AST_MATCHER(clang::ParmVarDecl, isArgvOfMain) { return FD ? FD->isMain() : false; } +bool isWithinImplicitTemplateInstantiation(const TypeLoc *ArrayType, + ASTContext *Context) { + const auto IsImplicitTemplateInstantiation = [](const auto *Node) { +return (Node != nullptr) && + (Node->getTemplateSpecializationKind() == TSK_ImplicitInstantiation); + }; + + auto ParentNodes = Context->getParents(*ArrayType); + while (!ParentNodes.empty()) { +const auto &ParentNode = ParentNodes[0]; +if (IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get()) || +IsImplicitTemplateInstantiation( +ParentNode.template get())) { + return true; +} +ParentNodes = Context->getParents(ParentNode); + } + + return false; +} + } // namespace AvoidCArraysCheck::AvoidCArraysCheck(StringRef Name, ClangTidyContext *Context) @@ -70,18 +94,49 @@ void AvoidCArraysCheck::registerMatchers(MatchFinder *Finder) { std::move(IgnoreStringArrayIfNeededMatcher)) .bind("typeloc"), this); + + Finder->addMatcher(templateArgumentLoc(hasTypeLoc(loc(arrayType( + .bind("template_argument_loc"), + this); } void AvoidCArraysCheck::check(const MatchFinder::MatchResult &Result) { - const auto *ArrayType = Result.Nodes.getNodeAs("typeloc"); + clang::TypeLoc ArrayTypeLoc{}; + + if (const auto *ArrayType = Result.Nodes.getNodeAs("typeloc"); + ArrayType != nullptr && + not(isWithinImplicitTemplateInstantiation(ArrayType, Result.Context))) { +ArrayTypeLoc = *ArrayType; + } + + if (const auto *TemplateArgLoc = + Result.Nodes.getNodeAs("template_argument_loc"); + TemplateArgLoc != nullptr) { +if (const auto *TypeSourceInfo = +TemplateArgLoc->getLocInfo().getAsTypeSourceInfo(); +TypeSourceInfo != nullptr) { + if (TypeSourceInfo->getTypeLoc().getSourceRange() != + TypeSourceInfo->getTypeLoc().getLocalSourceRange()) { +// only in case the above condition is fulfilled, we matched a written +// array type and not a template type parameter which got deduced to one +ArrayTypeLoc = TypeSourceInfo->getTypeLoc(); + } +} + } + + // check whether the match result is a real array type (based on above checks) + if (ArrayType
[clang-tools-extra] [clang-tidy] offer option to check sugared types in avoid-c-arrays check (PR #131468)
stmuench wrote: > Example: > > ``` > #include > > template > void f(T &&value) {} > > void test() { > int t[10]; > f(t); > } > ``` > > ``` > /root/1.cpp:3:50: warning: do not declare C-style arrays, use 'std::array' > instead > [cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays] > 3 | template > void f(T > &&value) {} > | ^ > /root/1.cpp:6:3: warning: do not declare C-style arrays, use 'std::array' > instead > [cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays] > 6 | int t[10]; > | ^ > ``` > > In above code check is executed in template instance even that > TK_IgnoreUnlessSpelledInSource is set. With your change it will be even worst. > > Thing is that if check would properly handled implicit code then "template > parameters" would never be catch. At the end check should point places in > code where actually arrays are defined, otherwise if you would put array into > type alias, then you would get warning in every place that type alias is > used, and that's stupid as there is only one place where such array can be > fixed, and that is a definition of that type alias. @PiotrZSL in https://github.com/llvm/llvm-project/pull/132924 I added a proposal for not diagnosing array types within implicit instantiations of a template. Kindly have a look and provide feedback in case you are interested. https://github.com/llvm/llvm-project/pull/131468 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
stmuench wrote: @vbvictor since is there no further activity by reviewers here, any idea on how to continue with this PR? https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] do not diagnose array types within implicit instantiations of a template (PR #132924)
https://github.com/stmuench edited https://github.com/llvm/llvm-project/pull/132924 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits