/leetgocode

leetcode by go code

Primary LanguageGo

PDB code formating

JenkinsCI will automatically reject any PRs which do not follow our coding conventions. The easiest way to ensure your PR adheres to those conventions is to use the pdb_indent tool. This tool uses clang-format under the hood.

This tool only check *.c files which are added by us, we don't check the upstream files, if you cherrypicked the upstream files which are new added into project, please also update the .gitattributes file and add the new upstream file into .gitattributes with attribute "-pdb-style" (black list).

Install clang-format, you must make sure the version is 14.0.5:

clang-format-style-options

clang-format-style-options.14

clang-format.14

For Ubuntu20.04

Uninstall old version package if you have

apt-get remove clang-format-14

Install new version package and the installation file is here

apt-get install -f ./clang-format-14.0.5.deb
rm -f /usr/bin/clang-format
ln -s /usr/bin/clang-format-14 /usr/bin/clang-format

For MacOS Uninstall old version package if you have

brew uninstall clang-format@14

Install new version package and the installation file is here

brew install -f ./clang-format--14.0.5.big_sur.bottle.tar.gz

Check your code and format it (If you want to check and format code by mannual without IDE)

Go to the top level of our project and run below command to check the code style:

make stylecheck

and then run the command as below will format them:

make styleformat

About some clang-format arguments and features you need to known

Go to the top level of project, run below commands will format specified files(clang-format --help for details):

clang-format -i file1 file2 file3

Below will only check specified files and not format them(clang-format --help for details):

clang-format -i file1 file2 file3 --dry-run

Disabling formatting on a piece of strange code that clang-format does not format it correctly by using clang-format comment as below:

int formatted_code;
/* clang-format off */
    void    unformatted_code  ;
/* clang-format on */
void formatted_code_again;

Nested function with pointer and double pointers, the alignment of pointers is not right, there is weird formated results: llvm/llvm-project#55745 Consecutive variable declarations binpack aligments has problems: llvm/llvm-project#55792

So please use this trick for this kind of case.

Integrate clang-format into your editor

clang-format config file .clang-format located on top level of our project which you could be used to integrate it into your editor.

  • A community VIM plugin that looks promising and claims to be better than the official VIM integration
  • Clion detects .clang-format automatically and switches to using it for the built-in "Reformat Code" and "Reformat File...".

Code style conventions

Consecutive variables and functions definition will be aligned with tab, if not enough one tab use spaces instead
Pointer alignment is right side to the var name, eg void *test vs void* test
Limit code width to 90 columns, more then 90 columes will make a break line
Formatting uses 4 columns tab spacing
Each logical indentation level is one additional tab stop
Curly braces for the controlled blocks of if, while, switch, etc go on their own lines
Do not use C++ style comments (// comments). pdb_indent will replace them with /* ... */ and add a space before online comment.
Add space around "+", "-", "%", "<<", "=", "+=", "||", "&&", "<", ">", "==", etc
Do not use "{}" in a sinle-line "if", "while", "for" statement
Align function args and variable uses tab, if not enough one tab use spaces instead
Add space before '(' of control statements ('if', 'for', 'switch', 'while', etc.)
Add space after ',', i.e. 'a,b' vs. 'a, b'
Align function params, variable definition and marco definition with tabs, if not enough one tab(4 spaces) use spaces instead