Skip to content

Commit 28c9745

Browse files
committed
Minor changes to function definition
Add templated test case
1 parent 81773c5 commit 28c9745

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ enum class CCEKind {
834834
///< message.
835835
};
836836

837-
void inferNoReturnAttr(Sema &S, FunctionDecl *FD);
837+
void inferNoReturnAttr(Sema &S, const Decl *D);
838838

839839
/// Sema - This implements semantic analysis and AST building for C.
840840
/// \nosubgrouping

clang/lib/Sema/Sema.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,9 +2435,7 @@ Sema::PopFunctionScopeInfo(const AnalysisBasedWarnings::Policy *WP,
24352435

24362436
// Issue any analysis-based warnings.
24372437
if (WP && D) {
2438-
if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2439-
inferNoReturnAttr(*this, const_cast<FunctionDecl *>(FD));
2440-
}
2438+
inferNoReturnAttr(*this, D);
24412439
AnalysisWarnings.IssueWarnings(*WP, Scope.get(), D, BlockType);
24422440
} else
24432441
for (const auto &PUD : Scope->PossiblyUnreachableDiags)

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,15 +1965,20 @@ static bool isKnownToAlwaysThrow(const FunctionDecl *FD) {
19651965
return isa<CXXThrowExpr>(OnlyStmt);
19661966
}
19671967

1968-
void clang::inferNoReturnAttr(Sema &S, FunctionDecl *FD) {
1968+
void clang::inferNoReturnAttr(Sema &S, const Decl *D) {
1969+
auto *FD = dyn_cast<FunctionDecl>(D);
1970+
if (!FD)
1971+
return;
1972+
1973+
auto *NonConstFD = const_cast<FunctionDecl *>(FD);
19691974
DiagnosticsEngine &Diags = S.getDiagnostics();
19701975
if (Diags.isIgnored(diag::warn_falloff_nonvoid, FD->getLocation()) &&
19711976
Diags.isIgnored(diag::warn_suggest_noreturn_function, FD->getLocation()))
19721977
return;
19731978

19741979
if (!FD->hasAttr<NoReturnAttr>() && !FD->hasAttr<InferredNoReturnAttr>() &&
19751980
isKnownToAlwaysThrow(FD)) {
1976-
FD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
1981+
NonConstFD->addAttr(InferredNoReturnAttr::CreateImplicit(S.Context));
19771982
}
19781983
}
19791984

clang/test/SemaCXX/wreturn-always-throws.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace std {
1212
};
1313
}
1414

15+
// Non-template version.
16+
1517
void throwError(const std::string& msg) {
1618
throw std::runtime_error(msg);
1719
}
@@ -24,3 +26,21 @@ int ensureZero(const int i) {
2426
int alwaysThrows() {
2527
throw std::runtime_error("This function always throws"); // no-warning
2628
}
29+
30+
// Template version.
31+
32+
template<typename T>
33+
void throwErrorTemplate(const T& msg) {
34+
throw msg;
35+
}
36+
37+
template <typename T>
38+
int ensureZeroTemplate(T i) {
39+
if (i == 0) return 0;
40+
throwErrorTemplate("ERROR"); // no-warning
41+
}
42+
43+
void testTemplates() {
44+
throwErrorTemplate("ERROR");
45+
(void)ensureZeroTemplate(42);
46+
}

0 commit comments

Comments
 (0)