Skip to content

Commit 9858ef7

Browse files
committed
test various nested templated contexts and fix bugs
1 parent 93673b4 commit 9858ef7

File tree

6 files changed

+486
-47
lines changed

6 files changed

+486
-47
lines changed

clang/lib/Sema/DynamicCountPointerAssignmentAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,8 +3484,8 @@ namespace clang {
34843484

34853485
void DynamicCountPointerAssignmentAnalysis::run() {
34863486
AnalysisDeclContext AC(/* AnalysisDeclContextManager */ nullptr, dcl);
3487-
if (isa<TemplateDecl>(dcl)) // delay processing until template has been
3488-
// instantiated
3487+
if (dcl->isTemplated()) // delay processing until template has been
3488+
// instantiated
34893489
return;
34903490

34913491
CFG *cfg = AC.getCFG();

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17302,6 +17302,11 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1730217302
// the declaration context below. Otherwise, we're unable to transform
1730317303
// 'this' expressions when transforming immediate context functions.
1730417304

17305+
/*TO_UPSTREAM(BoundsSafety) ON*/
17306+
if (LangOpts.BoundsSafety)
17307+
DynamicCountPointerAssignmentAnalysis(*this, dcl).run();
17308+
/*TO_UPSTREAM(BoundsSafety) OFF*/
17309+
1730517310
if (!IsInstantiation)
1730617311
PopDeclContext();
1730717312

@@ -17325,11 +17330,6 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1732517330
if (FD && !FD->isDeleted())
1732617331
checkTypeSupport(FD->getType(), FD->getLocation(), FD);
1732717332

17328-
/*TO_UPSTREAM(BoundsSafety) ON*/
17329-
if (LangOpts.BoundsSafety)
17330-
DynamicCountPointerAssignmentAnalysis(*this, dcl).run();
17331-
/*TO_UPSTREAM(BoundsSafety) OFF*/
17332-
1733317333
return dcl;
1733417334
}
1733517335

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6751,7 +6751,7 @@ static void handlePtrCountedByEndedByAttr(Sema &S, Decl *D,
67516751
if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(1), Level))
67526752
return;
67536753

6754-
if (D->getDescribedTemplate() || S.CurContext->isDependentContext()) {
6754+
if (D->isTemplated()) {
67556755
DynamicBoundsAttrInfo Info(D, Level);
67566756
// Scope information will be invalid by the time we instantiate the
67576757
// template, so perform these checks now and delay the rest of the

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,20 @@ instantiateBoundsSafetyAttr(Sema &S,
674674
} else
675675
llvm_unreachable("unexpected late instantiated bounds safety attribute");
676676

677+
DeclContext *DC;
678+
if (auto *FD = dyn_cast<FunctionDecl>(New)) {
679+
DC = FD; // make sure we can refer to parameters in attribute
680+
} else {
681+
DC = New->getDeclContext();
682+
}
683+
Sema::ContextRAII SetContext(S, DC);
684+
if (auto *RD = dyn_cast<CXXRecordDecl>(DC)) {
685+
// Set `this` type as if we were in a decl scope lambda
686+
// It's reset along with CurContext when SetContext goes out of scope
687+
QualType ClassTy = S.Context.getTypeDeclType(RD);
688+
S.CXXThisTypeOverride = S.Context.getPointerType(ClassTy);
689+
}
690+
677691
ExprResult InstantiatedArgExpr = S.SubstExpr(ArgExpr, TemplateArgs);
678692
if (InstantiatedArgExpr.isInvalid())
679693
return;

0 commit comments

Comments
 (0)