Index: clang-tidy/performance/UnnecessaryValueParamCheck.h =================================================================== --- clang-tidy/performance/UnnecessaryValueParamCheck.h +++ clang-tidy/performance/UnnecessaryValueParamCheck.h @@ -12,6 +12,7 @@ #include "../ClangTidy.h" #include "../utils/IncludeInserter.h" +#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h" namespace clang { namespace tidy { @@ -29,11 +30,14 @@ void check(const ast_matchers::MatchFinder::MatchResult &Result) override; void registerPPCallbacks(CompilerInstance &Compiler) override; void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + void onEndOfTranslationUnit() override; private: void handleMoveFix(const ParmVarDecl &Var, const DeclRefExpr &CopyArgument, const ASTContext &Context); + llvm::DenseMap + MutationAnalyzers; std::unique_ptr Inserter; const utils::IncludeSorter::IncludeStyle IncludeStyle; }; Index: clang-tidy/performance/UnnecessaryValueParamCheck.cpp =================================================================== --- clang-tidy/performance/UnnecessaryValueParamCheck.cpp +++ clang-tidy/performance/UnnecessaryValueParamCheck.cpp @@ -13,7 +13,6 @@ #include "../utils/FixItHintUtils.h" #include "../utils/Matchers.h" #include "../utils/TypeTraits.h" -#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Lex/Lexer.h" #include "clang/Lex/Preprocessor.h" @@ -92,21 +91,11 @@ const auto *Param = Result.Nodes.getNodeAs("param"); const auto *Function = Result.Nodes.getNodeAs("functionDecl"); - // Do not trigger on non-const value parameters when they are mutated either - // within the function body or within init expression(s) when the function is - // a ctor. - if (ExprMutationAnalyzer(*Function->getBody(), *Result.Context) - .isMutated(Param)) + FunctionParmMutationAnalyzer &Analyzer = + MutationAnalyzers.try_emplace(Function, *Function, *Result.Context) + .first->second; + if (Analyzer.isMutated(Param)) return; - // CXXCtorInitializer might also mutate Param but they're not part of function - // body, so check them separately here. - if (const auto *Ctor = dyn_cast(Function)) { - for (const auto *Init : Ctor->inits()) { - if (ExprMutationAnalyzer(*Init->getInit(), *Result.Context) - .isMutated(Param)) - return; - } - } const bool IsConstQualified = Param->getType().getCanonicalType().isConstQualified(); @@ -186,6 +175,10 @@ utils::IncludeSorter::toString(IncludeStyle)); } +void UnnecessaryValueParamCheck::onEndOfTranslationUnit() { + MutationAnalyzers.clear(); +} + void UnnecessaryValueParamCheck::handleMoveFix(const ParmVarDecl &Var, const DeclRefExpr &CopyArgument, const ASTContext &Context) {