From ce4dc26cf4bf36fbb93d5ca3b8ae38f82c921396 Mon Sep 17 00:00:00 2001 From: Serge Pavlov Date: Thu, 6 Feb 2014 03:49:11 +0000 Subject: [PATCH] Allow transformation of VariableArray to ConstantArray. In the following code: struct A { static const int sz; }; template void f() { T arr[A::sz]; } the array 'arr' is represented as a variable size array in the template. If 'A::sz' gets value below in the translation unit, the array in instantiation can turn into constant size array. This change fixes PR18633. Differential Revision: http://llvm-reviews.chandlerc.com/D2688 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@200899 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/TreeTransform.h | 4 +++- test/SemaCXX/c99-variable-length-array.cpp | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 93cb483321..7afed1a448 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -3929,7 +3929,9 @@ TreeTransform::TransformVariableArrayType(TypeLocBuilder &TLB, return QualType(); } - VariableArrayTypeLoc NewTL = TLB.push(Result); + // We might have constant size array now, but fortunately it has the same + // location layout. + ArrayTypeLoc NewTL = TLB.push(Result); NewTL.setLBracketLoc(TL.getLBracketLoc()); NewTL.setRBracketLoc(TL.getRBracketLoc()); NewTL.setSizeExpr(Size); diff --git a/test/SemaCXX/c99-variable-length-array.cpp b/test/SemaCXX/c99-variable-length-array.cpp index bb620c71fa..237f56458d 100644 --- a/test/SemaCXX/c99-variable-length-array.cpp +++ b/test/SemaCXX/c99-variable-length-array.cpp @@ -140,3 +140,24 @@ namespace PR11744 { } int test = f(0); // expected-note {{instantiation of}} } + +namespace pr18633 { + struct A1 { + static const int sz; + static const int sz2; + }; + const int A1::sz2 = 11; + template + void func () { + int arr[A1::sz]; // expected-warning{{variable length arrays are a C99 feature}} + } + template + void func2 () { + int arr[A1::sz2]; + } + const int A1::sz = 12; + void func2() { + func(); + func2(); + } +} -- 2.40.0