-
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
3f55041
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>]; | ||
steakhal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 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'd prefer not to overcomplicate things right now and just focus on the core approach and C++ implementation - mainly because I'm completely clueless about testing ObjC code =) |
||
let Documentation = [Undocumented]; | ||
} | ||
|
||
|
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); | ||
steakhal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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. 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've discussed this with @Xazax-hun and I agree with him that we should have a look at the performance of some warnings. I think they live under the AnalysisBasedWarnings.cpp file. Another problem with the use of this 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. @steakhal @Xazax-hun I'm thinking about making this feature configurable through a flag. It will clearly have significant performance implications, but it's really only required for specific use cases (like clang-tidy checkers). It might be safer to explicitly opt-in only where necessary and where performance drops are more acceptable (e.g., in the unchecked-optional-access checker) |
||
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())); | ||
Comment on lines
+6347
to
+6349
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. Another problem with this attribute that we drop the previous one. 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 will drop this attribute only if we've created it before: auto *CanCD = FD->getCanonicalDecl();
auto *DefFD = CanCD->getDefinition();
auto NoRetAttrOpt = CanCD->getAnalyzerNoReturn();
auto NoReturn = false;
if (!NoRetAttrOpt && DefFD && DefFD->getBody()) {
...
// Override to `analyzer_noreturn(true)`
if (NoReturn) {
|
||
} | ||
|
||
} 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
?