New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .clang-format #8113
base: master
Are you sure you want to change the base?
Add .clang-format #8113
Conversation
08a2b1a
to
5dc241d
Compare
5dc241d
to
e728c9c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some initial comments/findings.
Basically totally in favor of this idea, we just have to think carefully when to merge this. I would like to have some pending PRs merged before applying this, as we otherwise give us a hard time.
src/common/conf.cpp
Outdated
config_init(config); | ||
if (!config_read_file(config, config_filename)) { | ||
ShowError("%s:%d - %s\n", config_error_file(config), | ||
config_error_line(config), config_error_text(config)); | ||
ShowError("%s:%d - %s\n", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ColumnLimit: 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want no column limit? Lines get hard to understand at 140+ characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like these totally depend on the output. Sometimes they can be one line but sometimes like @vstumpf said, it can drag on too far horizontally. The big kicker is to see what this would look like in char/login with all the SQL stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually the only long lines are output formats, like this. Where it is not really problematic.
I do agree that you should not put that much logic into a single line, but this should already be detected during review progress. I tend to comment negative on overloaded short-ifs.
As for the SQL queries I totally agree with that as well, but it should be possible to break the strings into multiple lines, even with that configuration and that would make sense, if we see that clang format is breaking it somewhere.
But for simple format parameter lists I would tend to say that nowdays programmer's screens and editors can display more than 100 characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 | ||
}; | ||
|
||
static const uint8_t mask[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BreakArrays: true
Or is there another option for how many elements? Right now it seems to use the maximum line length.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no option for number of elements. And BreakArrays
is only for formatting JSON, not cpp files :c
For cases where we want to ignore the clang format, we can always just // clang-format off
and // clang-format on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sad but true... llvm/llvm-project#61560
Guess we really have to turn clang format off and back on in the relevant places.
src/common/socket.hpp
Outdated
do { \ | ||
if(session[fd]->rdata_size == session[fd]->rdata_pos){ \ | ||
#define RFIFOREST(fd) (session[fd]->flag.eof ? 0 : session[fd]->rdata_size - session[fd]->rdata_pos) | ||
#define RFIFOFLUSH(fd) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea which format option is applied here, but this should be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is AlignEscapedNewlines
, honestly I like the right align, it makes it obvious how far the macro goes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me it makes it harder to review here on github, especially because it uses a mix of tabs and spaces.
If you see a \ at the end of the line you know that it still continues. Which is fair and easy I guess.
a8b56a4
to
22dc728
Compare
It makes sense, but I'm scared this will never get merged because there will always be "another PR". If we can have a list of PRs or a timeline, it'd be nice |
Yeah that is why I said some. We would have to work that out I guess. Also please stop force pushing. This only makes it harder to see, which comments you really resolved and is not helpful for reviewing your PR. |
I was force pushing to reapply the format changes, i just find it more consistent to reapply all the changes, but I'll stop |
Fixed macro newline alignment
…g-formatter # Conflicts: # src/common/core.cpp # src/common/mmo.hpp # src/common/packets.hpp # src/common/showmsg.cpp # src/common/socket.cpp # src/common/strlib.cpp # src/common/strlib.hpp # src/common/utilities.cpp
src/common/des.cpp
Outdated
for( i = 0; i*8 < size; i += 8 ) | ||
des_decrypt_block(p); | ||
for( i = 0; i * 8 < size; i += 8 ) { | ||
des_decrypt_block( p ); |
Check failure
Code scanning / CodeQL
Use of a broken or risky cryptographic algorithm High
call to des_decrypt_block
src/common/grfio.cpp
Outdated
for( i = 0; i < 20 && i < nblocks; ++i ) | ||
des_decrypt_block(&p[i]); | ||
for( i = 0; i < 20 && i < nblocks; ++i ) { | ||
des_decrypt_block( &p[i] ); |
Check failure
Code scanning / CodeQL
Use of a broken or risky cryptographic algorithm High
call to des_decrypt
This file makes use of a broken or weak cryptographic algorithm (specified by
call to des_decrypt_block
This file makes use of a broken or weak cryptographic algorithm (specified by
call to des_decrypt_block
This file makes use of a broken or weak cryptographic algorithm (specified by
call to des_decrypt_block
These are only supported in version 15 and above
…g-formatter # Conflicts: # src/common/cbasetypes.hpp # src/common/core.cpp # src/common/grfio.cpp # src/common/malloc.cpp # src/common/nullpo.cpp # src/common/sql.cpp # src/common/sql.hpp # src/common/timer.cpp
{ | ||
ShowError("sv_readdb: Too many columns in line %d of \"%s\" (found %d, maximum is %d).\n", lines, path, columns, maxcols ); | ||
if(columns > maxcols) { | ||
ShowError("sv_readdb: Too many columns in line %d of \"%s\" (found %d, maximum is %d).\n", lines, path, columns, maxcols); |
Check failure
Code scanning / CodeQL
Wrong type of arguments to formatting function High
{ | ||
ShowError("sv_readdb: Too many columns in line %d of \"%s\" (found %d, maximum is %d).\n", lines, path, columns, maxcols ); | ||
if(columns > maxcols) { | ||
ShowError("sv_readdb: Too many columns in line %d of \"%s\" (found %d, maximum is %d).\n", lines, path, columns, maxcols); |
Check failure
Code scanning / CodeQL
Wrong type of arguments to formatting function High
|
||
if( fgets(line, sizeof(line), fp) && sscanf(line, "%40s", rev) ) | ||
if (fgets(line, sizeof(line), fp) && sscanf(line, "%40s", rev)) { |
Check failure
Code scanning / CodeQL
Incorrect return-value check for a 'scanf'-like function High
Addressed Issue(s): N/A
Server Mode: Both
Description of Pull Request:
This PR has 3 things, the clang format file, the application of it on src/common, and a github action.
0). Why a formatter?
It's a quick way to improve the quality of our code. If we enforce one style, it becomes easier to review code: no more huge list of comments about changing whitespace or a newline. By removing this noise, important comments are not lost.
1). The .clang-format file
This configuration requires at least
clang-format 9
. MSVS 2019 installs clang-format 10 and MSVS 2022 installs 16.0.5.To check this, find your installation of visual studio and run
clang-format.exe --version
, like so:I'm not sure what version MSVS 2017 currently has. If an active developer is still using 2017, then we can rethink the
IndentPPDirectives
option, as theBeforeHash
argument is the only thing that requires version 9, I think everything else should be supported by 4.Formatting is only required for people who wish to contribute back to the project, and I think the ones who do are using a newer version of VS.
2) Applying this to src/common
I went ahead and applied this to src/common so we can see the effects of the current configuration. If you like it, great! Let me know. If not, then please put a comment so we can work it out. There's only a couple of options that I'm deadset on, but I'll let you start the discussion c:
Once we can agree on the config, we can apply it to the rest of the directories.
3) The github action.
In the past, people have brought up the idea of a github action that formats the code and makes a commit with the changes. I don't really like this because:
git pull
their changes, or when they go togit push
the remote will reject the changes. Teaching someone how to fix a conflict is not fun, believe me.If you want to manually run the formatter,
Ctrl+K Ctrl+D
runs formatting on the whole file,Ctrl+K Ctrl+F
runs it on the current selection.xaver.clang-format
to runclang-format
as their default formatter.Instead, we can just have an action that checks to see if the code is formatted; if it isn't, it will print out the changes required to fix the issue. The developer can either use the output of that to fix the formatting, or start using clang-format.