[PATCH] D142139: [clang-format] Disallow templates to be followed by literal

Emilia Dreamer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 19 10:14:59 PST 2023


rymiel created this revision.
rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay.
rymiel added a project: clang-format.
Herald added a project: All.
rymiel requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

There should not be any cases where the angle brackets of template
parameters are directly followed by a literal. It is more likely that a
comparison is taking place instead.

This patch makes the TokenAnnotator prefer to annotate < and > as
operators when directly followed by a literal. A similar check already
exists for literals directly *before* potential template args.

Fixes https://github.com/llvm/llvm-project/issues/60140


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D142139

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10345,6 +10345,7 @@
 
   // Not template parameters.
   verifyFormat("return a < b && c > d;");
+  verifyFormat("a < 0 ? b : a > 0 ? c : d;");
   verifyFormat("void f() {\n"
                "  while (a < b && c > d) {\n"
                "  }\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -125,19 +125,18 @@
     if (NonTemplateLess.count(CurrentToken->Previous))
       return false;
 
-    const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
-    if (Previous.Previous) {
-      if (Previous.Previous->Tok.isLiteral())
+    FormatToken *Left = CurrentToken->Previous; // The '<'.
+    if (Left->Previous) {
+      if (Left->Previous->Tok.isLiteral())
         return false;
-      if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
-          (!Previous.Previous->MatchingParen ||
-           !Previous.Previous->MatchingParen->is(
+      if (Left->Previous->is(tok::r_paren) && Contexts.size() > 1 &&
+          (!Left->Previous->MatchingParen ||
+           !Left->Previous->MatchingParen->is(
                TT_OverloadedOperatorLParen))) {
         return false;
       }
     }
 
-    FormatToken *Left = CurrentToken->Previous;
     Left->ParentBracket = Contexts.back().ContextKind;
     ScopedContextCreator ContextCreator(*this, tok::less, 12);
 
@@ -185,6 +184,8 @@
         } else {
           CurrentToken->setType(TT_TemplateCloser);
         }
+        if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral())
+          return false;
         next();
         return true;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142139.490578.patch
Type: text/x-patch
Size: 1887 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230119/ce97fa37/attachment.bin>


More information about the cfe-commits mailing list