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));
59bool ComponentFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
60 Components.push_back(E);
65bool ComponentFinderASTVisitor::VisitMemberExpr(MemberExpr *Member) {
66 Components.push_back(Member);
72bool DependencyFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
73 if (
auto *V = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
74 return VisitVarDecl(V);
79bool DependencyFinderASTVisitor::VisitVarDecl(VarDecl *V) {
80 const Stmt *Curr = DeclParents->lookup(V);
82 while (Curr !=
nullptr) {
83 if (Curr == ContainingStmt) {
84 DependsOnInsideVariable =
true;
87 Curr = StmtParents->lookup(Curr);
92 if (llvm::none_of(*ReplacedVars,
93 [&](
const auto &I) {
return I.second == V; }))
95 DependsOnInsideVariable =
true;
101bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
102 const StmtGeneratedVarNameMap::const_iterator I =
103 GeneratedDecls->find(TheLoop);
104 if (I != GeneratedDecls->end() && I->second == Name) {
113bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
114 const IdentifierInfo *Ident =
D->getIdentifier();
115 if (Ident && Ident->getName() == Name) {
124bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
125 if (
auto *D = dyn_cast<NamedDecl>(DeclRef->getDecl()))
126 return VisitNamedDecl(D);
132bool DeclFinderASTVisitor::VisitTypeLoc(TypeLoc TL) {
133 const QualType QType = TL.getType();
136 if (QType.getAsString() == Name) {
143 if (
const IdentifierInfo *Ident = QType.getBaseTypeIdentifier()) {
144 if (Ident->getName() == Name) {
169 E = E->IgnoreImplicit();
170 if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(E)) {
173 if (ConstructExpr->getNumArgs() != 1 ||
174 ConstructExpr->getConstructionKind() != CXXConstructionKind::Complete)
176 E = ConstructExpr->getArg(0);
177 if (
const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))
178 E = Temp->getSubExpr();
183 if (
const auto *ME = dyn_cast<CXXMemberCallExpr>(E))
184 if (isa<CXXConversionDecl>(ME->getMethodDecl()))
190bool areSameExpr(ASTContext *Context,
const Expr *First,
const Expr *Second) {
196 return dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
201 return First && Second &&
202 First->getCanonicalDecl() == Second->getCanonicalDecl();
217 if (
const auto *Uop = dyn_cast<UnaryOperator>(E))
218 return Uop->getOpcode() == UO_Deref ? Uop->getSubExpr() :
nullptr;
220 if (
const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
221 return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1
230template <
typename ContainerT>
231static bool containsExpr(ASTContext *Context,
const ContainerT *Container,
233 llvm::FoldingSetNodeID ID;
234 E->Profile(ID, *Context,
true);
235 return llvm::any_of(*Container,
236 [&](
const auto &I) {
return ID == I.second; });
248 const VarDecl *IndexVar) {
249 const DeclRefExpr *Idx =
getDeclRef(IndexExpr);
250 return Idx && Idx->getType()->isIntegerType() &&
282 const VarDecl *IndexVar,
const Expr *Obj,
283 const Expr *SourceExpr,
bool PermitDeref) {
287 if (
areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
288 Obj->IgnoreParenImpCasts()))
292 if (PermitDeref &&
areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
293 InnerObj->IgnoreParenImpCasts()))
308 const VarDecl *IndexVar) {
309 return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1 &&
321 const VarDecl *IndexVar) {
322 return Uop->getOpcode() == UO_Deref &&
344 const VarDecl *IndexVar) {
345 const auto *VDecl = dyn_cast<VarDecl>(TheDecl);
348 if (!VDecl->hasInit())
351 bool OnlyCasts =
true;
352 const Expr *Init = VDecl->getInit()->IgnoreParenImpCasts();
353 if (isa_and_nonnull<CXXConstructExpr>(Init)) {
363 const QualType InitType = Init->getType();
364 QualType DeclarationType = VDecl->getType();
365 if (!DeclarationType.isNull() && DeclarationType->isReferenceType())
366 DeclarationType = DeclarationType.getNonReferenceType();
368 if (InitType.isNull() || DeclarationType.isNull() ||
369 !ASTContext::hasSameUnqualifiedType(DeclarationType, InitType))
373 switch (Init->getStmtClass()) {
374 case Stmt::ArraySubscriptExprClass: {
375 const auto *E = cast<ArraySubscriptExpr>(Init);
381 case Stmt::UnaryOperatorClass:
384 case Stmt::CXXOperatorCallExprClass: {
385 const auto *OpCall = cast<CXXOperatorCallExpr>(Init);
386 if (OpCall->getOperator() == OO_Star)
388 if (OpCall->getOperator() == OO_Subscript) {
389 return OpCall->getNumArgs() == 2 &&
395 case Stmt::CXXMemberCallExprClass: {
396 const auto *MemCall = cast<CXXMemberCallExpr>(Init);
399 const auto *MDecl = MemCall->getMethodDecl();
400 if (MDecl && !isa<CXXConversionDecl>(MDecl) &&
401 MDecl->getNameAsString() ==
"at" && MemCall->getNumArgs() == 1) {
427 const QualType &ArrayType,
428 const Expr *ConditionExpr) {
429 if (!ConditionExpr || ConditionExpr->isValueDependent())
431 const ConstantArrayType *ConstType =
432 Context->getAsConstantArrayType(ArrayType);
435 std::optional<llvm::APSInt> ConditionSize =
436 ConditionExpr->getIntegerConstantExpr(*Context);
439 const llvm::APSInt ArraySize(ConstType->getSize());
440 return llvm::APSInt::isSameValue(*ConditionSize, ArraySize);
444 const VarDecl *IndexVar,
445 const VarDecl *EndVar,
446 const Expr *ContainerExpr,
447 const Expr *ArrayBoundExpr,
448 bool ContainerNeedsDereference)
449 : Context(Context), IndexVar(IndexVar), EndVar(EndVar),
450 ContainerExpr(ContainerExpr), ArrayBoundExpr(ArrayBoundExpr),
451 ContainerNeedsDereference(ContainerNeedsDereference),
455 addComponent(ContainerExpr);
459 TraverseStmt(
const_cast<Stmt *
>(Body));
460 return OnlyUsedAsIndex && ContainerExpr;
465 for (
const auto &I : Components)
469void ForLoopIndexUseVisitor::addComponent(
const Expr *E) {
470 llvm::FoldingSetNodeID ID;
471 const Expr *Node = E->IgnoreParenImpCasts();
472 Node->Profile(ID, *Context,
true);
473 DependentExprs.push_back(std::make_pair(Node, ID));
477 SourceLocation Begin = U.
Range.getBegin();
478 if (Begin.isMacroID())
479 Begin = Context->getSourceManager().getSpellingLoc(Begin);
481 if (UsageLocations.insert(Begin).second)
496bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) {
504 return VisitorBase::TraverseUnaryOperator(Uop);
532bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) {
533 const Expr *Base = Member->getBase();
535 const Expr *ResultExpr = Member;
537 if (
const auto *Call =
538 dyn_cast<CXXOperatorCallExpr>(Base->IgnoreParenImpCasts())) {
546 if (Call->getOperator() == OO_Arrow) {
547 assert(Call->getNumArgs() == 1 &&
548 "Operator-> takes more than one argument");
551 ExprType = Call->getCallReturnType(*Context);
557 if (!Member->isArrow()) {
558 OnlyUsedAsIndex =
false;
562 if (ExprType.isNull())
563 ExprType =
Obj->getType();
565 if (!ExprType->isPointerType())
570 const SourceLocation ArrowLoc = Lexer::getLocForEndOfToken(
571 Base->getExprLoc(), 0, Context->getSourceManager(),
572 Context->getLangOpts());
575 if (ArrowLoc.isValid()) {
577 SourceRange(Base->getExprLoc(), ArrowLoc)));
581 return VisitorBase::TraverseMemberExpr(Member);
591bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr(
592 CXXMemberCallExpr *MemberCall) {
594 dyn_cast<MemberExpr>(MemberCall->getCallee()->IgnoreParenImpCasts());
596 return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
601 const IdentifierInfo *Ident = Member->getMemberDecl()->getIdentifier();
602 if (Ident && Ident->isStr(
"at") && MemberCall->getNumArgs() == 1) {
604 Member->getBase(), ContainerExpr,
605 ContainerNeedsDereference)) {
611 if (
containsExpr(Context, &DependentExprs, Member->getBase()))
614 return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
636bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
637 CXXOperatorCallExpr *OpCall) {
638 switch (OpCall->getOperator()) {
647 if (OpCall->getNumArgs() != 2)
650 OpCall->getArg(0), ContainerExpr,
651 ContainerNeedsDereference)) {
660 return VisitorBase::TraverseCXXOperatorCallExpr(OpCall);
682bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) {
683 Expr *Arr = E->getBase();
685 return VisitorBase::TraverseArraySubscriptExpr(E);
687 if ((ContainerExpr && !
areSameExpr(Context, Arr->IgnoreParenImpCasts(),
688 ContainerExpr->IgnoreParenImpCasts())) ||
693 OnlyUsedAsIndex =
false;
694 return VisitorBase::TraverseArraySubscriptExpr(E);
736bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
737 const ValueDecl *TheDecl = E->getDecl();
741 OnlyUsedAsIndex =
false;
769bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
770 const LambdaCapture *C,
772 if (
C->capturesVariable()) {
773 ValueDecl *VDecl =
C->getCapturedVar();
783 if (VDecl->isInitCapture())
784 traverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
786 return VisitorBase::TraverseLambdaCapture(LE, C, Init);
793bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
794 if (!AliasDecl && S->isSingleDecl() &&
795 isAliasDecl(Context, S->getSingleDecl(), IndexVar)) {
797 if (CurrStmtParent) {
798 if (isa<IfStmt>(CurrStmtParent) || isa<WhileStmt>(CurrStmtParent) ||
799 isa<SwitchStmt>(CurrStmtParent))
800 ReplaceWithAliasUse =
true;
801 else if (isa<ForStmt>(CurrStmtParent)) {
802 if (cast<ForStmt>(CurrStmtParent)->getConditionVariableDeclStmt() == S)
803 ReplaceWithAliasUse =
true;
806 AliasFromForInit =
true;
814bool ForLoopIndexUseVisitor::traverseStmtImpl(Stmt *S) {
817 const Stmt *OldNextParent = NextStmtParent;
818 CurrStmtParent = NextStmtParent;
820 const bool Result = VisitorBase::TraverseStmt(S);
821 NextStmtParent = OldNextParent;
825bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
830 if (
const auto *LE = dyn_cast_or_null<LambdaExpr>(NextStmtParent)) {
833 if (S != LE->getBody()) {
837 return traverseStmtImpl(S);
844 std::string IteratorName;
845 StringRef ContainerName;
847 ContainerName = TheContainer->getName();
849 const size_t Len = ContainerName.size();
850 if (Len > 1 && ContainerName.ends_with(Style ==
NS_UpperCase ?
"S" :
"s")) {
851 IteratorName = std::string(ContainerName.substr(0, Len - 1));
853 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
857 if (Len > 2 && ContainerName.ends_with(Style ==
NS_UpperCase ?
"S_" :
"s_")) {
858 IteratorName = std::string(ContainerName.substr(0, Len - 2));
860 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
864 return std::string(OldIndex->getName());
873bool VariableNamer::declarationExists(StringRef Symbol) {
874 assert(Context !=
nullptr &&
"Expected an ASTContext");
875 const IdentifierInfo &Ident = Context->Idents.get(Symbol);
878 if (!isAnyIdentifier(Ident.getTokenID()))
882 if (Ident.hasMacroDefinition())
886 for (
const Stmt *S = SourceStmt; S !=
nullptr; S = ReverseAST->lookup(S)) {
887 const StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(S);
888 if (I != GeneratedDecls->end() && I->second == Symbol)
899 DeclFinderASTVisitor DeclFinder(std::string(Symbol), GeneratedDecls);
900 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.