sousajo created this revision. Herald added subscribers: PiotrZSL, carlosgalvezp, arphaman, kbarton, xazax.hun, nemanjai. Herald added a reviewer: njames93. Herald added a project: All. sousajo requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
Index accessing checks are not performed for derived classes of of `std::array`, as only `std::array` itself and its aliases seems to be checked. This patch aims to extend it for derived classes such as: template<class T, size_t N> class DerivedArray : public std::array<T, N> {}; https://reviews.llvm.org/D156624 Files: clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp @@ -23,6 +23,9 @@ return base + 3; } +template<class T, size_t N> +class DerivedArray : public std::array<T, N> {}; + void f(std::array<int, 10> a, int pos) { a [ pos / 2 /*comment*/] = 1; // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] @@ -68,6 +71,51 @@ m[const_index(6)] = 3; // OK, constant index and inside bounds } +void f_derived(DerivedArray<int, 10> a, int pos) { + a [ pos / 2 /*comment*/] = 1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] + int j = a[pos - 1]; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not use array subscript when the index is not an integer constant expression + + a.at(pos-1) = 2; // OK, at() instead of [] + gsl::at(a, pos-1) = 2; // OK, gsl::at() instead of [] + + a[-1] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index] + a[10] = 4; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index] + + a[const_index(7)] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) + + a[0] = 3; // OK, constant index and inside bounds + a[1] = 3; // OK, constant index and inside bounds + a[9] = 3; // OK, constant index and inside bounds + a[const_index(6)] = 3; // OK, constant index and inside bounds + + using MyArray = DerivedArray<int, 10>; + MyArray m{}; + m [ pos / 2 /*comment*/] = 1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] + int jj = m[pos - 1]; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use array subscript when the index is not an integer constant expression + + m.at(pos-1) = 2; // OK, at() instead of [] + gsl::at(m, pos-1) = 2; // OK, gsl::at() instead of [] + m[-1] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index] + m[10] = 4; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index] + + m[const_index(7)] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) + + m[0] = 3; // OK, constant index and inside bounds + m[1] = 3; // OK, constant index and inside bounds + m[9] = 3; // OK, constant index and inside bounds + m[const_index(6)] = 3; // OK, constant index and inside bounds +} + void g() { int a[10]; for (int i = 0; i < 10; ++i) { Index: clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -50,7 +50,8 @@ hasOverloadedOperatorName("[]"), hasArgument( 0, hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration( - cxxRecordDecl(hasName("::std::array")).bind("type")))))), + cxxRecordDecl(isSameOrDerivedFrom(hasName("::std::array"))) + .bind("type")))))), hasArgument(1, expr().bind("index"))) .bind("expr"), this);
Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp +++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp @@ -23,6 +23,9 @@ return base + 3; } +template<class T, size_t N> +class DerivedArray : public std::array<T, N> {}; + void f(std::array<int, 10> a, int pos) { a [ pos / 2 /*comment*/] = 1; // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] @@ -68,6 +71,51 @@ m[const_index(6)] = 3; // OK, constant index and inside bounds } +void f_derived(DerivedArray<int, 10> a, int pos) { + a [ pos / 2 /*comment*/] = 1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] + int j = a[pos - 1]; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not use array subscript when the index is not an integer constant expression + + a.at(pos-1) = 2; // OK, at() instead of [] + gsl::at(a, pos-1) = 2; // OK, gsl::at() instead of [] + + a[-1] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index] + a[10] = 4; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index] + + a[const_index(7)] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) + + a[0] = 3; // OK, constant index and inside bounds + a[1] = 3; // OK, constant index and inside bounds + a[9] = 3; // OK, constant index and inside bounds + a[const_index(6)] = 3; // OK, constant index and inside bounds + + using MyArray = DerivedArray<int, 10>; + MyArray m{}; + m [ pos / 2 /*comment*/] = 1; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index] + int jj = m[pos - 1]; + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use array subscript when the index is not an integer constant expression + + m.at(pos-1) = 2; // OK, at() instead of [] + gsl::at(m, pos-1) = 2; // OK, gsl::at() instead of [] + m[-1] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index] + m[10] = 4; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index] + + m[const_index(7)] = 3; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) + + m[0] = 3; // OK, constant index and inside bounds + m[1] = 3; // OK, constant index and inside bounds + m[9] = 3; // OK, constant index and inside bounds + m[const_index(6)] = 3; // OK, constant index and inside bounds +} + void g() { int a[10]; for (int i = 0; i < 10; ++i) { Index: clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -50,7 +50,8 @@ hasOverloadedOperatorName("[]"), hasArgument( 0, hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration( - cxxRecordDecl(hasName("::std::array")).bind("type")))))), + cxxRecordDecl(isSameOrDerivedFrom(hasName("::std::array"))) + .bind("type")))))), hasArgument(1, expr().bind("index"))) .bind("expr"), this);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits