Author: hans Date: Thu Aug 10 18:00:59 2017 New Revision: 310674 URL: http://llvm.org/viewvc/llvm-project?rev=310674&view=rev Log: Merging r309569: ------------------------------------------------------------------------ r309569 | alexfh | 2017-07-31 08:21:26 -0700 (Mon, 31 Jul 2017) | 39 lines
Fix -Wshadow false positives with function-local classes. Summary: Fixes http://llvm.org/PR33947. https://godbolt.org/g/54XRMT void f(int a) { struct A { void g(int a) {} A() { int a; } }; } 3 : <source>:3:16: warning: declaration shadows a local variable [-Wshadow] void g(int a) {} ^ 1 : <source>:1:12: note: previous declaration is here void f(int a) { ^ 4 : <source>:4:15: warning: declaration shadows a local variable [-Wshadow] A() { int a; } ^ 1 : <source>:1:12: note: previous declaration is here void f(int a) { ^ 2 warnings generated. The local variable `a` of the function `f` can't be accessed from a method of the function-local class A, thus no shadowing occurs and no diagnostic is needed. Reviewers: rnk, rsmith, arphaman, Quuxplusone Reviewed By: rnk, Quuxplusone Subscribers: Quuxplusone, cfe-commits Differential Revision: https://reviews.llvm.org/D35941 ------------------------------------------------------------------------ Modified: cfe/branches/release_50/ (props changed) cfe/branches/release_50/lib/Sema/SemaDecl.cpp cfe/branches/release_50/test/SemaCXX/warn-shadow.cpp Propchange: cfe/branches/release_50/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Thu Aug 10 18:00:59 2017 @@ -1,4 +1,4 @@ /cfe/branches/type-system-rewrite:134693-134817 -/cfe/trunk:308455,308722,308824,308897,308996,309058,309112-309113,309226,309263,309327,309382-309383,309488,309503,309523,309722,309752,309975,310057,310158,310191,310359 +/cfe/trunk:308455,308722,308824,308897,308996,309058,309112-309113,309226,309263,309327,309382-309383,309488,309503,309523,309569,309722,309752,309975,310057,310158,310191,310359 /cfe/trunk/test:170344 /cfe/trunk/test/SemaTemplate:126920 Modified: cfe/branches/release_50/lib/Sema/SemaDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_50/lib/Sema/SemaDecl.cpp?rev=310674&r1=310673&r2=310674&view=diff ============================================================================== --- cfe/branches/release_50/lib/Sema/SemaDecl.cpp (original) +++ cfe/branches/release_50/lib/Sema/SemaDecl.cpp Thu Aug 10 18:00:59 2017 @@ -6999,6 +6999,21 @@ void Sema::CheckShadow(NamedDecl *D, Nam return; } } + + if (cast<VarDecl>(ShadowedDecl)->hasLocalStorage()) { + // A variable can't shadow a local variable in an enclosing scope, if + // they are separated by a non-capturing declaration context. + for (DeclContext *ParentDC = NewDC; + ParentDC && !ParentDC->Equals(OldDC); + ParentDC = getLambdaAwareParentOfDeclContext(ParentDC)) { + // Only block literals, captured statements, and lambda expressions + // can capture; other scopes don't. + if (!isa<BlockDecl>(ParentDC) && !isa<CapturedDecl>(ParentDC) && + !isLambdaCallOperator(ParentDC)) { + return; + } + } + } } } Modified: cfe/branches/release_50/test/SemaCXX/warn-shadow.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_50/test/SemaCXX/warn-shadow.cpp?rev=310674&r1=310673&r2=310674&view=diff ============================================================================== --- cfe/branches/release_50/test/SemaCXX/warn-shadow.cpp (original) +++ cfe/branches/release_50/test/SemaCXX/warn-shadow.cpp Thu Aug 10 18:00:59 2017 @@ -213,3 +213,12 @@ typedef int externC; // expected-note {{ void handleLinkageSpec() { typedef void externC; // expected-warning {{declaration shadows a typedef in the global namespace}} } + +namespace PR33947 { +void f(int a) { + struct A { + void g(int a) {} + A() { int a; } + }; +} +} _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits