xref: /llvm-project/clang/test/SemaCXX/vla-ext-diag.cpp (revision f34a5205aa481a6d9a15054bcc5f7b9875906a17)
1 // RUN: %clang_cc1 -verify=gnu -std=gnu++11 %s
2 // RUN: %clang_cc1 -verify=expected,cxx11 -Wvla -std=gnu++11 %s
3 // RUN: %clang_cc1 -verify=expected,cxx11 -std=c++11 %s
4 // RUN: %clang_cc1 -verify=expected,cxx98 -std=c++98 %s
5 // RUN: %clang_cc1 -verify=expected,off -std=c++11 -Wno-vla-extension-static-assert %s
6 // gnu-no-diagnostics
7 
8 // Demonstrate that we do not diagnose use of VLAs by default in GNU mode, but
9 // we do diagnose them in C++ mode. Also note that we suggest use of
10 // static_assert, but only in C++11 and later and only if the warning group is
11 // not disabled.
12 
13 // C++98 mode does not emit the same notes as C++11 mode because in C++98,
14 // we're looking for an integer constant expression, whereas in C++11 and later,
15 // we're looking for a constant expression that is of integer type (these are
16 // different operations; ICE looks at the syntactic form of the expression, but
17 // C++11 constant expressions require calculating the expression value).
func(int n)18 void func(int n) { // cxx11-note {{declared here}} off-note {{declared here}}
19   int vla[n]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
20                  cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}} \
21                  off-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
22 }
23 
old_style_static_assert(int n)24 void old_style_static_assert(int n) { // cxx11-note 5 {{declared here}} off-note 2 {{declared here}}
25   int array1[n != 12 ? 1 : -1]; // cxx11-warning {{variable length arrays in C++ are a Clang extension; did you mean to use 'static_assert'?}} \
26                                    cxx98-warning {{variable length arrays in C++ are a Clang extension}} \
27                                    cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
28   int array2[n != 12 ? -1 : 1]; // cxx11-warning {{variable length arrays in C++ are a Clang extension; did you mean to use 'static_assert'?}} \
29                                    cxx98-warning {{variable length arrays in C++ are a Clang extension}} \
30                                    cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
31   int array3[n != 12 ? 1 : n];  // expected-warning {{variable length arrays in C++ are a Clang extension}} \
32                                    cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}} \
33                                    off-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
34   int array4[(n ? 1 : -1)];     // cxx11-warning {{variable length arrays in C++ are a Clang extension; did you mean to use 'static_assert'?}} \
35                                    cxx98-warning {{variable length arrays in C++ are a Clang extension}} \
36                                    cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
37   int array5[n ? 1 : 0];        // expected-warning {{variable length arrays in C++ are a Clang extension}} \
38                                    cxx11-note {{function parameter 'n' with unknown value cannot be used in a constant expression}} \
39                                    off-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
40 }
41