xref: /llvm-project/clang/test/SemaCXX/constexpr-array-unknown-bound.cpp (revision 84a3aadf0f2483dde0acfc4e79f2a075a5f35bd1)
1 // RUN: %clang_cc1 %s -Wno-uninitialized -std=c++1z -fsyntax-only -verify
2 
3 const extern int arr[];
4 constexpr auto p = arr; // ok
f(int i)5 constexpr int f(int i) {return p[i];} // expected-note {{read of dereferenced one-past-the-end pointer}}
6 
7 constexpr int arr[] {1, 2, 3};
8 constexpr auto p2 = arr + 2; // ok
9 constexpr int x = f(2); // ok
10 constexpr int y = f(3); // expected-error {{constant expression}}
11 // expected-note-re@-1 {{in call to 'f({{.*}})'}}
12 
13 // FIXME: consider permitting this case
14 struct A {int m[];} a;
15 constexpr auto p3 = a.m; // expected-error {{constant expression}} expected-note {{without known bound}}
16 constexpr auto p4 = a.m + 1; // expected-error {{constant expression}} expected-note {{without known bound}}
17 
g(int i)18 void g(int i) {  // expected-note 2{{declared here}}
19   int arr[i]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
20                  expected-note {{function parameter 'i' with unknown value cannot be used in a constant expression}}
21   constexpr auto *p = arr + 2; // expected-error {{constant expression}} expected-note {{without known bound}}
22 
23   // FIXME: Give a better diagnostic here. The issue is that computing
24   // sizeof(*arr2) within the array indexing fails due to the VLA.
25   int arr2[2][i]; // expected-warning 2{{variable length arrays in C++ are a Clang extension}} \
26                      expected-note {{function parameter 'i' with unknown value cannot be used in a constant expression}}
27   constexpr int m = ((void)arr2[2], 0); // expected-error {{constant expression}}
28 }
29