[clang-format] Don't crash on malformed preprocessor conditions
authorsstwcw <f0gukp2nk@protonmail.com>
Sun, 30 Oct 2022 02:18:58 +0000 (02:18 +0000)
committersstwcw <f0gukp2nk@protonmail.com>
Sun, 30 Oct 2022 02:18:58 +0000 (02:18 +0000)
commitd5be1550f1406844501ab87a3e92622927a203e0
tree308ef618d22e13838e2f18797df761fe6b183527
parent5de49af54036f1427ae8669b6482a198cbfeff2a
[clang-format] Don't crash on malformed preprocessor conditions

Previously the program would crash on this input:

```
#else
#if X
```

The problem was that in `parsePPElse`, `PPBranchLevel` would be
incremented when the first `#else` gets parsed, but `PPLevelBranchCount`
and `PPLevelBranchIndex` would not be updated together.

I found the problem when working on D135740.

Differential Revision: https://reviews.llvm.org/D135972
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp