[PATCH] D79293: [clang-format] [PR45218] Fix an issue where < and > and >> in a for loop gets incorrectly interpreted at a TemplateOpener/Closer

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 6 11:20:00 PDT 2020


krasimir added a comment.

I think these examples are too ambiguous for clang-format to try and format correctly.
Sadly for C++, correctly distinguishing between `>>`, the binary operator, and `>>`, the sequence of two closing template brackets, requires semantic analysis, I believe.
Such heuristics are very brittle, e.g. this patch fixes `if (i < x >> 1)` but regresses this:

  % cat ~/test.cc
  bool f() {
    if (w<u<v<x>>, 1>::t) return true;
  }
  % bin/clang-format ~/test.cc 
  bool f() {
    if (w < u < v < x >>, 1 > ::t)
      return true;
  }
  % 

We can of course enumerate a fixed set of patterns where we apply such fixes fixes, but I'm not sure how much is that worth.
I think clang-format's current approach to choose to interpret as a template, even if silly at times, is reasonable.
A way to deal with `if (i < x >> 1)` is to give clang-format a hint e.g. `if (i < (x >> 1))`. You can ~always surround an expression with braces, but if clang-format guesses incorrectly two template closers as a binary operator, I can't think of a good way to force it to stop.


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

https://reviews.llvm.org/D79293





More information about the cfe-commits mailing list