r200974 - clang-format: Fix column limit violation for merged lines in macros.

Sean Silva silvas at purdue.edu
Sat Feb 8 18:19:06 PST 2014


+  unsigned
+  LimitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator
I,
+                         SmallVectorImpl<AnnotatedLine *>::const_iterator
E,
+                         unsigned Limit) {

Naming (do we not have a clang-tidy check for this yet?).

Also, can this be static?

-- Sean Silva


On Fri, Feb 7, 2014 at 8:45 AM, Daniel Jasper <djasper at google.com> wrote:

> Author: djasper
> Date: Fri Feb  7 07:45:27 2014
> New Revision: 200974
>
> URL: http://llvm.org/viewvc/llvm-project?rev=200974&view=rev
> Log:
> clang-format: Fix column limit violation for merged lines in macros.
>
> Before (81 columns):
>   #define A
>         \
>     void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() { return
> aaaaaaaa; } \
>     int i;
>
> After:
>   #define A                                                    \
>     void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() { \
>       return aaaaaaaa;                                         \
>     }                                                          \
>     int i;
>
> Modified:
>     cfe/trunk/lib/Format/Format.cpp
>     cfe/trunk/unittests/Format/FormatTest.cpp
>
> Modified: cfe/trunk/lib/Format/Format.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=200974&r1=200973&r2=200974&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Format/Format.cpp (original)
> +++ cfe/trunk/lib/Format/Format.cpp Fri Feb  7 07:45:27 2014
> @@ -581,6 +581,7 @@ private:
>      if (I[1]->InPPDirective != (*I)->InPPDirective ||
>          (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
>        return 0;
> +    Limit = LimitConsideringMacros(I + 1, E, Limit);
>      AnnotatedLine &Line = **I;
>      if (Line.Last->isNot(tok::r_paren))
>        return 0;
> @@ -624,6 +625,7 @@ private:
>        // Check that we still have three lines and they fit into the limit.
>        if (I + 2 == E || I[2]->Type == LT_Invalid)
>          return 0;
> +      Limit = LimitConsideringMacros(I + 2, E, Limit);
>
>        if (!nextTwoLinesFitInto(I, Limit))
>          return 0;
> @@ -649,6 +651,19 @@ private:
>      return 0;
>    }
>
> +  /// Returns the modified column limit for \p I if it is inside a macro
> and
> +  /// needs a trailing '\'.
> +  unsigned
> +  LimitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator
> I,
> +                         SmallVectorImpl<AnnotatedLine *>::const_iterator
> E,
> +                         unsigned Limit) {
> +    if (I[0]->InPPDirective && I + 1 != E &&
> +        !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
> +      return Limit < 2 ? 0 : Limit - 2;
> +    }
> +    return Limit;
> +  }
> +
>    bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine
> *>::const_iterator I,
>                             unsigned Limit) {
>      return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <=
> Limit;
>
> Modified: cfe/trunk/unittests/Format/FormatTest.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=200974&r1=200973&r2=200974&view=diff
>
> ==============================================================================
> --- cfe/trunk/unittests/Format/FormatTest.cpp (original)
> +++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Feb  7 07:45:27 2014
> @@ -5070,6 +5070,25 @@ TEST_F(FormatTest, PullTrivialFunctionDe
>              "    : b(0) {\n"
>              "}",
>              format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
> +
> +  verifyFormat("#define A          \\\n"
> +               "  void f() {       \\\n"
> +               "    int i;         \\\n"
> +               "  }",
> +               getLLVMStyleWithColumns(20));
> +  verifyFormat("#define A           \\\n"
> +               "  void f() { int i; }",
> +               getLLVMStyleWithColumns(21));
> +  verifyFormat("#define A            \\\n"
> +               "  void f() {         \\\n"
> +               "    int i;           \\\n"
> +               "  }                  \\\n"
> +               "  int j;",
> +               getLLVMStyleWithColumns(22));
> +  verifyFormat("#define A             \\\n"
> +               "  void f() { int i; } \\\n"
> +               "  int j;",
> +               getLLVMStyleWithColumns(23));
>  }
>
>  TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
> @@ -5183,6 +5202,17 @@ TEST_F(FormatTest, MergeHandlingInTheFac
>                 "  if (true) continue;\n"
>                 "}",
>                 ShortMergedIf);
> +  ShortMergedIf.ColumnLimit = 29;
> +  verifyFormat("#define A                   \\\n"
> +               "  if (aaaaaaaaaa) return 1; \\\n"
> +               "  return 2;",
> +               ShortMergedIf);
> +  ShortMergedIf.ColumnLimit = 28;
> +  verifyFormat("#define A         \\\n"
> +               "  if (aaaaaaaaaa) \\\n"
> +               "    return 1;     \\\n"
> +               "  return 2;",
> +               ShortMergedIf);
>  }
>
>  TEST_F(FormatTest, BlockCommentsInControlLoops) {
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140208/7faadb53/attachment.html>


More information about the cfe-commits mailing list