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