11#include "clang/Basic/IdentifierTable.h"
12#include "clang/Basic/LLVM.h"
13#include "clang/Basic/Lambda.h"
14#include "clang/Basic/SourceLocation.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Basic/TokenKinds.h"
17#include "clang/Lex/Lexer.h"
18#include "llvm/ADT/APSInt.h"
19#include "llvm/ADT/FoldingSet.h"
20#include "llvm/ADT/StringRef.h"
37bool StmtAncestorASTVisitor::TraverseStmt(Stmt *Statement) {
38 StmtAncestors.insert(std::make_pair(Statement, StmtStack.back()));
39 StmtStack.push_back(Statement);
40 RecursiveASTVisitor<StmtAncestorASTVisitor>::TraverseStmt(Statement);
50bool StmtAncestorASTVisitor::VisitDeclStmt(DeclStmt *Statement) {
51 for (
const auto *Decl : Statement->decls())
52 if (
const auto *V = dyn_cast<VarDecl>(Decl))
53 DeclParents.insert(std::make_pair(V, Statement));
58bool ComponentFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
59 Components.push_back(E);
64bool ComponentFinderASTVisitor::VisitMemberExpr(MemberExpr *Member) {
65 Components.push_back(Member);
71bool DependencyFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
72 if (
auto *V = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
73 return VisitVarDecl(V);
78bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
79 const Stmt *Curr = DeclParents->lookup(V);
81 while (Curr !=
nullptr) {
82 if (Curr == ContainingStmt) {
83 DependsOnInsideVariable =
true;
86 Curr = StmtParents->lookup(Curr);
91 if (llvm::none_of(*ReplacedVars,
92 [&](
const auto &I) {
return I.second == V; }))
94 DependsOnInsideVariable =
true;
100bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
101 const StmtGeneratedVarNameMap::const_iterator I =
102 GeneratedDecls->find(TheLoop);
103 if (I != GeneratedDecls->end() && I->second == Name) {
112bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
113 const IdentifierInfo *Ident =
D->getIdentifier();
114 if (Ident && Ident->getName() == Name) {
123bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
124 if (
auto *D = dyn_cast<NamedDecl>(DeclRef->getDecl()))
125 return VisitNamedDecl(D);
131bool DeclFinderASTVisitor::VisitTypeLoc(TypeLoc TL) {
132 const QualType QType = TL.getType();
135 if (QType.getAsString() == Name) {
142 if (
const IdentifierInfo *Ident = QType.getBaseTypeIdentifier()) {
143 if (Ident->getName() == Name) {
168 E = E->IgnoreImplicit();
169 if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(E)) {
172 if (ConstructExpr->getNumArgs() != 1 ||
173 ConstructExpr->getConstructionKind() != CXXConstructionKind::Complete)
175 E = ConstructExpr->getArg(0);
176 if (
const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))
177 E = Temp->getSubExpr();
182 if (
const auto *ME = dyn_cast<CXXMemberCallExpr>(E))
183 if (isa<CXXConversionDecl>(ME->getMethodDecl()))
189bool areSameExpr(ASTContext *Context,
const Expr *First,
const Expr *Second) {
195 return dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
200 return First && Second &&
201 First->getCanonicalDecl() == Second->getCanonicalDecl();
216 if (
const auto *Uop = dyn_cast<UnaryOperator>(E))
217 return Uop->getOpcode() == UO_Deref ? Uop->getSubExpr() :
nullptr;
219 if (
const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
220 return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1
229template <
typename ContainerT>
230static bool containsExpr(ASTContext *Context,
const ContainerT *Container,
232 llvm::FoldingSetNodeID ID;
233 E->Profile(ID, *Context,
true);
234 return llvm::any_of(*Container,
235 [&](
const auto &I) {
return ID == I.second; });
247 const VarDecl *IndexVar) {
248 const DeclRefExpr *Idx =
getDeclRef(IndexExpr);
249 return Idx && Idx->getType()->isIntegerType() &&
281 const VarDecl *IndexVar,
const Expr *Obj,
282 const Expr *SourceExpr,
bool PermitDeref) {
286 if (
areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
287 Obj->IgnoreParenImpCasts()))
291 if (PermitDeref &&
areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
292 InnerObj->IgnoreParenImpCasts()))
307 const VarDecl *IndexVar) {
308 return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1 &&
320 const VarDecl *IndexVar) {
321 return Uop->getOpcode() == UO_Deref &&
343 const VarDecl *IndexVar) {
344 const auto *VDecl = dyn_cast<VarDecl>(TheDecl);
347 if (!VDecl->hasInit())
350 bool OnlyCasts =
true;
351 const Expr *Init = VDecl->getInit()->IgnoreParenImpCasts();
352 if (isa_and_nonnull<CXXConstructExpr>(Init)) {
362 const QualType InitType = Init->getType();
363 QualType DeclarationType = VDecl->getType();
364 if (!DeclarationType.isNull() && DeclarationType->isReferenceType())
365 DeclarationType = DeclarationType.getNonReferenceType();
367 if (InitType.isNull() || DeclarationType.isNull() ||
368 !ASTContext::hasSameUnqualifiedType(DeclarationType, InitType))
372 switch (Init->getStmtClass()) {
373 case Stmt::ArraySubscriptExprClass: {
374 const auto *E = cast<ArraySubscriptExpr>(Init);
380 case Stmt::UnaryOperatorClass:
383 case Stmt::CXXOperatorCallExprClass: {
384 const auto *OpCall = cast<CXXOperatorCallExpr>(Init);
385 if (OpCall->getOperator() == OO_Star)
387 if (OpCall->getOperator() == OO_Subscript) {
388 return OpCall->getNumArgs() == 2 &&
394 case Stmt::CXXMemberCallExprClass: {
395 const auto *MemCall = cast<CXXMemberCallExpr>(Init);
398 const auto *MDecl = MemCall->getMethodDecl();
399 if (MDecl && !isa<CXXConversionDecl>(MDecl) &&
400 MDecl->getNameAsString() ==
"at" && MemCall->getNumArgs() == 1) {
426 const QualType &ArrayType,
427 const Expr *ConditionExpr) {
428 if (!ConditionExpr || ConditionExpr->isValueDependent())
430 const ConstantArrayType *ConstType =
431 Context->getAsConstantArrayType(ArrayType);
434 std::optional<llvm::APSInt> ConditionSize =
435 ConditionExpr->getIntegerConstantExpr(*Context);
438 const llvm::APSInt ArraySize(ConstType->getSize());
439 return llvm::APSInt::isSameValue(*ConditionSize, ArraySize);
443 const VarDecl *IndexVar,
444 const VarDecl *EndVar,
445 const Expr *ContainerExpr,
446 const Expr *ArrayBoundExpr,
447 bool ContainerNeedsDereference)
448 : Context(Context), IndexVar(IndexVar), EndVar(EndVar),
449 ContainerExpr(ContainerExpr), ArrayBoundExpr(ArrayBoundExpr),
450 ContainerNeedsDereference(ContainerNeedsDereference),
454 addComponent(ContainerExpr);
458 TraverseStmt(
const_cast<Stmt *
>(Body));
459 return OnlyUsedAsIndex && ContainerExpr;
464 for (
const auto &I : Components)
468void ForLoopIndexUseVisitor::addComponent(
const Expr *E) {
469 llvm::FoldingSetNodeID ID;
470 const Expr *Node = E->IgnoreParenImpCasts();
471 Node->Profile(ID, *Context,
true);
472 DependentExprs.push_back(std::make_pair(Node, ID));
476 SourceLocation Begin = U.
Range.getBegin();
477 if (Begin.isMacroID())
478 Begin = Context->getSourceManager().getSpellingLoc(Begin);
480 if (UsageLocations.insert(Begin).second)
495bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) {
503 return VisitorBase::TraverseUnaryOperator(Uop);
531bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) {
532 const Expr *Base = Member->getBase();
534 const Expr *ResultExpr = Member;
536 if (
const auto *Call =
537 dyn_cast<CXXOperatorCallExpr>(Base->IgnoreParenImpCasts())) {
545 if (Call->getOperator() == OO_Arrow) {
546 assert(Call->getNumArgs() == 1 &&
547 "Operator-> takes more than one argument");
550 ExprType = Call->getCallReturnType(*Context);
556 if (!Member->isArrow()) {
557 OnlyUsedAsIndex =
false;
561 if (ExprType.isNull())
562 ExprType =
Obj->getType();
564 if (!ExprType->isPointerType())
569 const SourceLocation ArrowLoc = Lexer::getLocForEndOfToken(
570 Base->getExprLoc(), 0, Context->getSourceManager(),
571 Context->getLangOpts());
574 if (ArrowLoc.isValid()) {
576 SourceRange(Base->getExprLoc(), ArrowLoc)));
580 return VisitorBase::TraverseMemberExpr(Member);
590bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr(
591 CXXMemberCallExpr *MemberCall) {
593 dyn_cast<MemberExpr>(MemberCall->getCallee()->IgnoreParenImpCasts());
595 return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
600 const IdentifierInfo *Ident = Member->getMemberDecl()->getIdentifier();
601 if (Ident && Ident->isStr(
"at") && MemberCall->getNumArgs() == 1) {
603 Member->getBase(), ContainerExpr,
604 ContainerNeedsDereference)) {
610 if (
containsExpr(Context, &DependentExprs, Member->getBase()))
613 return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
635bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
636 CXXOperatorCallExpr *OpCall) {
637 switch (OpCall->getOperator()) {
646 if (OpCall->getNumArgs() != 2)
649 OpCall->getArg(0), ContainerExpr,
650 ContainerNeedsDereference)) {
659 return VisitorBase::TraverseCXXOperatorCallExpr(OpCall);
681bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) {
682 Expr *Arr = E->getBase();
684 return VisitorBase::TraverseArraySubscriptExpr(E);
686 if ((ContainerExpr && !
areSameExpr(Context, Arr->IgnoreParenImpCasts(),
687 ContainerExpr->IgnoreParenImpCasts())) ||
692 OnlyUsedAsIndex =
false;
693 return VisitorBase::TraverseArraySubscriptExpr(E);
735bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
736 const ValueDecl *TheDecl = E->getDecl();
740 OnlyUsedAsIndex =
false;
768bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
769 const LambdaCapture *C,
771 if (
C->capturesVariable()) {
772 ValueDecl *VDecl =
C->getCapturedVar();
782 if (VDecl->isInitCapture())
783 traverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
785 return VisitorBase::TraverseLambdaCapture(LE, C, Init);
792bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
793 if (!AliasDecl && S->isSingleDecl() &&
794 isAliasDecl(Context, S->getSingleDecl(), IndexVar)) {
796 if (CurrStmtParent) {
797 if (isa<IfStmt>(CurrStmtParent) || isa<WhileStmt>(CurrStmtParent) ||
798 isa<SwitchStmt>(CurrStmtParent))
799 ReplaceWithAliasUse =
true;
800 else if (isa<ForStmt>(CurrStmtParent)) {
801 if (cast<ForStmt>(CurrStmtParent)->getConditionVariableDeclStmt() == S)
802 ReplaceWithAliasUse =
true;
805 AliasFromForInit =
true;
813bool ForLoopIndexUseVisitor::traverseStmtImpl(Stmt *S) {
816 const Stmt *OldNextParent = NextStmtParent;
817 CurrStmtParent = NextStmtParent;
819 const bool Result = VisitorBase::TraverseStmt(S);
820 NextStmtParent = OldNextParent;
824bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
829 if (
const auto *LE = dyn_cast_or_null<LambdaExpr>(NextStmtParent)) {
832 if (S != LE->getBody())
835 return traverseStmtImpl(S);
842 std::string IteratorName;
843 StringRef ContainerName;
845 ContainerName = TheContainer->getName();
847 const size_t Len = ContainerName.size();
848 if (Len > 1 && ContainerName.ends_with(Style ==
NS_UpperCase ?
"S" :
"s")) {
849 IteratorName = std::string(ContainerName.substr(0, Len - 1));
851 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
855 if (Len > 2 && ContainerName.ends_with(Style ==
NS_UpperCase ?
"S_" :
"s_")) {
856 IteratorName = std::string(ContainerName.substr(0, Len - 2));
858 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
862 return std::string(OldIndex->getName());
871bool VariableNamer::declarationExists(StringRef Symbol) {
872 assert(Context !=
nullptr &&
"Expected an ASTContext");
873 const IdentifierInfo &Ident = Context->Idents.get(Symbol);
876 if (!isAnyIdentifier(Ident.getTokenID()))
880 if (Ident.hasMacroDefinition())
884 for (
const Stmt *S = SourceStmt; S !=
nullptr; S = ReverseAST->lookup(S)) {
885 const StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(S);
886 if (I != GeneratedDecls->end() && I->second == Symbol)
897 DeclFinderASTVisitor DeclFinder(std::string(Symbol), GeneratedDecls);
898 return DeclFinder.findUsages(SourceStmt);
A class to encapsulate lowering of the tool's confidence level.
ForLoopIndexUseVisitor(ASTContext *Context, const VarDecl *IndexVar, const VarDecl *EndVar, const Expr *ContainerExpr, const Expr *ArrayBoundExpr, bool ContainerNeedsDereference)
bool findAndVerifyUsages(const Stmt *Body)
Finds all uses of IndexVar in Body, placing all usages in Usages, and returns true if IndexVar was on...
void addUsage(const Usage &U)
Adds the Usage if it was not added before.
void addComponents(const ComponentVector &Components)
Add a set of components that we should consider relevant to the container.
std::string createIndexName()
Generate a new index name.
static bool arrayMatchesBoundExpr(ASTContext *Context, const QualType &ArrayType, const Expr *ConditionExpr)
Determines whether the bound of a for loop condition expression is the same as the statically computa...
static bool containsExpr(ASTContext *Context, const ContainerT *Container, const Expr *E)
Returns true when the Container contains an Expr equivalent to E.
const DeclRefExpr * getDeclRef(const Expr *E)
Returns the DeclRefExpr represented by E, or NULL if there isn't one.
static bool exprReferencesVariable(const ValueDecl *Target, const Expr *E)
Determines if an expression is a declaration reference to a particular variable.
bool areSameVariable(const ValueDecl *First, const ValueDecl *Second)
Returns true when two ValueDecls are the same variable.
llvm::SmallVector< const clang::Expr *, 16 > ComponentVector
A vector used to store the AST subtrees of an Expr.
static const Expr * getDereferenceOperand(const Expr *E)
If the expression is a dereference or call to operator*(), return the operand.
const Expr * digThroughConstructorsConversions(const Expr *E)
Look through conversion/copy constructors and member functions to find the explicit initialization ex...
static bool isAliasDecl(ASTContext *Context, const Decl *TheDecl, const VarDecl *IndexVar)
Determines whether the given Decl defines a variable initialized to the loop object.
static bool isIndexInSubscriptExpr(const Expr *IndexExpr, const VarDecl *IndexVar)
Returns true when the index expression is a declaration reference to IndexVar.
static bool isDereferenceOfOpCall(const CXXOperatorCallExpr *OpCall, const VarDecl *IndexVar)
Returns true when Opcall is a call a one-parameter dereference of IndexVar.
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second)
Returns true when two Exprs are equivalent.
static bool isDereferenceOfUop(const UnaryOperator *Uop, const VarDecl *IndexVar)
Returns true when Uop is a dereference of IndexVar.
bool areStatementsIdentical(const Stmt *FirstStmt, const Stmt *SecondStmt, const ASTContext &Context, bool Canonical)
The information needed to describe a valid convertible usage of an array index or iterator.