[PATCH] D103021: [clang-tidy] performance-unnecessary-copy-initialization: Search whole function body for variable initializations.

Yitzhak Mandelbaum via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 2 07:28:54 PDT 2021


ymandel added inline comments.


================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp:97
+  // Search the whole function body for decl statements of the initialization
+  // variable not just the current block statement.
   auto Matches =
----------------
Maybe a bit clearer:
// Search the whole function body, not just the current block statement, for decl statements of the initialization variable.


================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp:98-101
   auto Matches =
       match(findAll(declStmt(has(varDecl(equalsNode(&InitializingVar))))
                         .bind("declStmt")),
+            Body, Context);
----------------
Consider inspecting the `DeclContext`s instead, which should be much more efficient than searching the entire block.  Pass the `FunctionDecl` as an argument instead of `Body`, since it is a `DeclContext`.  e.g. `const DeclContext &Fun`

Then, either
1. Call `Fun.containsDecl(InitializingVar)`, or
2. Search through the contexts yourself; something like:
```
DeclContext* DC = InitializingVar->getDeclContext(); 
while (DC != nullptr && DC != &Fun)
  DC = DC->getLexicalParent();
if (DC == nullptr)
  // The reference or pointer is not initialized anywhere witin the function. We
  // assume its pointee is not modified then.
  return true;
```


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp:530
+
+  auto Labmda = []() {
+    ExpensiveToCopyType Orig;
----------------
typo: Lambda


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103021/new/

https://reviews.llvm.org/D103021



More information about the cfe-commits mailing list