Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H |
| 11 | |
| 12 | #include "../ClangTidyCheck.h" |
| 13 | |
| 14 | namespace clang { |
| 15 | namespace tidy { |
| 16 | namespace performance { |
| 17 | |
| 18 | // The check detects local variable declarations that are copy initialized with |
| 19 | // the const reference of a function call or the const reference of a method |
| 20 | // call whose object is guaranteed to outlive the variable's scope and suggests |
| 21 | // to use a const reference. |
| 22 | // |
| 23 | // The check currently only understands a subset of variables that are |
| 24 | // guaranteed to outlive the const reference returned, namely: const variables, |
| 25 | // const references, and const pointers to const. |
| 26 | class UnnecessaryCopyInitialization : public ClangTidyCheck { |
| 27 | public: |
| 28 | UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context); |
| 29 | bool isLanguageVersionSupported(const LangOptions &LangOpts) const override{ |
| 30 | return LangOpts.CPlusPlus; |
| 31 | } |
| 32 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 33 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 34 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 35 | |
| 36 | private: |
| 37 | void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt, |
| 38 | bool IssueFix, const VarDecl *ObjectArg, |
| 39 | ASTContext &Context); |
| 40 | void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar, |
| 41 | const Stmt &BlockStmt, bool IssueFix, |
| 42 | ASTContext &Context); |
| 43 | const std::vector<std::string> AllowedTypes; |
| 44 | }; |
| 45 | |
| 46 | } // namespace performance |
| 47 | } // namespace tidy |
| 48 | } // namespace clang |
| 49 | |
| 50 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H |