【问题标题】:Keep indentation for particular preprocessor include blocks with clang-format保留特定预处理器的缩进包括具有 clang 格式的块
【发布时间】:2018-10-02 05:58:04
【问题描述】:

我们在包含外部库时包含保护某些定义的机制,我们希望在格式化代码时保持它们原样,最终按字母顺序排列块的内容。

例如:

#include <ExternalIncludeBegin.h>
#    include <somelib/someheader.h>
#    include <somelib/anotherheader.h>
#include <ExternalIncludeEnd.h>

现在,clang-format 将此块转换为

#include <ExternalIncludeBegin.h>
#include <somelib/someheader.h>
#include <somelib/anotherheader.h>
#include <ExternalIncludeEnd.h>

但是我想保留原始缩进,如果可能的话,不必用新代码封装所有内容(我们要格式化的代码库非常古老而且很大),clang-format 在这里可以为我做些什么吗?

【问题讨论】:

    标签: clang-format


    【解决方案1】:

    看看“禁用格式”功能

    Disabling Formatting on a Piece of Code
    Clang-format understands also special comments that switch formatting in a delimited range. The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted. The comments themselves will be formatted (aligned) normally.
    
    int formatted_code;
    // clang-format off
        void    unformatted_code  ;
    // clang-format on
    void formatted_code_again;
    

    更多详情:https://clang.llvm.org/docs/ClangFormatStyleOptions.html

    因此,您也可以使用“sed”或其他工具在整个项目中注入特定于 clang 的 cmets。

    【讨论】:

    • 即使 sed 确实可以用来注入这种东西,我不确定我们是否真的可以要求每个开发人员使用特定于 clang 格式的声明来封装每个调用。我们正在考虑使用那些工具来自动化愚蠢的东西,人们不应该担心它(在我看来)
    猜你喜欢
    • 2014-08-20
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多