llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-tools-extra Author: LeeYoungJoon (IamYJLee) <details> <summary>Changes</summary> ## Summary This change adds `unless(isInstantiationDependent())` to the `ArraySubscript` matcher. ## Motivation In template code, some array subscript expressions are not fully resolved yet. Because of this, the matcher may treat them incorrectly and produce false positives. ## Change This patch skips instantiation-dependent expressions in templates. It helps the matcher work only on resolved expressions. Closes https://github.com/llvm/llvm-project/issues/187009. --- Full diff: https://github.com/llvm/llvm-project/pull/187452.diff 4 Files Affected: - (modified) clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp (+3-1) - (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) - (added) clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/std/map (+11) - (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp (+12) ``````````diff diff --git a/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp index c21abad947912..ef5fee9f3d2c3 100644 --- a/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp @@ -53,7 +53,9 @@ void PointerArithmeticOnPolymorphicObjectCheck::registerMatchers( ? PointerExprWithVirtualMethod : PolymorphicPointerExpr; - const auto ArraySubscript = arraySubscriptExpr(hasBase(SelectedPointerExpr)); + const auto ArraySubscript = + expr(arraySubscriptExpr(hasBase(SelectedPointerExpr)), + unless(isInstantiationDependent())); const auto BinaryOperators = binaryOperator(hasAnyOperatorName("+", "-", "+=", "-="), diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a02eda85a7e53..2874f523977fc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -216,6 +216,10 @@ Changes in existing checks <clang-tidy/checks/bugprone/macro-parentheses>` check by printing the macro definition in the warning message if the macro is defined on command line. +- Improved :doc:`bugprone-pointer-arithmetic-on-polymorphic-object + <clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object>` check + by fixing a false positive when ``operator[]`` is used in a dependent context. + - Improved :doc:`bugprone-std-namespace-modification <clang-tidy/checks/bugprone/std-namespace-modification>` check by fixing false positives when extending the standard library with a specialization of diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/std/map b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/std/map new file mode 100644 index 0000000000000..5bf4e91e577a6 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/std/map @@ -0,0 +1,11 @@ +#ifndef _MAP_ +#define _MAP_ + +namespace std { +template <typename K, typename V> +struct map { + V &operator[](const K &); +}; +} // namespace std + +#endif // _MAP_ diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp index caf24f79ad2d8..f8163a2fd3fb8 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp @@ -1,5 +1,7 @@ // RUN: %check_clang_tidy %s bugprone-pointer-arithmetic-on-polymorphic-object %t -- +#include <map> + class Base { public: virtual ~Base() {} @@ -138,3 +140,13 @@ void typeAliases(BaseAlias *b, DerivedAlias *d, FinalDerivedAlias *fd, fdp += 1; // no-warning } + +template <typename T> +struct TemplateHolder : Base { + std::map<Base *, T> _map; + void test() { + auto &x = _map[this]; + // no-warning + (void)x; + } +}; `````````` </details> https://github.com/llvm/llvm-project/pull/187452 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
