[cfe-commits] r171038 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Sean Silva silvas at purdue.edu
Mon Dec 24 10:42:39 PST 2012


Your commit messages lately have been fantastic!

Keep up the good work!

-- Sean Silva

On Mon, Dec 24, 2012 at 9:43 AM, Daniel Jasper <djasper at google.com> wrote:
> Author: djasper
> Date: Mon Dec 24 10:43:00 2012
> New Revision: 171038
>
> URL: http://llvm.org/viewvc/llvm-project?rev=171038&view=rev
> Log:
> Penalize tokens with a lower parenthesis level than the start of the line.
>
> This prevents formattings like this (assuming "parameter" doesn't fit the line):
>   bool f = someFunction() && someFunctionWithParam(
>       parameter) && someOtherFunction();
>
> Here, "parameter" - the start of line 2 - has a parenthesis level of 2, but
> there are subsequent tokens ("&&" and "someOtherFunction") with a lower level.
> This is bad for readability as "parameter" hides "someOtherFunction". With this
> patch, this changes to:
>   bool f = someFunction() &&
>            someFunctionWithParam(parameter) &&
>            someOtherFunction();
>
> 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=171038&r1=171037&r2=171038&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Format/Format.cpp (original)
> +++ cfe/trunk/lib/Format/Format.cpp Mon Dec 24 10:43:00 2012
> @@ -83,6 +83,7 @@
>
>  struct OptimizationParameters {
>    unsigned PenaltyIndentLevel;
> +  unsigned PenaltyLevelDecrease;
>  };
>
>  class UnwrappedLineFormatter {
> @@ -95,6 +96,7 @@
>          Annotations(Annotations), Replaces(Replaces),
>          StructuralError(StructuralError) {
>      Parameters.PenaltyIndentLevel = 15;
> +    Parameters.PenaltyLevelDecrease = 10;
>    }
>
>    void format() {
> @@ -110,6 +112,7 @@
>      State.FirstLessLess.push_back(0);
>      State.ForLoopVariablePos = 0;
>      State.LineContainsContinuedForLoopSection = false;
> +    State.StartOfLineLevel = 1;
>
>      // The first token has already been indented and thus consumed.
>      moveStateToNextToken(State);
> @@ -155,6 +158,9 @@
>      /// \brief The number of tokens already consumed.
>      unsigned ConsumedTokens;
>
> +    /// \brief The parenthesis level of the first token on the current line.
> +    unsigned StartOfLineLevel;
> +
>      /// \brief The position to which a specific parenthesis level needs to be
>      /// indented.
>      std::vector<unsigned> Indent;
> @@ -186,6 +192,8 @@
>          return Other.ConsumedTokens > ConsumedTokens;
>        if (Other.Column != Column)
>          return Other.Column > Column;
> +      if (Other.StartOfLineLevel != StartOfLineLevel)
> +        return Other.StartOfLineLevel > StartOfLineLevel;
>        if (Other.Indent.size() != Indent.size())
>          return Other.Indent.size() > Indent.size();
>        for (int i = 0, e = Indent.size(); i != e; ++i) {
> @@ -247,6 +255,8 @@
>          State.Column = State.Indent[ParenLevel];
>        }
>
> +      State.StartOfLineLevel = ParenLevel + 1;
> +
>        if (Line.Tokens[0].Tok.is(tok::kw_for))
>          State.LineContainsContinuedForLoopSection =
>              Previous.Tok.isNot(tok::semi);
> @@ -343,7 +353,7 @@
>        return Level;
>
>      if (Right.Tok.is(tok::arrow) || Right.Tok.is(tok::period))
> -      return 200;
> +      return 50;
>
>      return 3;
>    }
> @@ -375,6 +385,10 @@
>      if (NewLine) {
>        CurrentPenalty += Parameters.PenaltyIndentLevel * State.Indent.size() +
>            splitPenalty(State.ConsumedTokens - 1);
> +    } else {
> +      if (State.Indent.size() < State.StartOfLineLevel)
> +        CurrentPenalty += Parameters.PenaltyLevelDecrease *
> +                          (State.StartOfLineLevel - State.Indent.size());
>      }
>
>      addTokenToState(NewLine, true, State);
>
> Modified: cfe/trunk/unittests/Format/FormatTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171038&r1=171037&r2=171038&view=diff
> ==============================================================================
> --- cfe/trunk/unittests/Format/FormatTest.cpp (original)
> +++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Dec 24 10:43:00 2012
> @@ -379,8 +379,9 @@
>
>  TEST_F(FormatTest, FormatsAwesomeMethodCall) {
>    verifyFormat(
> -      "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
> -      "    parameter, parameter, parameter)), SecondLongCall(parameter));");
> +      "SomeLongMethodName(SomeReallyLongMethod(\n"
> +      "    CallOtherReallyLongMethod(parameter, parameter, parameter)),\n"
> +      "                   SecondLongCall(parameter));");
>  }
>
>  TEST_F(FormatTest, ConstructorInitializers) {
> @@ -454,6 +455,13 @@
>        "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
>        "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
>        "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
> +
> +  verifyFormat(
> +      "{\n  {\n    {\n"
> +      "      Annotation.SpaceRequiredBefore =\n"
> +      "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
> +      "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
> +      "    }\n  }\n}");
>  }
>
>  TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list