[Cherry-pick][BoundsSafety][Sema] Allow counted_by and counted_by_or_null on pointers where the pointee type is incomplete but potentially completable #10514
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a cherry-pick of the change in llvm#106321. It is being cherry-pick now so that the merge conflict is handled upfront to minimize the work when the conflict from the upstream change reaches the automerger.
Conflicts:
clang/include/clang/AST/Type.h
clang/include/clang/Sema/Sema.h
clang/lib/AST/Type.cpp
clang/lib/Sema/SemaBoundsSafety.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/SemaInit.cpp
Previously using the
counted_by
orcounted_by_or_null
attribute on a pointer with an incomplete pointee type was forbidden. Unfortunately this prevented a situation like the following from being allowed.Header file:
Implementation file:
To allow code like the above but still enforce that the pointee type size is known in locations where
-fbounds-safety
needs to emit bounds checks the following scheme is used.void
) these are treated as error where the attribute is written (just like before this patch).For this patch a "use" of a FieldDecl covers:
In the swift lang fork of Clang the
counted_by
andcounted_by_or_null
attribute are allowed in many more contexts. That isn't the case for upstream Clang so the "use" checks for the attribute on VarDecl, ParamVarDecl, and function return type have been omitted from this patch because they can't be tested. However, theBoundsSafetyCheckAssignmentToCountAttrPtrWithIncompletePointeeTy
andBoundsSafetyCheckUseOfCountAttrPtrWithIncompletePointeeTy
functions retain the ability to emit diagnostics for these other contexts to avoid unnecessary divergence between upstream Clang and Apple's internal fork. Support for checking "uses" will be upstreamed when upstream Clang allows thecounted_by
andcounted_by_or_null
attribute in additional contexts.This patch has a few limitations:
** 1. Tentative Defition Initialization **
This patch currently allows something like:
The Tentative definition in this example becomes an actual definition whose initialization should be checked but it currently isn't. Addressing this problem will be done in a subseqent patch.
** 2. When the incomplete pointee type is a typedef diagnostics are slightly misleading **
For this situation:
This code emits
note: forward declaration of 'Incomplete_t' (aka 'struct IncompleteTy')
but the location is on thestruct IncompleteTy;
forward declaration. This is misleading becauseIncomplete_t
isn't actually forward declared there (instead the underlying type is). This could be resolved by additional diagnostics that walk the chain of typedefs and explain each step of the walk. However, that would be very verbose and didn't seem like a direction worth pursuing.rdar://133600117