Skip to content

Commit 0c9c62b

Browse files
committed
Rework diagnostics
1 parent b17b93d commit 0c9c62b

File tree

4 files changed

+789
-161
lines changed

4 files changed

+789
-161
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4445,7 +4445,6 @@ def err_mismatched_visibility: Error<"visibility does not match previous declara
44454445
def note_previous_attribute : Note<"previous attribute is here">;
44464446
def note_conflicting_attribute : Note<"conflicting attribute is here">;
44474447
def note_attribute : Note<"attribute is here">;
4448-
def note_named_attribute : Note<"%0 attribute is here">;
44494448
def err_mismatched_ms_inheritance : Error<
44504449
"inheritance model does not match %select{definition|previous declaration}0">;
44514450
def warn_ignored_ms_inheritance : Warning<
@@ -6713,40 +6712,31 @@ def err_counted_by_attr_pointee_unknown_size : Error<
67136712
"}2">;
67146713
def err_counted_by_on_incomplete_type_on_assign : Error <
67156714
"cannot %select{"
6716-
"assign to %select{object|'%1'}2 that has|" // AA_Assigning,
6717-
"pass argument to %select{parameter|parameter '%1'}2 that has|" // AA_Passing,
6715+
"assign to %select{object|'%1'}2 with|" // AA_Assigning,
6716+
"pass argument to %select{parameter|parameter '%1'}2 with|" // AA_Passing,
67186717
"return|" // AA_Returning,
67196718
"convert to|" // AA_Converting (UNUSED)
6720-
"%select{|implicitly }3initialize %select{object|'%1'}2 that has|" // AA_Initializing,
6721-
"pass argument to parameter that has|" // AA_Sending (UNUSED)
6719+
"%select{|implicitly }3initialize %select{object|'%1'}2 with|" // AA_Initializing,
6720+
"pass argument to parameter with|" // AA_Sending (UNUSED)
67226721
"cast to|" // AA_Casting (UNUSED)
6723-
"pass argument to parameter that has" // AA_Passing_CFAudited (UNUSED)
6724-
"}0 type %4 because the pointee type %6 is incomplete and the '%5' attribute "
6725-
"requires the pointee type be complete when %select{"
6726-
"assigning|" // AA_Assigning,
6727-
"passing|" // AA_Passing,
6728-
"returning|" // AA_Returning,
6729-
"converting|" // AA_Converting (UNUSED)
6730-
"initializing|" // AA_Initializing,
6731-
"passing|" // AA_Sending (UNUSED)
6732-
"casting|" // AA_Casting (UNUSED)
6733-
"passing" // AA_Passing_CFAudited (UNUSED)
6734-
"}0; "
6735-
"consider providing a complete definition for %6 or using the "
6736-
"'__sized_by%select{|_or_null}7' attribute"
6737-
>;
6722+
"pass argument to parameter with" // AA_Passing_CFAudited (UNUSED)
6723+
"}0 '%5' attributed type %4 because the pointee type %6 is incomplete">;
6724+
67386725
def err_counted_by_on_incomplete_type_on_use : Error <
67396726
"cannot %select{"
67406727
"use '%1'|" // Generic expr
67416728
"call '%1'" // CallExpr
67426729
"}0 with%select{"
67436730
"|" // Generic expr
67446731
" return" // CallExpr
6745-
"}0 type %2 because the pointee type %3 is incomplete "
6746-
"and the '%4' attribute requires the pointee type be complete in this context; "
6747-
"consider providing a complete definition for %3 or using the "
6748-
"'__sized_by%select{|_or_null}5' attribute"
6749-
>;
6732+
"}0 '%4' attributed type %2 because the pointee type %3 is incomplete">;
6733+
6734+
def note_counted_by_consider_completing_pointee_ty : Note<
6735+
"consider providing a complete definition for %0">;
6736+
6737+
def note_counted_by_consider_using_sized_by : Note<
6738+
"consider using '__sized_by%select{|_or_null}0' instead of "
6739+
"__counted_by%select{|_or_null}0">;
67506740

67516741
def warn_counted_by_attr_elt_type_unknown_size :
67526742
Warning<err_counted_by_attr_pointee_unknown_size.Summary>,

clang/lib/Sema/SemaBoundsSafety.cpp

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -220,41 +220,40 @@ bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes,
220220

221221
static void EmitIncompleteCountedByPointeeNotes(Sema &S,
222222
const CountAttributedType *CATy,
223-
NamedDecl *IncompleteTyDecl,
224-
bool NoteAttrLocation = true) {
223+
NamedDecl *IncompleteTyDecl) {
225224
assert(IncompleteTyDecl == nullptr || isa<TypeDecl>(IncompleteTyDecl));
226225

227-
if (NoteAttrLocation) {
228-
// Note where the attribute is declared
229-
// This is an approximation that's not quite right. This points to the
230-
// the expression inside the attribute rather than the attribute itself.
226+
if (IncompleteTyDecl) {
227+
// Suggest completing the pointee type if its a named typed (i.e.
228+
// IncompleteTyDecl isn't nullptr). Suggest this first as it is more likely
229+
// to be the correct fix.
231230
//
232-
// TODO: Implement logic to find the relevant TypeLoc for the attribute and
233-
// get the SourceRange from that (#113582).
234-
SourceRange AttrSrcRange = CATy->getCountExpr()->getSourceRange();
235-
S.Diag(AttrSrcRange.getBegin(), diag::note_named_attribute)
236-
<< CATy->getAttributeName(/*WithMacroPrefix=*/true) << AttrSrcRange;
231+
// Note the `IncompleteTyDecl` type is the underlying type which might not
232+
// be the same as `CATy->getPointeeType()` which could be a typedef.
233+
//
234+
// The diagnostic printed will be at the location of the underlying type but
235+
// the diagnostic text will print the type of `CATy->getPointeeType()` which
236+
// could be a typedef name rather than the underlying type. This is ok
237+
// though because the diagnostic will print the underlying type name too.
238+
S.Diag(IncompleteTyDecl->getBeginLoc(),
239+
diag::note_counted_by_consider_completing_pointee_ty)
240+
<< CATy->getPointeeType();
237241
}
238242

239-
if (!IncompleteTyDecl)
240-
return;
241-
242-
// If there's an associated forward declaration display it to emphasize
243-
// why the type is incomplete (all we have is a forward declaration).
244-
245-
// Note the `IncompleteTyDecl` type is the underlying type which might not
246-
// be the same as `CATy->getPointeeType()` which could be a typedef.
243+
// Suggest using __sized_by(_or_null) instead of __counted_by(_or_null) as
244+
// __sized_by(_or_null) doesn't have the complete type restriction.
245+
//
246+
// We use the source range of the expression on the CountAttributedType as an
247+
// approximation for the source range of the attribute. This isn't quite right
248+
// but isn't easy to fix right now.
247249
//
248-
// The diagnostic printed will be at the location of the underlying type but
249-
// the diagnostic text will print the type of `CATy->getPointeeType()` which
250-
// could be a typedef name rather than the underlying type. This is ok
251-
// though because the diagnostic will print the underlying type name too.
252-
// E.g:
250+
// TODO: Implement logic to find the relevant TypeLoc for the attribute and
251+
// get the SourceRange from that (#113582).
253252
//
254-
// `forward declaration of 'Incomplete_Struct_t'
255-
// (aka 'struct IncompleteStructTy')`
256-
S.Diag(IncompleteTyDecl->getBeginLoc(), diag::note_forward_declaration)
257-
<< CATy->getPointeeType();
253+
// TODO: We should emit a fix-it here.
254+
SourceRange AttrSrcRange = CATy->getCountExpr()->getSourceRange();
255+
S.Diag(AttrSrcRange.getBegin(), diag::note_counted_by_consider_using_sized_by)
256+
<< CATy->isOrNull() << AttrSrcRange;
258257
}
259258

260259
static std::tuple<const CountAttributedType *, QualType>

0 commit comments

Comments
 (0)