Skip to content

[BoundsSafety] Delay processing of bounds attrs in templates #10730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

delcypher
Copy link

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)

This is a cherry-pick of change that was landed to `stable/20240723`
(swiftlang#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)
@delcypher delcypher self-assigned this May 22, 2025
@delcypher delcypher added the clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang label May 22, 2025
@delcypher
Copy link
Author

@swift-ci test llvm

@delcypher
Copy link
Author

@swift-ci test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants