You've already forked linux-packaging-mono
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
change-namespace
clang-apply-replacements
clang-move
clang-query
clang-reorder-fields
clang-tidy
android
boost
bugprone
cert
cppcoreguidelines
fuchsia
google
hicpp
llvm
misc
modernize
mpi
objc
performance
CMakeLists.txt
FasterStringFindCheck.cpp
FasterStringFindCheck.h
ForRangeCopyCheck.cpp
ForRangeCopyCheck.h
ImplicitConversionInLoopCheck.cpp
ImplicitConversionInLoopCheck.h
InefficientAlgorithmCheck.cpp
InefficientAlgorithmCheck.h
InefficientStringConcatenationCheck.cpp
InefficientStringConcatenationCheck.h
InefficientVectorOperationCheck.cpp
InefficientVectorOperationCheck.h
MoveConstArgCheck.cpp
MoveConstArgCheck.h
MoveConstructorInitCheck.cpp
MoveConstructorInitCheck.h
NoexceptMoveConstructorCheck.cpp
NoexceptMoveConstructorCheck.h
PerformanceTidyModule.cpp
TypePromotionInMathFnCheck.cpp
TypePromotionInMathFnCheck.h
UnnecessaryCopyInitialization.cpp
UnnecessaryCopyInitialization.h
UnnecessaryValueParamCheck.cpp
UnnecessaryValueParamCheck.h
plugin
readability
tool
utils
CMakeLists.txt
ClangTidy.cpp
ClangTidy.h
ClangTidyDiagnosticConsumer.cpp
ClangTidyDiagnosticConsumer.h
ClangTidyModule.cpp
ClangTidyModule.h
ClangTidyModuleRegistry.h
ClangTidyOptions.cpp
ClangTidyOptions.h
add_new_check.py
rename_check.py
clang-tidy-vs
clangd
docs
include-fixer
modularize
pp-trace
test
tool-template
unittests
.arcconfig
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
LICENSE.TXT
README.txt
compiler-rt
libcxx
libcxxabi
libunwind
lld
lldb
llvm
openmp
polly
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mk
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
99 lines
3.9 KiB
C++
99 lines
3.9 KiB
C++
//===--- ImplicitConversionInLoopCheck.cpp - clang-tidy--------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "ImplicitConversionInLoopCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/AST/Decl.h"
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
namespace clang {
|
|
namespace tidy {
|
|
namespace performance {
|
|
|
|
// Checks if the stmt is a ImplicitCastExpr with a CastKind that is not a NoOp.
|
|
// The subtelty is that in some cases (user defined conversions), we can
|
|
// get to ImplicitCastExpr inside each other, with the outer one a NoOp. In this
|
|
// case we skip the first cast expr.
|
|
static bool IsNonTrivialImplicitCast(const Stmt *ST) {
|
|
if (const auto *ICE = dyn_cast<ImplicitCastExpr>(ST)) {
|
|
return (ICE->getCastKind() != CK_NoOp) ||
|
|
IsNonTrivialImplicitCast(ICE->getSubExpr());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ImplicitConversionInLoopCheck::registerMatchers(MatchFinder *Finder) {
|
|
// We look for const ref loop variables that (optionally inside an
|
|
// ExprWithCleanup) materialize a temporary, and contain a implicit
|
|
// conversion. The check on the implicit conversion is done in check() because
|
|
// we can't access implicit conversion subnode via matchers: has() skips casts
|
|
// and materialize! We also bind on the call to operator* to get the proper
|
|
// type in the diagnostic message.
|
|
//
|
|
// Note that when the implicit conversion is done through a user defined
|
|
// conversion operator, the node is a CXXMemberCallExpr, not a
|
|
// CXXOperatorCallExpr, so it should not get caught by the
|
|
// cxxOperatorCallExpr() matcher.
|
|
Finder->addMatcher(
|
|
cxxForRangeStmt(hasLoopVariable(
|
|
varDecl(hasType(qualType(references(qualType(isConstQualified())))),
|
|
hasInitializer(expr(hasDescendant(cxxOperatorCallExpr().bind(
|
|
"operator-call")))
|
|
.bind("init")))
|
|
.bind("faulty-var"))),
|
|
this);
|
|
}
|
|
|
|
void ImplicitConversionInLoopCheck::check(
|
|
const MatchFinder::MatchResult &Result) {
|
|
const auto *VD = Result.Nodes.getNodeAs<VarDecl>("faulty-var");
|
|
const auto *Init = Result.Nodes.getNodeAs<Expr>("init");
|
|
const auto *OperatorCall =
|
|
Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator-call");
|
|
|
|
if (const auto *Cleanup = dyn_cast<ExprWithCleanups>(Init))
|
|
Init = Cleanup->getSubExpr();
|
|
|
|
const auto *Materialized = dyn_cast<MaterializeTemporaryExpr>(Init);
|
|
if (!Materialized)
|
|
return;
|
|
|
|
// We ignore NoOp casts. Those are generated if the * operator on the
|
|
// iterator returns a value instead of a reference, and the loop variable
|
|
// is a reference. This situation is fine (it probably produces the same
|
|
// code at the end).
|
|
if (IsNonTrivialImplicitCast(Materialized->getTemporary()))
|
|
ReportAndFix(Result.Context, VD, OperatorCall);
|
|
}
|
|
|
|
void ImplicitConversionInLoopCheck::ReportAndFix(
|
|
const ASTContext *Context, const VarDecl *VD,
|
|
const CXXOperatorCallExpr *OperatorCall) {
|
|
// We only match on const ref, so we should print a const ref version of the
|
|
// type.
|
|
QualType ConstType = OperatorCall->getType().withConst();
|
|
QualType ConstRefType = Context->getLValueReferenceType(ConstType);
|
|
const char Message[] =
|
|
"the type of the loop variable %0 is different from the one returned "
|
|
"by the iterator and generates an implicit conversion; you can either "
|
|
"change the type to the matching one (%1 but 'const auto&' is always a "
|
|
"valid option) or remove the reference to make it explicit that you are "
|
|
"creating a new value";
|
|
diag(VD->getLocStart(), Message) << VD << ConstRefType;
|
|
}
|
|
|
|
} // namespace performance
|
|
} // namespace tidy
|
|
} // namespace clang
|