【问题标题】:Clang Format Issue with multiline function declaration parameters多行函数声明参数的 Clang 格式问题
【发布时间】:2016-11-21 20:22:55
【问题描述】:

Clang 格式一直在这样做:

bool importSomethingIn( const boost::property_tree::ptree& inBoostTree,
                        int inSomeIndex,
                        std::shared_ptr<Something>
                            inSomething,
                        int x,
                        int y );

当我希望它这样做时:

bool importSomethingIn( const boost::property_tree::ptree& inBoostTree,
                        int inSomeIndex,
                        std::shared_ptr<Something> inSomething,
                        int x,
                        int y );

请注意,它在符号inSomething 之前添加了换行符和缩进。似乎总是导致这种行为的模板化类型。

我怎样才能阻止它这样做?

这是我当前的 .clang 格式:

BasedOnStyle: LLVM
Language: Cpp
AccessModifierOffset: -4
AlignEscapedNewlinesLeft: false
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: true
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackParameters: false
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Allman
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 120
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DerivePointerBinding: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: true
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
NamespaceIndentation: All
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
PointerBindsToType: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: true
Standard: Cpp11
TabWidth: 8
UseTab: Never

编辑:我正在使用 2016 年 7 月在 github 上获得的 here 的 clang 格式 Xcode 插件(首先安装 Alcatraz,然后使用 Aclatraz 安装 clang 格式插件)。我不完全确定 clang 格式的二进制文件在哪里,因此没有看到插件附带的版本号。

但是,根据以下建议,我使用 Homebrew 安装了 clang-format,它为我提供了 3.9.0 版。然后我将 Xcode 插件设置为“使用系统 clang-format”,我得到了想要的结果,所以我猜它一定是一个错误。

【问题讨论】:

  • 您使用的是哪个版本的 clang-format?使用您的示例代码和 .clang-format,我可以使用 clang-format 3.8.0(在 Windows 上)获得所需的输出。
  • 相同 - 在 Mac 上使用安装了 Homebrew 的 clang 格式版本 3.9.0 (tags/google/stable/2016-06-27) 获得所需的输出;使用上述选项文件从命令行运行 clang-format。

标签: c++ clang-format


【解决方案1】:

在带有 clang-format 3.8 的 ubuntu 16.04 上,您的情况对我来说似乎很好

  • 安装 clang-format-3.8

      sudo apt-get install clang-format-3.8
    
  • ~/.vimrc

      map <F3> :pyf /usr/share/vim/addons/syntax/clang-format-3.8.py<cr>                  
      imap <F3> <C-o>:pyf /usr/share/vim/addons/syntax/clang-format-3.8.py<cr>            
    
  • 使用@OP 的 .clang 格式和源代码

      mkdir ~/test
      vim ~/test/.clang-format
      vim ~/test/test.cpp
    
  • 在vim中格式化test.cpp(使用F3),然后结果:

      bool importSomethingIn( const boost::property_tree::ptree& inBoostTree,
                              int inSomeIndex,
                              std::shared_ptr<Something> inSomething,
                              int x,
                              int y );
    

【讨论】:

  • 将此标记为正确答案,因为它引导我使用 Homebrew 获取 v 3.9 并告诉 Xcode 插件“使用系统 clang-format”来解决问题。
【解决方案2】:

我认为这是由ColumnLimit 引起的。我认为这是一个错误......我有这个问题,我通过更改这个变量来解决它。尝试设置:

 ColumnLimit: 0

原因

bool importSomethingIn( const boost::property_tree::ptree&amp; inBoostTree, int inSomeIndex, std::shared_ptr&lt;Something&gt;" has 117+-3 chars in dependence of computing invisible chars. 的长度是 117+-3,取决于计算不可见字符。 ColumnLimit: 120 已设置,我认为这是可能的原因之一,已被删除。

【讨论】:

  • 不是这个原因。如果是这样,那么 Clang 格式也会拆分第一行,即更长的行。但事实并非如此。
  • 115 个字符后被剪切。 ColumnLimit: 120 已设置。 inSomething, 有 12 个字符。这就是它被剪掉的原因。尝试学习计算:)。
  • 我认为我们应该考虑每行中的列数,而不是从整个声明的开始。在违规行上,我数了 50 个字符,包括 &gt; 被拆分的地方。但无论如何,从整个声明的开头是 163 个字符,而不是 115 个。所以,这也不能解释它。
  • "bool importSomethingIn( const boost::property_tree::ptree& inBoostTree, int inSomeIndex, std::shared_ptr" 有 117+-3 个字符,依赖于计算不可见字符。你懂我吗现在?
  • 我认为这是一个错误...我遇到了这个问题,我通过更改此变量解决了它...但它也可能是由其他原因引起的...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-20
相关资源
最近更新 更多