-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[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 1 commit
fc3b77d
0b2e72d
3562ea0
06cbe1d
400dbd1
918475d
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 |
---|---|---|
|
@@ -2835,8 +2835,37 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { | |
if (!FD->isVariadic()) | ||
findConstructionContextsForArguments(C); | ||
|
||
if (FD->isNoReturn() || C->isBuiltinAssumeFalse(*Context)) | ||
NoReturn = true; | ||
if (!NoReturn) | ||
NoReturn = FD->isAnalyzerNoReturn() || C->isBuiltinAssumeFalse(*Context); | ||
|
||
// Some well-known 'noreturn' functions | ||
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. Can we consolidate this list with the one in 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. @MikeWeller i've added comment in |
||
if (!NoReturn) | ||
NoReturn = llvm::StringSwitch<bool>(FD->getQualifiedNameAsString()) | ||
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. @AaronBallman I wonder how you feel about having a list of well known (non standard library) functions hardcoded in the compiler? I wonder if we want to have a more principled approach here long term, e.g., having one place in the compiler that injects annotations (like noreturn), and the rest of the code paths only handling that one annotation. |
||
.Case("BloombergLP::bsls::Assert::invokeHandler", true) | ||
.Case("std::terminate", true) | ||
.Case("std::abort", true) | ||
.Case("exit", true) | ||
.Case("abort", true) | ||
.Case("panic", true) | ||
.Case("error", true) | ||
.Case("Assert", true) | ||
.Case("ziperr", true) | ||
.Case("assfail", true) | ||
.Case("db_error", true) | ||
.Case("__assert", true) | ||
.Case("__assert2", true) | ||
.Case("_wassert", true) | ||
.Case("__assert_rtn", true) | ||
.Case("__assert_fail", true) | ||
.Case("dtrace_assfail", true) | ||
.Case("yy_fatal_error", true) | ||
.Case("_XCAssertionFailureHandler", true) | ||
.Case("_DTAssertionFailureHandler", true) | ||
.Case("_TSAssertionFailureHandler", true) | ||
.Case("__builtin_trap", true) | ||
.Case("__builtin_unreachable", true) | ||
.Default(false); | ||
|
||
if (FD->hasAttr<NoThrowAttr>()) | ||
AddEHEdge = false; | ||
if (isBuiltinAssumeWithSideEffects(FD->getASTContext(), C) || | ||
|
@@ -6308,6 +6337,35 @@ static bool isImmediateSinkBlock(const CFGBlock *Blk) { | |
})) | ||
return true; | ||
|
||
auto HasNoReturnCall = [](const CallExpr *CE) { | ||
if (!CE) | ||
return false; | ||
|
||
auto *FD = CE->getDirectCallee(); | ||
|
||
if (!FD) | ||
return false; | ||
|
||
auto NoReturnFromCFG = [FD]() { | ||
if (!FD->getBody()) | ||
return false; | ||
|
||
auto CalleeCFG = | ||
CFG::buildCFG(FD, FD->getBody(), &FD->getASTContext(), {}); | ||
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. Is it possible that we end up building the CFG for the same functions over and over again? This sounds like potentially wasteful. 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. As I understand it (please correct me if I'm wrong), if( FD->isAnalyzerNoReturn() || NoReturnFromCFG() ) {
const_cast<FunctionDecl *>(FD)->addAttr(AnalyzerNoReturnAttr::Create(
FD->getASTContext(), FD->getLocation()));
return true;
} 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. If we decide to create attrs, be sure to create them as implicit, as they were not spelled in source. |
||
|
||
return CalleeCFG && CalleeCFG->getEntry().isInevitablySinking(); | ||
}; | ||
|
||
return FD->isAnalyzerNoReturn() || NoReturnFromCFG(); | ||
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. What if we have redecls, does it matter which has the attribute, or do we always bind the attribute to the canonical decl? |
||
}; | ||
|
||
if (llvm::any_of(*Blk, [&](const CFGElement &Elm) { | ||
if (std::optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>()) | ||
return HasNoReturnCall(dyn_cast<CallExpr>(StmtElm->getStmt())); | ||
return false; | ||
})) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
|
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.
Is
no-warning
handled by the test harness? I'm familiar/have seen// CHECK-NOT: warning
comments. Unless any extraneous warnings already fail the test.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.
It's just a comment that we do not expect warning here.
check_clang_tidy.py
ignores it.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.
Anyway, i consider moving this kind of check to
clang\unittests\Analysis\FlowSensitive\UncheckedOptionalAccessModelTest.cpp
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.
OK, I was just checking/asking whether the test will fail if a warning is emitted or whether you need a
// CHECK-NOT: warning
. But it seems for most tests any warning will produce a failure.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.
clang-tidy tests require an exact match of diagnostics, so too many or too few diagnostics will fail the test