- Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] Forward Correct SourceLocation for Unreachable Code Diagnostics #152839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mr-Anyone wants to merge 1 commit into llvm:main Choose a base branch from Mr-Anyone:fix_unreachable_code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Member
| @llvm/pr-subscribers-clang Author: Vincent (Mr-Anyone) Changesfixes #152477 Full diff: https://github.com/llvm/llvm-project/pull/152839.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 17e3df467593d..f3b21479c7470 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -178,6 +178,8 @@ Bug Fixes to C++ Support - Diagnose binding a reference to ``*nullptr`` during constant evaluation. (#GH48665) - Suppress ``-Wdeprecated-declarations`` in implicitly generated functions. (#GH147293) - Fix a crash when deleting a pointer to an incomplete array (#GH150359). +- Diagnosing the correct location for a function with non-POD data return-type being + labeled ``[[noreturn]]``. - Fix an assertion failure when expression in assumption attribute (``[[assume(expr)]]``) creates temporary objects. diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index 4a9ab5d9f0f73..c7de8836932a3 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -601,6 +601,11 @@ static SourceLocation GetUnreachableLoc(const Stmt *S, S = Ex->IgnoreParenImpCasts(); switch (S->getStmtClass()) { + case Expr::CXXBindTemporaryExprClass: { + const CXXBindTemporaryExpr *TemporaryExpr = cast<CXXBindTemporaryExpr>(S); + R1 = TemporaryExpr->getTemporary()->getDestructor()->getSourceRange(); + return R1.getBegin(); + } case Expr::BinaryOperatorClass: { const BinaryOperator *BO = cast<BinaryOperator>(S); return BO->getOperatorLoc(); diff --git a/clang/test/SemaCXX/warn-unreachable.cpp b/clang/test/SemaCXX/warn-unreachable.cpp index e6f5bc5ef8e12..ce574cfafa8e1 100644 --- a/clang/test/SemaCXX/warn-unreachable.cpp +++ b/clang/test/SemaCXX/warn-unreachable.cpp @@ -414,3 +414,16 @@ void tautological_compare(bool x, int y) { calledFun(); } + +namespace GH152477{ + class A{ + public: + ~A(); // expected-warning {{will never be executed}} + }; + + [[noreturn]] A never_return_so_destructor_never_called(); + + void func(){ + never_return_so_destructor_never_called(); + } +}; |
Member
| @llvm/pr-subscribers-clang-analysis Author: Vincent (Mr-Anyone) Changesfixes #152477 Full diff: https://github.com/llvm/llvm-project/pull/152839.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 17e3df467593d..f3b21479c7470 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -178,6 +178,8 @@ Bug Fixes to C++ Support - Diagnose binding a reference to ``*nullptr`` during constant evaluation. (#GH48665) - Suppress ``-Wdeprecated-declarations`` in implicitly generated functions. (#GH147293) - Fix a crash when deleting a pointer to an incomplete array (#GH150359). +- Diagnosing the correct location for a function with non-POD data return-type being + labeled ``[[noreturn]]``. - Fix an assertion failure when expression in assumption attribute (``[[assume(expr)]]``) creates temporary objects. diff --git a/clang/lib/Analysis/ReachableCode.cpp b/clang/lib/Analysis/ReachableCode.cpp index 4a9ab5d9f0f73..c7de8836932a3 100644 --- a/clang/lib/Analysis/ReachableCode.cpp +++ b/clang/lib/Analysis/ReachableCode.cpp @@ -601,6 +601,11 @@ static SourceLocation GetUnreachableLoc(const Stmt *S, S = Ex->IgnoreParenImpCasts(); switch (S->getStmtClass()) { + case Expr::CXXBindTemporaryExprClass: { + const CXXBindTemporaryExpr *TemporaryExpr = cast<CXXBindTemporaryExpr>(S); + R1 = TemporaryExpr->getTemporary()->getDestructor()->getSourceRange(); + return R1.getBegin(); + } case Expr::BinaryOperatorClass: { const BinaryOperator *BO = cast<BinaryOperator>(S); return BO->getOperatorLoc(); diff --git a/clang/test/SemaCXX/warn-unreachable.cpp b/clang/test/SemaCXX/warn-unreachable.cpp index e6f5bc5ef8e12..ce574cfafa8e1 100644 --- a/clang/test/SemaCXX/warn-unreachable.cpp +++ b/clang/test/SemaCXX/warn-unreachable.cpp @@ -414,3 +414,16 @@ void tautological_compare(bool x, int y) { calledFun(); } + +namespace GH152477{ + class A{ + public: + ~A(); // expected-warning {{will never be executed}} + }; + + [[noreturn]] A never_return_so_destructor_never_called(); + + void func(){ + never_return_so_destructor_never_called(); + } +}; |
| ✅ With the latest revision this PR passed the C/C++ code formatter. |
ccfa6a7 to ee1bcce Compare Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Unreachable destructor of temporary object leads to confusing diagnostics messages.
This patch forwards the correct source location for better and less confusing diagnostics.
fixes #152477