This is an archive of the discontinued LLVM Phabricator instance.

[Clang][DiagnosticSemaKinds] combine diagnostic texts
ClosedPublic

Authored by nickdesaulniers on Aug 5 2021, 4:42 PM.

Details

Summary

The diagnostic texts for warning on attributes that don't appear on the
initial declaration is generally useful. We'd like to re-use it in
D106030, but first let's combine two that already are very similar so we
may re-use it a third time in that commit.

Also, fix a few places that were using notePreviousDefinition to point
to declarations, to instead use diag::note_previous_declaration.

Diff Detail

Event Timeline

nickdesaulniers requested review of this revision.Aug 5 2021, 4:42 PM
nickdesaulniers created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptAug 5 2021, 4:42 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman accepted this revision.Aug 6 2021, 4:15 AM

LGTM with some minor tweaks to avoid hasAttr followed by getAttr. Thanks!

clang/lib/Sema/SemaDecl.cpp
3355–3356
4168–4169
This revision is now accepted and ready to land.Aug 6 2021, 4:15 AM
nickdesaulniers marked 2 inline comments as done.
  • prefer getAttr to hasAttr+getAttr, remove another diag
clang/lib/Sema/SemaDecl.cpp
3685

ah, this isn't quite right. It replaces warning of the first declaration with warning of the first definition.

This should instead use

Diag(Old->getLocation(), diag::note_previous_declaration);

aaron.ballman added inline comments.Aug 6 2021, 11:21 AM
clang/lib/Sema/SemaDecl.cpp
3685

Good catch! I think we want to use note_previous_declaration in all three cases though, don't we? It's a bit weird that we diagnose "attribute not present on first declaration" but then try to note a definition. e.g., https://godbolt.org/z/jd9oG96sT

I know it's a bit of a drive-by, but I think it's probably worth correcting that as well. WDYT?

nickdesaulniers planned changes to this revision.Aug 6 2021, 11:27 AM
nickdesaulniers added inline comments.
clang/lib/Sema/SemaDecl.cpp
3685

Looks like we do that for WeakImportAttr, too. Will fix all cases and amend description.

nickdesaulniers edited the summary of this revision. (Show Details)
  • more use of note_previous_declaration, fix WeakImportAttr, too
This revision is now accepted and ready to land.Aug 6 2021, 11:39 AM
nickdesaulniers marked an inline comment as done.Aug 6 2021, 11:39 AM
nickdesaulniers added inline comments.
clang/lib/Sema/SemaDecl.cpp
3686

should CXX11NoReturnAttr be dropAttr on New like the other cases, too?

aaron.ballman accepted this revision.Aug 6 2021, 11:54 AM

LGTM!

clang/lib/Sema/SemaDecl.cpp
3686

I think it's better to leave it -- the user gets an error so they don't get object code out of it, but leaving the attribute as-if the user had specified everything properly means later diagnostics may behave a bit better. Probably doesn't matter all that much in this case, though, so I don't feel strongly.

nickdesaulniers marked an inline comment as done.Aug 6 2021, 1:45 PM

It looks like this error is intended to catch mismatches for attributes that can affect codegen such as noreturn (in which case it makes sense to have it as an error) but it also now fires for cases such as __attribute__(warning()) which often do not duplicate the attribute on the definition. Is that intended?

It looks like this error is intended to catch mismatches for attributes that can affect codegen such as noreturn (in which case it makes sense to have it as an error) but it also now fires for cases such as __attribute__(warning()) which often do not duplicate the attribute on the definition. Is that intended?

Duplicating the attribute on the definition is not needed for __attribute__(warning()).