]> granicus.if.org Git - clang/commitdiff
clang-format: Allow ternary expressions inside template parameters if
authorDaniel Jasper <djasper@google.com>
Wed, 6 May 2015 14:53:50 +0000 (14:53 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 6 May 2015 14:53:50 +0000 (14:53 +0000)
the template parameters aren't inside an expression context.

This fixes llvm.org/PR23270.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236603 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index be29239c062876c7eaaaaadf240fcbdd9d397950..793746137a515b9c59bfcdb0a70581e8aeccf709 100644 (file)
@@ -47,6 +47,11 @@ private:
     FormatToken *Left = CurrentToken->Previous;
     Left->ParentBracket = Contexts.back().ContextKind;
     ScopedContextCreator ContextCreator(*this, tok::less, 10);
+
+    // If this angle is in the context of an expression, we need to be more
+    // hesitant to detect it as opening template parameters. 
+    bool InExprContext = Contexts.back().IsExpression;
+
     Contexts.back().IsExpression = false;
     // If there's a template keyword before the opening angle bracket, this is a
     // template parameter, not an argument.
@@ -70,8 +75,8 @@ private:
         next();
         continue;
       }
-      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
-                                tok::colon, tok::question))
+      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
+          (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext))
         return false;
       // If a && or || is found and interpreted as a binary operator, this set
       // of angles is likely part of something like "a < b && c > d". If the
index cb1f7bd539f3329425efe277764a3a3d23bad44b..b4c8c26664ac5eb8a1a24a3ee535a72a32f59130 100644 (file)
@@ -5144,6 +5144,8 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) {
   verifyFormat("f<int>();");
   verifyFormat("template <typename T> void f() {}");
   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
+  verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
+               "sizeof(char)>::type>;");
 
   // Not template parameters.
   verifyFormat("return a < b && c > d;");