2016-03-06 05:17:58 +08:00
|
|
|
//===--- DeclRefExprUtils.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
|
2016-03-06 05:17:58 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DeclRefExprUtils.h"
|
2016-07-02 04:12:15 +08:00
|
|
|
#include "Matchers.h"
|
2016-03-06 05:17:58 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-16 22:34:20 +08:00
|
|
|
namespace utils {
|
2016-05-03 10:54:05 +08:00
|
|
|
namespace decl_ref_expr {
|
2016-03-06 05:17:58 +08:00
|
|
|
|
|
|
|
using namespace ::clang::ast_matchers;
|
|
|
|
using llvm::SmallPtrSet;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename S> bool isSetDifferenceEmpty(const S &S1, const S &S2) {
|
2020-01-02 00:23:21 +08:00
|
|
|
for (auto E : S1)
|
2016-03-06 05:17:58 +08:00
|
|
|
if (S2.count(E) == 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extracts all Nodes keyed by ID from Matches and inserts them into Nodes.
|
|
|
|
template <typename Node>
|
|
|
|
void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
|
|
|
|
SmallPtrSet<const Node *, 16> &Nodes) {
|
|
|
|
for (const auto &Match : Matches)
|
|
|
|
Nodes.insert(Match.getNodeAs<Node>(ID));
|
|
|
|
}
|
|
|
|
|
2016-07-02 04:12:15 +08:00
|
|
|
} // namespace
|
2016-03-06 05:17:58 +08:00
|
|
|
|
|
|
|
// Finds all DeclRefExprs where a const method is called on VarDecl or VarDecl
|
|
|
|
// is the a const reference or value argument to a CallExpr or CXXConstructExpr.
|
|
|
|
SmallPtrSet<const DeclRefExpr *, 16>
|
|
|
|
constReferenceDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt,
|
|
|
|
ASTContext &Context) {
|
|
|
|
auto DeclRefToVar =
|
|
|
|
declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef");
|
|
|
|
auto ConstMethodCallee = callee(cxxMethodDecl(isConst()));
|
|
|
|
// Match method call expressions where the variable is referenced as the this
|
|
|
|
// implicit object argument and opertor call expression for member operators
|
|
|
|
// where the variable is the 0-th argument.
|
|
|
|
auto Matches = match(
|
|
|
|
findAll(expr(anyOf(cxxMemberCallExpr(ConstMethodCallee, on(DeclRefToVar)),
|
|
|
|
cxxOperatorCallExpr(ConstMethodCallee,
|
|
|
|
hasArgument(0, DeclRefToVar))))),
|
|
|
|
Stmt, Context);
|
|
|
|
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
|
|
|
|
extractNodesByIdTo(Matches, "declRef", DeclRefs);
|
|
|
|
auto ConstReferenceOrValue =
|
2020-11-21 05:31:52 +08:00
|
|
|
qualType(anyOf(matchers::isReferenceToConst(),
|
2020-10-23 02:38:38 +08:00
|
|
|
unless(anyOf(referenceType(), pointerType(),
|
|
|
|
substTemplateTypeParmType()))));
|
|
|
|
auto ConstReferenceOrValueOrReplaced = qualType(anyOf(
|
|
|
|
ConstReferenceOrValue,
|
|
|
|
substTemplateTypeParmType(hasReplacementType(ConstReferenceOrValue))));
|
2016-03-06 05:17:58 +08:00
|
|
|
auto UsedAsConstRefOrValueArg = forEachArgumentWithParam(
|
2020-10-23 02:38:38 +08:00
|
|
|
DeclRefToVar, parmVarDecl(hasType(ConstReferenceOrValueOrReplaced)));
|
2021-01-03 05:18:29 +08:00
|
|
|
Matches = match(findAll(invocation(UsedAsConstRefOrValueArg)), Stmt, Context);
|
2016-03-06 05:17:58 +08:00
|
|
|
extractNodesByIdTo(Matches, "declRef", DeclRefs);
|
2020-11-21 05:31:52 +08:00
|
|
|
// References and pointers to const assignments.
|
|
|
|
Matches =
|
|
|
|
match(findAll(declStmt(
|
|
|
|
has(varDecl(hasType(qualType(matchers::isReferenceToConst())),
|
|
|
|
hasInitializer(ignoringImpCasts(DeclRefToVar)))))),
|
|
|
|
Stmt, Context);
|
|
|
|
extractNodesByIdTo(Matches, "declRef", DeclRefs);
|
|
|
|
Matches =
|
|
|
|
match(findAll(declStmt(has(varDecl(
|
|
|
|
hasType(qualType(matchers::isPointerToConst())),
|
|
|
|
hasInitializer(ignoringImpCasts(unaryOperator(
|
|
|
|
hasOperatorName("&"), hasUnaryOperand(DeclRefToVar)))))))),
|
|
|
|
Stmt, Context);
|
|
|
|
extractNodesByIdTo(Matches, "declRef", DeclRefs);
|
2016-03-06 05:17:58 +08:00
|
|
|
return DeclRefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOnlyUsedAsConst(const VarDecl &Var, const Stmt &Stmt,
|
|
|
|
ASTContext &Context) {
|
|
|
|
// Collect all DeclRefExprs to the loop variable and all CallExprs and
|
|
|
|
// CXXConstructExprs where the loop variable is used as argument to a const
|
|
|
|
// reference parameter.
|
|
|
|
// If the difference is empty it is safe for the loop variable to be a const
|
|
|
|
// reference.
|
2016-07-02 04:12:15 +08:00
|
|
|
auto AllDeclRefs = allDeclRefExprs(Var, Stmt, Context);
|
2016-03-06 05:17:58 +08:00
|
|
|
auto ConstReferenceDeclRefs = constReferenceDeclRefExprs(Var, Stmt, Context);
|
|
|
|
return isSetDifferenceEmpty(AllDeclRefs, ConstReferenceDeclRefs);
|
|
|
|
}
|
|
|
|
|
2016-07-02 04:12:15 +08:00
|
|
|
SmallPtrSet<const DeclRefExpr *, 16>
|
|
|
|
allDeclRefExprs(const VarDecl &VarDecl, const Stmt &Stmt, ASTContext &Context) {
|
|
|
|
auto Matches = match(
|
|
|
|
findAll(declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef")),
|
|
|
|
Stmt, Context);
|
|
|
|
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
|
|
|
|
extractNodesByIdTo(Matches, "declRef", DeclRefs);
|
|
|
|
return DeclRefs;
|
|
|
|
}
|
|
|
|
|
2017-01-03 20:10:44 +08:00
|
|
|
SmallPtrSet<const DeclRefExpr *, 16>
|
|
|
|
allDeclRefExprs(const VarDecl &VarDecl, const Decl &Decl, ASTContext &Context) {
|
|
|
|
auto Matches = match(
|
|
|
|
decl(forEachDescendant(
|
|
|
|
declRefExpr(to(varDecl(equalsNode(&VarDecl)))).bind("declRef"))),
|
|
|
|
Decl, Context);
|
|
|
|
SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
|
|
|
|
extractNodesByIdTo(Matches, "declRef", DeclRefs);
|
|
|
|
return DeclRefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isCopyConstructorArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
|
2016-07-02 04:12:15 +08:00
|
|
|
ASTContext &Context) {
|
|
|
|
auto UsedAsConstRefArg = forEachArgumentWithParam(
|
|
|
|
declRefExpr(equalsNode(&DeclRef)),
|
|
|
|
parmVarDecl(hasType(matchers::isReferenceToConst())));
|
|
|
|
auto Matches = match(
|
2017-01-03 20:10:44 +08:00
|
|
|
decl(hasDescendant(
|
2016-07-02 04:12:15 +08:00
|
|
|
cxxConstructExpr(UsedAsConstRefArg, hasDeclaration(cxxConstructorDecl(
|
|
|
|
isCopyConstructor())))
|
|
|
|
.bind("constructExpr"))),
|
2017-01-03 20:10:44 +08:00
|
|
|
Decl, Context);
|
2016-07-02 04:12:15 +08:00
|
|
|
return !Matches.empty();
|
|
|
|
}
|
|
|
|
|
2017-01-03 20:10:44 +08:00
|
|
|
bool isCopyAssignmentArgument(const DeclRefExpr &DeclRef, const Decl &Decl,
|
2016-07-02 04:12:15 +08:00
|
|
|
ASTContext &Context) {
|
|
|
|
auto UsedAsConstRefArg = forEachArgumentWithParam(
|
|
|
|
declRefExpr(equalsNode(&DeclRef)),
|
|
|
|
parmVarDecl(hasType(matchers::isReferenceToConst())));
|
|
|
|
auto Matches = match(
|
2017-01-03 20:10:44 +08:00
|
|
|
decl(hasDescendant(
|
2017-01-19 23:51:10 +08:00
|
|
|
cxxOperatorCallExpr(UsedAsConstRefArg, hasOverloadedOperatorName("="),
|
|
|
|
callee(cxxMethodDecl(isCopyAssignmentOperator())))
|
2016-07-02 04:12:15 +08:00
|
|
|
.bind("operatorCallExpr"))),
|
2017-01-03 20:10:44 +08:00
|
|
|
Decl, Context);
|
2016-07-02 04:12:15 +08:00
|
|
|
return !Matches.empty();
|
|
|
|
}
|
|
|
|
|
2016-05-03 10:54:05 +08:00
|
|
|
} // namespace decl_ref_expr
|
|
|
|
} // namespace utils
|
2016-03-06 05:17:58 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|