clang-tools 6.0.0

ForRangeCopyCheck.cpp
Go to the documentation of this file.
1 //===--- ForRangeCopyCheck.cpp - clang-tidy--------------------------------===//
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 #include "ForRangeCopyCheck.h"
11 #include "../utils/DeclRefExprUtils.h"
12 #include "../utils/FixItHintUtils.h"
13 #include "../utils/TypeTraits.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace performance {
20
21 ForRangeCopyCheck::ForRangeCopyCheck(StringRef Name, ClangTidyContext *Context)
22 : ClangTidyCheck(Name, Context),
23 WarnOnAllAutoCopies(Options.get("WarnOnAllAutoCopies", 0)) {}
24
25 void ForRangeCopyCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
26 Options.store(Opts, "WarnOnAllAutoCopies", WarnOnAllAutoCopies);
27 }
28
29 void ForRangeCopyCheck::registerMatchers(MatchFinder *Finder) {
30 // Match loop variables that are not references or pointers or are already
31 // initialized through MaterializeTemporaryExpr which indicates a type
32 // conversion.
33 auto LoopVar = varDecl(
34 hasType(hasCanonicalType(unless(anyOf(referenceType(), pointerType())))),
35 unless(hasInitializer(expr(hasDescendant(materializeTemporaryExpr())))));
36 Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar")))
37 .bind("forRange"),
38 this);
39 }
40
41 void ForRangeCopyCheck::check(const MatchFinder::MatchResult &Result) {
42 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("loopVar");
43 // Ignore code in macros since we can't place the fixes correctly.
44 if (Var->getLocStart().isMacroID())
45 return;
46 if (handleConstValueCopy(*Var, *Result.Context))
47 return;
48 const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange");
49 handleCopyIsOnlyConstReferenced(*Var, *ForRange, *Result.Context);
50 }
51
52 bool ForRangeCopyCheck::handleConstValueCopy(const VarDecl &LoopVar,
53 ASTContext &Context) {
54 if (WarnOnAllAutoCopies) {
55 // For aggressive check just test that loop variable has auto type.
56 if (!isa<AutoType>(LoopVar.getType()))
57 return false;
58 } else if (!LoopVar.getType().isConstQualified()) {
59 return false;
60 }
61 llvm::Optional<bool> Expensive =
62 utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
63 if (!Expensive || !*Expensive)
64 return false;
65 auto Diagnostic =
66 diag(LoopVar.getLocation(),
67 "the loop variable's type is not a reference type; this creates a "
68 "copy in each iteration; consider making this a reference")
69 << utils::fixit::changeVarDeclToReference(LoopVar, Context);
70 if (!LoopVar.getType().isConstQualified())
71 Diagnostic << utils::fixit::changeVarDeclToConst(LoopVar);
72 return true;
73 }
74
75 bool ForRangeCopyCheck::handleCopyIsOnlyConstReferenced(
76 const VarDecl &LoopVar, const CXXForRangeStmt &ForRange,
77 ASTContext &Context) {
78 llvm::Optional<bool> Expensive =
79 utils::type_traits::isExpensiveToCopy(LoopVar.getType(), Context);
80 if (LoopVar.getType().isConstQualified() || !Expensive || !*Expensive)
81 return false;
82 if (!utils::decl_ref_expr::isOnlyUsedAsConst(LoopVar, *ForRange.getBody(),
83 Context))
84 return false;
85 diag(LoopVar.getLocation(),
86 "loop variable is copied but only used as const reference; consider "
87 "making it a const reference")
88 << utils::fixit::changeVarDeclToConst(LoopVar)
89 << utils::fixit::changeVarDeclToReference(LoopVar, Context);
90 return true;
91 }
92
93 } // namespace performance
94 } // namespace tidy
95 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
clang::tidy::OptionsView::store
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:449
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::utils::decl_ref_expr::isOnlyUsedAsConst
bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt, ASTContext &Context)
Returns true if all DeclRefExpr to the variable within Stmt do not modify it.
Definition: DeclRefExprUtils.cpp:108
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
ForRangeCopyCheck.h
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
clang::tidy::utils::type_traits::isExpensiveToCopy
llvm::Optional< bool > isExpensiveToCopy(QualType Type, const ASTContext &Context)
Returns true if Type is expensive to copy.
Definition: TypeTraits.cpp:42
clang::tidy::performance::ForRangeCopyCheck::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: ForRangeCopyCheck.cpp:25
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:99
clang::tidy::performance::ForRangeCopyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ForRangeCopyCheck.cpp:29
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104
clang::tidy::performance::ForRangeCopyCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ForRangeCopyCheck.cpp:41
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:416
clang::tidy::utils::fixit::changeVarDeclToConst
FixItHint changeVarDeclToConst(const VarDecl &Var)
Creates fix to make VarDecl const qualified.
Definition: FixItHintUtils.cpp:29
clang::tidy::utils::fixit::changeVarDeclToReference
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context)
Creates fix to make VarDecl a reference by adding &.
Definition: FixItHintUtils.cpp:19