Skip to content

Conversation

@term-est
Copy link
Contributor

The following snippet,

#include <cassert>
#include <stdexcept>

[[noreturn]] void foo() { throw std::runtime_error("Why...?"); }

int main() {
    try {
        foo();
    } catch (std::runtime_error const& ex) {
        (void)ex;
    }
}

is diagnosed with error: function 'foo' could be declared with attribute 'noreturn' [-Werror,-Wmissing-noreturn] when compiled with clang_trunk using -Werror=missing-noreturn flag. See more at godbolt

AST shows

| `-CXX11NoReturnAttr 0x1896e81eb00 <line:2:20> Inherited noreturn
|-FunctionDecl 0x1896e81ebd8 <line:10:14, col:46> col:19 used foo 'void ()'
| |-CompoundStmt 0x1896e81ee20 <col:25, col:46>
| | `-CXXThrowExpr 0x1896e81ee08 <col:27, col:43> 'void'
| |   `-ImplicitCastExpr 0x1896e81edf0 <col:33, col:43> 'const char *' <ArrayToPointerDecay>
| |     `-ParenExpr 0x1896e81eda0 <col:33, col:43> 'const char[8]' lvalue
| |       `-StringLiteral 0x1896e81ed80 <col:34> 'const char[8]' lvalue "Why...?"
| |-CXX11NoReturnAttr 0x1896e81ec80 <col:3> noreturn
| `-InferredNoReturnAttr 0x1896e81ee38 <<invalid sloc>> Implicit

but the responsible code only checks for NoReturnAttr, adding a check for CXX11NoReturnAttr before emitting the diagnostic should fix this issue.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 14, 2025

@llvm/pr-subscribers-clang

Author: None (term-est)

Changes

The following snippet,

#include &lt;cassert&gt;
#include &lt;stdexcept&gt;

[[noreturn]] void foo() { throw std::runtime_error("Why...?"); }

int main() {
    try {
        foo();
    } catch (std::runtime_error const&amp; ex) {
        (void)ex;
    }
}

is diagnosed with error: function 'foo' could be declared with attribute 'noreturn' [-Werror,-Wmissing-noreturn] when compiled with clang_trunk using -Werror=missing-noreturn flag. See more at godbolt

AST shows

| `-CXX11NoReturnAttr 0x1896e81eb00 &lt;line:2:20&gt; Inherited noreturn
|-FunctionDecl 0x1896e81ebd8 &lt;line:10:14, col:46&gt; col:19 used foo 'void ()'
| |-CompoundStmt 0x1896e81ee20 &lt;col:25, col:46&gt;
| | `-CXXThrowExpr 0x1896e81ee08 &lt;col:27, col:43&gt; 'void'
| |   `-ImplicitCastExpr 0x1896e81edf0 &lt;col:33, col:43&gt; 'const char *' &lt;ArrayToPointerDecay&gt;
| |     `-ParenExpr 0x1896e81eda0 &lt;col:33, col:43&gt; 'const char[8]' lvalue
| |       `-StringLiteral 0x1896e81ed80 &lt;col:34&gt; 'const char[8]' lvalue "Why...?"
| |-CXX11NoReturnAttr 0x1896e81ec80 &lt;col:3&gt; noreturn
| `-InferredNoReturnAttr 0x1896e81ee38 &lt;&lt;invalid sloc&gt;&gt; Implicit

but the responsible code only checks for NoReturnAttr, adding a check for CXX11NoReturnAttr before emitting the diagnostic should fix this issue.


Full diff: https://github.com/llvm/llvm-project/pull/148695.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 099207727c8c8..6d0a5d71d49ff 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -1976,7 +1976,7 @@ void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
       Diags.isIgnored(diag::warn_suggest_noreturn_function, FD->getLocation()))
     return;
 
-  if (!FD->hasAttr<NoReturnAttr>() && !FD->hasAttr<InferredNoReturnAttr>() &&
+  if (!FD->hasAttr<NoReturnAttr>() && !FD->hasAttr<CXX11NoReturnAttr>() && !FD->hasAttr<InferredNoReturnAttr>() &&
       isKnownToAlwaysThrow(FD)) {
     NonConstFD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
 

@term-est term-est force-pushed the attribute-warning-regression-fix branch from f8082dd to d8979be Compare July 14, 2025 18:23
@zwuis
Copy link
Contributor

zwuis commented Jul 15, 2025

It seems that #148552 has fixed this issue.

@cor3ntin
Copy link
Contributor

It seems that #148552 has fixed this issue.

Indeed. We appreciate the work you put in the PR though, thanks :)

@cor3ntin cor3ntin closed this Jul 15, 2025
@term-est term-est deleted the attribute-warning-regression-fix branch July 15, 2025 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants