Skip to content

Commit f5043f4

Browse files
committed
Revert "Diagnose use of VLAs in C++ by default"
This reverts commit 7339c0f. Breaks bots: https://lab.llvm.org/buildbot/#/builders/139/builds/51875 https://lab.llvm.org/buildbot/#/builders/164/builds/45262
1 parent 7339c0f commit f5043f4

File tree

170 files changed

+2489
-2635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

170 files changed

+2489
-2635
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,6 @@ Improvements to Clang's diagnostics
346346
| ~~~~~~~~~^~~~~~~~
347347
- Clang now always diagnoses when using non-standard layout types in ``offsetof`` .
348348
(`#64619: <https://github.com/llvm/llvm-project/issues/64619>`_)
349-
- Clang now diagnoses use of variable-length arrays in C++ by default (and
350-
under ``-Wall`` in GNU++ mode). This is an extension supported by Clang and
351-
GCC, but is very easy to accidentally use without realizing it's a
352-
nonportable construct that has different semantics from a constant-sized
353-
array. (`#62836 <https://github.com/llvm/llvm-project/issues/62836>`_)
354349

355350
Bug Fixes in This Version
356351
-------------------------

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,7 @@ def OverridingMethodMismatch : DiagGroup<"overriding-method-mismatch">;
848848
def VariadicMacros : DiagGroup<"variadic-macros">;
849849
def VectorConversion : DiagGroup<"vector-conversion">; // clang specific
850850
def VexingParse : DiagGroup<"vexing-parse">;
851-
def VLAUseStaticAssert : DiagGroup<"vla-extension-static-assert">;
852-
def VLAExtension : DiagGroup<"vla-extension", [VLAUseStaticAssert]>;
851+
def VLAExtension : DiagGroup<"vla-extension">;
853852
def VLA : DiagGroup<"vla", [VLAExtension]>;
854853
def VolatileRegisterVar : DiagGroup<"volatile-register-var">;
855854
def Visibility : DiagGroup<"visibility">;
@@ -1086,7 +1085,7 @@ def Consumed : DiagGroup<"consumed">;
10861085
// warning should be active _only_ when -Wall is passed in, mark it as
10871086
// DefaultIgnore in addition to putting it here.
10881087
def All : DiagGroup<"all", [Most, Parentheses, Switch, SwitchBool,
1089-
MisleadingIndentation, PackedNonPod, VLAExtension]>;
1088+
MisleadingIndentation, PackedNonPod]>;
10901089

10911090
// Warnings that should be in clang-cl /w4.
10921091
def : DiagGroup<"CL4", [All, Extra]>;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,6 @@ def err_half_const_requires_fp16 : Error<
137137
// C99 variable-length arrays
138138
def ext_vla : Extension<"variable length arrays are a C99 feature">,
139139
InGroup<VLAExtension>;
140-
// In C++ language modes, we warn by default as an extension, while in GNU++
141-
// language modes, we warn as an extension but add the warning group to -Wall.
142-
def ext_vla_cxx : ExtWarn<
143-
"variable length arrays in C++ are a Clang extension">,
144-
InGroup<VLAExtension>;
145-
def ext_vla_cxx_in_gnu_mode : Extension<ext_vla_cxx.Summary>,
146-
InGroup<VLAExtension>;
147-
def ext_vla_cxx_static_assert : ExtWarn<
148-
"variable length arrays in C++ are a Clang extension; did you mean to use "
149-
"'static_assert'?">, InGroup<VLAUseStaticAssert>;
150-
def ext_vla_cxx_in_gnu_mode_static_assert : Extension<
151-
ext_vla_cxx_static_assert.Summary>, InGroup<VLAUseStaticAssert>;
152140
def warn_vla_used : Warning<"variable length array used">,
153141
InGroup<VLA>, DefaultIgnore;
154142
def err_vla_in_sfinae : Error<

clang/lib/Sema/SemaType.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,24 +2582,6 @@ QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
25822582
return QualType();
25832583
}
25842584

2585-
auto IsStaticAssertLike = [](const Expr *ArraySize, ASTContext &Context) {
2586-
// If the array size expression is a conditional expression whose branches
2587-
// are both integer constant expressions, one negative and one positive,
2588-
// then it's assumed to be like an old-style static assertion. e.g.,
2589-
// int old_style_assert[expr ? 1 : -1];
2590-
// We will accept any integer constant expressions instead of assuming the
2591-
// values 1 and -1 are always used.
2592-
if (const auto *CondExpr = dyn_cast_if_present<ConditionalOperator>(
2593-
ArraySize->IgnoreParenImpCasts())) {
2594-
std::optional<llvm::APSInt> LHS =
2595-
CondExpr->getLHS()->getIntegerConstantExpr(Context);
2596-
std::optional<llvm::APSInt> RHS =
2597-
CondExpr->getRHS()->getIntegerConstantExpr(Context);
2598-
return LHS && RHS && LHS->isNegative() != RHS->isNegative();
2599-
}
2600-
return false;
2601-
};
2602-
26032585
// VLAs always produce at least a -Wvla diagnostic, sometimes an error.
26042586
unsigned VLADiag;
26052587
bool VLAIsError;
@@ -2616,15 +2598,6 @@ QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
26162598
} else if (getLangOpts().OpenMP && isInOpenMPTaskUntiedContext()) {
26172599
VLADiag = diag::err_openmp_vla_in_task_untied;
26182600
VLAIsError = true;
2619-
} else if (getLangOpts().CPlusPlus) {
2620-
if (getLangOpts().CPlusPlus11 && IsStaticAssertLike(ArraySize, Context))
2621-
VLADiag = getLangOpts().GNUMode
2622-
? diag::ext_vla_cxx_in_gnu_mode_static_assert
2623-
: diag::ext_vla_cxx_static_assert;
2624-
else
2625-
VLADiag = getLangOpts().GNUMode ? diag::ext_vla_cxx_in_gnu_mode
2626-
: diag::ext_vla_cxx;
2627-
VLAIsError = false;
26282601
} else {
26292602
VLADiag = diag::ext_vla;
26302603
VLAIsError = false;

clang/test/AST/Interp/literals.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-vla -fms-extensions -std=c++11 -verify %s
2-
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-vla -fms-extensions -std=c++20 -verify %s
3-
// RUN: %clang_cc1 -std=c++11 -fms-extensions -Wno-vla -verify=ref %s
4-
// RUN: %clang_cc1 -std=c++20 -fms-extensions -Wno-vla -verify=ref %s
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++11 -verify %s
2+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++20 -verify %s
3+
// RUN: %clang_cc1 -std=c++11 -fms-extensions -verify=ref %s
4+
// RUN: %clang_cc1 -std=c++20 -fms-extensions -verify=ref %s
55

66
#define INT_MIN (~__INT_MAX__)
77
#define INT_MAX __INT_MAX__

clang/test/Analysis/lambdas.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ void testFunctionPointerCapture() {
194194
// Captured variable-length array.
195195

196196
void testVariableLengthArrayCaptured() {
197-
int n = 2; // expected-note {{declared here}}
198-
int array[n]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
199-
expected-note {{read of non-const variable 'n' is not allowed in a constant expression}}
197+
int n = 2;
198+
int array[n];
200199
array[0] = 7;
201200

202201
int i = [&]{

clang/test/CXX/basic/basic.types/p10.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ constexpr int f2(S &) { return 0; }
2020

2121
struct BeingDefined;
2222
extern BeingDefined beingdefined;
23-
struct BeingDefined {
23+
struct BeingDefined {
2424
static constexpr BeingDefined& t = beingdefined;
2525
};
2626

@@ -136,15 +136,11 @@ struct ArrBad {
136136
};
137137
constexpr int f(ArrBad) { return 0; } // expected-error {{1st parameter type 'ArrBad' is not a literal type}}
138138

139-
constexpr int arb(int n) { // expected-note {{declared here}}
140-
int a[n]; // expected-error {{variable of non-literal type 'int[n]' cannot be defined in a constexpr function}} \
141-
expected-warning {{variable length arrays in C++ are a Clang extension}} \
142-
expected-note {{function parameter 'n' with unknown value cannot be used in a constant expression}}
139+
constexpr int arb(int n) {
140+
int a[n]; // expected-error {{variable of non-literal type 'int[n]' cannot be defined in a constexpr function}}
143141
}
144-
constexpr long Overflow[(1 << 30) << 2]{}; // expected-warning {{requires 34 bits to represent}} \
145-
expected-warning {{variable length array folded to constant array as an extension}} \
146-
expected-warning {{variable length arrays in C++ are a Clang extension}} \
147-
expected-note {{signed left shift discards bits}}
142+
// expected-warning@+1 {{variable length array folded to constant array as an extension}}
143+
constexpr long Overflow[(1 << 30) << 2]{}; // expected-warning {{requires 34 bits to represent}}
148144

149145
namespace inherited_ctor {
150146
struct A { constexpr A(int); };

clang/test/CXX/dcl.dcl/dcl.spec/dcl.typedef/p2-0x.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,18 @@ namespace IllegalSyntax {
3434
namespace VariableLengthArrays {
3535
using T = int[42]; // ok
3636

37-
int n = 32; // expected-note {{declared here}}
38-
using T = int[n]; // expected-error {{variable length array declaration not allowed at file scope}} \
39-
expected-warning {{variable length arrays in C++ are a Clang extension}} \
40-
expected-note {{read of non-const variable 'n' is not allowed in a constant expression}}
37+
int n = 32;
38+
using T = int[n]; // expected-error {{variable length array declaration not allowed at file scope}}
4139

4240
const int m = 42;
4341
using U = int[m];
4442
using U = int[42]; // expected-note {{previous definition}}
4543
using U = int; // expected-error {{type alias redefinition with different types ('int' vs 'int[42]')}}
4644

4745
void f() {
48-
int n = 42; // expected-note {{declared here}}
46+
int n = 42;
4947
goto foo; // expected-error {{cannot jump}}
50-
using T = int[n]; // expected-note {{bypasses initialization of VLA type alias}} \
51-
expected-warning {{variable length arrays in C++ are a Clang extension}} \
52-
expected-note {{read of non-const variable 'n' is not allowed in a constant expression}}
48+
using T = int[n]; // expected-note {{bypasses initialization of VLA type alias}}
5349
foo: ;
5450
}
5551
}

clang/test/CXX/expr/expr.prim/expr.prim.lambda/p4.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,21 @@ X infer_X_return_type_2(X x) {
4444
}
4545

4646
struct Incomplete; // expected-note 2{{forward declaration of 'Incomplete'}}
47-
void test_result_type(int N) { // expected-note {{declared here}}
47+
void test_result_type(int N) {
4848
auto l1 = [] () -> Incomplete { }; // expected-error{{incomplete result type 'Incomplete' in lambda expression}}
4949

50-
typedef int vla[N]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
51-
expected-note {{function parameter 'N' with unknown value cannot be used in a constant expression}}
50+
typedef int vla[N];
5251
auto l2 = [] () -> vla { }; // expected-error{{function cannot return array type 'vla' (aka 'int[N]')}}
5352
}
5453

5554
template <typename T>
56-
void test_result_type_tpl(int N) { // expected-note 2{{declared here}}
55+
void test_result_type_tpl(int N) {
5756
auto l1 = []() -> T {}; // expected-error{{incomplete result type 'Incomplete' in lambda expression}}
5857
// expected-note@-1{{while substituting into a lambda expression here}}
59-
typedef int vla[N]; // expected-warning 2{{variable length arrays in C++ are a Clang extension}} \
60-
expected-note 2{{function parameter 'N' with unknown value cannot be used in a constant expression}}
58+
typedef int vla[N];
6159
auto l2 = []() -> vla {}; // expected-error{{function cannot return array type 'vla' (aka 'int[N]')}}
6260
}
6361

6462
void test_result_type_call() {
65-
test_result_type_tpl<Incomplete>(10); // expected-note 2{{requested here}}
63+
test_result_type_tpl<Incomplete>(10); // expected-note {{requested here}}
6664
}

clang/test/CXX/temp/temp.arg/temp.arg.type/p2.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -Wvla %s
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
22
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
33
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
44

@@ -19,17 +19,13 @@ enum {e};
1919
// expected-note@-2 {{unnamed type used in template argument was declared here}}
2020
#endif
2121

22-
void test_f0(int n) { // #here
22+
void test_f0(int n) {
2323
int i = f0(0, e);
2424
#if __cplusplus <= 199711L
2525
// expected-warning@-2 {{template argument uses unnamed type}}
2626
#endif
2727

28-
int vla[n]; // expected-warning {{variable length arrays in C++ are a Clang extension}}
29-
#if __cplusplus > 199711L
30-
// expected-note@-2 {{function parameter 'n' with unknown value cannot be used in a constant expression}}
31-
// expected-note@#here {{declared here}}
32-
#endif
28+
int vla[n];
3329
f0(0, vla); // expected-error{{no matching function for call to 'f0'}}
3430
}
3531

0 commit comments

Comments
 (0)