[PATCH] D30192: [Sema] Detecting more array index out of bounds

2017-02-28 Thread Daniel Marjamäki via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL296477: [Sema] Detect more array index out of bounds when 
C++ overloaded operators are… (authored by danielmarjamaki).

Changed prior to commit:
  https://reviews.llvm.org/D30192?vs=89183=90027#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D30192

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


Index: cfe/trunk/test/SemaCXX/array-bounds.cpp
===
--- cfe/trunk/test/SemaCXX/array-bounds.cpp
+++ cfe/trunk/test/SemaCXX/array-bounds.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
 
 int foo() {
   int x[2]; // expected-note 4 {{array 'x' declared here}}
@@ -253,3 +253,19 @@
int a[128]; // expected-note {{array 'a' declared here}}
a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is 
past the end of the array}}
 }
+
+struct P {
+  int a;
+  int b;
+};
+
+void test_struct_array_index() {
+  struct P p[10]; // expected-note {{array 'p' declared here}}
+  p[11] = {0, 1}; // expected-warning {{array index 11 is past the end of the 
array (which contains 10 elements)}}
+}
+
+int operator+(const struct P , const struct P );
+int test_operator_overload_struct_array_index() {
+  struct P x[10] = {0}; // expected-note {{array 'x' declared here}}
+  return x[1] + x[11]; // expected-warning {{array index 11 is past the end of 
the array (which contains 10 elements)}}
+}
Index: cfe/trunk/lib/Sema/SemaChecking.cpp
===
--- cfe/trunk/lib/Sema/SemaChecking.cpp
+++ cfe/trunk/lib/Sema/SemaChecking.cpp
@@ -10609,6 +10609,12 @@
   CheckArrayAccess(rhs);
 return;
   }
+  case Stmt::CXXOperatorCallExprClass: {
+const auto *OCE = cast(expr);
+for (const auto *Arg : OCE->arguments())
+  CheckArrayAccess(Arg);
+return;
+  }
   default:
 return;
 }


Index: cfe/trunk/test/SemaCXX/array-bounds.cpp
===
--- cfe/trunk/test/SemaCXX/array-bounds.cpp
+++ cfe/trunk/test/SemaCXX/array-bounds.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
 
 int foo() {
   int x[2]; // expected-note 4 {{array 'x' declared here}}
@@ -253,3 +253,19 @@
 	int a[128]; // expected-note {{array 'a' declared here}}
 	a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is past the end of the array}}
 }
+
+struct P {
+  int a;
+  int b;
+};
+
+void test_struct_array_index() {
+  struct P p[10]; // expected-note {{array 'p' declared here}}
+  p[11] = {0, 1}; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
+}
+
+int operator+(const struct P , const struct P );
+int test_operator_overload_struct_array_index() {
+  struct P x[10] = {0}; // expected-note {{array 'x' declared here}}
+  return x[1] + x[11]; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
+}
Index: cfe/trunk/lib/Sema/SemaChecking.cpp
===
--- cfe/trunk/lib/Sema/SemaChecking.cpp
+++ cfe/trunk/lib/Sema/SemaChecking.cpp
@@ -10609,6 +10609,12 @@
   CheckArrayAccess(rhs);
 return;
   }
+  case Stmt::CXXOperatorCallExprClass: {
+const auto *OCE = cast(expr);
+for (const auto *Arg : OCE->arguments())
+  CheckArrayAccess(Arg);
+return;
+  }
   default:
 return;
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D30192: [Sema] Detecting more array index out of bounds

2017-02-27 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

A few minor nits that can be resolved when you commit, but aside from those, 
LGTM.




Comment at: lib/Sema/SemaChecking.cpp:10613
+  case Stmt::CXXOperatorCallExprClass: {
+const CXXOperatorCallExpr *OCE = cast(expr);
+for (auto Arg : OCE->arguments())

You can use `const auto *` here.



Comment at: lib/Sema/SemaChecking.cpp:10614
+const CXXOperatorCallExpr *OCE = cast(expr);
+for (auto Arg : OCE->arguments())
+  CheckArrayAccess(Arg);

`const auto *` instead of just `auto`.


Repository:
  rL LLVM

https://reviews.llvm.org/D30192



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


[PATCH] D30192: [Sema] Detecting more array index out of bounds

2017-02-21 Thread Daniel Marjamäki via Phabricator via cfe-commits
danielmarjamaki created this revision.

Diagnose array index out of bounds when there is overloaded C++ operators also.


Repository:
  rL LLVM

https://reviews.llvm.org/D30192

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
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
 
 int foo() {
   int x[2]; // expected-note 4 {{array 'x' declared here}}
@@ -253,3 +253,19 @@
int a[128]; // expected-note {{array 'a' declared here}}
a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is 
past the end of the array}}
 }
+
+struct P {
+  int a;
+  int b;
+};
+
+void test_struct_array_index() {
+  struct P p[10]; // expected-note {{array 'p' declared here}}
+  p[11] = {0, 1}; // expected-warning {{array index 11 is past the end of the 
array (which contains 10 elements)}}
+}
+
+int operator+(const struct P , const struct P );
+int test_operator_overload_struct_array_index() {
+  struct P x[10] = {0}; // expected-note {{array 'x' declared here}}
+  return x[1] + x[11]; // expected-warning {{array index 11 is past the end of 
the array (which contains 10 elements)}}
+}
Index: lib/Sema/SemaChecking.cpp
===
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -10609,6 +10609,12 @@
   CheckArrayAccess(rhs);
 return;
   }
+  case Stmt::CXXOperatorCallExprClass: {
+const CXXOperatorCallExpr *OCE = cast(expr);
+for (auto Arg : OCE->arguments())
+  CheckArrayAccess(Arg);
+return;
+  }
   default:
 return;
 }


Index: test/SemaCXX/array-bounds.cpp
===
--- test/SemaCXX/array-bounds.cpp
+++ test/SemaCXX/array-bounds.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -verify -std=c++11 %s
 
 int foo() {
   int x[2]; // expected-note 4 {{array 'x' declared here}}
@@ -253,3 +253,19 @@
 	int a[128]; // expected-note {{array 'a' declared here}}
 	a[(unsigned char)'\xA1'] = 1; // expected-warning {{array index 161 is past the end of the array}}
 }
+
+struct P {
+  int a;
+  int b;
+};
+
+void test_struct_array_index() {
+  struct P p[10]; // expected-note {{array 'p' declared here}}
+  p[11] = {0, 1}; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
+}
+
+int operator+(const struct P , const struct P );
+int test_operator_overload_struct_array_index() {
+  struct P x[10] = {0}; // expected-note {{array 'x' declared here}}
+  return x[1] + x[11]; // expected-warning {{array index 11 is past the end of the array (which contains 10 elements)}}
+}
Index: lib/Sema/SemaChecking.cpp
===
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -10609,6 +10609,12 @@
   CheckArrayAccess(rhs);
 return;
   }
+  case Stmt::CXXOperatorCallExprClass: {
+const CXXOperatorCallExpr *OCE = cast(expr);
+for (auto Arg : OCE->arguments())
+  CheckArrayAccess(Arg);
+return;
+  }
   default:
 return;
 }
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits