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 for (
const auto &I : *ReplacedVars) {
94 DependsOnInsideVariable =
true;
103bool DeclFinderASTVisitor::VisitForStmt(ForStmt *TheLoop) {
104 const StmtGeneratedVarNameMap::const_iterator I =
105 GeneratedDecls->find(TheLoop);
106 if (I != GeneratedDecls->end() && I->second == Name) {
115bool DeclFinderASTVisitor::VisitNamedDecl(NamedDecl *D) {
116 const IdentifierInfo *Ident =
D->getIdentifier();
117 if (Ident && Ident->getName() == Name) {
126bool DeclFinderASTVisitor::VisitDeclRefExpr(DeclRefExpr *DeclRef) {
127 if (
auto *D = dyn_cast<NamedDecl>(DeclRef->getDecl()))
128 return VisitNamedDecl(D);
134bool DeclFinderASTVisitor::VisitTypeLoc(TypeLoc TL) {
135 const QualType QType = TL.getType();
138 if (QType.getAsString() == Name) {
145 if (
const IdentifierInfo *Ident = QType.getBaseTypeIdentifier()) {
146 if (Ident->getName() == Name) {
171 E = E->IgnoreImplicit();
172 if (
const auto *ConstructExpr = dyn_cast<CXXConstructExpr>(E)) {
175 if (ConstructExpr->getNumArgs() != 1 ||
176 ConstructExpr->getConstructionKind() != CXXConstructionKind::Complete)
178 E = ConstructExpr->getArg(0);
179 if (
const auto *Temp = dyn_cast<MaterializeTemporaryExpr>(E))
180 E = Temp->getSubExpr();
185 if (
const auto *ME = dyn_cast<CXXMemberCallExpr>(E))
186 if (isa<CXXConversionDecl>(ME->getMethodDecl()))
192bool areSameExpr(ASTContext *Context,
const Expr *First,
const Expr *Second) {
198 return dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts());
203 return First && Second &&
204 First->getCanonicalDecl() == Second->getCanonicalDecl();
219 if (
const auto *Uop = dyn_cast<UnaryOperator>(E))
220 return Uop->getOpcode() == UO_Deref ? Uop->getSubExpr() :
nullptr;
222 if (
const auto *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
223 return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1
232template <
typename ContainerT>
233static bool containsExpr(ASTContext *Context,
const ContainerT *Container,
235 llvm::FoldingSetNodeID ID;
236 E->Profile(ID, *Context,
true);
237 for (
const auto &I : *Container) {
253 const VarDecl *IndexVar) {
254 const DeclRefExpr *Idx =
getDeclRef(IndexExpr);
255 return Idx && Idx->getType()->isIntegerType() &&
287 const VarDecl *IndexVar,
const Expr *Obj,
288 const Expr *SourceExpr,
bool PermitDeref) {
292 if (
areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
293 Obj->IgnoreParenImpCasts()))
297 if (PermitDeref &&
areSameExpr(Context, SourceExpr->IgnoreParenImpCasts(),
298 InnerObj->IgnoreParenImpCasts()))
313 const VarDecl *IndexVar) {
314 return OpCall->getOperator() == OO_Star && OpCall->getNumArgs() == 1 &&
326 const VarDecl *IndexVar) {
327 return Uop->getOpcode() == UO_Deref &&
349 const VarDecl *IndexVar) {
350 const auto *VDecl = dyn_cast<VarDecl>(TheDecl);
353 if (!VDecl->hasInit())
356 bool OnlyCasts =
true;
357 const Expr *Init = VDecl->getInit()->IgnoreParenImpCasts();
358 if (isa_and_nonnull<CXXConstructExpr>(Init)) {
368 const QualType InitType = Init->getType();
369 QualType DeclarationType = VDecl->getType();
370 if (!DeclarationType.isNull() && DeclarationType->isReferenceType())
371 DeclarationType = DeclarationType.getNonReferenceType();
373 if (InitType.isNull() || DeclarationType.isNull() ||
374 !ASTContext::hasSameUnqualifiedType(DeclarationType, InitType))
378 switch (Init->getStmtClass()) {
379 case Stmt::ArraySubscriptExprClass: {
380 const auto *E = cast<ArraySubscriptExpr>(Init);
386 case Stmt::UnaryOperatorClass:
389 case Stmt::CXXOperatorCallExprClass: {
390 const auto *OpCall = cast<CXXOperatorCallExpr>(Init);
391 if (OpCall->getOperator() == OO_Star)
393 if (OpCall->getOperator() == OO_Subscript) {
394 return OpCall->getNumArgs() == 2 &&
400 case Stmt::CXXMemberCallExprClass: {
401 const auto *MemCall = cast<CXXMemberCallExpr>(Init);
404 const auto *MDecl = MemCall->getMethodDecl();
405 if (MDecl && !isa<CXXConversionDecl>(MDecl) &&
406 MDecl->getNameAsString() ==
"at" && MemCall->getNumArgs() == 1) {
432 const QualType &ArrayType,
433 const Expr *ConditionExpr) {
434 if (!ConditionExpr || ConditionExpr->isValueDependent())
436 const ConstantArrayType *ConstType =
437 Context->getAsConstantArrayType(ArrayType);
440 std::optional<llvm::APSInt> ConditionSize =
441 ConditionExpr->getIntegerConstantExpr(*Context);
444 const llvm::APSInt ArraySize(ConstType->getSize());
445 return llvm::APSInt::isSameValue(*ConditionSize, ArraySize);
449 const VarDecl *IndexVar,
450 const VarDecl *EndVar,
451 const Expr *ContainerExpr,
452 const Expr *ArrayBoundExpr,
453 bool ContainerNeedsDereference)
454 : Context(Context), IndexVar(IndexVar), EndVar(EndVar),
455 ContainerExpr(ContainerExpr), ArrayBoundExpr(ArrayBoundExpr),
456 ContainerNeedsDereference(ContainerNeedsDereference),
460 addComponent(ContainerExpr);
464 TraverseStmt(
const_cast<Stmt *
>(Body));
465 return OnlyUsedAsIndex && ContainerExpr;
470 for (
const auto &I : Components)
474void ForLoopIndexUseVisitor::addComponent(
const Expr *E) {
475 llvm::FoldingSetNodeID ID;
476 const Expr *Node = E->IgnoreParenImpCasts();
477 Node->Profile(ID, *Context,
true);
478 DependentExprs.push_back(std::make_pair(Node, ID));
482 SourceLocation Begin = U.
Range.getBegin();
483 if (Begin.isMacroID())
484 Begin = Context->getSourceManager().getSpellingLoc(Begin);
486 if (UsageLocations.insert(Begin).second)
501bool ForLoopIndexUseVisitor::TraverseUnaryOperator(UnaryOperator *Uop) {
509 return VisitorBase::TraverseUnaryOperator(Uop);
537bool ForLoopIndexUseVisitor::TraverseMemberExpr(MemberExpr *Member) {
538 const Expr *Base = Member->getBase();
540 const Expr *ResultExpr = Member;
542 if (
const auto *Call =
543 dyn_cast<CXXOperatorCallExpr>(Base->IgnoreParenImpCasts())) {
551 if (Call->getOperator() == OO_Arrow) {
552 assert(Call->getNumArgs() == 1 &&
553 "Operator-> takes more than one argument");
556 ExprType = Call->getCallReturnType(*Context);
562 if (!Member->isArrow()) {
563 OnlyUsedAsIndex =
false;
567 if (ExprType.isNull())
568 ExprType =
Obj->getType();
570 if (!ExprType->isPointerType())
575 const SourceLocation ArrowLoc = Lexer::getLocForEndOfToken(
576 Base->getExprLoc(), 0, Context->getSourceManager(),
577 Context->getLangOpts());
580 if (ArrowLoc.isValid()) {
582 SourceRange(Base->getExprLoc(), ArrowLoc)));
586 return VisitorBase::TraverseMemberExpr(Member);
596bool ForLoopIndexUseVisitor::TraverseCXXMemberCallExpr(
597 CXXMemberCallExpr *MemberCall) {
599 dyn_cast<MemberExpr>(MemberCall->getCallee()->IgnoreParenImpCasts());
601 return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
606 const IdentifierInfo *Ident = Member->getMemberDecl()->getIdentifier();
607 if (Ident && Ident->isStr(
"at") && MemberCall->getNumArgs() == 1) {
609 Member->getBase(), ContainerExpr,
610 ContainerNeedsDereference)) {
616 if (
containsExpr(Context, &DependentExprs, Member->getBase()))
619 return VisitorBase::TraverseCXXMemberCallExpr(MemberCall);
641bool ForLoopIndexUseVisitor::TraverseCXXOperatorCallExpr(
642 CXXOperatorCallExpr *OpCall) {
643 switch (OpCall->getOperator()) {
652 if (OpCall->getNumArgs() != 2)
655 OpCall->getArg(0), ContainerExpr,
656 ContainerNeedsDereference)) {
665 return VisitorBase::TraverseCXXOperatorCallExpr(OpCall);
687bool ForLoopIndexUseVisitor::TraverseArraySubscriptExpr(ArraySubscriptExpr *E) {
688 Expr *Arr = E->getBase();
690 return VisitorBase::TraverseArraySubscriptExpr(E);
692 if ((ContainerExpr && !
areSameExpr(Context, Arr->IgnoreParenImpCasts(),
693 ContainerExpr->IgnoreParenImpCasts())) ||
698 OnlyUsedAsIndex =
false;
699 return VisitorBase::TraverseArraySubscriptExpr(E);
741bool ForLoopIndexUseVisitor::VisitDeclRefExpr(DeclRefExpr *E) {
742 const ValueDecl *TheDecl = E->getDecl();
746 OnlyUsedAsIndex =
false;
774bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
775 const LambdaCapture *C,
777 if (
C->capturesVariable()) {
778 ValueDecl *VDecl =
C->getCapturedVar();
788 if (VDecl->isInitCapture())
789 traverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
791 return VisitorBase::TraverseLambdaCapture(LE, C, Init);
798bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
799 if (!AliasDecl && S->isSingleDecl() &&
800 isAliasDecl(Context, S->getSingleDecl(), IndexVar)) {
802 if (CurrStmtParent) {
803 if (isa<IfStmt>(CurrStmtParent) || isa<WhileStmt>(CurrStmtParent) ||
804 isa<SwitchStmt>(CurrStmtParent))
805 ReplaceWithAliasUse =
true;
806 else if (isa<ForStmt>(CurrStmtParent)) {
807 if (cast<ForStmt>(CurrStmtParent)->getConditionVariableDeclStmt() == S)
808 ReplaceWithAliasUse =
true;
811 AliasFromForInit =
true;
819bool ForLoopIndexUseVisitor::traverseStmtImpl(Stmt *S) {
822 const Stmt *OldNextParent = NextStmtParent;
823 CurrStmtParent = NextStmtParent;
825 const bool Result = VisitorBase::TraverseStmt(S);
826 NextStmtParent = OldNextParent;
830bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
835 if (
const auto *LE = dyn_cast_or_null<LambdaExpr>(NextStmtParent)) {
838 if (S != LE->getBody()) {
842 return traverseStmtImpl(S);
849 std::string IteratorName;
850 StringRef ContainerName;
852 ContainerName = TheContainer->getName();
854 const size_t Len = ContainerName.size();
855 if (Len > 1 && ContainerName.ends_with(Style ==
NS_UpperCase ?
"S" :
"s")) {
856 IteratorName = std::string(ContainerName.substr(0, Len - 1));
858 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
862 if (Len > 2 && ContainerName.ends_with(Style ==
NS_UpperCase ?
"S_" :
"s_")) {
863 IteratorName = std::string(ContainerName.substr(0, Len - 2));
865 if (!declarationExists(IteratorName) || IteratorName == OldIndex->getName())
869 return std::string(OldIndex->getName());
878bool VariableNamer::declarationExists(StringRef Symbol) {
879 assert(Context !=
nullptr &&
"Expected an ASTContext");
880 const IdentifierInfo &Ident = Context->Idents.get(Symbol);
883 if (!isAnyIdentifier(Ident.getTokenID()))
887 if (Ident.hasMacroDefinition())
891 for (
const Stmt *S = SourceStmt; S !=
nullptr; S = ReverseAST->lookup(S)) {
892 const StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(S);
893 if (I != GeneratedDecls->end() && I->second == Symbol)
904 DeclFinderASTVisitor DeclFinder(std::string(Symbol), GeneratedDecls);
905 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.