Skip to content
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

Clang format for Common++ #1451

Merged
merged 15 commits into from
Jun 21, 2024
Merged

Clang format for Common++ #1451

merged 15 commits into from
Jun 21, 2024

Conversation

tigercosmos
Copy link
Collaborator

@tigercosmos tigercosmos commented Jun 17, 2024

apply clang-format for Common++

@tigercosmos tigercosmos requested a review from seladb as a code owner June 17, 2024 07:48
@tigercosmos tigercosmos marked this pull request as draft June 17, 2024 07:48
@tigercosmos tigercosmos added this to the Augest 2024 Release milestone Jun 17, 2024
AllowShortFunctionsOnASingleLine: All
NamespaceIndentation: All

SortIncludes: false
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prevent some possible includes' error due to the ordering.

.clang-format Outdated
@@ -1,7 +1,8 @@
---
BasedOnStyle: Microsoft
UseTab: Always
BreakBeforeBraces: Allman
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the style like this:

void Foo ()
{
// ...
}

@@ -30,6 +30,7 @@ jobs:
run: |
apk update && apk add cppcheck python3-dev
python3 -m pip install cmake-format
pip install clang-format==18.1.6
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

specify the clang-formant here.

@@ -16,9 +16,11 @@ repos:
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.5
hooks:
- id: clang-format
args: ["--style=file"] # Use the .clang-format file for configuration
files: ^Common\+\+/.*\.(cpp|h)$
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apply on Common++ for now.

#pragma message("WARNING: Disabling of warnings is not implemented for this compiler")
#define DISABLE_WARNING_PUSH
#define DISABLE_WARNING_POP
// clang-format off
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the manual fix, since the clang-formant cannot do the formating well here. it tries to separate -Wdeprecated-declarations to -Wdeprecated - declarations

@tigercosmos tigercosmos changed the title Clang format Clang format for Commit++ Jun 18, 2024
@tigercosmos tigercosmos changed the base branch from master to dev June 18, 2024 09:07
@tigercosmos tigercosmos marked this pull request as ready for review June 18, 2024 09:07
@seladb
Copy link
Owner

seladb commented Jun 18, 2024

@tigercosmos the PR title should be:
"Clang format for Common++", not Commit++ 🙂

@tigercosmos tigercosmos changed the title Clang format for Commit++ Clang format for Common++ Jun 18, 2024
@tigercosmos
Copy link
Collaborator Author

sorry, fixed the title.

btw, seems that the CI already runs the check for clang-format

@tigercosmos
Copy link
Collaborator Author

Sorry, I think it's okay to squash the commits, and we can add the ignore hash in the next PR.

AllowShortFunctionsOnASingleLine: All
NamespaceIndentation: All

SortIncludes: false
IndentPPDirectives: AfterHash
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let the macro has indent


SortIncludes: false
IndentPPDirectives: AfterHash
IndentWidth: 4
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macro indent width

Common++/header/IpAddressUtils.h Outdated Show resolved Hide resolved
Common++/header/GeneralUtils.h Outdated Show resolved Hide resolved
Common++/header/IpAddress.h Outdated Show resolved Hide resolved
Common++/header/IpAddress.h Outdated Show resolved Hide resolved
Common++/header/IpAddress.h Outdated Show resolved Hide resolved
Common++/header/Logger.h Outdated Show resolved Hide resolved
Common++/header/Logger.h Outdated Show resolved Hide resolved
Common++/header/Logger.h Show resolved Hide resolved
Comment on lines +31 to +33
sstream << file << ": " << method << ":" << line;
std::cout << std::left << "[" << std::setw(5) << Logger::logLevelAsString(logLevel) << ": " << std::setw(45)
<< sstream.str() << "] " << logMessage << std::endl;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like the previous version better, but not sure if there is a way to keep it...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find the solution for this.

Common++/src/SystemUtils.cpp Outdated Show resolved Hide resolved
@tigercosmos tigercosmos requested a review from seladb June 19, 2024 09:06
Comment on lines 61 to 63
IPv4Address(const std::array<uint8_t, 4>& bytes) : m_Bytes(bytes)
{
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting change doesn't make a lot of sense, is there an option to avoid it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is due to BreakBeforeBraces: Allman

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change to Custom we can set SplitEmptyFunction: false which I think should solve it:
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#bracewrapping

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, I would stand for splitting the braces in this case. shall we just use the default Allman settings to prevent too much customization from using Custom?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the default values of the different flags under BraceWrapping. If all the defaults are what we need, I think it's ok to set:

BreakBeforeBraces: Custom
BraceWrapping:
  SplitEmptyFunction: false

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried your config, and the effect is like this:

void matchNetwork(const std::string& network) {}

become this:

void matchNetwork(const std::string& network)
{}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is slightly better I think. The rest of the curly braces formatting stays the same?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there is something in the braces, yes, it will be the same.

void matchNetwork(const std::string& network)
{
   // ...
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, applied your config.

Common++/header/LRUList.h Show resolved Hide resolved
MacAddress(const T& addr) : MacAddress(static_cast<std::string>(addr)) {};

template <typename T, typename = typename std::enable_if<std::is_convertible<T, std::string>::value>::type>
MacAddress(const T& addr) : MacAddress(static_cast<std::string>(addr)){};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it keeps the empty curly braces {} in the same line, as opposed to IpAddress.h where it didn't. Any idea why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe because it's a template?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I know why, probably due to ;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, after I removed ;, it becomes normal.
But for now I can do manually fixing, but after when we apply a lot of files, it's hard to pick these syntax issues one by one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, maybe we should just remove the ;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, after I removed ;, it becomes normal. But for now I can do manually fixing, but after when we apply a lot of files, it's hard to pick these syntax issues one by one.

we should fix things whenever we see them, of course we might miss some...

Comment on lines 55 to 57
vendorMap.insert({
std::stoull(line.key()), { val["vendor"], vLocalMaskedFilter }
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a weird mix of tabs and spaces in these lines, but not sure if it can be fixed 🤔

Copy link
Collaborator Author

@tigercosmos tigercosmos Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is in purpose. the indent is tab, the alignment is space.
for example:

<tab> void  foo(int a,
<tab><spaces>   int b) {}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but why does line 57 has spaces instead of tabs? Maybe we should just move }); to line 56?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... you are right.. it's weird here as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried all options of UseTab, and all options decide to use spaces there
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#usetab

Seems it is something that we cannot control, sometime clang-tidy is stupid.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you try moving }); from line 57 to line 56 before running clang-format?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, it doesn't help
no matter this;

vendorMap.insert({std::stoull(line.key()), { val["vendor"], vLocalMaskedFilter }});

or

			vendorMap.insert(
				{std::stoull(line.key()), { val["vendor"], vLocalMaskedFilter }});

they will be formatted as:

			vendorMap.insert({
				std::stoull(line.key()), { val["vendor"], vLocalMaskedFilter }
            });

one way is to use // clang-format off here, but I would suggest just let it be this if it doesn't break the build.
according to my experience, when there is too many braces, clang-format becomes stupid.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we can just leave it the way it is... 😕

Common++/src/SystemUtils.cpp Outdated Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add this as empty file in this commit, or add it later after we merge?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have it now, we will add the file anyway.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion, but I think it's better to add the file after we have the commit hash. But if you feel strongly about keeping it, I'm ok with it

@tigercosmos tigercosmos requested a review from seladb June 20, 2024 09:52
@tigercosmos tigercosmos merged commit e3128c6 into seladb:dev Jun 21, 2024
39 checks passed
@tigercosmos tigercosmos deleted the clang-format branch June 21, 2024 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants