r200899 - Allow transformation of VariableArray to ConstantArray.

Serge Pavlov sepavloff at gmail.com
Wed Feb 5 19:49:11 PST 2014


Author: sepavloff
Date: Wed Feb  5 21:49:11 2014
New Revision: 200899

URL: http://llvm.org/viewvc/llvm-project?rev=200899&view=rev
Log:
Allow transformation of VariableArray to ConstantArray.

In the following code:

    struct A { static const int sz; };
    template<class T> 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

Modified:
    cfe/trunk/lib/Sema/TreeTransform.h
    cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=200899&r1=200898&r2=200899&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Wed Feb  5 21:49:11 2014
@@ -3929,7 +3929,9 @@ TreeTransform<Derived>::TransformVariabl
       return QualType();
   }
 
-  VariableArrayTypeLoc NewTL = TLB.push<VariableArrayTypeLoc>(Result);
+  // We might have constant size array now, but fortunately it has the same
+  // location layout.
+  ArrayTypeLoc NewTL = TLB.push<ArrayTypeLoc>(Result);
   NewTL.setLBracketLoc(TL.getLBracketLoc());
   NewTL.setRBracketLoc(TL.getRBracketLoc());
   NewTL.setSizeExpr(Size);

Modified: cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp?rev=200899&r1=200898&r2=200899&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp (original)
+++ cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp Wed Feb  5 21:49:11 2014
@@ -140,3 +140,24 @@ namespace PR11744 {
   }
   int test = f<int>(0); // expected-note {{instantiation of}}
 }
+
+namespace pr18633 {
+  struct A1 {
+    static const int sz;
+    static const int sz2;
+  };
+  const int A1::sz2 = 11;
+  template<typename T>
+  void func () {
+    int arr[A1::sz]; // expected-warning{{variable length arrays are a C99 feature}}
+  }
+  template<typename T>
+  void func2 () {
+    int arr[A1::sz2];
+  }
+  const int A1::sz = 12;
+  void func2() {
+    func<int>();
+    func2<int>();
+  }
+}





More information about the cfe-commits mailing list