[PATCH] D37140: [clang-format] Fixed one-line if statement

Pawel Maciocha via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 25 05:42:17 PDT 2017


PriMee created this revision.
Herald added a subscriber: klimek.

**Short overview:**

Fixed bug: https://bugs.llvm.org/show_bug.cgi?id=34001
Clang-format bug resulting in a strange behavior of control statements short blocks. Different flags combinations do not guarantee expected result. Turned on option AllowShortBlocksOnASingleLine does not work as intended.

**Description of the problem:**

Cpp source file UnwrappedLineFormatter does not handle AllowShortBlocksOnASingleLine flag as it should. Putting a single-line control statement without any braces, clang-format works as expected (depending on AllowShortIfStatementOnASingleLine or AllowShortLoopsOnASingleLine value). Putting a single-line control statement in braces, we can observe strange and incorrect behavior.
Our short block is intercepted by tryFitMultipleLinesInOne function. The function returns a number of lines to be merged. Unfortunately, our control statement block is not covered properly. There are several if-return statements, but none of them handles our block. A block is identified by the line first token and by left and right braces. A function block works as expected, there is such an if-return statement doing proper job. A control statement block, from the other hand, falls into strange conditional construct, which depends on BraceWrapping.AfterFunction flag (with condition that the line’s last token is left brace, what is possible in our case) or goes even further. That should definitely not happen.

**Description of the patch:**

By adding three different if statements, we guarantee that our short control statement block, however it looks like (different brace wrapping flags may be turned on), is handled properly and does not fall into wrong conditional construct. Depending on appropriate options we return either 0 (when something disturbs our merging attempt) or let another function (tryMergeSimpleBlock) take the responsibility of returned result (number of merged lines). Nevertheless, one more correction is required in mentioned tryMergeSimpleBlock function. The function, previously, returned either 0 or 2. The problem was that this did not handle the case when our block had the left brace in a separate line, not the header one. After change, after adding condition, we return the result compatible with block’s structure. In case of left brace in the header’s line we do everything as before the patch. In case of left brace in a separate line we do the job similar to the one we do in case of a “non-header left brace” function short block. To be precise, we try to merge the block ignoring the header line. Then, if success, we increment our returned result.

**After fix:**

**CONFIG:**

  AllowShortBlocksOnASingleLine: true 
  AllowShortIfStatementsOnASingleLine: true 
  BreakBeforeBraces: Custom
  BraceWrapping: { 
  AfterClass: true, AfterControlStatement: true, AfterEnum: true, AfterFunction: true, AfterNamespace: false, AfterStruct: true, AfterUnion: true, BeforeCatch: true, BeforeElse: true 
  }

**BEFORE:**

  if (statement) doSomething();
  if (statement) { doSomething(); }
  if (statement) {
      doSomething();
  }
  if (statement)
  {
      doSomething();
  }
  if (statement)
      doSomething();
  if (statement) {
      doSomething1();
      doSomething2();
  }

**AFTER:**

  if (statement) doSomething();
  if (statement) { doSomething(); }
  if (statement) { doSomething(); }
  if (statement) { doSomething(); }
  if (statement) doSomething();
  if (statement)
  {
    doSomething1();
    doSomething2();
  }


https://reviews.llvm.org/D37140

Files:
  lib/Format/UnwrappedLineFormatter.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37140.112681.patch
Type: text/x-patch
Size: 5694 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170825/f013a81b/attachment.bin>


More information about the cfe-commits mailing list