Issue 233806
Summary [CFG] Range-based for loop has wrong iterator initialization order
Labels clang:analysis
Assignees
Reporter steakhal
    In the CFG we should see `__begin` initialized before `__end`, and in the end see `__end` destructed before `__begin`.

In the CFG today, the destruction order is correct - but the initialization order is swapped.
https://godbolt.org/z/GKGGhPz5e

```c++
struct B { ~B(); };
struct E { ~E(); };
bool operator!=(const B&, const E&);
void operator++(B&);
int& operator*(const B&);

struct V {
    B begin();
 E end();
};

void test(V v) {
    for (int& x : v) (void)x;
    // In a range-based for loop, [stmt.ranged] initializes __begin before __end:
    // auto __begin = begin-expr;
    //   auto __end   = end-expr;
    // and destroys them in reverse, i.e. __end first.
}
```

Have look at the (correct) destruction order:
```
 [B1]
   1: [B5.7].~E() (Implicit destructor)
   2: [B5.12].~B() (Implicit destructor)
   Preds (1): B2
 Succs (1): B0
```

Next look at the (incorrect) initialization order:
```
 [B5]
   1: v
   2: auto &&__range1 = v;
   3: __range1
 4: [B5.3].end
   5: [B5.4]() (CXXRecordTypedCall, [B5.7], [B5.6])
   6: [B5.5] (BindTemporary)
   7: auto __end1 = __range1.end();   <----- ouch!
 8: __range1
   9: [B5.8].begin
  10: [B5.9]() (CXXRecordTypedCall, [B5.12], [B5.11])
  11: [B5.10] (BindTemporary)
  12: auto __begin1 = __range1.begin();  <----
   Preds (1): B6
   Succs (1): B2
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to