Skip to content

[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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,42 @@ void nullable_value_after_swap(BloombergLP::bdlb::NullableValue<int> &opt1, Bloo
}
}

void assertion_handler_imp() __attribute__((analyzer_noreturn));

void assertion_handler() {
do {
assertion_handler_imp();
} while(0);
}

void function_calling_analyzer_noreturn(const bsl::optional<int>& opt)
{
if (!opt) {
assertion_handler();
}

*opt; // no-warning
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Author

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

Copy link
Contributor

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.

Copy link
Contributor

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

}

void abort();

void do_fail() {
abort(); // acts like 'abort()' C-function
}

void invoke_assertion_handler() {
do_fail();
}

void function_calling_well_known_noreturn(const bsl::optional<int>& opt)
{
if (!opt) {
invoke_assertion_handler();
}

*opt; // no-warning
}

template <typename T>
void function_template_without_user(const absl::optional<T> &opt) {
opt.value(); // no-warning
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2624,6 +2624,11 @@ class FunctionDecl : public DeclaratorDecl,
/// an attribute on its declaration or its type.
bool isNoReturn() const;

/// Determines whether this function is known to never return for CFG
/// analysis. Checks for noreturn attributes on the function declaration
/// or its type, including 'analyzer_noreturn' attribute.
bool isAnalyzerNoReturn() const;

/// True if the function was a definition but its body was skipped.
bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; }
void setHasSkippedBody(bool Skipped = true) {
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3603,6 +3603,10 @@ bool FunctionDecl::isNoReturn() const {
return false;
}

bool FunctionDecl::isAnalyzerNoReturn() const {
return isNoReturn() || hasAttr<AnalyzerNoReturnAttr>();
}

bool FunctionDecl::isMemberLikeConstrainedFriend() const {
// C++20 [temp.friend]p9:
// A non-template friend declaration with a requires-clause [or]
Expand Down
62 changes: 60 additions & 2 deletions clang/lib/Analysis/CFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we consolidate this list with the one in NoReturnFunctionChecker.cpp? Or at least reference the other one to remind people to update both.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeWeller i've added comment in NoreturnFunctionChecker.cpp

if (!NoReturn)
NoReturn = llvm::StringSwitch<bool>(FD->getQualifiedNameAsString())
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) ||
Expand Down Expand Up @@ -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(), {});
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it (please correct me if I'm wrong), FunctionDecl* instances within the constructed AST will be reused when building the CFG - if so, we could do something like:

if( FD->isAnalyzerNoReturn() || NoReturnFromCFG() ) {
     const_cast<FunctionDecl *>(FD)->addAttr(AnalyzerNoReturnAttr::Create(
      FD->getASTContext(), FD->getLocation()));
     return true;
   }

Copy link
Contributor

Choose a reason for hiding this comment

The 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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ computeBlockInputState(const CFGBlock &Block, AnalysisContext &AC) {
JoinedStateBuilder Builder(AC, JoinBehavior);
for (const CFGBlock *Pred : Preds) {
// Skip if the `Block` is unreachable or control flow cannot get past it.
if (!Pred || Pred->hasNoReturnElement())
if (!Pred || Pred->isInevitablySinking())
continue;

// Skip if `Pred` was not evaluated yet. This could happen if `Pred` has a
Expand Down Expand Up @@ -562,7 +562,7 @@ runTypeErasedDataflowAnalysis(
BlockStates[Block->getBlockID()] = std::move(NewBlockState);

// Do not add unreachable successor blocks to `Worklist`.
if (Block->hasNoReturnElement())
if (Block->isInevitablySinking())
continue;

Worklist.enqueueSuccessors(Block);
Expand Down