Files
clang-p2996/clang/test/CodeGenCXX/vla.cpp
Richard Smith 764d2fe666 Unlike in C++03, a constant-expression is not an unevaluated operand in C++11.
Split out a new ExpressionEvaluationContext flag for this case, and don't treat
it as unevaluated in C++11. This fixes some crash-on-invalids where we would
allow references to class members in potentially-evaluated constant expressions
in static member functions, and also fixes half of PR10177.

The fix to PR10177 exposed a case where template instantiation failed to provide
a source location for a diagnostic, so TreeTransform has been tweaked to supply
source locations when transforming a type. The source location is still not very
good, but MarkDeclarationsReferencedInType would need to operate on a TypeLoc to
improve it further.

Also fix MarkDeclarationReferenced in C++98 mode to trigger instantiation for
static data members of class templates which are used in constant expressions.
This fixes a link-time problem, but we still incorrectly treat the member as
non-constant. The rest of the fix for that issue is blocked on PCH support for
early-instantiated static data members, which will be added in a subsequent
patch.

llvm-svn: 146955
2011-12-20 02:08:33 +00:00

57 lines
2.0 KiB
C++

// RUN: %clang_cc1 -triple x86_64-apple-darwin %s -emit-llvm -o - | FileCheck %s
template<typename T>
struct S {
static int n;
};
template<typename T> int S<T>::n = 5;
int f() {
// Make sure that the reference here is enough to trigger the instantiation of
// the static data member.
// CHECK: @_ZN1SIiE1nE = weak_odr global i32 5
int a[S<int>::n];
return sizeof a;
}
// rdar://problem/9506377
void test0(void *array, int n) {
// CHECK: define void @_Z5test0Pvi(
// CHECK: [[ARRAY:%.*]] = alloca i8*, align 8
// CHECK-NEXT: [[N:%.*]] = alloca i32, align 4
// CHECK-NEXT: [[REF:%.*]] = alloca i16*, align 8
// CHECK-NEXT: [[S:%.*]] = alloca i16, align 2
// CHECK-NEXT: store i8*
// CHECK-NEXT: store i32
// Capture the bounds.
// CHECK-NEXT: [[T0:%.*]] = load i32* [[N]], align 4
// CHECK-NEXT: [[DIM0:%.*]] = zext i32 [[T0]] to i64
// CHECK-NEXT: [[T0:%.*]] = load i32* [[N]], align 4
// CHECK-NEXT: [[T1:%.*]] = add nsw i32 [[T0]], 1
// CHECK-NEXT: [[DIM1:%.*]] = zext i32 [[T1]] to i64
typedef short array_t[n][n+1];
// CHECK-NEXT: [[T0:%.*]] = load i8** [[ARRAY]], align 8
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to i16*
// CHECK-NEXT: store i16* [[T1]], i16** [[REF]], align 8
array_t &ref = *(array_t*) array;
// CHECK-NEXT: [[T0:%.*]] = load i16** [[REF]]
// CHECK-NEXT: [[T1:%.*]] = mul nsw i64 1, [[DIM1]]
// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i16* [[T0]], i64 [[T1]]
// CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i16* [[T2]], i64 2
// CHECK-NEXT: store i16 3, i16* [[T3]]
ref[1][2] = 3;
// CHECK-NEXT: [[T0:%.*]] = load i16** [[REF]]
// CHECK-NEXT: [[T1:%.*]] = mul nsw i64 4, [[DIM1]]
// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds i16* [[T0]], i64 [[T1]]
// CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i16* [[T2]], i64 5
// CHECK-NEXT: [[T4:%.*]] = load i16* [[T3]]
// CHECK-NEXT: store i16 [[T4]], i16* [[S]], align 2
short s = ref[4][5];
// CHECK-NEXT: ret void
}