]> granicus.if.org Git - clang/commitdiff
[Sema] Don't check for array bounds when the types in the base expression are dependent
authorBruno Ricci <riccibrun@gmail.com>
Mon, 25 Mar 2019 21:37:10 +0000 (21:37 +0000)
committerBruno Ricci <riccibrun@gmail.com>
Mon, 25 Mar 2019 21:37:10 +0000 (21:37 +0000)
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.

Differential Revision: https://reviews.llvm.org/D59776

Reviewed By: efriedma

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@356957 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/array-bounds.cpp

index 8f710cad6ff3da23dcc22416fadcdf77ee4d499d..b9b14a0ede5dcd71ddd0b222f5a874f968a28365 100644 (file)
@@ -12522,6 +12522,8 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
     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 6ebff8c9920680164ae28e73d5472b7930410125..495ccaf71bd6a4746f58f6e6f25615c47876227a 100644 (file)
@@ -296,3 +296,16 @@ namespace PR39746 {
   // 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}}
+  };
+}