2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

clang-format にて formatして欲しくない箇所を指定する

Last updated at Posted at 2019-11-14

最近clang-formatを知った。
しかし現状触っているコードはC言語なのだが、組み込みSQLがあるコードなので、clang-formatでフォーマットするとSQL部分がぐちゃぐちゃになってしまっていた。

問題

例えばフォーマット後、下記のようになってしまう

EXEC SQL
SELECT TEST INTO : h_VALUE
FROM TABLE_A
WHERE A = : lh_A
  AND B = : lh_B
  AND C = : lh_C
  AND D = : lh_D
  AND E = : lh_E
  AND F = : lh_F
  AND G = : lh_G
  AND H = : lh_H
  FOR
  UPDATE;

:(コロン)の後にスペースが挿入されてしまう。これだと現状ビルドエラーがでてしまい、フォーマット後手動で直すなんて意味がわからない状態になってしまった。

解決策

// clang-format off
EXEC SQL
SELECT TEST INTO :h_VALUE
FROM TABLE_A
WHERE A = :lh_A
  AND B = :lh_B
  AND C = :lh_C
  AND D = :lh_D
  AND E = :lh_E
  AND F = :lh_F
  AND G = :lh_G
  AND H = :lh_H
  FOR
  UPDATE;
// clang-format on

上記のように、整形して欲しくない部分をコメントで囲めば、確かにフォーマットはされないようになった。
Stack Overflowには、1行ずつ、行の最後に // を書けば無視される、との意見もあったが自身の環境では動かなかった。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?