diff options
author | Reid Kleckner <rnk@google.com> | 2016-03-11 18:59:12 +0000 |
---|---|---|
committer | Reid Kleckner <rnk@google.com> | 2016-03-11 18:59:12 +0000 |
commit | 1af391df138f39decde88b9517c741496fed7dd8 (patch) | |
tree | 831a63363b2e6f0e02748afe31c874f3c2981765 /clang/test/SemaTemplate/instantiate-sizeof.cpp | |
parent | e5a9a275d36b20f68bc6e015f83914126004ccf8 (diff) | |
download | bcm5719-llvm-1af391df138f39decde88b9517c741496fed7dd8.tar.gz bcm5719-llvm-1af391df138f39decde88b9517c741496fed7dd8.zip |
Allow sizeof(UnrelatedClass::field) in C++11 class template methods
This feature works outside of templates by forming a DeclRefExpr to a
FieldDecl instead of a MemberExpr, which requires a base object in
addition to the FieldDecl.
Previously, while building up the template AST before instantiation, we
formed a CXXDependentScopeMemberExpr, which always instantiates to a
MemberExpr. Now, in unevaluated contexts we form a
DependentScopeDeclRefExpr, which is a more flexible node that can
instantiate to either a MemberExpr or a DeclRefExpr depending on lookup
results.
Fixes PR26893.
llvm-svn: 263279
Diffstat (limited to 'clang/test/SemaTemplate/instantiate-sizeof.cpp')
-rw-r--r-- | clang/test/SemaTemplate/instantiate-sizeof.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/clang/test/SemaTemplate/instantiate-sizeof.cpp b/clang/test/SemaTemplate/instantiate-sizeof.cpp index bf66fdc17c6..04793f785e6 100644 --- a/clang/test/SemaTemplate/instantiate-sizeof.cpp +++ b/clang/test/SemaTemplate/instantiate-sizeof.cpp @@ -1,5 +1,4 @@ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s -// expected-no-diagnostics // Make sure we handle contexts correctly with sizeof template<typename T> void f(T n) { @@ -9,3 +8,29 @@ template<typename T> void f(T n) { int main() { f<int>(1); } + +// Make sure we handle references to non-static data members in unevaluated +// contexts in class template methods correctly. Previously we assumed these +// would be valid MemberRefExprs, but they have no 'this' so we need to form a +// DeclRefExpr to the FieldDecl instead. +// PR26893 +template <class T> +struct M { + M() {}; // expected-note {{in instantiation of default member initializer 'M<S>::m' requested here}} + int m = *T::x; // expected-error {{invalid use of non-static data member 'x'}} + void f() { + // These are valid. + static_assert(sizeof(T::x) == 8, "ptr"); + static_assert(sizeof(*T::x) == 4, "int"); + } +}; +struct S { int *x; }; +template struct M<S>; // expected-note {{in instantiation of member function 'M<S>::M' requested here}} + +// Similar test case for PR26893. +template <typename T=void> +struct bar { + struct foo { int array[10]; }; + int baz() { return sizeof(foo::array); } +}; +template struct bar<>; |