Skip to content

Commit cc30ed9

Browse files
hnrklssndelcypher
authored andcommitted
[BoundsSafety] Delay processing of bounds attrs in templates
This is a cherry-pick of change that was landed to `stable/20240723` (#9929) but not landed on the `next` branch. Aside from resolving merge conflicts it was also necessary to modify the `clang/test/BoundsSafety/(AST|Sema)/value-dependence.cpp` tests because they fail to work when the new bounds checks are enabled (rdar://150044760). To workaround that the test has been made to disable the new `return_size` bounds check when `-fbounds-safety` is enabled with C++. --- * [BoundsSafety] Delay processing of bounds attrs in templates Dynamic bounds attributes do not handle value dependent arguments. To enable wider interop with C++ code bases we delay the processing of these attributes inside templated contexts. Instead we apply the new type while instantiating the function. One issue with this approach is that instantiation happens after parsing is finished, and the Scope information we use to prevent attributes from referring to arguments from outer scopes is only available during parsing. These diagnostics were emitted at the end of the type processing. In templated contexts we instead perform that analysis during parsing, and delay the rest of the processing. To avoid churn in unrelated tests the rest of the attributes keep their processing as is, until we've investigated what impact hoisting it would have on the user experience. Conflicts: clang/include/clang/Sema/Sema.h clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaTemplateInstantiateDecl.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-count-attributed-pointer-argument.cpp rdar://150694971 (cherry picked from commit c87bc75)
1 parent 1af32e0 commit cc30ed9

11 files changed

+981
-71
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,8 @@ class Sema final : public SemaBase {
27922792
AttributeCommonInfo::Kind Kind,
27932793
Expr *AttrArg, SourceLocation Loc,
27942794
SourceRange Range, StringRef DiagName,
2795-
bool OriginatesInAPINotes = false);
2795+
bool OriginatesInAPINotes = false,
2796+
bool InInstantiatedTemplate = false);
27962797
/* TO_UPSTREAM(BoundsSafety) OFF*/
27972798

27982799

clang/lib/Sema/BoundsSafetySuggestions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,10 @@ void UnsafeOperationVisitor::
15941594
Current = PE->getSubExpr();
15951595
continue;
15961596
}
1597+
if (const auto *BCE = dyn_cast<BoundsCheckExpr>(Current)) {
1598+
Current = BCE->getGuardedExpr();
1599+
continue;
1600+
}
15971601
if (const auto *CE = dyn_cast<CastExpr>(Current)) {
15981602
if (CE->getCastKind() == clang::CK_BoundsSafetyPointerCast) {
15991603
// Found a cast we might want to warn about.

clang/lib/Sema/DynamicCountPointerAssignmentAnalysis.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,6 +3485,9 @@ namespace clang {
34853485

34863486
void DynamicCountPointerAssignmentAnalysis::run() {
34873487
AnalysisDeclContext AC(/* AnalysisDeclContextManager */ nullptr, dcl);
3488+
if (dcl->isTemplated()) // delay processing until template has been
3489+
// instantiated
3490+
return;
34883491

34893492
CFG *cfg = AC.getCFG();
34903493
if (!cfg)

clang/lib/Sema/SemaDecl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17764,6 +17764,11 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1776417764
if (FD)
1776517765
CheckImmediateEscalatingFunctionDefinition(FD, getCurFunction());
1776617766

17767+
/*TO_UPSTREAM(BoundsSafety) ON*/
17768+
if (LangOpts.BoundsSafety)
17769+
DynamicCountPointerAssignmentAnalysis(*this, dcl).run();
17770+
/*TO_UPSTREAM(BoundsSafety) OFF*/
17771+
1776717772
if (!IsInstantiation)
1776817773
PopDeclContext();
1776917774

@@ -17786,11 +17791,6 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt *Body,
1778617791
if (FD && !FD->isDeleted())
1778717792
checkTypeSupport(FD->getType(), FD->getLocation(), FD);
1778817793

17789-
/*TO_UPSTREAM(BoundsSafety) ON*/
17790-
if (LangOpts.BoundsSafety)
17791-
DynamicCountPointerAssignmentAnalysis(*this, dcl).run();
17792-
/*TO_UPSTREAM(BoundsSafety) OFF*/
17793-
1779417794
return dcl;
1779517795
}
1779617796

0 commit comments

Comments
 (0)