[clang] e83bea4 - [clang][Interp][NFC] Add some tests for invalid array access

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 14 05:42:20 PDT 2022


Author: Timm Bäder
Date: 2022-10-14T14:41:05+02:00
New Revision: e83bea40b6c45bf2a8d4983238da32b5d2be80fc

URL: https://github.com/llvm/llvm-project/commit/e83bea40b6c45bf2a8d4983238da32b5d2be80fc
DIFF: https://github.com/llvm/llvm-project/commit/e83bea40b6c45bf2a8d4983238da32b5d2be80fc.diff

LOG: [clang][Interp][NFC] Add some tests for invalid array access

Added: 
    

Modified: 
    clang/test/AST/Interp/arrays.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp
index 302c8b2a86cc..318793ff777a 100644
--- a/clang/test/AST/Interp/arrays.cpp
+++ b/clang/test/AST/Interp/arrays.cpp
@@ -98,3 +98,22 @@ struct fred {
 struct fred y [] = { [0] = { .s[0] = 'q' } };
 #endif
 #pragma clang diagnostic pop
+
+namespace indices {
+  constexpr int first[] = {1};
+  constexpr int firstValue = first[2]; // ref-error {{must be initialized by a constant expression}} \
+                                       // ref-note {{cannot refer to element 2 of array of 1}} \
+                                       // expected-error {{must be initialized by a constant expression}} \
+                                       // expected-note {{cannot refer to element 2 of array of 1}}
+
+  constexpr int second[10] = {17};
+  constexpr int secondValue = second[10];// ref-error {{must be initialized by a constant expression}} \
+                                         // ref-note {{read of dereferenced one-past-the-end pointer}} \
+                                         // expected-error {{must be initialized by a constant expression}} \
+                                         // expected-note {{read of dereferenced one-past-the-end pointer}}
+
+  constexpr int negative = second[-2]; // ref-error {{must be initialized by a constant expression}} \
+                                       // ref-note {{cannot refer to element -2 of array of 10}} \
+                                       // expected-error {{must be initialized by a constant expression}} \
+                                       // expected-note {{cannot refer to element -2 of array of 10}}
+};


        


More information about the cfe-commits mailing list