<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Call to constexpr static method is misclassified as having side effects"
   href="https://bugs.llvm.org/show_bug.cgi?id=52174">52174</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Call to constexpr static method is misclassified as having side effects
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>12.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>maxidlabs@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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!</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>