diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -7524,6 +7524,9 @@ return Error(E); } + bool VisitPredefinedExpr(const PredefinedExpr *E) { + return StmtVisitorTy::Visit(E->getFunctionName()); + } bool VisitConstantExpr(const ConstantExpr *E) { if (E->hasAPValueResult()) return DerivedSuccess(E->getAPValueResult(), E); diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -1492,15 +1492,11 @@ } Decl *Sema::getCurLocalScopeDecl() { - if (const BlockScopeInfo *BSI = getCurBlock()) - return BSI->TheDecl; - if (const LambdaScopeInfo *LSI = getCurLambda()) - return LSI->CallOperator; - if (const CapturedRegionScopeInfo *CSI = getCurCapturedRegion()) - return CSI->TheCapturedDecl; - if (NamedDecl *ND = getCurFunctionOrMethodDecl()) - return ND; - return nullptr; + DeclContext *DC = CurContext; + while (DC && !isa(DC) && !isa(DC) && + !isa(DC) && !isa(DC)) + DC = DC->getParent(); + return dyn_cast_or_null(DC); } LangAS Sema::getDefaultCXXMethodAddrSpace() const { diff --git a/clang/test/Sema/ms_predefined_expr.cpp b/clang/test/Sema/ms_predefined_expr.cpp --- a/clang/test/Sema/ms_predefined_expr.cpp +++ b/clang/test/Sema/ms_predefined_expr.cpp @@ -168,3 +168,40 @@ void test_char_injection(decltype(sizeof('"')), decltype(sizeof("()"))) { unused("" __FUNCSIG__); // expected-warning{{expansion of predefined identifier '__FUNCSIG__' to a string literal is a Microsoft extension}} } + +void test_in_struct_init() { + struct { + char F[sizeof(__FUNCTION__)]; + } s1 = { __FUNCTION__ }; // expected-warning{{initializing an array from a '__FUNCTION__' predefined identifier is a Microsoft extension}} + + struct { + char F[sizeof("F:" __FUNCTION__)]; // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + } s2 = { "F:" __FUNCTION__ }; // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + + class C { + public: + struct { + char F[sizeof(__FUNCTION__)]; + } s; + } c1 = { { __FUNCTION__ } }; // expected-warning{{initializing an array from a '__FUNCTION__' predefined identifier is a Microsoft extension}} +} + +void test_in_constexpr_struct_init() { + struct { + char F[sizeof(__FUNCTION__)]; + } constexpr s1 = { __FUNCTION__ }; // expected-warning{{initializing an array from a '__FUNCTION__' predefined identifier is a Microsoft extension}} + ASSERT_EQ(__FUNCTION__, s1.F); + + struct { + char F[sizeof("F:" __FUNCTION__)]; // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + } constexpr s2 = { "F:" __FUNCTION__ }; // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + ASSERT_EQ("F:" __FUNCTION__, s2.F); // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + + class C { + public: + struct { + char F[sizeof("F:" __FUNCTION__)]; // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + } s; + } constexpr c1 = { { "F:" __FUNCTION__ } }; // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} + ASSERT_EQ("F:" __FUNCTION__, c1.s.F); // expected-warning{{expansion of predefined identifier '__FUNCTION__' to a string literal is a Microsoft extension}} +}