Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make clang-format indent extern C blocks in C++?

I'm just learning to use clang-format. I mostly managed to configure it to match my taste, but I can't figure out how to make it indent extern "C" blocks.

This is the desired outcome:

extern "C" {
    void myFunction() {
        // ...
    }
}

But this is what I get:

extern "C" {
void myFunction() {
    // ...
}
}

Here is my configuration:

BasedOnStyle: 'LLVM'
IndentWidth: 4
AccessModifierOffset: -4
AlignAfterOpenBracket: 'false'
AlignConsecutiveAssignments: 'false'
AlignConsecutiveDeclarations: 'false'
AlignEscapedNewlinesLeft: 'true'
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: 'true'
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakAfterDefinitionReturnType: 'None'
AlwaysBreakAfterReturnType: 'None'
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BinPackArguments: 'true'
BinPackParameters: 'true'
BreakBeforeBraces: 'Custom'
BreakBeforeTernaryOperators: false
BraceWrapping: {
  AfterClass: 'false'
  AfterControlStatement: 'false'
  AfterEnum: 'false'
  AfterFunction: 'false'
  AfterNamespace: 'false'
  AfterStruct: 'false'
  AfterUnion: 'false'
  BeforeCatch: 'true'
  BeforeElse: 'true'
  IndentBraces: 'false'
}
BreakConstructorInitializersBeforeComma: 'false'
BreakStringLiterals: 'false'
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
DerivePointerAlignment: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'true'
MaxEmptyLinesToKeep: 1
NamespaceIndentation: 'All'
PointerAlignment: 'Right'
ReflowComments: 'false'
SortIncludes: 'false'
SpaceAfterCStyleCast: 'true'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: 'ControlStatements'
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'true'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: 'Cpp11'
TabWidth: 4
UseTab: 'Never'
like image 583
Venemo Avatar asked Nov 02 '25 01:11

Venemo


1 Answers

Newer versions of clang-format (clang-format-12 or later) does implement IndentExternBlock flag with (relevant) possible values of NoIndent and Indent.

Source:

  • https://clang.llvm.org/docs/ClangFormatStyleOptions.html
like image 93
kanayan Avatar answered Nov 03 '25 16:11

kanayan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!