Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 1 | //===--- UnnecessaryCopyInitialization.h - clang-tidy------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H |
| 12 | |
| 13 | #include "../ClangTidy.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace tidy { |
| 17 | namespace performance { |
| 18 | |
Felix Berger | adfdc14 | 2016-03-05 21:17:58 +0000 | [diff] [blame] | 19 | // The check detects local variable declarations that are copy initialized with |
| 20 | // the const reference of a function call or the const reference of a method |
| 21 | // call whose object is guaranteed to outlive the variable's scope and suggests |
| 22 | // to use a const reference. |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 23 | // |
| 24 | // The check currently only understands a subset of variables that are |
| 25 | // guaranteed to outlive the const reference returned, namely: const variables, |
| 26 | // const references, and const pointers to const. |
| 27 | class UnnecessaryCopyInitialization : public ClangTidyCheck { |
| 28 | public: |
Alexander Kornienko | 1589071 | 2015-12-30 11:39:30 +0000 | [diff] [blame] | 29 | UnnecessaryCopyInitialization(StringRef Name, ClangTidyContext *Context) |
| 30 | : ClangTidyCheck(Name, Context) {} |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 31 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 32 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 33 | |
| 34 | private: |
| 35 | void handleCopyFromMethodReturn(const VarDecl &Var, const Stmt &BlockStmt, |
Felix Berger | 98e4019 | 2016-05-31 00:25:57 +0000 | [diff] [blame] | 36 | bool IssueFix, const VarDecl *ObjectArg, |
| 37 | ASTContext &Context); |
Haojian Wu | 6ccb1dd | 2016-03-23 09:33:07 +0000 | [diff] [blame] | 38 | void handleCopyFromLocalVar(const VarDecl &NewVar, const VarDecl &OldVar, |
Felix Berger | 6d3d746 | 2016-05-13 02:47:56 +0000 | [diff] [blame] | 39 | const Stmt &BlockStmt, bool IssueFix, |
| 40 | ASTContext &Context); |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | } // namespace performance |
| 44 | } // namespace tidy |
| 45 | } // namespace clang |
| 46 | |
| 47 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H |