pjproject icon indicating copy to clipboard operation
pjproject copied to clipboard

Add .clang-format file

Open jimying opened this issue 3 years ago • 3 comments
trafficstars

Use clang-format to help format code. It could help contributors and avoid useless code style reviews.

According to Coding Convention.

This only contains basic functions, It can be expanded on this basis, add other features.

#3115

jimying avatar May 31 '22 02:05 jimying

Having a coding convention setting file is a good idea, but after playing around with clang-format, it doesn't seem to suit our need.

This only contains basic functions, It can be expanded on this basis, add other features.

You've pretty much summarised what we also want, which is that if we don't specify a setting, it should be ignored and accepted as is. So for example, if we only want to start with basic indentation convention (such as tab, indent width, and column width) and add more later, we should be able to do that. But with clang-format, if you don't specify a style a default will be chosen, so all style settings will be taken into account during formatting. This triggers a lot of warning for our existing codebase (warning: code should be clang-formatted [-Wclang-format-violations]) for something as simple as trailing whitespace, which is undesirable.

In general, if we are to use a code formatter, it should be clean (or produce only minimal warnings) when we run code-formatter any_PJSIP_codefile.c. So this will help contributors fix their code style since they know that any new warnings must be produced by their modifications.

sauwming avatar Jun 06 '22 09:06 sauwming

@sauwming

I use vscode edit code, it can format the selected code, which will not chang the format of a whole file.

Prepare:

  1. Make sure clang-format command in the PATH
  2. Put .clang-format style file in the root of your project
  3. vscode settting property: "C_Cpp.clang_format_style": "file"

Now you can use mouse select some code, and click right, in the menu click "Format Selection" ^_^

jimying avatar Jun 06 '22 09:06 jimying

Ah, that makes sense.

Unfortunately the .clang-format will fail the rest of PJSIP code :)

sauwming avatar Jun 06 '22 10:06 sauwming

We decide to proceed with #3210. Thanks for the suggestion.

sauwming avatar Sep 29 '22 01:09 sauwming