Skip to content

Commit 15dde87

Browse files
authored
Update SemaLambda.cpp
1 parent e2adf7c commit 15dde87

File tree

1 file changed

+42
-119
lines changed

1 file changed

+42
-119
lines changed

clang/lib/Sema/SemaLambda.cpp

Lines changed: 42 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
#include "clang/AST/CXXInheritance.h"
1616
#include "clang/AST/ExprCXX.h"
1717
#include "clang/AST/MangleNumberingContext.h"
18-
#include "clang/AST/Type.h"
1918
#include "clang/Basic/TargetInfo.h"
20-
#include "clang/Lex/Lexer.h"
2119
#include "clang/Sema/DeclSpec.h"
2220
#include "clang/Sema/Initialization.h"
2321
#include "clang/Sema/Lookup.h"
@@ -30,7 +28,6 @@
3028
#include "clang/Sema/SemaSYCL.h"
3129
#include "clang/Sema/Template.h"
3230
#include "llvm/ADT/STLExtras.h"
33-
3431
#include <optional>
3532
using namespace clang;
3633
using namespace sema;
@@ -1189,28 +1186,15 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
11891186
CheckCXXThisCapture(C->Loc, /*Explicit=*/true, /*BuildAndDiagnose*/ true,
11901187
/*FunctionScopeIndexToStopAtPtr*/ nullptr,
11911188
C->Kind == LCK_StarThis);
1192-
if (!LSI->Captures.empty()) {
1193-
SourceManager &SourceMgr = Context.getSourceManager();
1194-
const LangOptions &LangOpts = Context.getLangOpts();
1195-
SourceRange TrimmedRange =
1196-
Lexer::makeFileCharRange(
1197-
CharSourceRange::getTokenRange(C->ExplicitRange), SourceMgr,
1198-
LangOpts)
1199-
.getAsRange();
1200-
LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = TrimmedRange;
1201-
}
1202-
continue; // skip further processing for `this` and `*this` captures.
1203-
}
1204-
1205-
if (!C->Id) {
1206-
Diag(C->Loc, diag::err_expected_identifier_for_lambda_capture);
1189+
if (!LSI->Captures.empty())
1190+
LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
12071191
continue;
12081192
}
12091193

1210-
if (C->Init.isInvalid()) {
1211-
Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
1194+
assert(C->Id && "missing identifier for capture");
1195+
1196+
if (C->Init.isInvalid())
12121197
continue;
1213-
}
12141198

12151199
ValueDecl *Var = nullptr;
12161200
if (C->Init.isUsable()) {
@@ -1223,57 +1207,12 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
12231207
// for e.g., [n{0}] { }; <-- if no <initializer_list> is included.
12241208
// FIXME: we should create the init capture variable and mark it invalid
12251209
// in this case.
1226-
// Ensure the initialization is valid before proceeding
1227-
1228-
if (!C->InitCaptureType || C->InitCaptureType.get().isNull()) {
1229-
if (!C->Init.isUsable()) {
1230-
Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
1231-
continue;
1232-
}
1233-
1234-
if (!C->Init.get())
1235-
continue;
1236-
1237-
ASTContext &Ctx = this->Context;
1238-
QualType DeducedType = C->Init.get()->getType();
1239-
1240-
if (DeducedType.isNull())
1241-
continue;
1242-
1243-
if (DeducedType->isVoidType()) {
1244-
if (!DeducedType->isDependentType()) {
1245-
C->InitCaptureType = ParsedType::make(Ctx.DependentTy);
1246-
} else {
1247-
Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
1248-
}
1249-
continue;
1250-
}
1251-
1252-
if (isa<InitListExpr>(C->Init.get())) {
1253-
IdentifierInfo *DummyID = &Ctx.Idents.get("lambda_tmp_var");
1254-
QualType DummyType = Ctx.UnknownAnyTy;
1255-
1256-
auto *TempVarDecl =
1257-
VarDecl::Create(Ctx, nullptr, C->Loc, C->Loc, DummyID, DummyType,
1258-
nullptr, SC_None);
1259-
1260-
if (!TempVarDecl)
1261-
continue;
1262-
1263-
DeducedType = deduceVarTypeFromInitializer(
1264-
TempVarDecl, TempVarDecl->getDeclName(), TempVarDecl->getType(),
1265-
nullptr, TempVarDecl->getSourceRange(), false, C->Init.get());
1266-
1267-
if (DeducedType.isNull()) {
1268-
Diag(C->Loc, diag::err_invalid_lambda_capture_initializer_type);
1269-
C->InitCaptureType = ParsedType::make(Ctx.DependentTy);
1270-
continue;
1271-
}
1272-
}
1210+
if (C->InitCaptureType.get().isNull())
1211+
continue;
12731212

1274-
if (!DeducedType.isNull())
1275-
C->InitCaptureType = ParsedType::make(DeducedType);
1276-
}
1213+
if (C->Init.get()->containsUnexpandedParameterPack() &&
1214+
!C->InitCaptureType.get()->getAs<PackExpansionType>())
1215+
DiagnoseUnexpandedParameterPack(C->Init.get(), UPPC_Initializer);
12771216

12781217
unsigned InitStyle;
12791218
switch (C->InitKind) {
@@ -1308,12 +1247,12 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
13081247
if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) {
13091248
Diag(C->Loc, diag::err_reference_capture_with_reference_default)
13101249
<< FixItHint::CreateRemoval(
1311-
SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1250+
SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
13121251
continue;
13131252
} else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) {
13141253
Diag(C->Loc, diag::err_copy_capture_with_copy_default)
13151254
<< FixItHint::CreateRemoval(
1316-
SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1255+
SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
13171256
continue;
13181257
}
13191258

@@ -1413,16 +1352,8 @@ void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
14131352
: TryCaptureKind::ExplicitByVal;
14141353
tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc);
14151354
}
1416-
if (!LSI->Captures.empty()) {
1417-
SourceManager &SourceMgr = Context.getSourceManager();
1418-
const LangOptions &LangOpts = Context.getLangOpts();
1419-
SourceRange TrimmedRange =
1420-
Lexer::makeFileCharRange(
1421-
CharSourceRange::getTokenRange(C->ExplicitRange), SourceMgr,
1422-
LangOpts)
1423-
.getAsRange();
1424-
LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = TrimmedRange;
1425-
}
1355+
if (!LSI->Captures.empty())
1356+
LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
14261357
}
14271358
finishLambdaExplicitCaptures(LSI);
14281359
LSI->ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
@@ -1702,8 +1633,8 @@ static void repeatForLambdaConversionFunctionCallingConvs(
17021633
CC_C, CC_X86StdCall, CC_X86FastCall, CC_X86VectorCall,
17031634
DefaultFree, DefaultMember, CallOpCC};
17041635
llvm::sort(Convs);
1705-
llvm::iterator_range<CallingConv *> Range(
1706-
std::begin(Convs), std::unique(std::begin(Convs), std::end(Convs)));
1636+
llvm::iterator_range<CallingConv *> Range(std::begin(Convs),
1637+
llvm::unique(Convs));
17071638
const TargetInfo &TI = S.getASTContext().getTargetInfo();
17081639

17091640
for (CallingConv C : Range) {
@@ -1781,9 +1712,9 @@ static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
17811712
QualType ConvTy = S.Context.getFunctionType(PtrToFunctionTy, {}, ConvExtInfo);
17821713

17831714
SourceLocation Loc = IntroducerRange.getBegin();
1784-
DeclarationName ConversionName
1715+
DeclarationName ConversionName
17851716
= S.Context.DeclarationNames.getCXXConversionFunctionName(
1786-
S.Context.getCanonicalType(PtrToFunctionTy));
1717+
S.Context.getCanonicalType(PtrToFunctionTy));
17871718
// Construct a TypeSourceInfo for the conversion function, and wire
17881719
// all the parameters appropriately for the FunctionProtoTypeLoc
17891720
// so that everything works during transformation/instantiation of
@@ -1822,7 +1753,7 @@ static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
18221753
FunctionProtoTypeLoc CallOpConvTL =
18231754
PtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
18241755
FunctionProtoTypeLoc CallOpConvNameTL =
1825-
ConvNamePtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
1756+
ConvNamePtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
18261757

18271758
// Wire up the FunctionProtoTypeLocs with the call operator's parameters.
18281759
// These parameter's are essentially used to transform the name and
@@ -1947,9 +1878,9 @@ static void addFunctionPointerConversions(Sema &S, SourceRange IntroducerRange,
19471878

19481879
/// Add a lambda's conversion to block pointer.
19491880
static void addBlockPointerConversion(Sema &S,
1950-
SourceRange IntroducerRange,
1951-
CXXRecordDecl *Class,
1952-
CXXMethodDecl *CallOperator) {
1881+
SourceRange IntroducerRange,
1882+
CXXRecordDecl *Class,
1883+
CXXMethodDecl *CallOperator) {
19531884
const FunctionProtoType *CallOpProto =
19541885
CallOperator->getType()->castAs<FunctionProtoType>();
19551886
QualType FunctionTy = S.getLambdaConversionFunctionResultType(
@@ -1965,8 +1896,8 @@ static void addBlockPointerConversion(Sema &S,
19651896

19661897
SourceLocation Loc = IntroducerRange.getBegin();
19671898
DeclarationName Name
1968-
= S.Context.DeclarationNames.getCXXConversionFunctionName(
1969-
S.Context.getCanonicalType(BlockPtrTy));
1899+
= S.Context.DeclarationNames.getCXXConversionFunctionName(
1900+
S.Context.getCanonicalType(BlockPtrTy));
19701901
DeclarationNameLoc NameLoc = DeclarationNameLoc::makeNamedTypeLoc(
19711902
S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc));
19721903
CXXConversionDecl *Conversion = CXXConversionDecl::Create(
@@ -2023,7 +1954,7 @@ ExprResult Sema::BuildCaptureInit(const Capture &Cap,
20231954
ValueDecl *Var = Cap.getVariable();
20241955
Name = Var->getIdentifier();
20251956
Init = BuildDeclarationNameExpr(
2026-
CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var);
1957+
CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var);
20271958
}
20281959

20291960
// In OpenMP, the capture kind doesn't actually describe how to capture:
@@ -2038,7 +1969,8 @@ ExprResult Sema::BuildCaptureInit(const Capture &Cap,
20381969
Expr *InitExpr = Init.get();
20391970
InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture(
20401971
Name, Cap.getCaptureType(), Loc);
2041-
InitializationKind InitKind = InitializationKind::CreateDirect(Loc, Loc, Loc);
1972+
InitializationKind InitKind =
1973+
InitializationKind::CreateDirect(Loc, Loc, Loc);
20421974
InitializationSequence InitSeq(*this, Entity, InitKind, InitExpr);
20431975
return InitSeq.Perform(*this, Entity, InitKind, InitExpr);
20441976
}
@@ -2209,8 +2141,8 @@ ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
22092141

22102142
// True if the current capture has a used capture or default before it.
22112143
bool CurHasPreviousCapture = CaptureDefault != LCD_None;
2212-
SourceLocation PrevCaptureLoc =
2213-
CurHasPreviousCapture ? CaptureDefaultLoc : IntroducerRange.getBegin();
2144+
SourceLocation PrevCaptureLoc = CurHasPreviousCapture ?
2145+
CaptureDefaultLoc : IntroducerRange.getBegin();
22142146

22152147
for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
22162148
const Capture &From = LSI->Captures[I];
@@ -2227,34 +2159,26 @@ ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
22272159
SourceRange CaptureRange = LSI->ExplicitCaptureRanges[I];
22282160

22292161
// Warn about unused explicit captures.
2230-
22312162
bool IsCaptureUsed = true;
2232-
22332163
if (!CurContext->isDependentContext() && !IsImplicit &&
22342164
!From.isODRUsed()) {
2235-
// Handle non-ODR used init captures separately.
2165+
// Initialized captures that are non-ODR used may not be eliminated.
2166+
// FIXME: Where did the IsGenericLambda here come from?
22362167
bool NonODRUsedInitCapture =
22372168
IsGenericLambda && From.isNonODRUsed() && From.isInitCapture();
2238-
22392169
if (!NonODRUsedInitCapture) {
22402170
bool IsLast = (I + 1) == LSI->NumExplicitCaptures;
22412171
SourceRange FixItRange;
2242-
22432172
if (CaptureRange.isValid()) {
22442173
if (!CurHasPreviousCapture && !IsLast) {
2245-
// No previous capture and not the last capture: remove current
2246-
// and next comma.
2247-
FixItRange =
2248-
SourceRange(CaptureRange.getBegin(),
2249-
getLocForEndOfToken(CaptureRange.getEnd()));
2250-
} else if (CurHasPreviousCapture && !IsLast) {
2251-
// Previous capture exists and not the last: remove current and
2252-
// preceding comma.
2174+
// If there are no captures preceding this capture, remove the
2175+
// following comma.
2176+
FixItRange = SourceRange(CaptureRange.getBegin(),
2177+
getLocForEndOfToken(CaptureRange.getEnd()));
2178+
} else {
2179+
// Otherwise, remove the comma since the last used capture.
22532180
FixItRange = SourceRange(getLocForEndOfToken(PrevCaptureLoc),
22542181
CaptureRange.getEnd());
2255-
} else if (CurHasPreviousCapture && IsLast) {
2256-
// Last capture: remove only the current capture.
2257-
FixItRange = CaptureRange;
22582182
}
22592183
}
22602184

@@ -2445,12 +2369,11 @@ ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
24452369
// to any actual memory location. However, the initializer copy-initializes
24462370
// the lambda object.
24472371
TypeSourceInfo *CapVarTSI =
2448-
Context.getTrivialTypeSourceInfo(Src->getType());
2449-
VarDecl *CapVar =
2450-
VarDecl::Create(Context, Block, ConvLocation,
2451-
ConvLocation, nullptr,
2452-
Src->getType(), CapVarTSI,
2453-
SC_None);
2372+
Context.getTrivialTypeSourceInfo(Src->getType());
2373+
VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation,
2374+
ConvLocation, nullptr,
2375+
Src->getType(), CapVarTSI,
2376+
SC_None);
24542377
BlockDecl::Capture Capture(/*variable=*/CapVar, /*byRef=*/false,
24552378
/*nested=*/false, /*copy=*/Init.get());
24562379
Block->setCaptures(Context, Capture, /*CapturesCXXThis=*/false);

0 commit comments

Comments
 (0)