-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[clang]: Propagate *noreturn
attributes in CFG
#146355
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
base: main
Are you sure you want to change the base?
Changes from all commits
fc3b77d
0b2e72d
3562ea0
06cbe1d
400dbd1
918475d
c738e07
a581a5c
737e9ca
f2f356e
7b3efaf
a7c20d4
560f7ba
f590dd9
424c067
375480e
d1d33a3
64e7689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -974,7 +974,8 @@ def AnalyzerNoReturn : InheritableAttr { | |
// vendor namespace, or should it use a vendor namespace specific to the | ||
// analyzer? | ||
let Spellings = [GNU<"analyzer_noreturn">]; | ||
// TODO: Add subject list. | ||
let Args = [DefaultBoolArgument<"Value", /*default=*/1, /*fake=*/0>]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
let Subjects = SubjectList<[Function]>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could attach this attribute to |
||
let Documentation = [Undocumented]; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3596,6 +3596,16 @@ bool FunctionDecl::isNoReturn() const { | |||||||||||||||||||||
return false; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
std::optional<bool> FunctionDecl::getAnalyzerNoReturn() const { | ||||||||||||||||||||||
if (isNoReturn()) | ||||||||||||||||||||||
return true; | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (auto *Attr = getAttr<AnalyzerNoReturnAttr>()) | ||||||||||||||||||||||
return Attr->getValue(); | ||||||||||||||||||||||
|
||||||||||||||||||||||
return std::nullopt; | ||||||||||||||||||||||
Comment on lines
+3600
to
+3606
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot use // getAnalyzerNoReturn() => std::nullopt as we don't have explicit value of `analyzer_noreturn` attr
void foo(); // getAnalyzerNoReturn() => `false`, this function 100% returns control back to caller
void foo() __attribute__((analyzer_noreturn(false))); // getAnalyzerNoReturn() => `true`, this function is no-return
void foo() __attribute__((analyzer_noreturn(true))); |
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
bool FunctionDecl::isMemberLikeConstrainedFriend() const { | ||||||||||||||||||||||
// C++20 [temp.friend]p9: | ||||||||||||||||||||||
// A non-template friend declaration with a requires-clause [or] | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2833,8 +2833,8 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { | |
if (!FD->isVariadic()) | ||
findConstructionContextsForArguments(C); | ||
|
||
if (FD->isNoReturn() || C->isBuiltinAssumeFalse(*Context)) | ||
NoReturn = true; | ||
NoReturn |= FD->getAnalyzerNoReturn().value_or(false) || C->isBuiltinAssumeFalse(*Context); | ||
|
||
if (FD->hasAttr<NoThrowAttr>()) | ||
AddEHEdge = false; | ||
if (isBuiltinAssumeWithSideEffects(FD->getASTContext(), C) || | ||
|
@@ -6288,6 +6288,12 @@ void CFGBlock::printTerminatorJson(raw_ostream &Out, const LangOptions &LO, | |
// There may be many more reasons why a sink would appear during analysis | ||
// (eg. checkers may generate sinks arbitrarily), but here we only consider | ||
// sinks that would be obvious by looking at the CFG. | ||
// | ||
// This function also performs inter-procedural analysis by recursively | ||
// examining called functions to detect forwarding chains to noreturn | ||
// functions. When a function is determined to never return through this | ||
// analysis, it's automatically marked with analyzer_noreturn attribute | ||
// for caching and future reference. | ||
static bool isImmediateSinkBlock(const CFGBlock *Blk) { | ||
if (Blk->hasNoReturnElement()) | ||
return true; | ||
|
@@ -6298,10 +6304,60 @@ static bool isImmediateSinkBlock(const CFGBlock *Blk) { | |
// at least for now, but once we have better support for exceptions, | ||
// we'd need to carefully handle the case when the throw is being | ||
// immediately caught. | ||
if (llvm::any_of(*Blk, [](const CFGElement &Elm) { | ||
if (llvm::any_of(*Blk, [](const CFGElement &Elm) -> bool { | ||
if (std::optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>()) | ||
return isa<CXXThrowExpr>(StmtElm->getStmt()); | ||
return false; | ||
})) | ||
return true; | ||
|
||
auto HasNoReturnCall = [&](const CallExpr *CE) { | ||
if (!CE) | ||
return false; | ||
|
||
auto *FD = CE->getDirectCallee(); | ||
|
||
if (!FD) | ||
return false; | ||
|
||
auto *CanCD = FD->getCanonicalDecl(); | ||
auto *DefFD = CanCD->getDefinition(); | ||
auto NoRetAttrOpt = CanCD->getAnalyzerNoReturn(); | ||
auto NoReturn = false; | ||
|
||
if (!NoRetAttrOpt && DefFD && DefFD->getBody()) { | ||
// HACK: we are gonna cache analysis result as implicit | ||
// `analyzer_noreturn` attribute | ||
auto *MutCD = const_cast<FunctionDecl *>(CanCD); | ||
Comment on lines
+6328
to
+6331
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see now how you use the implicit attribute to cache this deduced noreturn property. int fib(int n) {
return fib(n - 2) + fib(n - 1);
} How I understand, while building the CFG of There are ways around this, but it's not easy during CFG construction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should treat recursive calls as creating 'no-return' sinks - it would make the |
||
|
||
// Mark function as `analyzer_noreturn(false)` to: | ||
// * prevent infinite recursion in noreturn analysis | ||
// * indicate that we've already analyzed(-ing) this function | ||
// * serve as a safe default assumption (function may return) | ||
MutCD->addAttr(AnalyzerNoReturnAttr::CreateImplicit( | ||
CanCD->getASTContext(), false, CanCD->getLocation())); | ||
|
||
auto CalleeCFG = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While performance-wise this is a big step ahead, we might still end up building the CFG twice for large functions. Once when we first do this inter-procedural no-return analysis and once when we run the actual analysis on them. Did you do any benchmarking if this has any affect on the compilation with some of the frequently used configurations? (default warnings, In case there is a measurable regression, we might want to move this behind a flag. |
||
CFG::buildCFG(DefFD, DefFD->getBody(), &DefFD->getASTContext(), {}); | ||
|
||
NoReturn = CalleeCFG && CalleeCFG->getEntry().isInevitablySinking(); | ||
|
||
// Override to `analyzer_noreturn(true)` | ||
if (NoReturn) { | ||
MutCD->dropAttr<AnalyzerNoReturnAttr>(); | ||
MutCD->addAttr(AnalyzerNoReturnAttr::CreateImplicit( | ||
CanCD->getASTContext(), NoReturn, CanCD->getLocation())); | ||
} | ||
|
||
} else if (NoRetAttrOpt) | ||
NoReturn = *NoRetAttrOpt; | ||
|
||
return NoReturn; | ||
}; | ||
|
||
if (llvm::any_of(*Blk, [&](const CFGElement &Elm) { | ||
if (std::optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>()) | ||
if (isa<CXXThrowExpr>(StmtElm->getStmt())) | ||
return true; | ||
return HasNoReturnCall(dyn_cast<CallExpr>(StmtElm->getStmt())); | ||
return false; | ||
})) | ||
return true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this function would suggest that it only checks the presence of
analyzer_noreturn
.How about if we would rename this to something like
isNoreturnForAnalyses
?BTW, how about other callables that are not FunctionDecls?
Such as
ObjCMethodDecl
orBlockDecl
?