This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Model noexcept more properly in bugprone-exception-escape
ClosedPublic

Authored by PiotrZSL on Jun 21 2023, 12:50 PM.

Details

Summary

During call stack analysis skip called noexcept functions
as they wont throw exceptions, they will crash.
Check will emit warnings for those functions separately.

Fixes: #43667, #49151, #51596, #54668, #54956

Diff Detail

Event Timeline

PiotrZSL created this revision.Jun 21 2023, 12:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2023, 12:50 PM
Herald added a subscriber: xazax.hun. · View Herald Transcript
PiotrZSL requested review of this revision.Jun 21 2023, 12:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2023, 12:50 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
isuckatcs added inline comments.Jun 23 2023, 12:02 PM
clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
319

Put this in the anonymous namespace above please to remain consistent.

453

Is cannotThrow(Func) really needed here? Isn't it possible to bail out after the body of the function has been analyzed?

I understand that you want to prevent some recursive calls and bail out early, but I don't think that it worths adding some additional logic, which is not needed anyway.

If you really want to optimize this or you're worried about stack overflows, consider rewriting the recursive solution to an iterative one.

PiotrZSL added inline comments.Jun 23 2023, 12:25 PM
clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
319

well, llvm style require static, if we want to be consistent, I can change all other into static later.

453

Yes, it should be here because it's called in 3 places so that an best place to put it.
It's not about recursion or performance, it's already exist and I simply do no change it.

Simply if we analyse callExpr to function/destructor/constructor/... and that FunctionDecl is noexcept, then we should jump to that function and analyse statements in that function body at all.
And this is what is done here. If this isn't first function !CallStack.empty() (aka: call from check code), and it's noexcept, then assume that it cannot throw and do not go deeper.

Looks good, great to see all these issues fixed! Have a couple small comments.

clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
319

Nit: I personally find it more readable as "canThrow". People with issues reading double negations will probably appreciate that too :) But if you strongly prefer this name I won't oppose.

330–332

Would be good to write a small comment documenting this logic.

332

Would be good to use the syntax /* Parameter = */true to document what this magic true means.

PiotrZSL updated this revision to Diff 540823.Jul 16 2023, 12:09 PM
PiotrZSL marked 5 inline comments as done.

Fix review comments

carlosgalvezp accepted this revision.Jul 17 2023, 4:02 AM

LGTM, thanks!

This revision is now accepted and ready to land.Jul 17 2023, 4:02 AM