| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOP_CONVERT_UTILS_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOP_CONVERT_UTILS_H |
| 11 |
|
| 12 |
#include "clang/AST/ASTContext.h" |
| 13 |
#include "clang/AST/RecursiveASTVisitor.h" |
| 14 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 15 |
#include "clang/Basic/SourceLocation.h" |
| 16 |
#include "llvm/ADT/DenseMap.h" |
| 17 |
#include "llvm/ADT/SmallSet.h" |
| 18 |
#include "llvm/ADT/SmallVector.h" |
| 19 |
#include "llvm/ADT/StringRef.h" |
| 20 |
#include <algorithm> |
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
namespace clang::tidy::modernize { |
| 26 |
|
| 27 |
enum LoopFixerKind { |
| 28 |
LFK_Array, |
| 29 |
LFK_Iterator, |
| 30 |
LFK_ReverseIterator, |
| 31 |
LFK_PseudoArray |
| 32 |
}; |
| 33 |
|
| 34 |
|
| 35 |
typedef llvm::DenseMap<const clang::Stmt *, const clang::Stmt *> StmtParentMap; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
typedef llvm::DenseMap<const clang::VarDecl *, const clang::DeclStmt *> |
| 40 |
DeclParentMap; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
typedef llvm::DenseMap<const clang::ForStmt *, const clang::VarDecl *> |
| 45 |
ReplacedVarsMap; |
| 46 |
|
| 47 |
|
| 48 |
typedef llvm::DenseMap<const clang::Stmt *, std::string> |
| 49 |
StmtGeneratedVarNameMap; |
| 50 |
|
| 51 |
|
| 52 |
typedef llvm::SmallVector<const clang::Expr *, 16> ComponentVector; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class StmtAncestorASTVisitor |
| 57 |
: public clang::RecursiveASTVisitor<StmtAncestorASTVisitor> { |
| 58 |
public: |
| 59 |
StmtAncestorASTVisitor() { StmtStack.push_back(nullptr); } |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
void gatherAncestors(ASTContext &Ctx) { |
| 65 |
if (StmtAncestors.empty()) |
| 66 |
TraverseAST(Ctx); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
const StmtParentMap &getStmtToParentStmtMap() { return StmtAncestors; } |
| 71 |
|
| 72 |
|
| 73 |
const DeclParentMap &getDeclToParentStmtMap() { return DeclParents; } |
| 74 |
|
| 75 |
friend class clang::RecursiveASTVisitor<StmtAncestorASTVisitor>; |
| 76 |
|
| 77 |
private: |
| 78 |
StmtParentMap StmtAncestors; |
| 79 |
DeclParentMap DeclParents; |
| 80 |
llvm::SmallVector<const clang::Stmt *, 16> StmtStack; |
| 81 |
|
| 82 |
bool TraverseStmt(clang::Stmt *Statement); |
| 83 |
bool VisitDeclStmt(clang::DeclStmt *Statement); |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
class ComponentFinderASTVisitor |
| 89 |
: public clang::RecursiveASTVisitor<ComponentFinderASTVisitor> { |
| 90 |
public: |
| 91 |
ComponentFinderASTVisitor() = default; |
| 92 |
|
| 93 |
|
| 94 |
void findExprComponents(const clang::Expr *SourceExpr) { |
| 95 |
TraverseStmt(const_cast<clang::Expr *>(SourceExpr)); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
const ComponentVector &getComponents() { return Components; } |
| 100 |
|
| 101 |
friend class clang::RecursiveASTVisitor<ComponentFinderASTVisitor>; |
| 102 |
|
| 103 |
private: |
| 104 |
ComponentVector Components; |
| 105 |
|
| 106 |
bool VisitDeclRefExpr(clang::DeclRefExpr *E); |
| 107 |
bool VisitMemberExpr(clang::MemberExpr *Member); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
class DependencyFinderASTVisitor |
| 113 |
: public clang::RecursiveASTVisitor<DependencyFinderASTVisitor> { |
| 114 |
public: |
| 115 |
DependencyFinderASTVisitor(const StmtParentMap *StmtParents, |
| 116 |
const DeclParentMap *DeclParents, |
| 117 |
const ReplacedVarsMap *ReplacedVars, |
| 118 |
const clang::Stmt *ContainingStmt) |
| 119 |
: StmtParents(StmtParents), DeclParents(DeclParents), |
| 120 |
ContainingStmt(ContainingStmt), ReplacedVars(ReplacedVars) {} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bool dependsOnInsideVariable(const clang::Stmt *Body) { |
| 153 |
DependsOnInsideVariable = false; |
| 154 |
TraverseStmt(const_cast<clang::Stmt *>(Body)); |
| 155 |
return DependsOnInsideVariable; |
| 156 |
} |
| 157 |
|
| 158 |
friend class clang::RecursiveASTVisitor<DependencyFinderASTVisitor>; |
| 159 |
|
| 160 |
private: |
| 161 |
const StmtParentMap *StmtParents; |
| 162 |
const DeclParentMap *DeclParents; |
| 163 |
const clang::Stmt *ContainingStmt; |
| 164 |
const ReplacedVarsMap *ReplacedVars; |
| 165 |
bool DependsOnInsideVariable; |
| 166 |
|
| 167 |
bool VisitVarDecl(clang::VarDecl *V); |
| 168 |
bool VisitDeclRefExpr(clang::DeclRefExpr *D); |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
class DeclFinderASTVisitor |
| 176 |
: public clang::RecursiveASTVisitor<DeclFinderASTVisitor> { |
| 177 |
public: |
| 178 |
DeclFinderASTVisitor(const StringRef &Name, |
| 179 |
const StmtGeneratedVarNameMap *GeneratedDecls) |
| 180 |
: Name(Name), GeneratedDecls(GeneratedDecls), Found(false) {} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
bool findUsages(const clang::Stmt *Body) { |
| 186 |
Found = false; |
| 187 |
TraverseStmt(const_cast<clang::Stmt *>(Body)); |
| 188 |
return Found; |
| 189 |
} |
| 190 |
|
| 191 |
friend class clang::RecursiveASTVisitor<DeclFinderASTVisitor>; |
| 192 |
|
| 193 |
private: |
| 194 |
std::string Name; |
| 195 |
|
| 196 |
|
| 197 |
const StmtGeneratedVarNameMap *GeneratedDecls; |
| 198 |
bool Found; |
| 199 |
|
| 200 |
bool VisitForStmt(clang::ForStmt *F); |
| 201 |
bool VisitNamedDecl(clang::NamedDecl *D); |
| 202 |
bool VisitDeclRefExpr(clang::DeclRefExpr *D); |
| 203 |
bool VisitTypeLoc(clang::TypeLoc TL); |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
struct Usage { |
| 209 |
enum UsageKind { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
UK_Default, |
| 221 |
|
| 222 |
|
| 223 |
UK_MemberThroughArrow, |
| 224 |
|
| 225 |
|
| 226 |
UK_CaptureByCopy, |
| 227 |
UK_CaptureByRef |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
const Expr *Expression; |
| 232 |
|
| 233 |
UsageKind Kind; |
| 234 |
|
| 235 |
|
| 236 |
SourceRange Range; |
| 237 |
|
| 238 |
explicit Usage(const Expr *E) |
| 239 |
: Expression(E), Kind(UK_Default), Range(Expression->getSourceRange()) {} |
| 240 |
Usage(const Expr *E, UsageKind Kind, SourceRange Range) |
| 241 |
: Expression(E), Kind(Kind), Range(std::move(Range)) {} |
| 242 |
}; |
| 243 |
|
| 244 |
|
| 245 |
class Confidence { |
| 246 |
public: |
| 247 |
enum Level { |
| 248 |
|
| 249 |
CL_Risky, |
| 250 |
|
| 251 |
|
| 252 |
CL_Reasonable, |
| 253 |
|
| 254 |
|
| 255 |
CL_Safe |
| 256 |
}; |
| 257 |
|
| 258 |
explicit Confidence(Confidence::Level Level) : CurrentLevel(Level) {} |
| 259 |
|
| 260 |
|
| 261 |
void lowerTo(Confidence::Level Level) { |
| 262 |
CurrentLevel = std::min(Level, CurrentLevel); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
Level getLevel() const { return CurrentLevel; } |
| 267 |
|
| 268 |
private: |
| 269 |
Level CurrentLevel; |
| 270 |
}; |
| 271 |
|
| 272 |
|
| 273 |
typedef llvm::SmallVector<Usage, 8> UsageResult; |
| 274 |
|
| 275 |
|
| 276 |
const Expr *digThroughConstructorsConversions(const Expr *E); |
| 277 |
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second); |
| 278 |
const DeclRefExpr *getDeclRef(const Expr *E); |
| 279 |
bool areSameVariable(const ValueDecl *First, const ValueDecl *Second); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
class ForLoopIndexUseVisitor |
| 287 |
: public RecursiveASTVisitor<ForLoopIndexUseVisitor> { |
| 288 |
public: |
| 289 |
ForLoopIndexUseVisitor(ASTContext *Context, const VarDecl *IndexVar, |
| 290 |
const VarDecl *EndVar, const Expr *ContainerExpr, |
| 291 |
const Expr *ArrayBoundExpr, |
| 292 |
bool ContainerNeedsDereference); |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
bool findAndVerifyUsages(const Stmt *Body); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
void addComponents(const ComponentVector &Components); |
| 310 |
|
| 311 |
|
| 312 |
const UsageResult &getUsages() const { return Usages; } |
| 313 |
|
| 314 |
|
| 315 |
void addUsage(const Usage &U); |
| 316 |
|
| 317 |
|
| 318 |
const Expr *getContainerIndexed() const { return ContainerExpr; } |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
const DeclStmt *getAliasDecl() const { return AliasDecl; } |
| 323 |
|
| 324 |
|
| 325 |
Confidence::Level getConfidenceLevel() const { |
| 326 |
return ConfidenceLevel.getLevel(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
bool aliasUseRequired() const { return ReplaceWithAliasUse; } |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
bool aliasFromForInit() const { return AliasFromForInit; } |
| 339 |
|
| 340 |
private: |
| 341 |
|
| 342 |
typedef RecursiveASTVisitor<ForLoopIndexUseVisitor> VisitorBase; |
| 343 |
friend class RecursiveASTVisitor<ForLoopIndexUseVisitor>; |
| 344 |
|
| 345 |
|
| 346 |
bool TraverseArraySubscriptExpr(ArraySubscriptExpr *E); |
| 347 |
bool TraverseCXXMemberCallExpr(CXXMemberCallExpr *MemberCall); |
| 348 |
bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *OpCall); |
| 349 |
bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C, |
| 350 |
Expr *Init); |
| 351 |
bool TraverseMemberExpr(MemberExpr *Member); |
| 352 |
bool TraverseUnaryOperator(UnaryOperator *Uop); |
| 353 |
bool VisitDeclRefExpr(DeclRefExpr *E); |
| 354 |
bool VisitDeclStmt(DeclStmt *S); |
| 355 |
bool TraverseStmt(Stmt *S); |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
void addComponent(const Expr *E); |
| 360 |
|
| 361 |
|
| 362 |
ASTContext *Context; |
| 363 |
|
| 364 |
const VarDecl *IndexVar; |
| 365 |
|
| 366 |
const VarDecl *EndVar; |
| 367 |
|
| 368 |
const Expr *ContainerExpr; |
| 369 |
|
| 370 |
const Expr *ArrayBoundExpr; |
| 371 |
bool ContainerNeedsDereference; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
UsageResult Usages; |
| 377 |
llvm::SmallSet<SourceLocation, 8> UsageLocations; |
| 378 |
bool OnlyUsedAsIndex; |
| 379 |
|
| 380 |
const DeclStmt *AliasDecl; |
| 381 |
Confidence ConfidenceLevel; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
llvm::SmallVector<std::pair<const Expr *, llvm::FoldingSetNodeID>, 16> |
| 387 |
DependentExprs; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
const Stmt *NextStmtParent; |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
const Stmt *CurrStmtParent; |
| 396 |
|
| 397 |
|
| 398 |
bool ReplaceWithAliasUse; |
| 399 |
|
| 400 |
bool AliasFromForInit; |
| 401 |
}; |
| 402 |
|
| 403 |
struct TUTrackingInfo { |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
TUTrackingInfo() : ParentFinder(new StmtAncestorASTVisitor) {} |
| 408 |
|
| 409 |
StmtAncestorASTVisitor &getParentFinder() { return *ParentFinder; } |
| 410 |
StmtGeneratedVarNameMap &getGeneratedDecls() { return GeneratedDecls; } |
| 411 |
ReplacedVarsMap &getReplacedVars() { return ReplacedVars; } |
| 412 |
|
| 413 |
private: |
| 414 |
std::unique_ptr<StmtAncestorASTVisitor> ParentFinder; |
| 415 |
StmtGeneratedVarNameMap GeneratedDecls; |
| 416 |
ReplacedVarsMap ReplacedVars; |
| 417 |
}; |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
class VariableNamer { |
| 426 |
public: |
| 427 |
|
| 428 |
enum NamingStyle { |
| 429 |
NS_CamelBack, |
| 430 |
NS_CamelCase, |
| 431 |
NS_LowerCase, |
| 432 |
NS_UpperCase, |
| 433 |
}; |
| 434 |
|
| 435 |
VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls, |
| 436 |
const StmtParentMap *ReverseAST, const clang::Stmt *SourceStmt, |
| 437 |
const clang::VarDecl *OldIndex, |
| 438 |
const clang::ValueDecl *TheContainer, |
| 439 |
const clang::ASTContext *Context, NamingStyle Style) |
| 440 |
: GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST), |
| 441 |
SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer), |
| 442 |
Context(Context), Style(Style) {} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
std::string createIndexName(); |
| 450 |
|
| 451 |
private: |
| 452 |
StmtGeneratedVarNameMap *GeneratedDecls; |
| 453 |
const StmtParentMap *ReverseAST; |
| 454 |
const clang::Stmt *SourceStmt; |
| 455 |
const clang::VarDecl *OldIndex; |
| 456 |
const clang::ValueDecl *TheContainer; |
| 457 |
const clang::ASTContext *Context; |
| 458 |
const NamingStyle Style; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
bool declarationExists(llvm::StringRef Symbol); |
| 463 |
}; |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
#endif |