Skip to content

Instantly share code, notes, and snippets.

@rhysd
Last active January 7, 2018 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhysd/7466359 to your computer and use it in GitHub Desktop.
Save rhysd/7466359 to your computer and use it in GitHub Desktop.
歌舞伎座 tech #2 LT 資料

clang-format

13 Nov 2013 歌舞伎座 tech#2

    @Linda_pp
    https://twitter.com/Linda_pp
    https://github.com/rhysd

コーディングスタイルにまつわるお悩み

  • チーム内でコーディングスタイルが決まっていて 書きづらい

    • Google コーディングスタイルの80桁制限とか

      自分のスタイルで書いて後から修正したい

  • 他人が書いたコードがぐちゃぐちゃで読みづらい

    手元で整形してから読みたい

  • そもそも手で整形するのが面倒くさい

それ clang-format で解決できるのでは

clang-format とは

  • C, C++, Objective-C のコードフォーマットツール

    • 指定したコードスタイルで自動整形してくれる
    • Clang の LibFormat を利用

    http://clang.llvm.org/docs/ClangFormat.html

  • コマンドラインで利用

clang-format -style={style} [-i] {file}
{style} = LLVM, Google, Chromium,
          Mozilla, WebKit

// フォーマット例

for(int i=0;i<4;++i){
    if(i%2==0) std::cout << hoge[i] << std::endl;
}
for (int i = 0; i < 4; ++i) {
    if (i % 2 == 0) std::cout << hoge[i] << std::endl;
}

// フォーマット例

template<class ArrayL, class ArrayR>
constexpr bool operator()(ArrayL const& lhs, ArrayR const& rhs, size_t i) const
{
    return i == ls ? false :
           i == rs ? true :
           lhs[i] < rhs[i] ? false :
           lhs[i] > rhs[i] ? true :
           operator()(lhs, rhs, i+1);
}
template <class ArrayL, class ArrayR>
constexpr bool operator()(ArrayL const& lhs,
                          ArrayR const& rhs, size_t i) const
{
    return i == ls
               ? false
               : i == rs ? true
                         : lhs[i] < rhs[i]
                               ? false
                               : lhs[i] > rhs[i]
                                     ? true
                                     : operator()(lhs, rhs,
                                                  i + 1);
}

// フォーマット例

std::cout << "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:";
std::cout
    << "Permission is hereby granted, free of charge, to "
       "any person obtaining a copy of this software and "
       "associated documentation files (the \"Software\"), "
       "to deal in the Software without restriction, "
       "including without limitation the rights to use, "
       "copy, modify, merge, publish, distribute, "
       "sublicense, and/or sell copies of the Software, "
       "and to permit persons to whom the Software is "
       "furnished to do so, subject to the following "
       "conditions:";

スタイルを細かく設定する

  • -style オプションに辞書を渡す
# Linux カーネルスタイル
clang-format -style='{ \
        BasedOnStyle: LLVM \
        IndentWidth: 8 \
        UseTab: Always \
        BreakBeforeBraces: Linux \
        AllowShortIfStatementsOnASingleLine: false \
        IndentCaseLabels: false }' \
        hoge.cpp

設定ファイルにスタイルの設定を保存する

# -dump-config の出力をファイルに保存
clang-format [...] -dump-config > .clang-format

# -style オプションにファイル名を渡す
clang-format -style=.clang-format {file}

エディタから利用する

  • Emacs
(load "path/to/clang-format.el")
(global-set-key [C-M-tab] 'clang-format-region)

エディタから利用する

  • Vim
    • 公式の python スクリプトを使う
map <C-K> :pyf path/to/clang-format.py<CR>
imap <C-K> <ESC>:pyf path/to/clang-format.py<CR>i

まとめ

  • clang-format で C や C++, Objective-C のコードを 特定のスタイルに簡単に整形できる

  • -style オプションに色々渡すことでスタイルを細かく カスタマイズできる

  • 各エディタから利用できる
 (Vim は vim-clang-format がおすすめ)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment