clang-format というソースコード整形ツールを使ってみたのでその時のメモをここに残します。
clang-format
clang-format は、C/C++ をターゲットとしたコード整形ツールです。
インストール
$ sudo apt install clang-format
使い方
.clang-format 作成
.clang-format というファイルを書きます。
$ cat .clang-format BasedOnStyle: Mozilla
BasedOnStyle
LLVM | LLVMコーディング規約 |
Google C++ Style Guide | |
Chromium | Chromium coding style |
Mozilla | Coding style — Firefox Source Docs documentation |
WebKit | Code Style Guidelines | WebKit |
Microsoft | .NET code style rule options - .NET | Microsoft Learn |
GNU | GNU Coding Standards |
コード作成
テスト用のソースコード(.test.cpp)を作成します。
$ cat test.cpp #include <iostream> #include "json.hpp" int main() { using json = nlohmann::json; std::string jsonstr = R"({"str":"おはよう。","boolian":false,"person":[{"name":"Sato","age":20},{"name":"Kato","age":30}],})"; auto jobj = json::parse(jsonstr); std::cout << jobj << std::endl; }
コード整形
.clang-format と test.cpp を同じカレントディレクトリに置いてコマンドを実行します。
$ clang-format test.cpp #include "json.hpp" #include <iostream> int main() { using json = nlohmann::json; std::string jsonstr = R"({"str":"おはよう。","boolian":false,"person":[{"name":"Sato","age":20},{"name":"Kato","age":30}],})"; auto jobj = json::parse(jsonstr); std::cout << jobj << std::endl; }