2015-08-19 17:11:46 +08:00
|
|
|
//===--- LoopConvertCheck.cpp - clang-tidy---------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-19 17:11:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LoopConvertCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2016-11-30 02:24:01 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2016-12-14 00:38:45 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2020-04-08 03:04:13 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2016-11-30 02:24:01 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2020-07-11 17:10:59 +08:00
|
|
|
|
|
|
|
template <> struct OptionEnumMapping<modernize::Confidence::Level> {
|
|
|
|
static llvm::ArrayRef<std::pair<modernize::Confidence::Level, StringRef>>
|
|
|
|
getEnumMapping() {
|
|
|
|
static constexpr std::pair<modernize::Confidence::Level, StringRef>
|
|
|
|
Mapping[] = {{modernize::Confidence::CL_Reasonable, "reasonable"},
|
|
|
|
{modernize::Confidence::CL_Safe, "safe"},
|
|
|
|
{modernize::Confidence::CL_Risky, "risky"}};
|
|
|
|
return makeArrayRef(Mapping);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct OptionEnumMapping<modernize::VariableNamer::NamingStyle> {
|
|
|
|
static llvm::ArrayRef<
|
|
|
|
std::pair<modernize::VariableNamer::NamingStyle, StringRef>>
|
|
|
|
getEnumMapping() {
|
|
|
|
static constexpr std::pair<modernize::VariableNamer::NamingStyle, StringRef>
|
|
|
|
Mapping[] = {{modernize::VariableNamer::NS_CamelCase, "CamelCase"},
|
|
|
|
{modernize::VariableNamer::NS_CamelBack, "camelBack"},
|
|
|
|
{modernize::VariableNamer::NS_LowerCase, "lower_case"},
|
|
|
|
{modernize::VariableNamer::NS_UpperCase, "UPPER_CASE"}};
|
|
|
|
return makeArrayRef(Mapping);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-19 17:11:46 +08:00
|
|
|
namespace modernize {
|
|
|
|
|
2015-10-22 17:48:23 +08:00
|
|
|
static const char LoopNameArray[] = "forLoopArray";
|
|
|
|
static const char LoopNameIterator[] = "forLoopIterator";
|
|
|
|
static const char LoopNamePseudoArray[] = "forLoopPseudoArray";
|
|
|
|
static const char ConditionBoundName[] = "conditionBound";
|
|
|
|
static const char ConditionVarName[] = "conditionVar";
|
|
|
|
static const char IncrementVarName[] = "incrementVar";
|
|
|
|
static const char InitVarName[] = "initVar";
|
|
|
|
static const char BeginCallName[] = "beginCall";
|
|
|
|
static const char EndCallName[] = "endCall";
|
|
|
|
static const char ConditionEndVarName[] = "conditionEndVar";
|
|
|
|
static const char EndVarName[] = "endVar";
|
|
|
|
static const char DerefByValueResultName[] = "derefByValueResult";
|
|
|
|
static const char DerefByRefResultName[] = "derefByRefResult";
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
// shared matchers
|
2019-06-11 22:19:09 +08:00
|
|
|
static const TypeMatcher AnyType() { return anything(); }
|
2015-08-19 17:11:46 +08:00
|
|
|
|
2019-06-11 22:19:09 +08:00
|
|
|
static const StatementMatcher IntegerComparisonMatcher() {
|
|
|
|
return expr(ignoringParenImpCasts(
|
|
|
|
declRefExpr(to(varDecl(hasType(isInteger())).bind(ConditionVarName)))));
|
|
|
|
}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
2019-06-11 22:19:09 +08:00
|
|
|
static const DeclarationMatcher InitToZeroMatcher() {
|
|
|
|
return varDecl(
|
|
|
|
hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
|
|
|
|
.bind(InitVarName);
|
|
|
|
}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
2019-06-11 22:19:09 +08:00
|
|
|
static const StatementMatcher IncrementVarMatcher() {
|
|
|
|
return declRefExpr(to(varDecl(hasType(isInteger())).bind(IncrementVarName)));
|
|
|
|
}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// The matcher for loops over arrays.
|
2015-08-19 17:11:46 +08:00
|
|
|
///
|
|
|
|
/// In this general example, assuming 'j' and 'k' are of integral type:
|
|
|
|
/// \code
|
|
|
|
/// for (int i = 0; j < 3 + 2; ++k) { ... }
|
|
|
|
/// \endcode
|
|
|
|
/// The following string identifiers are bound to these parts of the AST:
|
|
|
|
/// ConditionVarName: 'j' (as a VarDecl)
|
|
|
|
/// ConditionBoundName: '3 + 2' (as an Expr)
|
|
|
|
/// InitVarName: 'i' (as a VarDecl)
|
|
|
|
/// IncrementVarName: 'k' (as a VarDecl)
|
|
|
|
/// LoopName: The entire for loop (as a ForStmt)
|
|
|
|
///
|
|
|
|
/// Client code will need to make sure that:
|
|
|
|
/// - The three index variables identified by the matcher are the same
|
|
|
|
/// VarDecl.
|
|
|
|
/// - The index variable is only used as an array index.
|
|
|
|
/// - All arrays indexed by the loop are the same.
|
|
|
|
StatementMatcher makeArrayLoopMatcher() {
|
|
|
|
StatementMatcher ArrayBoundMatcher =
|
|
|
|
expr(hasType(isInteger())).bind(ConditionBoundName);
|
|
|
|
|
|
|
|
return forStmt(
|
2015-08-25 23:44:00 +08:00
|
|
|
unless(isInTemplateInstantiation()),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasLoopInit(declStmt(hasSingleDecl(InitToZeroMatcher()))),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasCondition(anyOf(
|
|
|
|
binaryOperator(hasOperatorName("<"),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasLHS(IntegerComparisonMatcher()),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasRHS(ArrayBoundMatcher)),
|
|
|
|
binaryOperator(hasOperatorName(">"), hasLHS(ArrayBoundMatcher),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasRHS(IntegerComparisonMatcher())))),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasIncrement(unaryOperator(hasOperatorName("++"),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasUnaryOperand(IncrementVarMatcher()))))
|
2015-08-19 17:11:46 +08:00
|
|
|
.bind(LoopNameArray);
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// The matcher used for iterator-based for loops.
|
2015-08-19 17:11:46 +08:00
|
|
|
///
|
|
|
|
/// This matcher is more flexible than array-based loops. It will match
|
|
|
|
/// catch loops of the following textual forms (regardless of whether the
|
|
|
|
/// iterator type is actually a pointer type or a class type):
|
|
|
|
///
|
|
|
|
/// Assuming f, g, and h are of type containerType::iterator,
|
|
|
|
/// \code
|
|
|
|
/// for (containerType::iterator it = container.begin(),
|
|
|
|
/// e = createIterator(); f != g; ++h) { ... }
|
|
|
|
/// for (containerType::iterator it = container.begin();
|
|
|
|
/// f != anotherContainer.end(); ++h) { ... }
|
|
|
|
/// \endcode
|
|
|
|
/// The following string identifiers are bound to the parts of the AST:
|
|
|
|
/// InitVarName: 'it' (as a VarDecl)
|
|
|
|
/// ConditionVarName: 'f' (as a VarDecl)
|
|
|
|
/// LoopName: The entire for loop (as a ForStmt)
|
|
|
|
/// In the first example only:
|
|
|
|
/// EndVarName: 'e' (as a VarDecl)
|
|
|
|
/// ConditionEndVarName: 'g' (as a VarDecl)
|
|
|
|
/// In the second example only:
|
|
|
|
/// EndCallName: 'container.end()' (as a CXXMemberCallExpr)
|
|
|
|
///
|
|
|
|
/// Client code will need to make sure that:
|
|
|
|
/// - The iterator variables 'it', 'f', and 'h' are the same.
|
|
|
|
/// - The two containers on which 'begin' and 'end' are called are the same.
|
|
|
|
/// - If the end iterator variable 'g' is defined, it is the same as 'f'.
|
|
|
|
StatementMatcher makeIteratorLoopMatcher() {
|
|
|
|
StatementMatcher BeginCallMatcher =
|
2020-07-07 21:30:52 +08:00
|
|
|
cxxMemberCallExpr(argumentCountIs(0),
|
|
|
|
callee(cxxMethodDecl(hasAnyName("begin", "cbegin"))))
|
2015-08-19 17:11:46 +08:00
|
|
|
.bind(BeginCallName);
|
|
|
|
|
|
|
|
DeclarationMatcher InitDeclMatcher =
|
|
|
|
varDecl(hasInitializer(anyOf(ignoringParenImpCasts(BeginCallMatcher),
|
|
|
|
materializeTemporaryExpr(
|
|
|
|
ignoringParenImpCasts(BeginCallMatcher)),
|
|
|
|
hasDescendant(BeginCallMatcher))))
|
|
|
|
.bind(InitVarName);
|
|
|
|
|
|
|
|
DeclarationMatcher EndDeclMatcher =
|
|
|
|
varDecl(hasInitializer(anything())).bind(EndVarName);
|
|
|
|
|
2015-09-17 21:31:25 +08:00
|
|
|
StatementMatcher EndCallMatcher = cxxMemberCallExpr(
|
2020-07-07 21:30:52 +08:00
|
|
|
argumentCountIs(0), callee(cxxMethodDecl(hasAnyName("end", "cend"))));
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
StatementMatcher IteratorBoundMatcher =
|
|
|
|
expr(anyOf(ignoringParenImpCasts(
|
|
|
|
declRefExpr(to(varDecl().bind(ConditionEndVarName)))),
|
|
|
|
ignoringParenImpCasts(expr(EndCallMatcher).bind(EndCallName)),
|
|
|
|
materializeTemporaryExpr(ignoringParenImpCasts(
|
|
|
|
expr(EndCallMatcher).bind(EndCallName)))));
|
|
|
|
|
|
|
|
StatementMatcher IteratorComparisonMatcher = expr(
|
|
|
|
ignoringParenImpCasts(declRefExpr(to(varDecl().bind(ConditionVarName)))));
|
|
|
|
|
2016-06-24 17:39:28 +08:00
|
|
|
auto OverloadedNEQMatcher = ignoringImplicit(
|
2015-09-17 21:31:25 +08:00
|
|
|
cxxOperatorCallExpr(hasOverloadedOperatorName("!="), argumentCountIs(2),
|
|
|
|
hasArgument(0, IteratorComparisonMatcher),
|
2016-06-22 04:11:20 +08:00
|
|
|
hasArgument(1, IteratorBoundMatcher)));
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
// This matcher tests that a declaration is a CXXRecordDecl that has an
|
|
|
|
// overloaded operator*(). If the operator*() returns by value instead of by
|
|
|
|
// reference then the return type is tagged with DerefByValueResultName.
|
|
|
|
internal::Matcher<VarDecl> TestDerefReturnsByValue =
|
2017-08-02 21:13:11 +08:00
|
|
|
hasType(hasUnqualifiedDesugaredType(
|
2018-11-25 10:41:01 +08:00
|
|
|
recordType(hasDeclaration(cxxRecordDecl(hasMethod(cxxMethodDecl(
|
2017-08-02 21:13:11 +08:00
|
|
|
hasOverloadedOperatorName("*"),
|
|
|
|
anyOf(
|
|
|
|
// Tag the return type if it's by value.
|
|
|
|
returns(qualType(unless(hasCanonicalType(referenceType())))
|
|
|
|
.bind(DerefByValueResultName)),
|
|
|
|
returns(
|
|
|
|
// Skip loops where the iterator's operator* returns an
|
|
|
|
// rvalue reference. This is just weird.
|
|
|
|
qualType(unless(hasCanonicalType(rValueReferenceType())))
|
|
|
|
.bind(DerefByRefResultName))))))))));
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
return forStmt(
|
2015-08-25 23:44:00 +08:00
|
|
|
unless(isInTemplateInstantiation()),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasLoopInit(anyOf(declStmt(declCountIs(2),
|
|
|
|
containsDeclaration(0, InitDeclMatcher),
|
|
|
|
containsDeclaration(1, EndDeclMatcher)),
|
|
|
|
declStmt(hasSingleDecl(InitDeclMatcher)))),
|
|
|
|
hasCondition(
|
|
|
|
anyOf(binaryOperator(hasOperatorName("!="),
|
|
|
|
hasLHS(IteratorComparisonMatcher),
|
|
|
|
hasRHS(IteratorBoundMatcher)),
|
|
|
|
binaryOperator(hasOperatorName("!="),
|
|
|
|
hasLHS(IteratorBoundMatcher),
|
|
|
|
hasRHS(IteratorComparisonMatcher)),
|
|
|
|
OverloadedNEQMatcher)),
|
|
|
|
hasIncrement(anyOf(
|
|
|
|
unaryOperator(hasOperatorName("++"),
|
|
|
|
hasUnaryOperand(declRefExpr(
|
2019-06-11 22:19:09 +08:00
|
|
|
to(varDecl(hasType(pointsTo(AnyType())))
|
2015-08-19 17:11:46 +08:00
|
|
|
.bind(IncrementVarName))))),
|
2015-09-17 21:31:25 +08:00
|
|
|
cxxOperatorCallExpr(
|
2015-08-19 17:11:46 +08:00
|
|
|
hasOverloadedOperatorName("++"),
|
|
|
|
hasArgument(
|
|
|
|
0, declRefExpr(to(varDecl(TestDerefReturnsByValue)
|
|
|
|
.bind(IncrementVarName))))))))
|
|
|
|
.bind(LoopNameIterator);
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// The matcher used for array-like containers (pseudoarrays).
|
2015-08-19 17:11:46 +08:00
|
|
|
///
|
|
|
|
/// This matcher is more flexible than array-based loops. It will match
|
|
|
|
/// loops of the following textual forms (regardless of whether the
|
|
|
|
/// iterator type is actually a pointer type or a class type):
|
|
|
|
///
|
|
|
|
/// Assuming f, g, and h are of type containerType::iterator,
|
|
|
|
/// \code
|
|
|
|
/// for (int i = 0, j = container.size(); f < g; ++h) { ... }
|
|
|
|
/// for (int i = 0; f < container.size(); ++h) { ... }
|
|
|
|
/// \endcode
|
|
|
|
/// The following string identifiers are bound to the parts of the AST:
|
|
|
|
/// InitVarName: 'i' (as a VarDecl)
|
|
|
|
/// ConditionVarName: 'f' (as a VarDecl)
|
|
|
|
/// LoopName: The entire for loop (as a ForStmt)
|
|
|
|
/// In the first example only:
|
|
|
|
/// EndVarName: 'j' (as a VarDecl)
|
|
|
|
/// ConditionEndVarName: 'g' (as a VarDecl)
|
|
|
|
/// In the second example only:
|
|
|
|
/// EndCallName: 'container.size()' (as a CXXMemberCallExpr)
|
|
|
|
///
|
|
|
|
/// Client code will need to make sure that:
|
|
|
|
/// - The index variables 'i', 'f', and 'h' are the same.
|
|
|
|
/// - The containers on which 'size()' is called is the container indexed.
|
|
|
|
/// - The index variable is only used in overloaded operator[] or
|
|
|
|
/// container.at().
|
|
|
|
/// - If the end iterator variable 'g' is defined, it is the same as 'j'.
|
|
|
|
/// - The container's iterators would not be invalidated during the loop.
|
|
|
|
StatementMatcher makePseudoArrayLoopMatcher() {
|
|
|
|
// Test that the incoming type has a record declaration that has methods
|
|
|
|
// called 'begin' and 'end'. If the incoming type is const, then make sure
|
|
|
|
// these methods are also marked const.
|
|
|
|
//
|
|
|
|
// FIXME: To be completely thorough this matcher should also ensure the
|
|
|
|
// return type of begin/end is an iterator that dereferences to the same as
|
|
|
|
// what operator[] or at() returns. Such a test isn't likely to fail except
|
|
|
|
// for pathological cases.
|
|
|
|
//
|
|
|
|
// FIXME: Also, a record doesn't necessarily need begin() and end(). Free
|
|
|
|
// functions called begin() and end() taking the container as an argument
|
|
|
|
// are also allowed.
|
2015-10-30 17:37:57 +08:00
|
|
|
TypeMatcher RecordWithBeginEnd = qualType(anyOf(
|
|
|
|
qualType(
|
2017-08-02 21:13:11 +08:00
|
|
|
isConstQualified(),
|
|
|
|
hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(
|
|
|
|
hasMethod(cxxMethodDecl(hasName("begin"), isConst())),
|
|
|
|
hasMethod(cxxMethodDecl(hasName("end"),
|
|
|
|
isConst())))) // hasDeclaration
|
|
|
|
))), // qualType
|
|
|
|
qualType(unless(isConstQualified()),
|
|
|
|
hasUnqualifiedDesugaredType(recordType(hasDeclaration(
|
|
|
|
cxxRecordDecl(hasMethod(hasName("begin")),
|
|
|
|
hasMethod(hasName("end"))))))) // qualType
|
2015-10-30 17:37:57 +08:00
|
|
|
));
|
2015-08-19 17:11:46 +08:00
|
|
|
|
2015-09-17 21:31:25 +08:00
|
|
|
StatementMatcher SizeCallMatcher = cxxMemberCallExpr(
|
2020-07-07 21:30:52 +08:00
|
|
|
argumentCountIs(0), callee(cxxMethodDecl(hasAnyName("size", "length"))),
|
2015-08-19 17:11:46 +08:00
|
|
|
on(anyOf(hasType(pointsTo(RecordWithBeginEnd)),
|
|
|
|
hasType(RecordWithBeginEnd))));
|
|
|
|
|
|
|
|
StatementMatcher EndInitMatcher =
|
|
|
|
expr(anyOf(ignoringParenImpCasts(expr(SizeCallMatcher).bind(EndCallName)),
|
|
|
|
explicitCastExpr(hasSourceExpression(ignoringParenImpCasts(
|
|
|
|
expr(SizeCallMatcher).bind(EndCallName))))));
|
|
|
|
|
|
|
|
DeclarationMatcher EndDeclMatcher =
|
|
|
|
varDecl(hasInitializer(EndInitMatcher)).bind(EndVarName);
|
|
|
|
|
|
|
|
StatementMatcher IndexBoundMatcher =
|
|
|
|
expr(anyOf(ignoringParenImpCasts(declRefExpr(to(
|
|
|
|
varDecl(hasType(isInteger())).bind(ConditionEndVarName)))),
|
|
|
|
EndInitMatcher));
|
|
|
|
|
|
|
|
return forStmt(
|
2015-08-25 23:44:00 +08:00
|
|
|
unless(isInTemplateInstantiation()),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasLoopInit(
|
|
|
|
anyOf(declStmt(declCountIs(2),
|
2019-06-11 22:19:09 +08:00
|
|
|
containsDeclaration(0, InitToZeroMatcher()),
|
2015-08-19 17:11:46 +08:00
|
|
|
containsDeclaration(1, EndDeclMatcher)),
|
2019-06-11 22:19:09 +08:00
|
|
|
declStmt(hasSingleDecl(InitToZeroMatcher())))),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasCondition(anyOf(
|
|
|
|
binaryOperator(hasOperatorName("<"),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasLHS(IntegerComparisonMatcher()),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasRHS(IndexBoundMatcher)),
|
|
|
|
binaryOperator(hasOperatorName(">"), hasLHS(IndexBoundMatcher),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasRHS(IntegerComparisonMatcher())))),
|
2015-08-19 17:11:46 +08:00
|
|
|
hasIncrement(unaryOperator(hasOperatorName("++"),
|
2019-06-11 22:19:09 +08:00
|
|
|
hasUnaryOperand(IncrementVarMatcher()))))
|
2015-08-19 17:11:46 +08:00
|
|
|
.bind(LoopNamePseudoArray);
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Determine whether Init appears to be an initializing an iterator.
|
2015-08-19 17:11:46 +08:00
|
|
|
///
|
|
|
|
/// If it is, returns the object whose begin() or end() method is called, and
|
|
|
|
/// the output parameter isArrow is set to indicate whether the initialization
|
|
|
|
/// is called via . or ->.
|
|
|
|
static const Expr *getContainerFromBeginEndCall(const Expr *Init, bool IsBegin,
|
|
|
|
bool *IsArrow) {
|
|
|
|
// FIXME: Maybe allow declaration/initialization outside of the for loop.
|
|
|
|
const auto *TheCall =
|
|
|
|
dyn_cast_or_null<CXXMemberCallExpr>(digThroughConstructors(Init));
|
|
|
|
if (!TheCall || TheCall->getNumArgs() != 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto *Member = dyn_cast<MemberExpr>(TheCall->getCallee());
|
|
|
|
if (!Member)
|
|
|
|
return nullptr;
|
|
|
|
StringRef Name = Member->getMemberDecl()->getName();
|
|
|
|
StringRef TargetName = IsBegin ? "begin" : "end";
|
2015-10-01 16:57:11 +08:00
|
|
|
StringRef ConstTargetName = IsBegin ? "cbegin" : "cend";
|
|
|
|
if (Name != TargetName && Name != ConstTargetName)
|
2015-08-19 17:11:46 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Expr *SourceExpr = Member->getBase();
|
|
|
|
if (!SourceExpr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
*IsArrow = Member->isArrow();
|
|
|
|
return SourceExpr;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Determines the container whose begin() and end() functions are called
|
2015-08-19 17:11:46 +08:00
|
|
|
/// for an iterator-based loop.
|
|
|
|
///
|
|
|
|
/// BeginExpr must be a member call to a function named "begin()", and EndExpr
|
|
|
|
/// must be a member.
|
|
|
|
static const Expr *findContainer(ASTContext *Context, const Expr *BeginExpr,
|
|
|
|
const Expr *EndExpr,
|
|
|
|
bool *ContainerNeedsDereference) {
|
|
|
|
// Now that we know the loop variable and test expression, make sure they are
|
|
|
|
// valid.
|
|
|
|
bool BeginIsArrow = false;
|
|
|
|
bool EndIsArrow = false;
|
|
|
|
const Expr *BeginContainerExpr =
|
|
|
|
getContainerFromBeginEndCall(BeginExpr, /*IsBegin=*/true, &BeginIsArrow);
|
|
|
|
if (!BeginContainerExpr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Expr *EndContainerExpr =
|
|
|
|
getContainerFromBeginEndCall(EndExpr, /*IsBegin=*/false, &EndIsArrow);
|
|
|
|
// Disallow loops that try evil things like this (note the dot and arrow):
|
|
|
|
// for (IteratorType It = Obj.begin(), E = Obj->end(); It != E; ++It) { }
|
|
|
|
if (!EndContainerExpr || BeginIsArrow != EndIsArrow ||
|
|
|
|
!areSameExpr(Context, EndContainerExpr, BeginContainerExpr))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
*ContainerNeedsDereference = BeginIsArrow;
|
|
|
|
return BeginContainerExpr;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Obtain the original source code text from a SourceRange.
|
2015-08-19 17:11:46 +08:00
|
|
|
static StringRef getStringFromRange(SourceManager &SourceMgr,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
SourceRange Range) {
|
|
|
|
if (SourceMgr.getFileID(Range.getBegin()) !=
|
2015-08-20 21:18:23 +08:00
|
|
|
SourceMgr.getFileID(Range.getEnd())) {
|
|
|
|
return StringRef(); // Empty string.
|
|
|
|
}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
|
|
|
|
LangOpts);
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// If the given expression is actually a DeclRefExpr or a MemberExpr,
|
2015-11-06 17:59:14 +08:00
|
|
|
/// find and return the underlying ValueDecl; otherwise, return NULL.
|
2015-11-04 00:38:31 +08:00
|
|
|
static const ValueDecl *getReferencedVariable(const Expr *E) {
|
2015-08-19 17:11:46 +08:00
|
|
|
if (const DeclRefExpr *DRE = getDeclRef(E))
|
|
|
|
return dyn_cast<VarDecl>(DRE->getDecl());
|
2015-11-06 17:59:14 +08:00
|
|
|
if (const auto *Mem = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts()))
|
2015-11-04 00:38:31 +08:00
|
|
|
return dyn_cast<FieldDecl>(Mem->getMemberDecl());
|
2015-08-19 17:11:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Returns true when the given expression is a member expression
|
2015-08-19 17:11:46 +08:00
|
|
|
/// whose base is `this` (implicitly or not).
|
|
|
|
static bool isDirectMemberExpr(const Expr *E) {
|
|
|
|
if (const auto *Member = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts()))
|
|
|
|
return isa<CXXThisExpr>(Member->getBase()->IgnoreParenImpCasts());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Given an expression that represents an usage of an element from the
|
2015-11-03 01:02:52 +08:00
|
|
|
/// containter that we are iterating over, returns false when it can be
|
|
|
|
/// guaranteed this element cannot be modified as a result of this usage.
|
|
|
|
static bool canBeModified(ASTContext *Context, const Expr *E) {
|
2015-11-04 00:31:36 +08:00
|
|
|
if (E->getType().isConstQualified())
|
|
|
|
return false;
|
2015-11-03 01:02:52 +08:00
|
|
|
auto Parents = Context->getParents(*E);
|
|
|
|
if (Parents.size() != 1)
|
|
|
|
return true;
|
|
|
|
if (const auto *Cast = Parents[0].get<ImplicitCastExpr>()) {
|
|
|
|
if ((Cast->getCastKind() == CK_NoOp &&
|
|
|
|
Cast->getType() == E->getType().withConst()) ||
|
|
|
|
(Cast->getCastKind() == CK_LValueToRValue &&
|
|
|
|
!Cast->getType().isNull() && Cast->getType()->isFundamentalType()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// FIXME: Make this function more generic.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Returns true when it can be guaranteed that the elements of the
|
2015-09-01 23:05:15 +08:00
|
|
|
/// container are not being modified.
|
2015-11-03 01:02:52 +08:00
|
|
|
static bool usagesAreConst(ASTContext *Context, const UsageResult &Usages) {
|
|
|
|
for (const Usage &U : Usages) {
|
|
|
|
// Lambda captures are just redeclarations (VarDecl) of the same variable,
|
|
|
|
// not expressions. If we want to know if a variable that is captured by
|
|
|
|
// reference can be modified in an usage inside the lambda's body, we need
|
|
|
|
// to find the expression corresponding to that particular usage, later in
|
|
|
|
// this loop.
|
|
|
|
if (U.Kind != Usage::UK_CaptureByCopy && U.Kind != Usage::UK_CaptureByRef &&
|
|
|
|
canBeModified(Context, U.Expression))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-09-01 23:05:15 +08:00
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Returns true if the elements of the container are never accessed
|
2015-09-01 23:05:15 +08:00
|
|
|
/// by reference.
|
|
|
|
static bool usagesReturnRValues(const UsageResult &Usages) {
|
|
|
|
for (const auto &U : Usages) {
|
2015-09-08 17:01:21 +08:00
|
|
|
if (U.Expression && !U.Expression->isRValue())
|
2015-09-01 23:05:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Returns true if the container is const-qualified.
|
2015-09-11 18:02:07 +08:00
|
|
|
static bool containerIsConst(const Expr *ContainerExpr, bool Dereference) {
|
|
|
|
if (const auto *VDec = getReferencedVariable(ContainerExpr)) {
|
|
|
|
QualType CType = VDec->getType();
|
|
|
|
if (Dereference) {
|
|
|
|
if (!CType->isPointerType())
|
|
|
|
return false;
|
|
|
|
CType = CType->getPointeeType();
|
|
|
|
}
|
2015-09-24 02:40:47 +08:00
|
|
|
// If VDec is a reference to a container, Dereference is false,
|
|
|
|
// but we still need to check the const-ness of the underlying container
|
|
|
|
// type.
|
2015-09-24 21:26:28 +08:00
|
|
|
CType = CType.getNonReferenceType();
|
2015-09-11 18:02:07 +08:00
|
|
|
return CType.isConstQualified();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
LoopConvertCheck::RangeDescriptor::RangeDescriptor()
|
|
|
|
: ContainerNeedsDereference(false), DerefByConstRef(false),
|
2015-10-22 21:23:46 +08:00
|
|
|
DerefByValue(false) {}
|
2015-09-21 17:32:59 +08:00
|
|
|
|
2015-08-19 17:11:46 +08:00
|
|
|
LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo),
|
2020-06-22 02:01:09 +08:00
|
|
|
MaxCopySize(Options.get("MaxCopySize", 16ULL)),
|
2020-07-11 17:10:59 +08:00
|
|
|
MinConfidence(Options.get("MinConfidence", Confidence::CL_Reasonable)),
|
|
|
|
NamingStyle(Options.get("NamingStyle", VariableNamer::NS_CamelCase)) {}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
2015-10-30 17:37:57 +08:00
|
|
|
Options.store(Opts, "MaxCopySize", std::to_string(MaxCopySize));
|
2020-07-11 17:10:59 +08:00
|
|
|
Options.store(Opts, "MinConfidence", MinConfidence);
|
|
|
|
Options.store(Opts, "NamingStyle", NamingStyle);
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
void LoopConvertCheck::registerMatchers(MatchFinder *Finder) {
|
2019-11-12 23:15:56 +08:00
|
|
|
Finder->addMatcher(traverse(ast_type_traits::TK_AsIs, makeArrayLoopMatcher()),
|
|
|
|
this);
|
|
|
|
Finder->addMatcher(
|
|
|
|
traverse(ast_type_traits::TK_AsIs, makeIteratorLoopMatcher()), this);
|
|
|
|
Finder->addMatcher(
|
|
|
|
traverse(ast_type_traits::TK_AsIs, makePseudoArrayLoopMatcher()), this);
|
2015-09-21 17:32:59 +08:00
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Given the range of a single declaration, such as:
|
2015-10-01 21:08:21 +08:00
|
|
|
/// \code
|
|
|
|
/// unsigned &ThisIsADeclarationThatCanSpanSeveralLinesOfCode =
|
|
|
|
/// InitializationValues[I];
|
|
|
|
/// next_instruction;
|
|
|
|
/// \endcode
|
|
|
|
/// Finds the range that has to be erased to remove this declaration without
|
|
|
|
/// leaving empty lines, by extending the range until the beginning of the
|
|
|
|
/// next instruction.
|
|
|
|
///
|
|
|
|
/// We need to delete a potential newline after the deleted alias, as
|
|
|
|
/// clang-format will leave empty lines untouched. For all other formatting we
|
|
|
|
/// rely on clang-format to fix it.
|
|
|
|
void LoopConvertCheck::getAliasRange(SourceManager &SM, SourceRange &Range) {
|
|
|
|
bool Invalid = false;
|
|
|
|
const char *TextAfter =
|
|
|
|
SM.getCharacterData(Range.getEnd().getLocWithOffset(1), &Invalid);
|
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
unsigned Offset = std::strspn(TextAfter, " \t\r\n");
|
|
|
|
Range =
|
|
|
|
SourceRange(Range.getBegin(), Range.getEnd().getLocWithOffset(Offset));
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Computes the changes needed to convert a given for loop, and
|
2015-09-21 17:32:59 +08:00
|
|
|
/// applies them.
|
2015-08-19 17:11:46 +08:00
|
|
|
void LoopConvertCheck::doConversion(
|
2015-11-04 00:38:31 +08:00
|
|
|
ASTContext *Context, const VarDecl *IndexVar,
|
|
|
|
const ValueDecl *MaybeContainer, const UsageResult &Usages,
|
|
|
|
const DeclStmt *AliasDecl, bool AliasUseRequired, bool AliasFromForInit,
|
|
|
|
const ForStmt *Loop, RangeDescriptor Descriptor) {
|
2015-08-19 17:11:46 +08:00
|
|
|
std::string VarName;
|
|
|
|
bool VarNameFromAlias = (Usages.size() == 1) && AliasDecl;
|
|
|
|
bool AliasVarIsRef = false;
|
2015-10-05 19:15:39 +08:00
|
|
|
bool CanCopy = true;
|
2020-04-23 22:20:56 +08:00
|
|
|
std::vector<FixItHint> FixIts;
|
2015-08-19 17:11:46 +08:00
|
|
|
if (VarNameFromAlias) {
|
|
|
|
const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl());
|
|
|
|
VarName = AliasVar->getName().str();
|
2016-07-20 20:32:06 +08:00
|
|
|
|
|
|
|
// Use the type of the alias if it's not the same
|
|
|
|
QualType AliasVarType = AliasVar->getType();
|
|
|
|
assert(!AliasVarType.isNull() && "Type in VarDecl is null");
|
|
|
|
if (AliasVarType->isReferenceType()) {
|
|
|
|
AliasVarType = AliasVarType.getNonReferenceType();
|
|
|
|
AliasVarIsRef = true;
|
|
|
|
}
|
|
|
|
if (Descriptor.ElemType.isNull() ||
|
|
|
|
!Context->hasSameUnqualifiedType(AliasVarType, Descriptor.ElemType))
|
|
|
|
Descriptor.ElemType = AliasVarType;
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
// We keep along the entire DeclStmt to keep the correct range here.
|
2015-10-01 21:08:21 +08:00
|
|
|
SourceRange ReplaceRange = AliasDecl->getSourceRange();
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
std::string ReplacementText;
|
|
|
|
if (AliasUseRequired) {
|
|
|
|
ReplacementText = VarName;
|
|
|
|
} else if (AliasFromForInit) {
|
|
|
|
// FIXME: Clang includes the location of the ';' but only for DeclStmt's
|
|
|
|
// in a for loop's init clause. Need to put this ';' back while removing
|
|
|
|
// the declaration of the alias variable. This is probably a bug.
|
|
|
|
ReplacementText = ";";
|
2015-10-01 21:08:21 +08:00
|
|
|
} else {
|
|
|
|
// Avoid leaving empty lines or trailing whitespaces.
|
|
|
|
getAliasRange(Context->getSourceManager(), ReplaceRange);
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
2020-04-23 22:20:56 +08:00
|
|
|
FixIts.push_back(FixItHint::CreateReplacement(
|
|
|
|
CharSourceRange::getTokenRange(ReplaceRange), ReplacementText));
|
2015-08-19 17:11:46 +08:00
|
|
|
// No further replacements are made to the loop, since the iterator or index
|
|
|
|
// was used exactly once - in the initialization of AliasVar.
|
|
|
|
} else {
|
|
|
|
VariableNamer Namer(&TUInfo->getGeneratedDecls(),
|
|
|
|
&TUInfo->getParentFinder().getStmtToParentStmtMap(),
|
2015-09-25 01:02:19 +08:00
|
|
|
Loop, IndexVar, MaybeContainer, Context, NamingStyle);
|
2015-08-19 17:11:46 +08:00
|
|
|
VarName = Namer.createIndexName();
|
|
|
|
// First, replace all usages of the array subscript expression with our new
|
|
|
|
// variable.
|
2015-09-11 18:02:07 +08:00
|
|
|
for (const auto &Usage : Usages) {
|
|
|
|
std::string ReplaceText;
|
2015-09-24 23:29:46 +08:00
|
|
|
SourceRange Range = Usage.Range;
|
2015-09-11 18:02:07 +08:00
|
|
|
if (Usage.Expression) {
|
|
|
|
// If this is an access to a member through the arrow operator, after
|
|
|
|
// the replacement it must be accessed through the '.' operator.
|
|
|
|
ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow ? VarName + "."
|
|
|
|
: VarName;
|
2015-09-24 23:29:46 +08:00
|
|
|
auto Parents = Context->getParents(*Usage.Expression);
|
|
|
|
if (Parents.size() == 1) {
|
|
|
|
if (const auto *Paren = Parents[0].get<ParenExpr>()) {
|
|
|
|
// Usage.Expression will be replaced with the new index variable,
|
|
|
|
// and parenthesis around a simple DeclRefExpr can always be
|
|
|
|
// removed.
|
|
|
|
Range = Paren->getSourceRange();
|
2015-10-05 19:15:39 +08:00
|
|
|
} else if (const auto *UOP = Parents[0].get<UnaryOperator>()) {
|
|
|
|
// If we are taking the address of the loop variable, then we must
|
|
|
|
// not use a copy, as it would mean taking the address of the loop's
|
|
|
|
// local index instead.
|
|
|
|
// FIXME: This won't catch cases where the address is taken outside
|
|
|
|
// of the loop's body (for instance, in a function that got the
|
|
|
|
// loop's index as a const reference parameter), or where we take
|
|
|
|
// the address of a member (like "&Arr[i].A.B.C").
|
|
|
|
if (UOP->getOpcode() == UO_AddrOf)
|
|
|
|
CanCopy = false;
|
2015-09-24 23:29:46 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-11 18:02:07 +08:00
|
|
|
} else {
|
|
|
|
// The Usage expression is only null in case of lambda captures (which
|
|
|
|
// are VarDecl). If the index is captured by value, add '&' to capture
|
|
|
|
// by reference instead.
|
|
|
|
ReplaceText =
|
|
|
|
Usage.Kind == Usage::UK_CaptureByCopy ? "&" + VarName : VarName;
|
|
|
|
}
|
2015-09-21 17:32:59 +08:00
|
|
|
TUInfo->getReplacedVars().insert(std::make_pair(Loop, IndexVar));
|
2020-04-23 22:20:56 +08:00
|
|
|
FixIts.push_back(FixItHint::CreateReplacement(
|
|
|
|
CharSourceRange::getTokenRange(Range), ReplaceText));
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, we need to construct the new range expression.
|
2015-09-21 17:32:59 +08:00
|
|
|
SourceRange ParenRange(Loop->getLParenLoc(), Loop->getRParenLoc());
|
2015-08-19 17:11:46 +08:00
|
|
|
|
2015-10-22 21:23:46 +08:00
|
|
|
QualType Type = Context->getAutoDeductType();
|
|
|
|
if (!Descriptor.ElemType.isNull() && Descriptor.ElemType->isFundamentalType())
|
|
|
|
Type = Descriptor.ElemType.getUnqualifiedType();
|
2020-05-26 14:06:59 +08:00
|
|
|
Type = Type.getDesugaredType(*Context);
|
2015-08-19 17:11:46 +08:00
|
|
|
|
|
|
|
// If the new variable name is from the aliased variable, then the reference
|
|
|
|
// type for the new variable should only be used if the aliased variable was
|
|
|
|
// declared as a reference.
|
2015-10-30 17:37:57 +08:00
|
|
|
bool IsCheapToCopy =
|
2015-10-22 21:23:46 +08:00
|
|
|
!Descriptor.ElemType.isNull() &&
|
2015-10-30 17:37:57 +08:00
|
|
|
Descriptor.ElemType.isTriviallyCopyableType(*Context) &&
|
|
|
|
// TypeInfo::Width is in bits.
|
|
|
|
Context->getTypeInfo(Descriptor.ElemType).Width <= 8 * MaxCopySize;
|
2015-11-03 01:02:52 +08:00
|
|
|
bool UseCopy = CanCopy && ((VarNameFromAlias && !AliasVarIsRef) ||
|
|
|
|
(Descriptor.DerefByConstRef && IsCheapToCopy));
|
2015-09-24 06:28:14 +08:00
|
|
|
|
|
|
|
if (!UseCopy) {
|
2015-09-21 17:32:59 +08:00
|
|
|
if (Descriptor.DerefByConstRef) {
|
2015-10-22 21:23:46 +08:00
|
|
|
Type = Context->getLValueReferenceType(Context->getConstType(Type));
|
2015-09-21 17:32:59 +08:00
|
|
|
} else if (Descriptor.DerefByValue) {
|
2015-10-30 17:37:57 +08:00
|
|
|
if (!IsCheapToCopy)
|
2015-10-22 21:23:46 +08:00
|
|
|
Type = Context->getRValueReferenceType(Type);
|
2015-08-19 17:11:46 +08:00
|
|
|
} else {
|
2015-10-22 21:23:46 +08:00
|
|
|
Type = Context->getLValueReferenceType(Type);
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 17:01:21 +08:00
|
|
|
StringRef MaybeDereference = Descriptor.ContainerNeedsDereference ? "*" : "";
|
2015-10-22 21:23:46 +08:00
|
|
|
std::string TypeString = Type.getAsString(getLangOpts());
|
2015-08-20 00:54:51 +08:00
|
|
|
std::string Range = ("(" + TypeString + " " + VarName + " : " +
|
2015-09-21 17:32:59 +08:00
|
|
|
MaybeDereference + Descriptor.ContainerString + ")")
|
2015-09-01 23:05:15 +08:00
|
|
|
.str();
|
2020-04-23 22:20:56 +08:00
|
|
|
FixIts.push_back(FixItHint::CreateReplacement(
|
|
|
|
CharSourceRange::getTokenRange(ParenRange), Range));
|
|
|
|
diag(Loop->getForLoc(), "use range-based for loop instead") << FixIts;
|
2015-09-21 17:32:59 +08:00
|
|
|
TUInfo->getGeneratedDecls().insert(make_pair(Loop, VarName));
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Returns a string which refers to the container iterated over.
|
2015-09-21 17:32:59 +08:00
|
|
|
StringRef LoopConvertCheck::getContainerString(ASTContext *Context,
|
|
|
|
const ForStmt *Loop,
|
|
|
|
const Expr *ContainerExpr) {
|
2015-08-19 17:11:46 +08:00
|
|
|
StringRef ContainerString;
|
2020-03-16 23:37:19 +08:00
|
|
|
ContainerExpr = ContainerExpr->IgnoreParenImpCasts();
|
|
|
|
if (isa<CXXThisExpr>(ContainerExpr)) {
|
2015-08-19 17:11:46 +08:00
|
|
|
ContainerString = "this";
|
|
|
|
} else {
|
2020-03-16 23:37:19 +08:00
|
|
|
// For CXXOperatorCallExpr (e.g. vector_ptr->size()), its first argument is
|
|
|
|
// the class object (vector_ptr) we are targeting.
|
|
|
|
if (const auto* E = dyn_cast<CXXOperatorCallExpr>(ContainerExpr))
|
|
|
|
ContainerExpr = E->getArg(0);
|
2015-08-19 17:11:46 +08:00
|
|
|
ContainerString =
|
|
|
|
getStringFromRange(Context->getSourceManager(), Context->getLangOpts(),
|
|
|
|
ContainerExpr->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ContainerString;
|
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Determines what kind of 'auto' must be used after converting a for
|
2015-09-21 17:32:59 +08:00
|
|
|
/// loop that iterates over an array or pseudoarray.
|
|
|
|
void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context,
|
|
|
|
const BoundNodes &Nodes,
|
|
|
|
const Expr *ContainerExpr,
|
|
|
|
const UsageResult &Usages,
|
|
|
|
RangeDescriptor &Descriptor) {
|
|
|
|
// On arrays and pseudoarrays, we must figure out the qualifiers from the
|
|
|
|
// usages.
|
2015-11-03 01:02:52 +08:00
|
|
|
if (usagesAreConst(Context, Usages) ||
|
2015-09-24 06:28:14 +08:00
|
|
|
containerIsConst(ContainerExpr, Descriptor.ContainerNeedsDereference)) {
|
2015-09-21 17:32:59 +08:00
|
|
|
Descriptor.DerefByConstRef = true;
|
2015-09-24 06:28:14 +08:00
|
|
|
}
|
|
|
|
if (usagesReturnRValues(Usages)) {
|
|
|
|
// If the index usages (dereference, subscript, at, ...) return rvalues,
|
|
|
|
// then we should not use a reference, because we need to keep the code
|
|
|
|
// correct if it mutates the returned objects.
|
|
|
|
Descriptor.DerefByValue = true;
|
|
|
|
}
|
|
|
|
// Try to find the type of the elements on the container, to check if
|
|
|
|
// they are trivially copyable.
|
|
|
|
for (const Usage &U : Usages) {
|
|
|
|
if (!U.Expression || U.Expression->getType().isNull())
|
|
|
|
continue;
|
|
|
|
QualType Type = U.Expression->getType().getCanonicalType();
|
|
|
|
if (U.Kind == Usage::UK_MemberThroughArrow) {
|
|
|
|
if (!Type->isPointerType()) {
|
|
|
|
continue;
|
2015-09-01 23:05:15 +08:00
|
|
|
}
|
2015-09-24 06:28:14 +08:00
|
|
|
Type = Type->getPointeeType();
|
2015-09-01 23:05:15 +08:00
|
|
|
}
|
2015-10-22 21:23:46 +08:00
|
|
|
Descriptor.ElemType = Type;
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
2015-09-21 17:32:59 +08:00
|
|
|
}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Determines what kind of 'auto' must be used after converting an
|
2015-09-21 17:32:59 +08:00
|
|
|
/// iterator based for loop.
|
|
|
|
void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
|
|
|
|
const BoundNodes &Nodes,
|
|
|
|
RangeDescriptor &Descriptor) {
|
|
|
|
// The matchers for iterator loops provide bound nodes to obtain this
|
|
|
|
// information.
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *InitVar = Nodes.getNodeAs<VarDecl>(InitVarName);
|
2015-09-21 17:32:59 +08:00
|
|
|
QualType CanonicalInitVarType = InitVar->getType().getCanonicalType();
|
|
|
|
const auto *DerefByValueType =
|
|
|
|
Nodes.getNodeAs<QualType>(DerefByValueResultName);
|
|
|
|
Descriptor.DerefByValue = DerefByValueType;
|
|
|
|
|
|
|
|
if (Descriptor.DerefByValue) {
|
|
|
|
// If the dereference operator returns by value then test for the
|
|
|
|
// canonical const qualification of the init variable type.
|
|
|
|
Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified();
|
2015-10-22 21:23:46 +08:00
|
|
|
Descriptor.ElemType = *DerefByValueType;
|
2015-09-21 17:32:59 +08:00
|
|
|
} else {
|
|
|
|
if (const auto *DerefType =
|
|
|
|
Nodes.getNodeAs<QualType>(DerefByRefResultName)) {
|
|
|
|
// A node will only be bound with DerefByRefResultName if we're dealing
|
|
|
|
// with a user-defined iterator type. Test the const qualification of
|
|
|
|
// the reference type.
|
2015-09-24 08:16:38 +08:00
|
|
|
auto ValueType = DerefType->getNonReferenceType();
|
2015-09-24 06:28:14 +08:00
|
|
|
|
|
|
|
Descriptor.DerefByConstRef = ValueType.isConstQualified();
|
2015-10-22 21:23:46 +08:00
|
|
|
Descriptor.ElemType = ValueType;
|
2015-09-21 17:32:59 +08:00
|
|
|
} else {
|
|
|
|
// By nature of the matcher this case is triggered only for built-in
|
|
|
|
// iterator types (i.e. pointers).
|
|
|
|
assert(isa<PointerType>(CanonicalInitVarType) &&
|
|
|
|
"Non-class iterator type is not a pointer type");
|
|
|
|
|
|
|
|
// We test for const qualification of the pointed-at type.
|
|
|
|
Descriptor.DerefByConstRef =
|
|
|
|
CanonicalInitVarType->getPointeeType().isConstQualified();
|
2015-10-22 21:23:46 +08:00
|
|
|
Descriptor.ElemType = CanonicalInitVarType->getPointeeType();
|
2015-09-21 17:32:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 17:11:46 +08:00
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Determines the parameters needed to build the range replacement.
|
2015-09-21 17:32:59 +08:00
|
|
|
void LoopConvertCheck::determineRangeDescriptor(
|
|
|
|
ASTContext *Context, const BoundNodes &Nodes, const ForStmt *Loop,
|
|
|
|
LoopFixerKind FixerKind, const Expr *ContainerExpr,
|
|
|
|
const UsageResult &Usages, RangeDescriptor &Descriptor) {
|
2020-01-29 03:23:46 +08:00
|
|
|
Descriptor.ContainerString =
|
|
|
|
std::string(getContainerString(Context, Loop, ContainerExpr));
|
2015-09-21 17:32:59 +08:00
|
|
|
|
|
|
|
if (FixerKind == LFK_Iterator)
|
|
|
|
getIteratorLoopQualifiers(Context, Nodes, Descriptor);
|
|
|
|
else
|
|
|
|
getArrayLoopQualifiers(Context, Nodes, ContainerExpr, Usages, Descriptor);
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
Remove \brief commands from doxygen comments.
Summary:
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.
Patch produced by
for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
[This is analogous to LLVM r331272 and CFE r331834]
Subscribers: srhines, nemanjai, javed.absar, kbarton, MaskRay, jkorous, arphaman, jfb, kadircet, jsji, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66578
llvm-svn: 369643
2019-08-22 19:32:57 +08:00
|
|
|
/// Check some of the conditions that must be met for the loop to be
|
2015-09-21 17:32:59 +08:00
|
|
|
/// convertible.
|
|
|
|
bool LoopConvertCheck::isConvertible(ASTContext *Context,
|
|
|
|
const ast_matchers::BoundNodes &Nodes,
|
|
|
|
const ForStmt *Loop,
|
|
|
|
LoopFixerKind FixerKind) {
|
|
|
|
// If we already modified the range of this for loop, don't do any further
|
|
|
|
// updates on this iteration.
|
|
|
|
if (TUInfo->getReplacedVars().count(Loop))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that we have exactly one index variable and at most one end variable.
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *LoopVar = Nodes.getNodeAs<VarDecl>(IncrementVarName);
|
|
|
|
const auto *CondVar = Nodes.getNodeAs<VarDecl>(ConditionVarName);
|
|
|
|
const auto *InitVar = Nodes.getNodeAs<VarDecl>(InitVarName);
|
2015-09-21 17:32:59 +08:00
|
|
|
if (!areSameVariable(LoopVar, CondVar) || !areSameVariable(LoopVar, InitVar))
|
|
|
|
return false;
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *EndVar = Nodes.getNodeAs<VarDecl>(EndVarName);
|
|
|
|
const auto *ConditionEndVar = Nodes.getNodeAs<VarDecl>(ConditionEndVarName);
|
2015-09-21 17:32:59 +08:00
|
|
|
if (EndVar && !areSameVariable(EndVar, ConditionEndVar))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Try to put most of this logic inside a matcher.
|
|
|
|
if (FixerKind == LFK_Iterator) {
|
|
|
|
QualType InitVarType = InitVar->getType();
|
|
|
|
QualType CanonicalInitVarType = InitVarType.getCanonicalType();
|
|
|
|
|
|
|
|
const auto *BeginCall = Nodes.getNodeAs<CXXMemberCallExpr>(BeginCallName);
|
|
|
|
assert(BeginCall && "Bad Callback. No begin call expression");
|
|
|
|
QualType CanonicalBeginType =
|
|
|
|
BeginCall->getMethodDecl()->getReturnType().getCanonicalType();
|
|
|
|
if (CanonicalBeginType->isPointerType() &&
|
|
|
|
CanonicalInitVarType->isPointerType()) {
|
|
|
|
// If the initializer and the variable are both pointers check if the
|
|
|
|
// un-qualified pointee types match, otherwise we don't use auto.
|
|
|
|
if (!Context->hasSameUnqualifiedType(
|
|
|
|
CanonicalBeginType->getPointeeType(),
|
|
|
|
CanonicalInitVarType->getPointeeType()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (FixerKind == LFK_PseudoArray) {
|
|
|
|
// This call is required to obtain the container.
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(EndCallName);
|
2015-09-21 17:32:59 +08:00
|
|
|
if (!EndCall || !dyn_cast<MemberExpr>(EndCall->getCallee()))
|
|
|
|
return false;
|
2015-08-29 01:58:10 +08:00
|
|
|
}
|
2015-09-21 17:32:59 +08:00
|
|
|
return true;
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const BoundNodes &Nodes = Result.Nodes;
|
|
|
|
Confidence ConfidenceLevel(Confidence::CL_Safe);
|
|
|
|
ASTContext *Context = Result.Context;
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
const ForStmt *Loop;
|
2015-08-19 17:11:46 +08:00
|
|
|
LoopFixerKind FixerKind;
|
2015-09-21 17:32:59 +08:00
|
|
|
RangeDescriptor Descriptor;
|
2015-08-19 17:11:46 +08:00
|
|
|
|
2016-12-14 00:19:19 +08:00
|
|
|
if ((Loop = Nodes.getNodeAs<ForStmt>(LoopNameArray))) {
|
2015-08-19 17:11:46 +08:00
|
|
|
FixerKind = LFK_Array;
|
2016-12-14 00:19:19 +08:00
|
|
|
} else if ((Loop = Nodes.getNodeAs<ForStmt>(LoopNameIterator))) {
|
2015-08-19 17:11:46 +08:00
|
|
|
FixerKind = LFK_Iterator;
|
|
|
|
} else {
|
2016-12-14 00:19:19 +08:00
|
|
|
Loop = Nodes.getNodeAs<ForStmt>(LoopNamePseudoArray);
|
2015-09-21 17:32:59 +08:00
|
|
|
assert(Loop && "Bad Callback. No for statement");
|
2015-08-19 17:11:46 +08:00
|
|
|
FixerKind = LFK_PseudoArray;
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
if (!isConvertible(Context, Nodes, Loop, FixerKind))
|
2015-08-19 17:11:46 +08:00
|
|
|
return;
|
|
|
|
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *LoopVar = Nodes.getNodeAs<VarDecl>(IncrementVarName);
|
|
|
|
const auto *EndVar = Nodes.getNodeAs<VarDecl>(EndVarName);
|
2015-09-11 18:02:07 +08:00
|
|
|
|
2015-08-19 17:11:46 +08:00
|
|
|
// If the loop calls end()/size() after each iteration, lower our confidence
|
|
|
|
// level.
|
|
|
|
if (FixerKind != LFK_Array && !EndVar)
|
|
|
|
ConfidenceLevel.lowerTo(Confidence::CL_Reasonable);
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
// If the end comparison isn't a variable, we can try to work with the
|
|
|
|
// expression the loop variable is being tested against instead.
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(EndCallName);
|
|
|
|
const auto *BoundExpr = Nodes.getNodeAs<Expr>(ConditionBoundName);
|
2015-09-21 17:32:59 +08:00
|
|
|
|
|
|
|
// Find container expression of iterators and pseudoarrays, and determine if
|
|
|
|
// this expression needs to be dereferenced to obtain the container.
|
|
|
|
// With array loops, the container is often discovered during the
|
|
|
|
// ForLoopIndexUseVisitor traversal.
|
2015-08-19 17:11:46 +08:00
|
|
|
const Expr *ContainerExpr = nullptr;
|
|
|
|
if (FixerKind == LFK_Iterator) {
|
|
|
|
ContainerExpr = findContainer(Context, LoopVar->getInit(),
|
|
|
|
EndVar ? EndVar->getInit() : EndCall,
|
2015-09-08 17:01:21 +08:00
|
|
|
&Descriptor.ContainerNeedsDereference);
|
2015-08-19 17:11:46 +08:00
|
|
|
} else if (FixerKind == LFK_PseudoArray) {
|
|
|
|
ContainerExpr = EndCall->getImplicitObjectArgument();
|
2015-09-21 17:32:59 +08:00
|
|
|
Descriptor.ContainerNeedsDereference =
|
|
|
|
dyn_cast<MemberExpr>(EndCall->getCallee())->isArrow();
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We must know the container or an array length bound.
|
|
|
|
if (!ContainerExpr && !BoundExpr)
|
|
|
|
return;
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
ForLoopIndexUseVisitor Finder(Context, LoopVar, EndVar, ContainerExpr,
|
|
|
|
BoundExpr,
|
|
|
|
Descriptor.ContainerNeedsDereference);
|
|
|
|
|
|
|
|
// Find expressions and variables on which the container depends.
|
|
|
|
if (ContainerExpr) {
|
|
|
|
ComponentFinderASTVisitor ComponentFinder;
|
|
|
|
ComponentFinder.findExprComponents(ContainerExpr->IgnoreParenImpCasts());
|
|
|
|
Finder.addComponents(ComponentFinder.getComponents());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find usages of the loop index. If they are not used in a convertible way,
|
|
|
|
// stop here.
|
|
|
|
if (!Finder.findAndVerifyUsages(Loop->getBody()))
|
|
|
|
return;
|
|
|
|
ConfidenceLevel.lowerTo(Finder.getConfidenceLevel());
|
|
|
|
|
|
|
|
// Obtain the container expression, if we don't have it yet.
|
|
|
|
if (FixerKind == LFK_Array) {
|
|
|
|
ContainerExpr = Finder.getContainerIndexed()->IgnoreParenImpCasts();
|
|
|
|
|
|
|
|
// Very few loops are over expressions that generate arrays rather than
|
|
|
|
// array variables. Consider loops over arrays that aren't just represented
|
|
|
|
// by a variable to be risky conversions.
|
|
|
|
if (!getReferencedVariable(ContainerExpr) &&
|
|
|
|
!isDirectMemberExpr(ContainerExpr))
|
|
|
|
ConfidenceLevel.lowerTo(Confidence::CL_Risky);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out which qualifiers we have to use in the loop range.
|
2019-11-12 23:15:56 +08:00
|
|
|
TraversalKindScope RAII(*Context, ast_type_traits::TK_AsIs);
|
2015-09-21 17:32:59 +08:00
|
|
|
const UsageResult &Usages = Finder.getUsages();
|
|
|
|
determineRangeDescriptor(Context, Nodes, Loop, FixerKind, ContainerExpr,
|
|
|
|
Usages, Descriptor);
|
|
|
|
|
|
|
|
// Ensure that we do not try to move an expression dependent on a local
|
|
|
|
// variable declared inside the loop outside of it.
|
|
|
|
// FIXME: Determine when the external dependency isn't an expression converted
|
|
|
|
// by another loop.
|
2018-11-15 23:06:11 +08:00
|
|
|
TUInfo->getParentFinder().gatherAncestors(*Context);
|
2015-09-21 17:32:59 +08:00
|
|
|
DependencyFinderASTVisitor DependencyFinder(
|
|
|
|
&TUInfo->getParentFinder().getStmtToParentStmtMap(),
|
|
|
|
&TUInfo->getParentFinder().getDeclToParentStmtMap(),
|
|
|
|
&TUInfo->getReplacedVars(), Loop);
|
|
|
|
|
|
|
|
if (DependencyFinder.dependsOnInsideVariable(ContainerExpr) ||
|
|
|
|
Descriptor.ContainerString.empty() || Usages.empty() ||
|
|
|
|
ConfidenceLevel.getLevel() < MinConfidence)
|
2015-08-19 17:11:46 +08:00
|
|
|
return;
|
|
|
|
|
2015-09-21 17:32:59 +08:00
|
|
|
doConversion(Context, LoopVar, getReferencedVariable(ContainerExpr), Usages,
|
|
|
|
Finder.getAliasDecl(), Finder.aliasUseRequired(),
|
|
|
|
Finder.aliasFromForInit(), Loop, Descriptor);
|
2015-08-19 17:11:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|