[llvm-bugs] [Bug 52174] New: Call to constexpr static method is misclassified as having side effects
via llvm-bugs
llvm-bugs at lists.llvm.org
Thu Oct 14 03:29:57 PDT 2021
https://bugs.llvm.org/show_bug.cgi?id=52174
Bug ID: 52174
Summary: Call to constexpr static method is misclassified as
having side effects
Product: clang
Version: 12.0
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: C++
Assignee: unassignedclangbugs at nondot.org
Reporter: maxidlabs at gmail.com
CC: blitzrakete at gmail.com, dgregor at apple.com,
erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
richard-llvm at metafoo.co.uk
I am not sure if the following code is supposed to compile according to the
*Standard*, but:
* Both GCC and MSVC compile it fine.
* Looking at the snippet I can't see how the code would be ill-formed.
=============================
struct MyStruct {
static constexpr int get_value()
{
return 1 + 7;
}
};
void turbo_func(const MyStruct& varRef)
{
// OK with clang, gcc, msvc
constexpr int a = MyStruct::get_value();
// OK with gcc and msvc, but err_constexpr_var_requires_const_init with clang
constexpr int b = varRef.get_value();
}
=============================
I investigated and made the following observations:
In ExprConstant.cpp: LValueExprEvaluator::VisitMemberExpr() there is a block of
code:
// Handle static member functions.
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
if (MD->isStatic()) {
VisitIgnoredBaseExpression(E->getBase());
return Success(MD);
}
}
When 'VisitIgnoredBaseExpression' is called, the function ExprConstant.cpp:
LValueExprEvaluator::VisitVarDecl returns 'false' because the 'varRef' is a
'ParmVarDecl', and this in turn makes the function 'EvaluateIgnoredValue' call
Info.noteSideEffect().
To test this theory, I modified the 'LValueExprEvaluator::VisitMemberExpr' to
look like this:
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
if (MD->isStatic()) {
if ( const DeclRefExpr *baseDeclRefExpr = dyn_cast<DeclRefExpr>(
E->getBase() ) )
if ( const VarDecl *baseVarDecl = dyn_cast<VarDecl>(
baseDeclRefExpr->getDecl() ) )
if ( isa<ParmVarDecl>( baseVarDecl ) )
return Success(MD);
VisitIgnoredBaseExpression(E->getBase());
return Success(MD);
}
}
Basically this just (hopefully) tells clang that if base expression is a
function param, then it is fine to compile it.
Using this modified clang I was able to build the mentioned test program, which
had the desired behavior.
Can you please suggest if the *current* clang behavior is expected, given that
the mentioned constexpr call does not seem to have side effects?
Thanks!
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211014/147064f0/attachment-0001.html>
More information about the llvm-bugs
mailing list