<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJylVU1v2zoQ_DXyZVFBkezYPviQNgkaoC-9pGivlLiW2FCkQFJ2_H59h7TdKkbSSwEhivixMzs7u66tPGw-q6z8lBW3WXFz_PudyXHDJugDOWFImWDJ256pYyFF0zHJkQmLjZV4dyKQVNJk5TKQGAZcCziTXRdZeY-HGi1M-2FrXS9CVt2Q3W6xbA1OUBCtRxwHROCBCWmAKNP-ju-VPnIxNlDNcesYKrAk4UkF8p0dtczp7kX0g2YPBNpHWluByxKY0_wisfQ0w3BaeZ9n2s_zfHrwvePmIv6_gZ72v3l2HoIq85xkFbXdMbAgiFRe1DHdkx4nadIxwy-BtDIcJfWqV1q4JGmCC0pC66Wnx69fHh6fHu9-POF9d5HtRdqXVC-ZfjUN6LVCmYgJEodoHTYy4p64TqmmHN7kW48BV-FCoVU4JIclttEBqa4dbMbG_0Xv79ER0Rs102C9VxEcPFQ0SA9DEZzNzgG-Z-9FezIy3N08J0VpNGCKbxmtjOLCssgYnh2Ecj6r7ukptoJtxhhPBGUN7KxxGzVCBlkJZW7QQS374_-XRqzu6NNEUyBKFDsIIxFDe3TdwI0SGo3QRwx_5Oj3CrymUkIsQZI16hy7ImFOkR5yziHGbxHPpHE2RbSGqYEgP0cfsOLsHvGiWd8wJwQQIdWs5lYZk0q5TQtbdBt0leSDHab84qZjxJ4czOk_62PNnxk-SPNlTF6XFmexFOXslAdMJ3YKBYmR-wMNzqKioN50MU_aK62phlmeU5u0_myq_uylh1P_7Pk0KqINXOyjSCZYi_siRgTkRJpXwEnivTi6kcTOKhSpHoEWM0iBoKIHlmTfOFUjwXv1cs7-2LaJZfTaPrGAN1lgvh1IjMFCrNQiNT5lGoFv18BMShC7a6rptOif0SSH6HmMbUcHO0ZR7Nh2AXa8mslNJdfVWsyA3Vm3aYTT1rdC7_j_YTY6velCGHz0bOrvVoVurHNYER9a786vD1DyJ6Y3PpX3Y_T6_aK6Xs9n3aZeNVfLcrUoF9dczLeC6-18sZ6X3JTNink106Jm7TfZ4mNWlq8yLctscTv7dxJqUxYlnquiWBVlscqX1XrRzKuyrubVdllfZfOCe_xO5DFObl07c5sUMhYXm1r54P9sCsyS1jAnzmAYVNDp4xX7xS3dcgChyRRJY4Pe__FI82WW6G8S919ra4IJ>53694</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] Detect unmatched pairs // clang-format: on/off
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-format
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          carlosgalvezp
      </td>
    </tr>
</table>

<pre>
    Hi,

We recently ran into some headache due to code that didn't apply the `// clang-format: off/on` tags correctly, leading to code silently not being formatted as it should. Examples of what failed:

```cpp
// clang-format: off
...
//// clang-format: on
```

```cpp
// clang-format: off
// Users think the above only disables formatting for the next line, similar to clang-tidy's NOLINTNEXTLINE
...
...
// clang-format:off
// Once again, they intend to disable formatting only for the next line, but in reality that's not what happens
```

Would it be possible to implement an error message that checks for unmatched `on/off` pairs? The documentation talks about **ranges**:

> Clang-format understands also special comments that switch formatting in a delimited range

I.e. it's not documented that one can just throw a `clang-format: off` at the beginning of the file and stop formatting the rest of the file. Most likely some users do rely on this behavior and my proposed change will break things for them, but I think we should improve the tool based on documented behavior and in a way that avoids bugs like the ones I describe. Fixing the above breakage would be easily automatable by adding a `clang-format: on` at the end of the file.

Happy to hear your thoughts!
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylVcFu2zgQ_Rr5MqggS3ZsH3zINgkaYJteUnSvlEhLbChSICk77tf3DW13FSPZywJCFJHDeW_evKFrJ4_bLzorP2fFXVbcnv7-UORVo2w0R_LCkrbRUXC9ok4JKZpOkRwVYbFxEu9ORJJa2qxcRRLDgGMRMdlNkZUPeKgxwrafds73ImbVLbndDsvOIoKiaAPyeCACD0z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