【问题标题】:clang-format how to ignore extern C?clang-format 如何忽略外部 C?
【发布时间】:2020-08-03 00:12:58
【问题描述】:

这是我当前的示例代码

#ifdef __cplusplus
extern "C"
{
#endif

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

#ifdef __cplusplus
}
#endif

运行 clang 格式后,它的变化如下。

#ifdef __cplusplus
extern "C"
{
#endif

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

#ifdef __cplusplus
}
#endif

它为我的所有函数和类型定义添加了额外的两个空格。

是否有一个选项可以让我忽略外部 C 大括号,这样我的代码看起来就像我在上面粘贴的第一个版本的代码一样没有变化。

以下是我公司使用的clang版本

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

【问题讨论】:

    标签: clang-format


    【解决方案1】:

    如果您使用的是更高版本的 clang-format,您可能会非常接近。但是对于 3.4.2,我不这么认为。

    使用 6.0.0 版本,您可以非常接近,但似乎有必要将大括号与 extern "C" 放在同一行,以禁用 extern "C" 部分的缩进。这样做需要使用CustomBreakBeforeBraces 设置。这种禁用缩进 extern "C" 块的行为似乎没有在任何地方记录,但它确实对我有用。

    尝试修改您的 .clang-format 文件以包含以下内容:

    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
    

    请注意,您可以保留许多选项,但您通常会设置它们。只有AfterEnumAfterExternBlockBreakBeforeBraces 的值与此有关。有关这些设置的更多详细信息,请参阅documentation

    如果您还没有.clang-format 文件,您可以先执行clang-format -dump-config &gt; .clang-format,然后编辑该文件。

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 1970-01-01
      • 2018-11-11
      • 2017-11-27
      • 1970-01-01
      • 2011-12-05
      • 2014-11-17
      • 2015-09-29
      • 1970-01-01
      相关资源
      最近更新 更多