clang-tools
20.0.0git
llvm-project
clang-tools-extra
clang-tidy
performance
UnnecessaryCopyInitialization.h
Go to the documentation of this file.
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
protected
:
36
// A helper to manipulate the state common to
37
// `CopyFromMethodReturn` and `CopyFromLocalVar`.
38
struct
CheckContext
{
39
const
VarDecl &
Var
;
40
const
Stmt &
BlockStmt
;
41
const
DeclStmt &
VarDeclStmt
;
42
clang::ASTContext &
ASTCtx
;
43
const
bool
IssueFix
;
44
const
bool
IsVarUnused
;
45
const
bool
IsVarOnlyUsedAsConst
;
46
};
47
48
// Create diagnostics. These are virtual so that derived classes can change
49
// behaviour.
50
virtual
void
diagnoseCopyFromMethodReturn
(
const
CheckContext
&Ctx);
51
virtual
void
diagnoseCopyFromLocalVar
(
const
CheckContext
&Ctx,
52
const
VarDecl &OldVar);
53
54
private
:
55
void
handleCopyFromMethodReturn(
const
CheckContext
&Ctx,
56
const
VarDecl *ObjectArg);
57
void
handleCopyFromLocalVar(
const
CheckContext
&Ctx,
const
VarDecl &OldVar);
58
59
void
maybeIssueFixes(
const
CheckContext
&Ctx, DiagnosticBuilder &
Diagnostic
);
60
61
const
std::vector<StringRef> AllowedTypes;
62
const
std::vector<StringRef> ExcludedContainerTypes;
63
};
64
65
}
// namespace clang::tidy::performance
66
67
#endif
// LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_UNNECESSARY_COPY_INITIALIZATION_H
Name
llvm::SmallString< 256U > Name
Definition:
ChainedComparisonCheck.cpp:42
Diagnostic
DiagnosticCallback Diagnostic
Definition:
ConfigCompile.cpp:100
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition:
ClangTidyCheck.h:53
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition:
ClangTidyDiagnosticConsumer.h:69
clang::tidy::performance::UnnecessaryCopyInitialization
Definition:
UnnecessaryCopyInitialization.h:25
clang::tidy::performance::UnnecessaryCopyInitialization::diagnoseCopyFromLocalVar
virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar)
Definition:
UnnecessaryCopyInitialization.cpp:356
clang::tidy::performance::UnnecessaryCopyInitialization::diagnoseCopyFromMethodReturn
virtual void diagnoseCopyFromMethodReturn(const CheckContext &Ctx)
Definition:
UnnecessaryCopyInitialization.cpp:343
clang::tidy::performance::UnnecessaryCopyInitialization::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition:
UnnecessaryCopyInitialization.cpp:273
clang::tidy::performance::UnnecessaryCopyInitialization::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition:
UnnecessaryCopyInitialization.h:28
clang::tidy::performance::UnnecessaryCopyInitialization::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition:
UnnecessaryCopyInitialization.cpp:234
clang::tidy::performance::UnnecessaryCopyInitialization::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition:
UnnecessaryCopyInitialization.cpp:377
clang::tidy::performance
Definition:
AvoidEndlCheck.cpp:20
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition:
ClangTidyOptions.h:126
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext
Definition:
UnnecessaryCopyInitialization.h:38
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::BlockStmt
const Stmt & BlockStmt
Definition:
UnnecessaryCopyInitialization.h:40
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::IsVarOnlyUsedAsConst
const bool IsVarOnlyUsedAsConst
Definition:
UnnecessaryCopyInitialization.h:45
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::Var
const VarDecl & Var
Definition:
UnnecessaryCopyInitialization.h:39
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::IsVarUnused
const bool IsVarUnused
Definition:
UnnecessaryCopyInitialization.h:44
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::IssueFix
const bool IssueFix
Definition:
UnnecessaryCopyInitialization.h:43
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::VarDeclStmt
const DeclStmt & VarDeclStmt
Definition:
UnnecessaryCopyInitialization.h:41
clang::tidy::performance::UnnecessaryCopyInitialization::CheckContext::ASTCtx
clang::ASTContext & ASTCtx
Definition:
UnnecessaryCopyInitialization.h:42
Generated on Fri Oct 4 2024 07:42:29 for clang-tools by
1.9.6