Author: brunoricci
Date: Mon Mar 25 14:37:10 2019
New Revision: 356957

URL: http://llvm.org/viewvc/llvm-project?rev=356957&view=rev
Log:
[Sema] Don't check for array bounds when the types in the base expression are 
dependent

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


Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/SemaCXX/array-bounds.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=356957&r1=356956&r2=356957&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Mar 25 14:37:10 2019
@@ -12522,6 +12522,8 @@ void Sema::CheckArrayAccess(const Expr *
     return;
 
   const Type *BaseType = ArrayTy->getElementType().getTypePtr();
+  if (EffectiveType->isDependentType() || BaseType->isDependentType())
+    return;
 
   Expr::EvalResult Result;
   if (!IndexExpr->EvaluateAsInt(Result, Context, Expr::SE_AllowSideEffects))

Modified: cfe/trunk/test/SemaCXX/array-bounds.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/array-bounds.cpp?rev=356957&r1=356956&r2=356957&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/array-bounds.cpp (original)
+++ cfe/trunk/test/SemaCXX/array-bounds.cpp Mon Mar 25 14:37:10 2019
@@ -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}}
+  };
+}


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to