llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.11k stars 12.01k forks source link

clang-format c code - Incorrect formatting of else if over conditionally compiled code #54398

Open isolansky opened 2 years ago

isolansky commented 2 years ago

code:

while (true) {
#if DEF_SET_ANOTHER
    if (var == 1) {
        ;
    } else
#endif /* DEF_SET_ANOTHER */
        if (var == 3) {
        ;
    } else if (var == 4) {
        ;
    } else if (var == 5) {
        ;
    }
};

clang-format 12.0.0 image

clang-format 13.0.0, 14RC4 image

.clang-config:

BreakBeforeBraces: Custom
BraceWrapping:
  AfterEnum : true
  AfterStruct : true
  SplitEmptyFunction : true
  AfterClass : true
  AfterControlStatement : true
  AfterFunction : true
  AfterNamespace : true
  AfterObjCDeclaration : true
  AfterUnion : true
  AfterExternBlock : false
  BeforeCatch : true
  BeforeElse : true
  AfterCaseLabel : true
SortIncludes : false
AlignEscapedNewlines : "Left"
AlignConsecutiveMacros: false
AlignConsecutiveAssignments : true
AlignConsecutiveDeclarations : false
AlignTrailingComments            : true
AllowAllParametersOfDeclarationOnNextLine : true
AllowShortBlocksOnASingleLine : false
AllowShortFunctionsOnASingleLine : "None"
AllowShortIfStatementsOnASingleLine : false
AllowShortLoopsOnASingleLine : false
AlwaysBreakBeforeMultilineStrings : true
BasedOnStyle : "Google"
BinPackParameters : false
BreakBeforeBinaryOperators : false
BreakBeforeTernaryOperators : false
ColumnLimit : 120
ContinuationIndentWidth :     4
DerivePointerAlignment : false
DisableFormat : false
IndentCaseLabels : true
IndentWrappedFunctionNames : false
IndentWidth : 4
Language : "Cpp"
MaxEmptyLinesToKeep :         1
PointerBindsToType : false
SpaceBeforeAssignmentOperators : true
SpaceBeforeParens : "ControlStatements"
SpacesBeforeTrailingComments : 1
SpacesInCStyleCastParentheses : false
SpacesInContainerLiterals : false
SpacesInParentheses : false
Standard : "Cpp11"
TabWidth : 1
UseTab : "Never"

Issue is obvious, it formats following code differently

llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-format

mkurdej commented 2 years ago

Smaller repro with LLVM style:

#if SOMETHING
if (var == 1) {
} else
#endif //
  if (var == 2) {
  } else if (var == 3) {
  }

or

#if SOMETHING
if (var == 1) {
} else
#endif /**/
  if (var == 2) {
  } else if (var == 3) {
  }

But without comment on #endif, it's ok:

#if SOMETHING
if (var == 1) {
} else
#endif
    if (var == 2) {
} else if (var == 3) {
}
llvmbot commented 2 years ago

@llvm/issue-subscribers-bug

isolansky commented 2 years ago

Any update? I see no milestone has been assigned yet