[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)
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 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 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