Skip to content

Commit df8a3df

Browse files
bolshakov-atstellar
authored andcommitted
Fix analyzer crash on 'StructuralValue' (llvm#79764)
`OpaqueValueExpr` doesn't necessarily contain a source expression. Particularly, after llvm#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 llvm#79575. (cherry picked from commit ef67f63)
1 parent dc740e3 commit df8a3df

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

clang/lib/Sema/SemaDecl.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -12752,7 +12752,8 @@ namespace {
1275212752
}
1275312753

1275412754
if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) {
12755-
HandleValue(OVE->getSourceExpr());
12755+
if (Expr *SE = OVE->getSourceExpr())
12756+
HandleValue(SE);
1275612757
return;
1275712758
}
1275812759

clang/lib/StaticAnalyzer/Core/Environment.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ static const Expr *ignoreTransparentExprs(const Expr *E) {
4040

4141
switch (E->getStmtClass()) {
4242
case Stmt::OpaqueValueExprClass:
43-
E = cast<OpaqueValueExpr>(E)->getSourceExpr();
44-
break;
43+
if (const Expr *SE = cast<OpaqueValueExpr>(E)->getSourceExpr()) {
44+
E = SE;
45+
break;
46+
}
47+
return E;
4548
case Stmt::ExprWithCleanupsClass:
4649
E = cast<ExprWithCleanups>(E)->getSubExpr();
4750
break;
@@ -98,7 +101,6 @@ SVal Environment::getSVal(const EnvironmentEntry &Entry,
98101
case Stmt::CXXBindTemporaryExprClass:
99102
case Stmt::ExprWithCleanupsClass:
100103
case Stmt::GenericSelectionExprClass:
101-
case Stmt::OpaqueValueExprClass:
102104
case Stmt::ConstantExprClass:
103105
case Stmt::ParenExprClass:
104106
case Stmt::SubstNonTypeTemplateParmExprClass:

clang/test/Analysis/templates.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,16 @@ namespace rdar13954714 {
6868
// force instantiation
6969
template void blockWithStatic<true>();
7070
}
71+
72+
namespace structural_value_crash {
73+
constexpr char abc[] = "abc";
74+
75+
template <const char* in>
76+
void use_template_param() {
77+
const char *p = in;
78+
}
79+
80+
void force_instantiate() {
81+
use_template_param<abc>();
82+
}
83+
}

clang/test/SemaTemplate/temp_arg_nontype_cxx20.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,20 @@ namespace ReportedRegression1 {
354354
return dummy.exit_code();
355355
}
356356
}
357+
358+
namespace ReportedRegression2 {
359+
const char str[] = "dummy";
360+
361+
struct S {
362+
S operator+(const char*) const;
363+
};
364+
365+
template <const char* in>
366+
void fn() {
367+
auto s = S{} + in;
368+
}
369+
370+
void use() {
371+
fn<str>();
372+
}
373+
}

0 commit comments

Comments
 (0)