Skip to content

Commit

Permalink
[BoundsSafety] Do not check for bounds-attr output arguments in depen…
Browse files Browse the repository at this point in the history
…dent contexts (#9733)

The bounds-attribute-only mode may be applied to C++ programs, where
attributed functions/fields may be used in dependent contexts such as
a template.  DO NOT type check at those places.  Their instantiations
will still be checked.

(rdar://141708643)

(cherry picked from commit d4329b6)
  • Loading branch information
ziqingluo-90 committed Dec 19, 2024
1 parent 1a588e5 commit 37480ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/DependenceFlags.h"
#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
Expand Down Expand Up @@ -8251,7 +8252,7 @@ static bool checkDynamicCountPointerAsParameter(Sema &S, FunctionDecl *FDecl,
if (!ArgInfo.isCountInParamOrCountPointer() && !ArgInfo.isCountInRet() &&
!ParmInfo.isCountInParamOrCountPointer() && !ParmInfo.isCountInRet())
continue;
assert(!ActualArgExp->isValueDependent());

// Disable these checks for attribute-only mode because we don't want
// non-type-incompatibility errors in that mode.
if (!S.getLangOpts().isBoundsSafetyAttributeOnlyMode() &&
Expand Down Expand Up @@ -8822,7 +8823,10 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl,
if (getLangOpts().BoundsSafetyAttributes && FDecl) {
// FIXME: We need to support function pointers and blocks that don't have
// function decl.
if (!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))

// For C++, we don't want to check for any dependent constructs.
if (TheCall->getDependence() == ExprDependence::None &&
!checkDynamicCountPointerAsParameter(*this, FDecl, TheCall))
return ExprError();
if (getLangOpts().BoundsSafety)
if (!checkDynamicRangePointerAsParameter(*this, FDecl, TheCall))
Expand Down Expand Up @@ -26016,4 +26020,4 @@ ExprResult Sema::ActOnUnsafeTerminatedByFromIndexable(
PointerToTerminatorExpr, BuiltinLoc,
RParenLoc);
}
/* TO_UPSTREAM(BoundsSafety) OFF*/
/* TO_UPSTREAM(BoundsSafety) OFF*/
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ class MyClass {
namespace value_dependent_assertion_violation {
// Running attribute-only mode on the C++ code below crashes
// previously.
void g(unsigned);
void g(int * __counted_by(n) * p, size_t n);

template<typename T>
struct S {
void f(T p) {
g(sizeof(p));
g(nullptr, sizeof(p));
}
};

// expected-error@-4{{incompatible dynamic count pointer argument to parameter of type 'int * __counted_by(n)*' (aka 'int **')}}
template struct S<int>; // expected-note{{in instantiation of member function 'value_dependent_assertion_violation::S<int>::f' requested here}}
}

0 comments on commit 37480ab

Please sign in to comment.