]> granicus.if.org Git - clang/commitdiff
clang-format: Support weird lambda macros.
authorDaniel Jasper <djasper@google.com>
Thu, 7 Jan 2016 14:36:11 +0000 (14:36 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 7 Jan 2016 14:36:11 +0000 (14:36 +0000)
Before:
  MACRO((AA & a) { return 1; });

After:
  MACRO((AA &a) { return 1; });

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

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

index caff1312f300569d9cca60ec0138dde66726f16e..7390865d4cc98aa2ac019c0e595b849eb750aeb6 100644 (file)
@@ -199,6 +199,18 @@ private:
         Left->MatchingParen = CurrentToken;
         CurrentToken->MatchingParen = Left;
 
+        if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
+            Left->Previous && Left->Previous->is(tok::l_paren)) {
+          // Detect the case where macros are used to generate lambdas or
+          // function bodies, e.g.:
+          //   auto my_lambda = MARCO((Type *type, int i) { .. body .. });
+          for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
+            if (Tok->is(TT_BinaryOperator) &&
+                Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
+              Tok->Type = TT_PointerOrReference;
+          }
+        }
+
         if (StartsObjCMethodExpr) {
           CurrentToken->Type = TT_ObjCMethodExpr;
           if (Contexts.back().FirstObjCSelectorName) {
index c940bc75c80eb7737d8cf29c09889dd41e8b8822..db73c9ed2cee5b5f1e615484be7a1bf8f3ca588f 100644 (file)
@@ -10630,6 +10630,7 @@ TEST_F(FormatTest, FormatsLambdas) {
   // Lambdas created through weird macros.
   verifyFormat("void f() {\n"
                "  MACRO((const AA &a) { return 1; });\n"
+               "  MACRO((AA &a) { return 1; });\n"
                "}");
 
   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"