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

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 22 08:38:38 PST 2023


================
@@ -876,7 +876,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
           << PluginFN << toString(PassPlugin.takeError());
     }
   }
-  for (auto PassCallback : CodeGenOpts.PassBuilderCallbacks)
+  for (const auto &PassCallback : CodeGenOpts.PassBuilderCallbacks)
----------------
tahonermann wrote:

`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](https://wg21.link/n4348), [P0045](https://wg21.link/p0045)) but to my knowledge, the standard has not been changed.

This looks like a good change.

https://github.com/llvm/llvm-project/pull/75082


More information about the cfe-commits mailing list