================ @@ -186,4 +218,216 @@ bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes, return false; } +SourceRange Sema::BoundsSafetySourceRangeFor(const CountAttributedType *CATy) { + // This is an approximation that's not quite right. This points to the + // the expression inside the attribute rather than the attribute itself. + // + // TODO: Implement logic to find the relevant TypeLoc for the attribute and + // get the SourceRange from that (#113582). + return CATy->getCountExpr()->getSourceRange(); +} + +static void EmitIncompleteCountedByPointeeNotes(Sema &S, + const CountAttributedType *CATy, + NamedDecl *IncompleteTyDecl, + bool NoteAttrLocation = true) { + assert(IncompleteTyDecl == nullptr || isa<TypeDecl>(IncompleteTyDecl)); + + if (NoteAttrLocation) { + // Note where the attribute is declared + auto AttrSrcRange = S.BoundsSafetySourceRangeFor(CATy); + S.Diag(AttrSrcRange.getBegin(), diag::note_named_attribute) + << CATy->getAttributeName(/*WithMacroPrefix=*/true) << AttrSrcRange; + } + + if (!IncompleteTyDecl) + return; + + // If there's an associated forward declaration display it to emphasize + // why the type is incomplete (all we have is a forward declaration). + + // Note the `IncompleteTyDecl` type is the underlying type which might not + // be the same as `CATy->getPointeeType()` which could be a typedef. + // + // The diagnostic printed will be at the location of the underlying type but + // the diagnostic text will print the type of `CATy->getPointeeType()` which + // could be a typedef name rather than the underlying type. This is ok + // though because the diagnostic will print the underlying type name too. + // E.g: + // + // `forward declaration of 'Incomplete_Struct_t' + // (aka 'struct IncompleteStructTy')` + // + // If this ends up being confusing we could emit a second diagnostic (one + // explaining where the typedef is) but that seems overly verbose. + + S.Diag(IncompleteTyDecl->getBeginLoc(), diag::note_forward_declaration) + << CATy->getPointeeType(); +} + +static bool +HasCountedByAttrOnIncompletePointee(QualType Ty, NamedDecl **ND, + const CountAttributedType **CATyOut, + QualType *PointeeTyOut) { + auto *CATy = Ty->getAs<CountAttributedType>(); + if (!CATy) + return false; + + // Incomplete pointee type is only a problem for + // counted_by/counted_by_or_null + if (CATy->isCountInBytes()) + return false; + + auto PointeeTy = CATy->getPointeeType(); + if (PointeeTy.isNull()) + return false; // Reachable? + + if (!PointeeTy->isIncompleteType(ND)) + return false; + + if (CATyOut) + *CATyOut = CATy; + if (PointeeTyOut) + *PointeeTyOut = PointeeTy; + return true; +} + +/// Perform Checks for assigning to a `__counted_by` or +/// `__counted_by_or_null` pointer type \param LHSTy where the pointee type +/// is incomplete which is invalid. +/// +/// \param S The Sema instance. +/// \param LHSTy The type being assigned to. Checks will only be performed if +/// the type is a `counted_by` or `counted_by_or_null ` pointer. +/// \param RHSExpr The expression being assigned from. +/// \param Action The type assignment being performed +/// \param Loc The SourceLocation to use for error diagnostics +/// \param ComputeAssignee If provided this function will be called before +/// emitting a diagnostic. The function should return the name of +/// entity being assigned to or an empty string if this cannot be +/// determined. +/// +/// \returns True iff no diagnostic where emitted, false otherwise. +static bool CheckAssignmentToCountAttrPtrWithIncompletePointeeTy( + Sema &S, QualType LHSTy, Expr *RHSExpr, AssignmentAction Action, + SourceLocation Loc, llvm::function_ref<std::string()> ComputeAssignee) { + NamedDecl *IncompleteTyDecl = nullptr; + const CountAttributedType *CATy = nullptr; + QualType PointeeTy; + if (!HasCountedByAttrOnIncompletePointee(LHSTy, &IncompleteTyDecl, &CATy, + &PointeeTy)) + return true; + assert(CATy && !CATy->isCountInBytes() && !PointeeTy.isNull()); + + // It's not expected that the diagnostic be emitted in these cases. + // It's not necessarily a problem but we should catch when this starts + // to happen. ---------------- Sirraide wrote:
This comment on an assertion is a bit confusing to me: ‘it’s not necessarily a problem, but we should catch it if it does happen’? That to me at least implicates that it is in fact a problem if this happens, otherwise, why bother catching it? Should this maybe be a `TODO` or `FIXME` comment instead? https://github.com/llvm/llvm-project/pull/106321 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits