aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/AnalysisBasedWarnings.cpp
diff options
context:
space:
mode:
authorOleksandr T. <oleksandr.tarasiuk@outlook.com>2025-04-08 23:21:53 +0300
committerGitHub <noreply@github.com>2025-04-08 23:21:53 +0300
commit9f463056e6dd5ff94d35f8cc1f4aa4ecc87fa61d (patch)
tree582109298a003639a76b6857650cc33b4b8ea5f7 /clang/lib/Sema/AnalysisBasedWarnings.cpp
parent156e2532edeaea9a60c37c041e8059fc5693122f (diff)
downloadllvm-9f463056e6dd5ff94d35f8cc1f4aa4ecc87fa61d.zip
llvm-9f463056e6dd5ff94d35f8cc1f4aa4ecc87fa61d.tar.gz
llvm-9f463056e6dd5ff94d35f8cc1f4aa4ecc87fa61d.tar.bz2
[Clang] add ext warning for missing return in 'main' for C89 mode (#134617)
Fixes #21650 --- Clang currently inserts an implicit `return 0;` in `main()` when compiling in `C89` mode, even though the `C89` standard doesn't require this behavior. This patch changes that behavior by emitting a warning instead of silently inserting the implicit return under `-pedantic`.
Diffstat (limited to 'clang/lib/Sema/AnalysisBasedWarnings.cpp')
-rw-r--r--clang/lib/Sema/AnalysisBasedWarnings.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 3d6da4f7..38aa3f0 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -550,7 +550,8 @@ struct CheckFallThroughDiagnostics {
unsigned FunKind; // TODO: use diag::FalloffFunctionKind
SourceLocation FuncLoc;
- static CheckFallThroughDiagnostics MakeForFunction(const Decl *Func) {
+ static CheckFallThroughDiagnostics MakeForFunction(Sema &S,
+ const Decl *Func) {
CheckFallThroughDiagnostics D;
D.FuncLoc = Func->getLocation();
D.diag_FallThrough_HasNoReturn = diag::warn_noreturn_has_return_expr;
@@ -564,8 +565,13 @@ struct CheckFallThroughDiagnostics {
// Don't suggest that template instantiations be marked "noreturn"
bool isTemplateInstantiation = false;
- if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func))
+ if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Func)) {
isTemplateInstantiation = Function->isTemplateInstantiation();
+ if (!S.getLangOpts().CPlusPlus && !S.getLangOpts().C99 &&
+ Function->isMain()) {
+ D.diag_FallThrough_ReturnsNonVoid = diag::ext_main_no_return;
+ }
+ }
if (!isVirtualMethod && !isTemplateInstantiation)
D.diag_NeverFallThroughOrReturn = diag::warn_suggest_noreturn_function;
@@ -2737,15 +2743,14 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
// Warning: check missing 'return'
if (P.enableCheckFallThrough) {
const CheckFallThroughDiagnostics &CD =
- (isa<BlockDecl>(D)
- ? CheckFallThroughDiagnostics::MakeForBlock()
- : (isa<CXXMethodDecl>(D) &&
- cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call &&
- cast<CXXMethodDecl>(D)->getParent()->isLambda())
- ? CheckFallThroughDiagnostics::MakeForLambda()
- : (fscope->isCoroutine()
- ? CheckFallThroughDiagnostics::MakeForCoroutine(D)
- : CheckFallThroughDiagnostics::MakeForFunction(D)));
+ (isa<BlockDecl>(D) ? CheckFallThroughDiagnostics::MakeForBlock()
+ : (isa<CXXMethodDecl>(D) &&
+ cast<CXXMethodDecl>(D)->getOverloadedOperator() == OO_Call &&
+ cast<CXXMethodDecl>(D)->getParent()->isLambda())
+ ? CheckFallThroughDiagnostics::MakeForLambda()
+ : (fscope->isCoroutine()
+ ? CheckFallThroughDiagnostics::MakeForCoroutine(D)
+ : CheckFallThroughDiagnostics::MakeForFunction(S, D)));
CheckFallThroughForBody(S, D, Body, BlockType, CD, AC);
}