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

DataStorm linter suggested fixes #3293

Merged
merged 4 commits into from
Dec 19, 2024
Merged

Conversation

pepone
Copy link
Member

@pepone pepone commented Dec 19, 2024

This PR:

  • Includes fixes for issues reported by clang-tidy.
  • Added [[nodiscard]] to more DataStorm APIs where it make sense to do so.
  • Added using namespace Ice; to DataStorm implementation.

@@ -16,8 +16,8 @@ HeaderFilterRegex: '.*'
ExcludeHeaderFilterRegex: 'include/lmdb.h|Grammar.h'
UseColor: true
FormatStyle: 'file'
ExtraArgs: ['-std=c++20']
ExtraArgs: ['-std=c++17']
Copy link
Member Author

Choose a reason for hiding this comment

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

Otherwise we get lints for issues that require using C++ 20 and we are still using C++ 17 with GCC.

Copy link
Member

Choose a reason for hiding this comment

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

However, this breaks the linting of the Ice/span test. Maybe a better solution would be to disable the c++20 lints/suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could a .clang-tidy file to this test folder with an override to enable -std=c++20. I think clang-tidy supports this.

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 added a separate config for Ice/span test

// Public template based API implementation
//

namespace DataStorm
Copy link
Member Author

Choose a reason for hiding this comment

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

no point in declaring the same namespace twice.

@@ -140,7 +140,7 @@ namespace DataStorm
* provided the Node will use the default callback executor that executes callback in a dedicated thread.
*/
Node(
Ice::CommunicatorPtr communicator,
const Ice::CommunicatorPtr& communicator,
Copy link
Member Author

Choose a reason for hiding this comment

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

Because we always use it as a reference.

Copy link
Member

Choose a reason for hiding this comment

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

The signature is not / should not be about the implementation but what we signal to the caller.

Here, const& tells the caller: I am reading this communicator in this ctor but I don't keep a refcount on the communicator. Whereas, by value means "I keep a refcount".

I would expect the Node to keep a refcount on the communicator, possibly indirectly by holding an Ice::InstancePtr.

: sampleCount(std::move(sampleCount)),
sampleLifetime(std::move(sampleLifetime)),
clearHistory(std::move(clearHistory))
: sampleCount{sampleCount},
Copy link
Member Author

@pepone pepone Dec 19, 2024

Choose a reason for hiding this comment

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

see performance-move-const-arg for trivially-copyable

@@ -444,7 +446,6 @@ DataElementI::getConnectedElements() const
{
unique_lock<mutex> lock(_parent->_mutex);
vector<string> elements;
elements.reserve(_listeners.size());
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 reserve doesn't match the final size, there is an inner loop.

}
_executor->queue([self = shared_from_this(), init, keys = std::move(keys)] { init(std::move(keys)); }, true);
_executor->queue([init = std::move(init), keys = std::move(keys)]() mutable { init(std::move(keys)); }, true);
Copy link
Member Author

Choose a reason for hiding this comment

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

Without marking the lambda as mutable, the move keys doesn't have any effect.

Response response,
Exception exception,
std::function<void(bool, const Ice::ByteSeq&)> response,
std::function<void(std::exception_ptr)> exception,
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 added std::function to performance-unnecessary-value-param.AllowedTypes

But this doesn't work with the alias.

Copy link
Member

@bernardnormier bernardnormier Dec 19, 2024

Choose a reason for hiding this comment

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

I added std::function to performance-unnecessary-value-param.AllowedTypes

I would not do that. When you copy of std::function that holds a lambda, you copy its capture.

We often pass std::function by value because we move them around. In some cases, it's ok to pass them by const& as clang-tidy suggests.

Copy link
Member Author

Choose a reason for hiding this comment

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

reverted this, and fixed the forwarder manager to use pass by reference.

{
std::unique_lock<std::mutex> lock(_mutex);
if (_timer)
{
_timer->schedule(std::move(task), delay);
_timer->schedule(task, delay);
Copy link
Member Author

Choose a reason for hiding this comment

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

task is a const reference

// The subscriber and publisher collocated forwarders are initalized here to avoid using a nullable proxy. These
// objects are only used after the node is initialized and are removed in destroy implementation.
_publisherForwarder{instance->getCollocatedForwarder()->add<PublisherSessionPrx>(
[this](Ice::ByteSeq inParams, const Ice::Current& current) { forwardToPublishers(inParams, current); })},
[this](const ByteSeq& inParams, const Current& current) { forwardToPublishers(inParams, current); })},
Copy link
Member Author

Choose a reason for hiding this comment

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

The forwarding methods take a const reference for inParams.

NodePrx node)
: _nodeSessionManager(std::move(nodeSessionManager)),
_nodeSession(std::move(nodeSession)),
_nodeSession(nodeSession),
Copy link
Member Author

Choose a reason for hiding this comment

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

_nodeSession is a weak_ptr. The weak_ptr constructor takes a const reference to a shared_ptr.

@@ -477,7 +475,7 @@ SessionI::initSamples(int64_t topicId, DataSamplesSeq samplesSeq, const Ice::Cur

vector<shared_ptr<Sample>> samplesI;
samplesI.reserve(samples.samples.size());
auto sampleFactory = topic->getSampleFactory();
const auto& sampleFactory = topic->getSampleFactory();
Copy link
Member Author

Choose a reason for hiding this comment

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

getSampleFactory returns a const reference.

@@ -984,30 +982,30 @@ SessionI::unsubscribeFromKey(
int64_t keyId)
{
assert(_topics.find(topicId) != _topics.end());
auto& subscriber = _topics.at(topicId).getSubscriber(element->getTopic());
auto k = subscriber.get(elementId);
if (k)
Copy link
Member Author

Choose a reason for hiding this comment

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

Not lint related, using auto here was making the code harder to read.

}
};

class Warning : public Ice::Warning
Copy link
Member Author

Choose a reason for hiding this comment

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

The Warning class was never used.

reader.waitForUnread(0);
[[maybe_unused]] bool hasUnread = reader.hasUnread();
if (false)
Copy link
Member Author

@pepone pepone Dec 19, 2024

Choose a reason for hiding this comment

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

I don't see the point of this if (false), this API test is kind of weird with lots of unused variables.

Copy link
Member

Choose a reason for hiding this comment

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

I am surprised the compiler doesn't complain.

Copy link
Member

@bernardnormier bernardnormier 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 except for the addition of std::function to performance-unnecessary-value-param.AllowedTypes.

@@ -140,7 +140,7 @@ namespace DataStorm
* provided the Node will use the default callback executor that executes callback in a dedicated thread.
*/
Node(
Ice::CommunicatorPtr communicator,
const Ice::CommunicatorPtr& communicator,
Copy link
Member

Choose a reason for hiding this comment

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

The signature is not / should not be about the implementation but what we signal to the caller.

Here, const& tells the caller: I am reading this communicator in this ctor but I don't keep a refcount on the communicator. Whereas, by value means "I keep a refcount".

I would expect the Node to keep a refcount on the communicator, possibly indirectly by holding an Ice::InstancePtr.

{
}

Node::Node(
Ice::CommunicatorPtr communicator,
const CommunicatorPtr& communicator,
Copy link
Member

Choose a reason for hiding this comment

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

I assume _instance below keeps a refcount on communicator, so it should be by value. You may want to fix the DataStormI::Instance ctor - you actually already fixed it!

reader.waitForUnread(0);
[[maybe_unused]] bool hasUnread = reader.hasUnread();
if (false)
Copy link
Member

Choose a reason for hiding this comment

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

I am surprised the compiler doesn't complain.

@@ -28,7 +28,7 @@ namespace DataStormI
public:
TopicI(
std::shared_ptr<Instance>,
std::shared_ptr<TopicFactoryI>,
const std::shared_ptr<TopicFactoryI>&,
Copy link
Member

Choose a reason for hiding this comment

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

Here, switching to const shared_ptr& makes sense: we don't adopt a refcount but create and keep a weak_ptr from this const shared_ptr&.

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!

@@ -16,8 +16,8 @@ HeaderFilterRegex: '.*'
ExcludeHeaderFilterRegex: 'include/lmdb.h|Grammar.h'
UseColor: true
FormatStyle: 'file'
ExtraArgs: ['-std=c++20']
ExtraArgs: ['-std=c++17']
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could a .clang-tidy file to this test folder with an override to enable -std=c++20. I think clang-tidy supports this.

@pepone pepone merged commit 50fbf7e into zeroc-ice:main Dec 19, 2024
21 checks passed
InsertCreativityHere pushed a commit to InsertCreativityHere/compiler-comparison that referenced this pull request Jan 1, 2025
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