From dd117cf01ddc50d53e8133a39b367dc00ece9877 Mon Sep 17 00:00:00 2001 From: Matthias Gehre Date: Tue, 19 Jul 2016 17:02:54 +0000 Subject: [PATCH] cppcoreguidelines-pro-bounds-constant-array-index: ignore implicit constructor Summary: The code struct A { int x[3]; }; gets an compiler-generated copy constructor that uses ArraySubscriptExpr (see below). Previously, the check would generate a warning on that copy constructor. This commit disables the warning on implicitly generated code. AST: |-CXXConstructorDecl 0x337b3c8 col:8 implicit used constexpr A 'void (const struct A &) noexcept' inline | |-ParmVarDecl 0x337b510 col:8 used 'const struct A &' | |-CXXCtorInitializer Field 0x3379238 'x' 'int [3]' | | `-ImplicitCastExpr 0x337e158 'int' | | `-ArraySubscriptExpr 0x337e130 'const int' lvalue | | |-ImplicitCastExpr 0x337e118 'const int *' | | | `-MemberExpr 0x337dfc8 'int const[3]' lvalue .x 0x3379238 | | | `-DeclRefExpr 0x337dfa0 'const struct A' lvalue ParmVar 0x337b510 '' 'const struct A &' | | `-ImplicitCastExpr 0x337e098 'unsigned long' | | `-DeclRefExpr 0x337e070 'unsigned long' lvalue Var 0x337e010 '__i0' 'unsigned long' Reviewers: alexfh, aaron.ballman Subscribers: aemerson, nemanjai, cfe-commits Differential Revision: https://reviews.llvm.org/D22381 llvm-svn: 275993 --- .../cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp | 5 ++++- .../cppcoreguidelines-pro-bounds-constant-array-index.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp index a57ec49..56fbf74 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp @@ -45,9 +45,12 @@ void ProBoundsConstantArrayIndexCheck::registerMatchers(MatchFinder *Finder) { if (!getLangOpts().CPlusPlus) return; + // Note: if a struct contains an array member, the compiler-generated + // constructor has an arraySubscriptExpr. Finder->addMatcher(arraySubscriptExpr(hasBase(ignoringImpCasts(hasType( constantArrayType().bind("type")))), - hasIndex(expr().bind("index"))) + hasIndex(expr().bind("index")), + unless(hasAncestor(isImplicit()))) .bind("expr"), this); diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index.cpp index 8f22302..3519410 100644 --- a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index.cpp +++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index.cpp @@ -74,3 +74,14 @@ void customOperator() { int i = 0; s[i] = 3; // OK, custom operator } + +struct A { + // The compiler-generated copy constructor uses an ArraySubscriptExpr. Don't warn. + int x[3]; +}; + +void use_A() { + // Force the compiler to generate a copy constructor. + A a; + A a2(a); +} -- 2.7.4