[mlir][tblgen] Fix undefined behaviour found by MSVC debug iterators
authorMarkus Böck <markus.boeck02@gmail.com>
Sat, 14 Jan 2023 16:41:15 +0000 (17:41 +0100)
committerMarkus Böck <markus.boeck02@gmail.com>
Sat, 14 Jan 2023 16:49:48 +0000 (17:49 +0100)
commit1e60e7add7ce18a652a726d5ec267547399c744e
tree2908ff31b669cd1c6b4c03ead73e0efcd1060402
parente2d0eeba43268875ab198132c36d7486bcb9ecd6
[mlir][tblgen] Fix undefined behaviour found by MSVC debug iterators

Incrementing past the end iterator of any container in C++ is immediate undefined behaviour.
This is guaranteed to occur in the loop condition due to the expression cur = earlyIncIt++, which when earlyIncIt is the end iterator (aka we just did the last iteration of the loop), will do an increment on the end iterator.

To fix this, the patch refactors the loop to a more conventional loop using iterators, with the only difference being that the increment happens through the erase operation, which conveniently returns an iterator to the element after the erased element. Thanks to that guarantee there is also no need to use std::list over std::vector.
I also opted to reduce the inner loop find_if, because I felt splitting the "search" and the effects of if it was successful made the code (subjectively) nicer, and also avoided having to add an extra "bool erased" to the outer loop body.

Differential Revision: https://reviews.llvm.org/D141758
mlir/lib/TableGen/Operator.cpp