35 while (RD && (RD->isAnonymousStructOrUnion() || RD->getName().empty())) {
36 const auto *Parent = dyn_cast<RecordDecl>(RD->getParent());
64 const auto FieldTy = FD->
getType();
65 if (FieldTy->isArrayType() && (CountInBytes || OrNull)) {
67 diag::err_count_attr_not_on_ptr_or_flexible_array_member)
71 if (!FieldTy->isArrayType() && !FieldTy->isPointerType()) {
73 diag::err_count_attr_not_on_ptr_or_flexible_array_member)
80 if (FieldTy->isArrayType() &&
82 StrictFlexArraysLevel,
true)) {
84 diag::err_counted_by_attr_on_array_not_flexible_array_member)
92 int SelectPtrOrArr = 0;
93 if (FieldTy->isPointerType()) {
97 assert(FieldTy->isArrayType());
105 bool ShouldWarn =
false;
145 Diag(FD->
getBeginLoc(), diag::note_gnu_counted_by_void_ptr_use_sized_by)
156 if (FieldTy->isArrayType() && !
getLangOpts().BoundsSafety) {
169 unsigned DiagID = ShouldWarn
170 ? diag::warn_counted_by_attr_elt_type_unknown_size
171 : diag::err_counted_by_attr_pointee_unknown_size;
173 << SelectPtrOrArr << PointeeTy << (
int)InvalidTypeKind
186 auto *DRE = dyn_cast<DeclRefExpr>(E);
189 diag::err_count_attr_only_support_simple_decl_reference)
194 auto *CountDecl = DRE->getDecl();
195 FieldDecl *CountFD = dyn_cast<FieldDecl>(CountDecl);
196 if (
auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl)) {
197 CountFD = IFD->getAnonField();
203 Diag(CountDecl->getBeginLoc(),
204 diag::note_flexible_array_counted_by_attr_field)
205 << CountDecl << CountDecl->getSourceRange();
223 << CountFD << Kind << FieldTy->isArrayType() << E->
getSourceRange();
225 diag::note_flexible_array_counted_by_attr_field)
236 assert(IncompleteTyDecl ==
nullptr ||
isa<TypeDecl>(IncompleteTyDecl));
238 if (IncompleteTyDecl) {
251 diag::note_counted_by_consider_completing_pointee_ty)
267 S.
Diag(AttrSrcRange.
getBegin(), diag::note_counted_by_consider_using_sized_by)
268 << CATy->
isOrNull() << AttrSrcRange;
271static std::tuple<const CountAttributedType *, QualType>
276 if (!CATy || CATy->isCountInBytes())
280 if (PointeeTy.isNull()) {
285 if (!PointeeTy->isIncompleteType(ND))
288 if (PointeeTy->isVoidType())
291 return {CATy, PointeeTy};
315 bool ShowFullyQualifiedAssigneeName) {
317 auto [CATy, PointeeTy] =
322 std::string AssigneeStr;
324 if (ShowFullyQualifiedAssigneeName) {
331 S.
Diag(Loc, diag::err_counted_by_on_incomplete_type_on_assign)
332 <<
static_cast<int>(Action) << AssigneeStr << (AssigneeStr.size() > 0)
334 << CATy->getAttributeName(
true) << PointeeTy
343 const ValueDecl *Assignee,
bool ShowFullyQualifiedAssigneeName) {
345 *
this, LHSTy, RHSExpr, Action, Loc, Assignee,
346 ShowFullyQualifiedAssigneeName);
353 auto SL = Kind.getLocation();
367 *
this, LHSType, RHSExpr, Action, SL,
368 dyn_cast_or_null<ValueDecl>(Entity.
getDecl()),
379 if (!T->isPointerType())
383 auto [CATy, PointeeTy] =
392 bool IsDirectCall =
false;
393 if (
const auto *CE = dyn_cast<CallExpr>(E->
IgnoreParens())) {
394 if (
const auto *FD = CE->getDirectCallee()) {
395 UseStr = FD->getName();
401 llvm::raw_svector_ostream SS(UseStr);
406 << IsDirectCall << UseStr << T << PointeeTy
407 << CATy->getAttributeName(
true) << CATy->isOrNull()
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
QualType getElementType() const
Represents a sugar type with __counted_by or __sized_by annotations, including their _or_null variant...
Expr * getCountExpr() const
static bool isFlexibleArrayMemberLike(const ASTContext &Context, const Decl *D, QualType Ty, LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel, bool IgnoreTemplateOrMacroSubstitution)
Whether it resembles a flexible array member.
SourceLocation getLocation() const
SourceLocation getBeginLoc() const LLVM_READONLY
SourceLocation getBeginLoc() const LLVM_READONLY
This represents one expression.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Represents a member of a struct/union/class.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Describes the kind of initialization being performed, along with location information for tokens rela...
Describes an entity that is being initialized.
EntityKind getKind() const
Determine the kind of initialization.
ValueDecl * getDecl() const
Retrieve the variable, parameter, or field being initialized.
@ EK_Variable
The entity being initialized is a variable.
StrictFlexArraysLevelKind
@ IncompleteOnly
Any trailing array member of undefined size is a FAM.
This represents a decl that may have a name.
std::string getQualifiedNameAsString() const
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
A (possibly-)qualified type.
Represents a struct/union/class.
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Sema - This implements semantic analysis and AST building for C.
bool BoundsSafetyCheckAssignmentToCountAttrPtr(QualType LHSTy, Expr *RHSExpr, AssignmentAction Action, SourceLocation Loc, const ValueDecl *Assignee, bool ShowFullyQualifiedAssigneeName)
Perform Bounds Safety Semantic checks for assigning to a __counted_by or __counted_by_or_null pointer...
bool BoundsSafetyCheckUseOfCountAttrPtr(const Expr *E)
Perform Bounds Safety semantic checks for uses of invalid uses counted_by or counted_by_or_null point...
ASTContext & getASTContext() const
PrintingPolicy getPrintingPolicy() const
Retrieve a suitable printing policy for diagnostics.
const LangOptions & getLangOpts() const
bool CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes, bool OrNull)
Check if applying the specified attribute variant from the "counted by" family of attributes to Field...
bool BoundsSafetyCheckInitialization(const InitializedEntity &Entity, const InitializationKind &Kind, AssignmentAction Action, QualType LHSType, Expr *RHSExpr)
Perform Bounds Safety Semantic checks for initializing a Bounds Safety pointer.
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
void printPretty(raw_ostream &OS, PrinterHelper *Helper, const PrintingPolicy &Policy, unsigned Indentation=0, StringRef NewlineSymbol="\n", const ASTContext *Context=nullptr) const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SourceLocation getBeginLoc() const LLVM_READONLY
bool isSizelessType() const
As an extension, we classify types as one of "sized" or "sizeless"; every type is one or the other.
bool isBooleanType() const
bool isAlwaysIncompleteType() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isFunctionType() const
bool isStructureTypeWithFlexibleArrayMember() const
const T * getAs() const
Member-template getAs<specific type>'.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
static CountAttributedType::DynamicCountPointerKind getCountAttrKind(bool CountInBytes, bool OrNull)
CountedByInvalidPointeeTypeKind
static bool CheckAssignmentToCountAttrPtrWithIncompletePointeeTy(Sema &S, QualType LHSTy, Expr *RHSExpr, AssignmentAction Action, SourceLocation Loc, const ValueDecl *Assignee, bool ShowFullyQualifiedAssigneeName)
Perform Checks for assigning to a __counted_by or __counted_by_or_null pointer type.
static const RecordDecl * GetEnclosingNamedOrTopAnonRecord(const FieldDecl *FD)
static void EmitIncompleteCountedByPointeeNotes(Sema &S, const CountAttributedType *CATy, NamedDecl *IncompleteTyDecl)
static std::tuple< const CountAttributedType *, QualType > GetCountedByAttrOnIncompletePointee(QualType Ty, NamedDecl **ND)