[PATCH] D85528: [analyzer] Fix cast evaluation on scoped enums in ExprEngine

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 9 03:30:54 PDT 2020


steakhal added a comment.

In D85528#2205094 <https://reviews.llvm.org/D85528#2205094>, @xazax.hun wrote:

> Looks reasonable to me, but I am not very familiar with the impacts of the additional casts. Do we lose some modeling power when we are using the regular constraint solver?
>
> For example, when we have constraints both on the original and the cased symbol can the analyzer "merge" them?
>
> Something like:
>
>   ScopedPrimitive sym = conjure<ScopedPrimitive>();
>   if (sym == ScopedPrimitive::Max)
>     return;
>   int sym2 = static_cast<unsigned char>(sym);
>   if (sym2 == 0)
>     return;
>   // Do we know here that both sym and sym2 has the same range?
>   // Is there a change in the behavior compared to before the patch?

Huh, it's a really interesting question.
Here is the result:

---

Here is the test code:

  enum class ScopedPrimitive : unsigned char { Min = 2, Max = 8 };
  void foo() {
    auto sym = conjure<ScopedPrimitive>();
    if (sym == ScopedPrimitive::Max)
      return;
  
    int sym2 = static_cast<unsigned char>(sym);
    if (sym2 == 0)
      return;
  
    // Do we know here that both sym and sym2 has the same range?
    // Is there a change in the behavior compared to before the patch?
    clang_analyzer_printState();
    (void)sym;
    (void)sym2;
  }

Before the patch:

  "constraints": [
      { "symbol": "conj_$2{enum ScopedPrimitive, LC1, S1083, #1}", "range": "{ [1, 7], [9, 255] }" }
    ]

After the patch:

  "constraints": [
      { "symbol": "conj_$2{enum ScopedPrimitive, LC1, S1881, #1}", "range": "{ [0, 7], [9, 255] }" },
      { "symbol": "(unsigned char) (conj_$2{enum ScopedPrimitive, LC1, S1881, #1})", "range": "{ [1, 255] }" }
    ]



---

> For example, when we have constraints both on the original and the cased symbol can the analyzer "merge" them?

Apparently, not xD.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85528/new/

https://reviews.llvm.org/D85528



More information about the cfe-commits mailing list