riccibruno created this revision. riccibruno added reviewers: efriedma, aaron.ballman. riccibruno added a project: clang. Herald added a subscriber: cfe-commits.
Bail-out of `CheckArrayAccess` when the types of the base expression before and after eventual casts are dependent. We will get another chance to check for array bounds during instantiation. Fixes PR41087. Repository: rC Clang https://reviews.llvm.org/D59776 Files: lib/Sema/SemaChecking.cpp test/SemaCXX/array-bounds.cpp Index: test/SemaCXX/array-bounds.cpp =================================================================== --- test/SemaCXX/array-bounds.cpp +++ test/SemaCXX/array-bounds.cpp @@ -296,3 +296,16 @@ // We can still diagnose this. C &h() { return reinterpret_cast<C *>(xxx)[-1]; } // expected-warning {{array index -1 is before the beginning of the array}} } + +namespace PR41087 { + template <typename Ty> void foo() { + Ty buffer[2]; // expected-note 3{{array 'buffer' declared here}} + ((char *)buffer)[2] = 'A'; // expected-warning 1{{array index 2 is past the end of the array (which contains 2 elements)}} + ((char *)buffer)[-1] = 'A'; // expected-warning 2{{array index -1 is before the beginning of the array}} + } + + void f() { + foo<char>(); // expected-note 1{{in instantiation of function template specialization}} + foo<int>(); // expected-note 1{{in instantiation of function template specialization}} + }; +} Index: lib/Sema/SemaChecking.cpp =================================================================== --- lib/Sema/SemaChecking.cpp +++ lib/Sema/SemaChecking.cpp @@ -12949,6 +12949,8 @@ return; const Type *BaseType = ArrayTy->getElementType().getTypePtr(); + if (EffectiveType->isDependentType() || BaseType->isDependentType()) + return; Expr::EvalResult Result; if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))
Index: test/SemaCXX/array-bounds.cpp =================================================================== --- test/SemaCXX/array-bounds.cpp +++ test/SemaCXX/array-bounds.cpp @@ -296,3 +296,16 @@ // We can still diagnose this. C &h() { return reinterpret_cast<C *>(xxx)[-1]; } // expected-warning {{array index -1 is before the beginning of the array}} } + +namespace PR41087 { + template <typename Ty> void foo() { + Ty buffer[2]; // expected-note 3{{array 'buffer' declared here}} + ((char *)buffer)[2] = 'A'; // expected-warning 1{{array index 2 is past the end of the array (which contains 2 elements)}} + ((char *)buffer)[-1] = 'A'; // expected-warning 2{{array index -1 is before the beginning of the array}} + } + + void f() { + foo<char>(); // expected-note 1{{in instantiation of function template specialization}} + foo<int>(); // expected-note 1{{in instantiation of function template specialization}} + }; +} Index: lib/Sema/SemaChecking.cpp =================================================================== --- lib/Sema/SemaChecking.cpp +++ lib/Sema/SemaChecking.cpp @@ -12949,6 +12949,8 @@ return; const Type *BaseType = ArrayTy->getElementType().getTypePtr(); + if (EffectiveType->isDependentType() || BaseType->isDependentType()) + return; Expr::EvalResult Result; if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits