Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -8264,6 +8264,8 @@ def err_mixing_cxx_try_seh_try : Error<
"in the same function as SEH '__try'">;
def err_seh_try_unsupported : Error<
"SEH '__try' is not supported on this target">;
def err_seh_try_dtor : Error<"cannot use C++ object with a destructor "
"in the same function as SEH '__try'">;
def note_conflicting_try_here : Note<
"conflicting %0 here">;
def warn_jump_out_of_seh_finally : Warning<
Expand Down
45 changes: 45 additions & 0 deletions clang/lib/Sema/AnalysisBasedWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,47 @@ static bool isNoexcept(const FunctionDecl *FD) {
return false;
}

//===----------------------------------------------------------------------===//
// Check for SEH __try in a function with C++ objects that have destructors.
//===----------------------------------------------------------------------===//

static void emitDiagForSehTryUnwind(Sema &S, CFGElement &E) {
if (auto AD = E.getAs<CFGAutomaticObjDtor>()) {
const auto *VD = AD->getVarDecl();
S.Diag(VD->getLocation(), diag::err_seh_try_dtor);
} else if (auto TD = E.getAs<CFGTemporaryDtor>()) {
const auto *E = TD->getBindTemporaryExpr();
S.Diag(E->getBeginLoc(), diag::err_seh_try_dtor);
} else
llvm_unreachable("emitDiagForSehTryUnwind should only be used with "
"AutomaticObjectDtor or TemporaryDtor");
}

static void checkSehTryNeedsUnwind(Sema &S, const FunctionDecl *FD,
AnalysisDeclContext &AC) {
if (!FD->usesSEHTry())
return;
CFG *BodyCFG = AC.getCFG();
if (!BodyCFG)
return;
if (BodyCFG->getExit().pred_empty())
return;

llvm::BitVector Reachable(BodyCFG->getNumBlockIDs());
clang::reachable_code::ScanReachableFromBlock(&BodyCFG->getEntry(),
Reachable);
for (CFGBlock *B : *BodyCFG) {
if (!Reachable[B->getBlockID()])
continue;
for (CFGElement &E : *B) {
auto Kind = E.getKind();
if (Kind == CFGElement::AutomaticObjectDtor ||
Kind == CFGElement::TemporaryDtor)
emitDiagForSehTryUnwind(S, E);
}
}
}

//===----------------------------------------------------------------------===//
// Check for missing return value.
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2821,6 +2862,10 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
if (S.getLangOpts().CPlusPlus && !fscope->isCoroutine() && isNoexcept(FD))
checkThrowInNonThrowingFunc(S, FD, AC);

if (S.getLangOpts().CPlusPlus)
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
checkSehTryNeedsUnwind(S, FD, AC);

// If none of the previous checks caused a CFG build, trigger one here
// for the logical error handler.
if (LogicalErrorHandler::hasActiveDiagnostics(Diags, D->getBeginLoc())) {
Expand Down
18 changes: 18 additions & 0 deletions clang/test/Sema/seh-cxx-dtors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -fms-extensions -verify %s

struct Foo {
~Foo() {}
};

// These need '-fms-extensions'
void f1() {
Foo foo {}; // expected-error {{destructor}}
__try {} __except (1) {}
}

void bar(Foo foo);

void f2() {
bar(Foo {}); // expected-error {{destructor}}
__try {} __except (1) {}
}
Loading