From 2ee14504678565a83ad74c0e85f8cb58abb9ffe3 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 28 May 2019 23:09:42 +0000 Subject: [PATCH] Move code to mark a variable as odr-used adjacement to all the related code. No functional change intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@361890 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 2 ++ include/clang/Sema/SemaInternal.h | 30 -------------------------- lib/Sema/SemaExpr.cpp | 36 +++++++++++++++++++++++++++++++ lib/Sema/SemaExprCXX.cpp | 8 +++---- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index a1a7f5f6ea..db6435461e 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -4165,6 +4165,8 @@ public: void MarkVariableReferenced(SourceLocation Loc, VarDecl *Var); void MarkDeclRefReferenced(DeclRefExpr *E, const Expr *Base = nullptr); void MarkMemberReferenced(MemberExpr *E); + void MarkCaptureUsedInEnclosingContext(VarDecl *Capture, SourceLocation Loc, + unsigned CapturingScopeIndex); void UpdateMarkingForLValueToRValue(Expr *E); void CleanupVarDeclMarking(); diff --git a/include/clang/Sema/SemaInternal.h b/include/clang/Sema/SemaInternal.h index 07e633cab8..dfeca60349 100644 --- a/include/clang/Sema/SemaInternal.h +++ b/include/clang/Sema/SemaInternal.h @@ -59,36 +59,6 @@ inline bool DeclAttrsMatchCUDAMode(const LangOptions &LangOpts, Decl *D) { return isDeviceSideDecl == LangOpts.CUDAIsDevice; } -// Directly mark a variable odr-used. Given a choice, prefer to use -// MarkVariableReferenced since it does additional checks and then -// calls MarkVarDeclODRUsed. -// If the variable must be captured: -// - if FunctionScopeIndexToStopAt is null, capture it in the CurContext -// - else capture it in the DeclContext that maps to the -// *FunctionScopeIndexToStopAt on the FunctionScopeInfo stack. -inline void MarkVarDeclODRUsed(VarDecl *Var, - SourceLocation Loc, Sema &SemaRef, - const unsigned *const FunctionScopeIndexToStopAt) { - // Keep track of used but undefined variables. - // FIXME: We shouldn't suppress this warning for static data members. - if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && - (!Var->isExternallyVisible() || Var->isInline() || - SemaRef.isExternalWithNoLinkageType(Var)) && - !(Var->isStaticDataMember() && Var->hasInit())) { - SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()]; - if (old.isInvalid()) - old = Loc; - } - QualType CaptureType, DeclRefType; - SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, - /*EllipsisLoc*/ SourceLocation(), - /*BuildAndDiagnose*/ true, - CaptureType, DeclRefType, - FunctionScopeIndexToStopAt); - - Var->markUsed(SemaRef.Context); -} - /// Return a DLL attribute from the declaration. inline InheritableAttr *getDLLAttr(Decl *D) { assert(!(D->hasAttr() && D->hasAttr()) && diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 3a12c2dd84..cc3dea9ead 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -15013,6 +15013,42 @@ void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func, } } +/// Directly mark a variable odr-used. Given a choice, prefer to use +/// MarkVariableReferenced since it does additional checks and then +/// calls MarkVarDeclODRUsed. +/// If the variable must be captured: +/// - if FunctionScopeIndexToStopAt is null, capture it in the CurContext +/// - else capture it in the DeclContext that maps to the +/// *FunctionScopeIndexToStopAt on the FunctionScopeInfo stack. +static void +MarkVarDeclODRUsed(VarDecl *Var, SourceLocation Loc, Sema &SemaRef, + const unsigned *const FunctionScopeIndexToStopAt) { + // Keep track of used but undefined variables. + // FIXME: We shouldn't suppress this warning for static data members. + if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && + (!Var->isExternallyVisible() || Var->isInline() || + SemaRef.isExternalWithNoLinkageType(Var)) && + !(Var->isStaticDataMember() && Var->hasInit())) { + SourceLocation &old = SemaRef.UndefinedButUsed[Var->getCanonicalDecl()]; + if (old.isInvalid()) + old = Loc; + } + QualType CaptureType, DeclRefType; + SemaRef.tryCaptureVariable(Var, Loc, Sema::TryCapture_Implicit, + /*EllipsisLoc*/ SourceLocation(), + /*BuildAndDiagnose*/ true, + CaptureType, DeclRefType, + FunctionScopeIndexToStopAt); + + Var->markUsed(SemaRef.Context); +} + +void Sema::MarkCaptureUsedInEnclosingContext(VarDecl *Capture, + SourceLocation Loc, + unsigned CapturingScopeIndex) { + MarkVarDeclODRUsed(Capture, Loc, *this, &CapturingScopeIndex); +} + static void diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, ValueDecl *var, DeclContext *DC) { diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index e3286e8943..ef27fc2d71 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -7495,11 +7495,9 @@ static void CheckIfAnyEnclosingLambdasMustCaptureAnyPotentialCaptures( // capture the variable in that lambda (and all its enclosing lambdas). if (const Optional Index = getStackIndexOfNearestEnclosingCaptureCapableLambda( - S.FunctionScopes, Var, S)) { - const unsigned FunctionScopeIndexOfCapturableLambda = Index.getValue(); - MarkVarDeclODRUsed(Var, VarExpr->getExprLoc(), S, - &FunctionScopeIndexOfCapturableLambda); - } + S.FunctionScopes, Var, S)) + S.MarkCaptureUsedInEnclosingContext(Var, VarExpr->getExprLoc(), + Index.getValue()); const bool IsVarNeverAConstantExpression = VariableCanNeverBeAConstantExpression(Var, S.Context); if (!IsFullExprInstantiationDependent || IsVarNeverAConstantExpression) { -- 2.40.0