Skip to content
Snippets Groups Projects
Commit 58733dee authored by Richard Smith's avatar Richard Smith
Browse files

Improve handling of arrays of unknown bound in constant expressions.

Do not spuriously reject constexpr functions that access elements of an array
of unknown bound; this may later become valid once the bound is known. Permit
array-to-pointer decay on such arrays, but disallow pointer arithmetic (since
we do not know whether it will have defined behavior).

The standard is not clear on how this should work, but this seems to be a
decent answer.

Patch by Robert Haberlach!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@301822 91177308-0d34-0410-b5e6-96231b3b80d8
parent e3c69705
No related branches found
No related tags found
No related merge requests found
......@@ -154,12 +154,14 @@ def note_constexpr_baa_insufficient_alignment : Note<
def note_constexpr_baa_value_insufficient_alignment : Note<
"value of the aligned pointer (%0) is not a multiple of the asserted %1 "
"%plural{1:byte|:bytes}1">;
def note_constexpr_array_unknown_bound_arithmetic : Note<
"cannot perform pointer arithmetic on pointer to array without constant bound">;
def warn_integer_constant_overflow : Warning<
"overflow in expression; result is %0 with type %1">,
InGroup<DiagGroup<"integer-overflow">>;
// This is a temporary diagnostic, and shall be removed once our
// This is a temporary diagnostic, and shall be removed once our
// implementation is complete, and like the preceding constexpr notes belongs
// in Sema.
def note_unimplemented_constexpr_lambda_feature_ast : Note<
......
This diff is collapsed.
// RUN: %clang_cc1 %s -Wno-uninitialized -std=c++1z -fsyntax-only -verify
const extern int arr[];
constexpr auto p = arr; // ok
constexpr int f(int i) {return p[i];} // expected-note {{read of dereferenced one-past-the-end pointer}}
constexpr int arr[] {1, 2, 3};
constexpr auto p2 = arr + 2; // ok
constexpr int x = f(2); // ok
constexpr int y = f(3); // expected-error {{constant expression}}
// expected-note-re@-1 {{in call to 'f({{.*}})'}}
struct A {int m[];} a;
constexpr auto p3 = a.m; // ok
constexpr auto p4 = a.m + 1; // expected-error {{constant expression}} expected-note {{constant bound}}
void g(int i) {
int arr[i];
constexpr auto *p = arr + 2; // expected-error {{constant expression}} expected-note {{constant bound}}
// FIXME: Give a better diagnostic here. The issue is that computing
// sizeof(*arr2) within the array indexing fails due to the VLA.
int arr2[2][i];
constexpr int m = ((void)arr2[2], 0); // expected-error {{constant expression}}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment