[clang] [clang-tools-extra] [clang-tidy] Fix performance-unnecessary-value-param (PR #109145)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 18 08:05:30 PDT 2024


================
@@ -118,10 +118,19 @@ class FunctionParmMutationAnalyzer {
   static FunctionParmMutationAnalyzer *
   getFunctionParmMutationAnalyzer(const FunctionDecl &Func, ASTContext &Context,
                                   ExprMutationAnalyzer::Memoized &Memorized) {
-    auto [it, Inserted] = Memorized.FuncParmAnalyzer.try_emplace(&Func);
-    if (Inserted)
-      it->second = std::unique_ptr<FunctionParmMutationAnalyzer>(
-          new FunctionParmMutationAnalyzer(Func, Context, Memorized));
+    auto it = Memorized.FuncParmAnalyzer.find(&Func);
+    if (it == Memorized.FuncParmAnalyzer.end())
+      // We call try_emplace here, repeating a hash lookup on the same key. Note
+      // that creating a new instance of FunctionParmMutationAnalyzer below may
+      // add additional elements to FuncParmAnalyzer and invalidate iterators.
----------------
HerrCai0907 wrote:

creating a new instance of FunctionParmMutationAnalyzer below may add additional elements to FuncParmAnalyzer. If we do try_emplace before creating new instance, the returned iterator of try_emplace will be invalid.

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


More information about the cfe-commits mailing list