UnnecessaryCopyInitialization.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //===--- UnnecessaryCopyInitialization.h - clang-tidy------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
  10. #include "../ClangTidyCheck.h"
  11. #include "clang/AST/Decl.h"
  12. namespace clang::tidy::performance {
  13. // The check detects local variable declarations that are copy initialized with
  14. // the const reference of a function call or the const reference of a method
  15. // call whose object is guaranteed to outlive the variable's scope and suggests
  16. // to use a const reference.
  17. //
  18. // The check currently only understands a subset of variables that are
  19. // guaranteed to outlive the const reference returned, namely: const variables,
  20. // const references, and const pointers to const.
  21. class UnnecessaryCopyInitialization : public ClangTidyCheck {
  22. public:
  23. UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context);
  24. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{
  25. return LangOpts.CPlusPlus;
  26. }
  27. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  28. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  29. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  30. private:
  31. void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt,
  32. const DeclStmt &Stmt, bool IssueFix,
  33. const VarDecl *ObjectArg,
  34. ASTContext &Context);
  35. void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar,
  36. const Stmt &BlockStmt, const DeclStmt &Stmt,
  37. bool IssueFix, ASTContext &Context);
  38. const std::vector<StringRef> AllowedTypes;
  39. const std::vector<StringRef> ExcludedContainerTypes;
  40. };
  41. } // namespace clang::tidy::performance
  42. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H