This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Warn on attribute nothrow conflicting with language specifiers
ClosedPublic

Authored by erichkeane on Sep 22 2017, 4:12 PM.

Details

Summary

I discovered it was possible to create a 'nothrow' noexcept(false)
function, which is both non-sensical as well as seemingly breaking.

This patch warns if attribute nothrow is used with anything besides "noexcept".

"noexcept(true)" isn't possible, because the noexcept decl isn't parsed until
later.

Diff Detail

Repository
rL LLVM

Event Timeline

erichkeane created this revision.Sep 22 2017, 4:12 PM

Woops, didn't commit my correct tests!

aaron.ballman added inline comments.Sep 25 2017, 5:32 AM
include/clang/Basic/DiagnosticSemaKinds.td
1411 ↗(On Diff #116433)

How about: "attribute 'nothrow' ignored due to conflicting exception specification"

lib/Sema/SemaDeclAttr.cpp
1972 ↗(On Diff #116433)

attr doesn't meet the coding guidelines. I'd go with AL.

test/SemaCXX/warn-conflicting-nothrow-attr-exception-spec.cpp
3 ↗(On Diff #116433)

You should add some tests that include templates and test it on Windows as well as non-Windows. I'm wondering about computed exception specifications during template instantiation. e.g.,

void throwing() noexcept(false);
void non_throwing() noexcept;

template <typename Fn>
struct T {
  __attribute__((nothrow)) void f(Fn) noexcept(Fn());
};

template struct T<decltype(throwing)>;
template struct T<decltype(non_throwing)>;
erichkeane marked 2 inline comments as done.

changes that were suggested by @aaron.ballman

It DOES warn on the template correctly, however I'm not thrilled that it misses the 'warning' info. Any idea how I can get that information to give that 'note'? I'd like an "in instantiation of template ..." type note.

aaron.ballman accepted this revision.Sep 28 2017, 10:55 AM

changes that were suggested by @aaron.ballman

It DOES warn on the template correctly, however I'm not thrilled that it misses the 'warning' info. Any idea how I can get that information to give that 'note'? I'd like an "in instantiation of template ..." type note.

That's usually automatic. You might want to look at Sema::EmitCurrentDiagnostic() to see what's up, it's PrintContextStack() that does the magic.

The diagnostics LGTM as-is, but further improvement isn't discouraged.

include/clang/Basic/DiagnosticSemaKinds.td
1411 ↗(On Diff #116433)

Close, but not quite. The warning should not be capitalized and it's missing single quotes around nothrow.

This revision is now accepted and ready to land.Sep 28 2017, 10:55 AM
This revision was automatically updated to reflect the committed changes.