Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:21

0001 //===--- UnnecessaryCopyInitialization.h - clang-tidy------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 #include "clang/AST/Decl.h"
0014 
0015 namespace clang::tidy::performance {
0016 
0017 // The check detects local variable declarations that are copy initialized with
0018 // the const reference of a function call or the const reference of a method
0019 // call whose object is guaranteed to outlive the variable's scope and suggests
0020 // to use a const reference.
0021 //
0022 // The check currently only understands a subset of variables that are
0023 // guaranteed to outlive the const reference returned, namely: const variables,
0024 // const references, and const pointers to const.
0025 class UnnecessaryCopyInitialization : public ClangTidyCheck {
0026 public:
0027   UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context);
0028   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
0029     return LangOpts.CPlusPlus;
0030   }
0031   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
0032   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
0033   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0034 
0035 protected:
0036   // A helper to manipulate the state common to
0037   // `CopyFromMethodReturn` and `CopyFromLocalVar`.
0038   struct CheckContext {
0039     const VarDecl &Var;
0040     const Stmt &BlockStmt;
0041     const DeclStmt &VarDeclStmt;
0042     clang::ASTContext &ASTCtx;
0043     const bool IssueFix;
0044     const bool IsVarUnused;
0045     const bool IsVarOnlyUsedAsConst;
0046   };
0047 
0048   // Create diagnostics. These are virtual so that derived classes can change
0049   // behaviour.
0050   virtual void diagnoseCopyFromMethodReturn(const CheckContext &Ctx);
0051   virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx,
0052                                         const VarDecl &OldVar);
0053 
0054 private:
0055   void handleCopyFromMethodReturn(const CheckContext &Ctx,
0056                                   const VarDecl *ObjectArg);
0057   void handleCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar);
0058 
0059   void maybeIssueFixes(const CheckContext &Ctx, DiagnosticBuilder &Diagnostic);
0060 
0061   const std::vector<StringRef> AllowedTypes;
0062   const std::vector<StringRef> ExcludedContainerTypes;
0063 };
0064 
0065 } // namespace clang::tidy::performance
0066 
0067 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H