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 C++ #1851

Closed
wants to merge 6 commits into from

Conversation

bernardnormier
Copy link
Member

The goal is have C++ format that is close to:

  • the IceRPC C# style
  • the existing Ice C++ style
  • the cppreference style

And of course that looks nice.

Here is my initial attempt. Give it a try and improve it!

@pepone
Copy link
Member

pepone commented Feb 26, 2024

Would be good to apply the format to see the actual changes.

Copy link
Member

@pepone pepone left a comment

Choose a reason for hiding this comment

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

I run clang-format -i and changes look overall fine, we should wait until there isn't any large outgoing PRs to format the code.

Copy link
Member

@externl externl left a comment

Choose a reason for hiding this comment

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

Looks good!!!

@externl
Copy link
Member

externl commented Feb 27, 2024

I run clang-format -i and changes look overall fine, we should wait until there isn't any large outgoing PRs to format the code.

I agree, would be nice to get this in soon though.

@bernardnormier
Copy link
Member Author

I applied it to the C++ code so that you can see it on your favorite code. I am not proposing to merge this PR as-is - we need to coordinate to avoid big merge conflicts.

I had to disable include sorting as it broke the build.

We also need to write a separate clang format for the Objective-C/C++ code, including the header files.

Copy link
Member

@InsertCreativityHere InsertCreativityHere left a comment

Choose a reason for hiding this comment

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

I think it's an improvement in general.
But there were a few things that looked weird in the 20 or so files I skimmed.

Comment on lines 26 to +27

virtual void shutdown(const Ice::Current& current)
{
current.adapter->getCommunicator()->shutdown();
}
virtual void shutdown(const Ice::Current& current) { current.adapter->getCommunicator()->shutdown(); }

Choose a reason for hiding this comment

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

Even though this does fit within a 120 column line,
I think it's more readable to keep all but the trivialist of functions on multiple lines.

If there's no way to tweak this, so be it.

Choose a reason for hiding this comment

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

There's a configuration option for this:
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#allowshortfunctionsonasingleline
But it doesn't let you change it's behavior based on the length of the function body...
There are some other options for it that I think are worth looking at.

Personally, I'd prefer Empty or Inline... it's hard to tell what the values are by default.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer this kind of code on one line.

Choose a reason for hiding this comment

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

Should we add an exception for all the Bison generated files (so the formatter ignores them)?
A) No sane person will ever read this code
B) Regenerating the parsers will be slightly more painful if we need to fix the formatting of them

Copy link
Member Author

Choose a reason for hiding this comment

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

This would be desirable. I didn't research how to do it.

# define GLACIER2_API ICE_DECLSPEC_EXPORT
# else
# define GLACIER2_API ICE_DECLSPEC_IMPORT
# endif
#endif

namespace Glacier2Internal
{

Choose a reason for hiding this comment

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

What's everyone's thoughts on this trailing/leading whitespace around namespaces?

I've mostly only seen:

namespace Foo
{

stuff

}

and

namespace Foo
{
    stuff
}

So, what we're doing now feels kind of weird:

namespace Foo
{

    stuff

}

I personally think the 2nd option looks the best, and it's also what cppreference uses:
https://en.cppreference.com/w/cpp/language/namespace

Copy link
Member

@InsertCreativityHere InsertCreativityHere Feb 27, 2024

Choose a reason for hiding this comment

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

Doesn't look there's a way to enforce this with the formatter... sad.
Could probably still be fixed with a regex if everyone else agrees it looks weird.

Copy link
Member

Choose a reason for hiding this comment

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

I prefer 2nd option too

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we should remove these blank lines. They were not inserted by the formatter, it's just a consequence of our current zero-indent in namespace formatting.

cpp/include/Ice/DefaultValueFactory.h Outdated Show resolved Hide resolved
@pepone
Copy link
Member

pepone commented Feb 28, 2024

What about adding:

diff --git a/.clang-format b/.clang-format
index 4a4a7e7ad9..b4a36224fb 100644
--- a/.clang-format
+++ b/.clang-format
@@ -20,3 +20,9 @@ PointerAlignment: Left
 SortIncludes: Never
 TabWidth: 4
 UseTab: Never
+
+AlignAfterOpenBracket: AlwaysBreak
+AllowAllArgumentsOnNextLine: false
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowAllConstructorInitializersOnNextLine: false

I think it would be more consistent with our IceRCP C# style, for example:

diff --git a/cpp/src/Ice/ACM.cpp b/cpp/src/Ice/ACM.cpp
index dea228841d..bd27c19c28 100644
--- a/cpp/src/Ice/ACM.cpp
+++ b/cpp/src/Ice/ACM.cpp
@@ -20,10 +20,11 @@ IceInternal::ACMConfig::ACMConfig(bool server)
 {
 }
 
-IceInternal::ACMConfig::ACMConfig(const Ice::PropertiesPtr& p,
-                                  const Ice::LoggerPtr& l,
-                                  const string& prefix,
-                                  const ACMConfig& dflt)
+IceInternal::ACMConfig::ACMConfig(
+    const Ice::PropertiesPtr& p,
+    const Ice::LoggerPtr& l,
+    const string& prefix,
+    const ACMConfig& dflt)
 {
     string timeoutProperty;
     if ((prefix == "Ice.ACM.Client" || prefix == "Ice.ACM.Server") && p->getProperty(prefix + ".Timeout").empty())
@@ -128,8 +129,9 @@ IceInternal::FactoryACMMonitor::add(const ConnectionIPtr& connection)
     if (_connections.empty())
     {
         _connections.insert(connection);
-        _instance->timer()->scheduleRepeated(shared_from_this(),
-                                             chrono::duration_cast<chrono::nanoseconds>(_config.timeout) / 2);
+        _instance->timer()->scheduleRepeated(
+            shared_from_this(),
+            chrono::duration_cast<chrono::nanoseconds>(_config.timeout) / 2);
     }
     else
     {
@@ -158,9 +160,10 @@ IceInternal::FactoryACMMonitor::reap(const ConnectionIPtr& connection)
 }
 
 ACMMonitorPtr
-IceInternal::FactoryACMMonitor::acm(const optional<int>& timeout,
-                                    const optional<Ice::ACMClose>& close,
-                                    const optional<Ice::ACMHeartbeat>& heartbeat)
+IceInternal::FactoryACMMonitor::acm(
+    const optional<int>& timeout,
+    const optional<Ice::ACMClose>& close,
+    const optional<Ice::ACMHeartbeat>& heartbeat)
 {

@bernardnormier
Copy link
Member Author

What about adding:

Applied.

@bernardnormier
Copy link
Member Author

Replaced by #1916.

@bernardnormier bernardnormier deleted the clang-format branch May 10, 2024 23:45
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.

4 participants