googytech
googytech

Reputation: 479

clang-format how to ignore extern C?

This is my current sample code

#ifdef __cplusplus
extern "C"
{
#endif

typedef enum 
{
  BUY = 1, 
  SELL = 2
} OrderAction_e; 

#ifdef __cplusplus
}
#endif

After I run the clang format it is changing as below.

#ifdef __cplusplus
extern "C"
{
#endif

  typedef enum 
  {
    BUY = 1, 
    SELL = 2
  } OrderAction_e; 

#ifdef __cplusplus
}
#endif

It is adding an additional two spaces for all my functions and typedefs.

Is there an option which I can use to ignore the extern C braces so that, my code looks unchanged like the first version of code I pasted above.

Following is the clang version used in my company

LLVM (http://llvm.org/):
  LLVM version 3.4.2

Upvotes: 4

Views: 1678

Answers (1)

Eric Backus
Eric Backus

Reputation: 1924

If you were using a later version of clang-format, you could get very close. But with 3.4.2, I don't think so.

With version 6.0.0, you can get very close, but it seems to be necessary to put the brace on the same line as the extern "C" in order to disable the indentation of the extern "C" section. Doing this requires using the Custom setting for BreakBeforeBraces. This behavior of disabling indenting the extern "C" block doesn't appear to be documented anywhere, but it does work for me.

Try modifying your .clang-format file to contain this:

BraceWrapping:
  AfterClass:      true
  AfterControlStatement: true
  AfterEnum:       true # <-- You need this
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  AfterExternBlock: false # <-- And this
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    false
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false
BreakBeforeBraces: Custom # <-- And this

Note that there are a bunch of options there which you can leave however you normally set them. Only the values of AfterEnum, AfterExternBlock, and BreakBeforeBraces matter for this. See the documentation for more details about these settings.

If you don't already have a .clang-format file, you would start by doing clang-format -dump-config > .clang-format and then edit the file.

Upvotes: 3

Related Questions