[cfe-dev] [clang-tidy] Determining constexpr evaluation time

David Rector via cfe-dev cfe-dev at lists.llvm.org
Mon Aug 31 16:17:17 PDT 2020


There is `Expr::isCXX11ConstantExpr(const ASTContext &C, …)`, which returns e.g.:

```
constexpr int f(int i) {
  return i + 42;
}

int g(int i) {
  return
      f(i); // isCXX11ConstantExpr = false
}

int main() {
  int j =
      g(3); // isCXX11ConstantExpr = false

  int k =
      f(g(3)); // isCXX11ConstantExpr = false (both CallExprs)

  int l =
      f(3); // isCXX11ConstantExpr = true
}
```

It is defined in terms of `EvaluateAsRValue` FWIW.  Note that all the `EvaluateAs*` methods refer to evaluating at compile time, regardless of the `InConstantContext` argument, and (I think) all will return false whenever the expression cannot fully "fold" (i.e. if there will necessarily be run-time dependencies remaining in the expression, or there was an error during evaluation).

Hope that helps,

Dave

> On Aug 30, 2020, at 7:15 PM, Connor Davis via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> Hello all,
> 
> Is it possible to determine if a CallExpr will be evaluated
> at compile time from clang-tidy? I notice some references to
> InConstantContext in the EvaluateAs* functions in ExprConstant.cpp,
> but I'm not sure if that is applicable/usable from clang-tidy.
> 
> Thanks,
> Connor Davis
> 
> 
> 
> 
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200831/b642c298/attachment.html>


More information about the cfe-dev mailing list