]> granicus.if.org Git - clang/commitdiff
Merge branch 'yo-dawg-i-herd-u-like-arrays'
authorMatt Beaumont-Gay <matthewbg@google.com>
Tue, 29 Nov 2011 19:27:11 +0000 (19:27 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Tue, 29 Nov 2011 19:27:11 +0000 (19:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145421 91177308-0d34-0410-b5e6-96231b3b80d8

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

index c66b8da6fbc62ef2ba9103578813b2fe0a1abc82..9dd03133b994dd6c25f19a04e8833c25e2f65096 100644 (file)
@@ -4297,6 +4297,17 @@ void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
                           << IndexExpr->getSourceRange());
   }
 
+  if (!ND) {
+    // Try harder to find a NamedDecl to point at in the note.
+    while (const ArraySubscriptExpr *ASE =
+           dyn_cast<ArraySubscriptExpr>(BaseExpr))
+      BaseExpr = ASE->getBase()->IgnoreParenCasts();
+    if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
+      ND = dyn_cast<NamedDecl>(DRE->getDecl());
+    if (const MemberExpr *ME = dyn_cast<MemberExpr>(BaseExpr))
+      ND = dyn_cast<NamedDecl>(ME->getMemberDecl());
+  }
+
   if (ND)
     DiagRuntimeBehavior(ND->getLocStart(), BaseExpr,
                         PDiag(diag::note_array_index_out_of_bounds)
index 51a1937604a474114816318fb23608047cd8ecef..48788f67d74e0515bd91e6ece498b1395d8a6578 100644 (file)
@@ -4,10 +4,14 @@ int foo() {
   int x[2]; // expected-note 4 {{array 'x' declared here}}
   int y[2]; // expected-note 2 {{array 'y' declared here}}
   int z[1]; // expected-note {{array 'z' declared here}}
+  int w[1][1]; // expected-note {{array 'w' declared here}}
+  int v[1][1][1]; // expected-note {{array 'v' declared here}}
   int *p = &y[2]; // no-warning
   (void) sizeof(x[2]); // no-warning
   y[2] = 2; // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
   z[1] = 'x'; // expected-warning {{array index 1 is past the end of the array (which contains 1 element)}}
+  w[0][2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
+  v[0][0][2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
   return x[2] +  // expected-warning {{array index 2 is past the end of the array (which contains 2 elements)}}
          y[-1] + // expected-warning {{array index -1 is before the beginning of the array}}
          x[sizeof(x)] +  // expected-warning {{array index 8 is past the end of the array (which contains 2 elements)}}