Skip to content

[NFC][CLANG] Fix static analyzer bugs about unnecessary object copies with auto keyword #75082

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

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
<< PluginFN << toString(PassPlugin.takeError());
}
}
for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks)
for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
Copy link
Contributor

Choose a reason for hiding this comment

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

PassBuilderCallbacks is defined as:

clang/include/clang/Basic/CodeGenOptions.h:
404   /// List of pass builder callbacks.
405   std::vector<std::function<void(llvm::PassBuilder &)>> PassBuilderCallbacks;

In the case where a std::function element wraps a lambda, copies can be expensive, so such avoidance makes sense. The operator() member of std::function is already const qualified, so the addition of const doesn't effect the behavior, at least not in the current language standards. Such calls are valid even when a mutable lambda is wrapped. That arguably results in a const violation, so there have been some papers that argued for changes (N4348, P0045) but to my knowledge, the standard has not been changed.

This looks like a good change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @tahonermann for reviews and information!

PassCallback(PB);
#define HANDLE_EXTENSION(Ext) \
get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
Expand Down