Skip to content

Commit

Permalink
Fix crashes on 'StructuralValue'
Browse files Browse the repository at this point in the history
`OpaqueValueExpr` doesn't necessarily contain a source expression.
Particularly, after #78041, it is used to carry the type and the value
kind of a non-type template argument of floating-point type or referring
to a subobject (those are so called `StructuralValue` arguments).

This fixes #79575.
  • Loading branch information
bolshakov-a committed Jan 29, 2024
1 parent 72fd10a commit 474f866
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12753,7 +12753,8 @@ namespace {
}

if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) {
HandleValue(OVE->getSourceExpr());
if (Expr *SE = OVE->getSourceExpr())
HandleValue(SE);
return;
}

Expand Down
8 changes: 5 additions & 3 deletions clang/lib/StaticAnalyzer/Core/Environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ static const Expr *ignoreTransparentExprs(const Expr *E) {

switch (E->getStmtClass()) {
case Stmt::OpaqueValueExprClass:
E = cast<OpaqueValueExpr>(E)->getSourceExpr();
break;
if (const Expr *SE = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
E = SE;
break;
}
return E;
case Stmt::ExprWithCleanupsClass:
E = cast<ExprWithCleanups>(E)->getSubExpr();
break;
Expand Down Expand Up @@ -98,7 +101,6 @@ SVal Environment::getSVal(const EnvironmentEntry &Entry,
case Stmt::CXXBindTemporaryExprClass:
case Stmt::ExprWithCleanupsClass:
case Stmt::GenericSelectionExprClass:
case Stmt::OpaqueValueExprClass:
case Stmt::ConstantExprClass:
case Stmt::ParenExprClass:
case Stmt::SubstNonTypeTemplateParmExprClass:
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Analysis/templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ namespace rdar13954714 {
// force instantiation
template void blockWithStatic<true>();
}

namespace structural_value_crash {
constexpr char abc[] = "abc";

template <const char* in>
void use_template_param() {
const char *p = in;
}

void force_instantiate() {
use_template_param<abc>();
}
}

0 comments on commit 474f866

Please sign in to comment.