r263279 - Allow sizeof(UnrelatedClass::field) in C++11 class template methods
Reid Kleckner via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 11 10:59:13 PST 2016
Author: rnk
Date: Fri Mar 11 12:59:12 2016
New Revision: 263279
URL: http://llvm.org/viewvc/llvm-project?rev=263279&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Sema/SemaTemplate.cpp
cfe/trunk/test/SemaTemplate/instantiate-sizeof.cpp
Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=263279&r1=263278&r2=263279&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Mar 11 12:59:12 2016
@@ -414,9 +414,22 @@ Sema::ActOnDependentIdExpression(const C
const TemplateArgumentListInfo *TemplateArgs) {
DeclContext *DC = getFunctionLevelDeclContext();
- if (!isAddressOfOperand &&
- isa<CXXMethodDecl>(DC) &&
- cast<CXXMethodDecl>(DC)->isInstance()) {
+ // C++11 [expr.prim.general]p12:
+ // An id-expression that denotes a non-static data member or non-static
+ // member function of a class can only be used:
+ // (...)
+ // - if that id-expression denotes a non-static data member and it
+ // appears in an unevaluated operand.
+ //
+ // If this might be the case, form a DependentScopeDeclRefExpr instead of a
+ // CXXDependentScopeMemberExpr. The former can instantiate to either
+ // DeclRefExpr or MemberExpr depending on lookup results, while the latter is
+ // always a MemberExpr.
+ bool MightBeCxx11UnevalField =
+ getLangOpts().CPlusPlus11 && isUnevaluatedContext();
+
+ if (!MightBeCxx11UnevalField && !isAddressOfOperand &&
+ isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) {
QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
// Since the 'this' expression is synthesized, we don't need to
Modified: cfe/trunk/test/SemaTemplate/instantiate-sizeof.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-sizeof.cpp?rev=263279&r1=263278&r2=263279&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-sizeof.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-sizeof.cpp Fri Mar 11 12:59:12 2016
@@ -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<>;
More information about the cfe-commits
mailing list