| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "clang/Analysis/AnalysisDeclContext.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclBase.h" |
| 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/LambdaCapture.h" |
| 23 | #include "clang/AST/ParentMap.h" |
| 24 | #include "clang/AST/PrettyPrinter.h" |
| 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/StmtCXX.h" |
| 27 | #include "clang/AST/StmtVisitor.h" |
| 28 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
| 29 | #include "clang/Analysis/BodyFarm.h" |
| 30 | #include "clang/Analysis/CFG.h" |
| 31 | #include "clang/Analysis/CFGStmtMap.h" |
| 32 | #include "clang/Analysis/Support/BumpVector.h" |
| 33 | #include "clang/Basic/LLVM.h" |
| 34 | #include "clang/Basic/SourceLocation.h" |
| 35 | #include "clang/Basic/SourceManager.h" |
| 36 | #include "llvm/ADT/DenseMap.h" |
| 37 | #include "llvm/ADT/FoldingSet.h" |
| 38 | #include "llvm/ADT/STLExtras.h" |
| 39 | #include "llvm/ADT/SmallPtrSet.h" |
| 40 | #include "llvm/ADT/iterator_range.h" |
| 41 | #include "llvm/Support/Allocator.h" |
| 42 | #include "llvm/Support/Casting.h" |
| 43 | #include "llvm/Support/Compiler.h" |
| 44 | #include "llvm/Support/ErrorHandling.h" |
| 45 | #include "llvm/Support/SaveAndRestore.h" |
| 46 | #include "llvm/Support/raw_ostream.h" |
| 47 | #include <cassert> |
| 48 | #include <memory> |
| 49 | |
| 50 | using namespace clang; |
| 51 | |
| 52 | using ManagedAnalysisMap = llvm::DenseMap<const void *, ManagedAnalysis *>; |
| 53 | |
| 54 | AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
| 55 | const Decl *d, |
| 56 | const CFG::BuildOptions &buildOptions) |
| 57 | : Manager(Mgr), D(d), cfgBuildOptions(buildOptions) { |
| 58 | cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs; |
| 59 | } |
| 60 | |
| 61 | AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
| 62 | const Decl *d) |
| 63 | : Manager(Mgr), D(d) { |
| 64 | cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs; |
| 65 | } |
| 66 | |
| 67 | AnalysisDeclContextManager::AnalysisDeclContextManager( |
| 68 | ASTContext &ASTCtx, bool useUnoptimizedCFG, bool addImplicitDtors, |
| 69 | bool addInitializers, bool addTemporaryDtors, bool addLifetime, |
| 70 | bool addLoopExit, bool addScopes, bool synthesizeBodies, |
| 71 | bool addStaticInitBranch, bool addCXXNewAllocator, |
| 72 | bool addRichCXXConstructors, bool markElidedCXXConstructors, |
| 73 | CodeInjector *injector) |
| 74 | : Injector(injector), FunctionBodyFarm(ASTCtx, injector), |
| 75 | SynthesizeBodies(synthesizeBodies) { |
| 76 | cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG; |
| 77 | cfgBuildOptions.AddImplicitDtors = addImplicitDtors; |
| 78 | cfgBuildOptions.AddInitializers = addInitializers; |
| 79 | cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors; |
| 80 | cfgBuildOptions.AddLifetime = addLifetime; |
| 81 | cfgBuildOptions.AddLoopExit = addLoopExit; |
| 82 | cfgBuildOptions.AddScopes = addScopes; |
| 83 | cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch; |
| 84 | cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator; |
| 85 | cfgBuildOptions.AddRichCXXConstructors = addRichCXXConstructors; |
| 86 | cfgBuildOptions.MarkElidedCXXConstructors = markElidedCXXConstructors; |
| 87 | } |
| 88 | |
| 89 | void AnalysisDeclContextManager::clear() { Contexts.clear(); } |
| 90 | |
| 91 | Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const { |
| 92 | IsAutosynthesized = false; |
| 93 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 94 | Stmt *Body = FD->getBody(); |
| 95 | if (auto *CoroBody = dyn_cast_or_null<CoroutineBodyStmt>(Body)) |
| 96 | Body = CoroBody->getBody(); |
| 97 | if (Manager && Manager->synthesizeBodies()) { |
| 98 | Stmt *SynthesizedBody = Manager->getBodyFarm().getBody(FD); |
| 99 | if (SynthesizedBody) { |
| 100 | Body = SynthesizedBody; |
| 101 | IsAutosynthesized = true; |
| 102 | } |
| 103 | } |
| 104 | return Body; |
| 105 | } |
| 106 | else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 107 | Stmt *Body = MD->getBody(); |
| 108 | if (Manager && Manager->synthesizeBodies()) { |
| 109 | Stmt *SynthesizedBody = Manager->getBodyFarm().getBody(MD); |
| 110 | if (SynthesizedBody) { |
| 111 | Body = SynthesizedBody; |
| 112 | IsAutosynthesized = true; |
| 113 | } |
| 114 | } |
| 115 | return Body; |
| 116 | } else if (const auto *BD = dyn_cast<BlockDecl>(D)) |
| 117 | return BD->getBody(); |
| 118 | else if (const auto *FunTmpl = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 119 | return FunTmpl->getTemplatedDecl()->getBody(); |
| 120 | |
| 121 | llvm_unreachable("unknown code decl"); |
| 122 | } |
| 123 | |
| 124 | Stmt *AnalysisDeclContext::getBody() const { |
| 125 | bool Tmp; |
| 126 | return getBody(Tmp); |
| 127 | } |
| 128 | |
| 129 | bool AnalysisDeclContext::isBodyAutosynthesized() const { |
| 130 | bool Tmp; |
| 131 | getBody(Tmp); |
| 132 | return Tmp; |
| 133 | } |
| 134 | |
| 135 | bool AnalysisDeclContext::isBodyAutosynthesizedFromModelFile() const { |
| 136 | bool Tmp; |
| 137 | Stmt *Body = getBody(Tmp); |
| 138 | return Tmp && Body->getBeginLoc().isValid(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | static bool isSelfDecl(const VarDecl *VD) { |
| 143 | return isa<ImplicitParamDecl>(VD) && VD->getName() == "self"; |
| 144 | } |
| 145 | |
| 146 | const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const { |
| 147 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 148 | return MD->getSelfDecl(); |
| 149 | if (const auto *BD = dyn_cast<BlockDecl>(D)) { |
| 150 | |
| 151 | for (const auto &I : BD->captures()) { |
| 152 | const VarDecl *VD = I.getVariable(); |
| 153 | if (isSelfDecl(VD)) |
| 154 | return dyn_cast<ImplicitParamDecl>(VD); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | auto *CXXMethod = dyn_cast<CXXMethodDecl>(D); |
| 159 | if (!CXXMethod) |
| 160 | return nullptr; |
| 161 | |
| 162 | const CXXRecordDecl *parent = CXXMethod->getParent(); |
| 163 | if (!parent->isLambda()) |
| 164 | return nullptr; |
| 165 | |
| 166 | for (const auto &LC : parent->captures()) { |
| 167 | if (!LC.capturesVariable()) |
| 168 | continue; |
| 169 | |
| 170 | VarDecl *VD = LC.getCapturedVar(); |
| 171 | if (isSelfDecl(VD)) |
| 172 | return dyn_cast<ImplicitParamDecl>(VD); |
| 173 | } |
| 174 | |
| 175 | return nullptr; |
| 176 | } |
| 177 | |
| 178 | void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) { |
| 179 | if (!forcedBlkExprs) |
| 180 | forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs(); |
| 181 | |
| 182 | if (const auto *e = dyn_cast<Expr>(stmt)) |
| 183 | stmt = e->IgnoreParens(); |
| 184 | (void) (*forcedBlkExprs)[stmt]; |
| 185 | } |
| 186 | |
| 187 | const CFGBlock * |
| 188 | AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) { |
| 189 | assert(forcedBlkExprs); |
| 190 | if (const auto *e = dyn_cast<Expr>(stmt)) |
| 191 | stmt = e->IgnoreParens(); |
| 192 | CFG::BuildOptions::ForcedBlkExprs::const_iterator itr = |
| 193 | forcedBlkExprs->find(stmt); |
| 194 | end()", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/AnalysisDeclContext.cpp", 194, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(itr != forcedBlkExprs->end()); |
| 195 | return itr->second; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) { |
| 201 | if (!TheCFG) |
| 202 | return; |
| 203 | |
| 204 | for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(), |
| 205 | E = TheCFG->synthetic_stmt_end(); |
| 206 | I != E; ++I) { |
| 207 | PM.setParent(I->first, PM.getParent(I->second)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | CFG *AnalysisDeclContext::getCFG() { |
| 212 | if (!cfgBuildOptions.PruneTriviallyFalseEdges) |
| 213 | return getUnoptimizedCFG(); |
| 214 | |
| 215 | if (!builtCFG) { |
| 216 | cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions); |
| 217 | |
| 218 | |
| 219 | builtCFG = true; |
| 220 | |
| 221 | if (PM) |
| 222 | addParentsForSyntheticStmts(cfg.get(), *PM); |
| 223 | |
| 224 | |
| 225 | getCFGBuildOptions().Observer = nullptr; |
| 226 | } |
| 227 | return cfg.get(); |
| 228 | } |
| 229 | |
| 230 | CFG *AnalysisDeclContext::getUnoptimizedCFG() { |
| 231 | if (!builtCompleteCFG) { |
| 232 | SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges, |
| 233 | false); |
| 234 | completeCFG = |
| 235 | CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions); |
| 236 | |
| 237 | |
| 238 | builtCompleteCFG = true; |
| 239 | |
| 240 | if (PM) |
| 241 | addParentsForSyntheticStmts(completeCFG.get(), *PM); |
| 242 | |
| 243 | |
| 244 | getCFGBuildOptions().Observer = nullptr; |
| 245 | } |
| 246 | return completeCFG.get(); |
| 247 | } |
| 248 | |
| 249 | CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() { |
| 250 | if (cfgStmtMap) |
| 251 | return cfgStmtMap.get(); |
| 252 | |
| 253 | if (CFG *c = getCFG()) { |
| 254 | cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap())); |
| 255 | return cfgStmtMap.get(); |
| 256 | } |
| 257 | |
| 258 | return nullptr; |
| 259 | } |
| 260 | |
| 261 | CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() { |
| 262 | if (CFA) |
| 263 | return CFA.get(); |
| 264 | |
| 265 | if (CFG *c = getCFG()) { |
| 266 | CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c)); |
| 267 | return CFA.get(); |
| 268 | } |
| 269 | |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
| 273 | void AnalysisDeclContext::dumpCFG(bool ShowColors) { |
| 274 | getCFG()->dump(getASTContext().getLangOpts(), ShowColors); |
| 275 | } |
| 276 | |
| 277 | ParentMap &AnalysisDeclContext::getParentMap() { |
| 278 | if (!PM) { |
| 279 | PM.reset(new ParentMap(getBody())); |
| 280 | if (const auto *C = dyn_cast<CXXConstructorDecl>(getDecl())) { |
| 281 | for (const auto *I : C->inits()) { |
| 282 | PM->addStmt(I->getInit()); |
| 283 | } |
| 284 | } |
| 285 | if (builtCFG) |
| 286 | addParentsForSyntheticStmts(getCFG(), *PM); |
| 287 | if (builtCompleteCFG) |
| 288 | addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM); |
| 289 | } |
| 290 | return *PM; |
| 291 | } |
| 292 | |
| 293 | AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) { |
| 294 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 295 | |
| 296 | |
| 297 | FD->hasBody(FD); |
| 298 | D = FD; |
| 299 | } |
| 300 | |
| 301 | std::unique_ptr<AnalysisDeclContext> &AC = Contexts[D]; |
| 302 | if (!AC) |
| 303 | AC = llvm::make_unique<AnalysisDeclContext>(this, D, cfgBuildOptions); |
| 304 | return AC.get(); |
| 305 | } |
| 306 | |
| 307 | BodyFarm &AnalysisDeclContextManager::getBodyFarm() { return FunctionBodyFarm; } |
| 308 | |
| 309 | const StackFrameContext * |
| 310 | AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S, |
| 311 | const CFGBlock *Blk, unsigned Idx) { |
| 312 | return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx); |
| 313 | } |
| 314 | |
| 315 | const BlockInvocationContext * |
| 316 | AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent, |
| 317 | const BlockDecl *BD, |
| 318 | const void *ContextData) { |
| 319 | return getLocationContextManager().getBlockInvocationContext(this, parent, |
| 320 | BD, ContextData); |
| 321 | } |
| 322 | |
| 323 | bool AnalysisDeclContext::isInStdNamespace(const Decl *D) { |
| 324 | const DeclContext *DC = D->getDeclContext()->getEnclosingNamespaceContext(); |
| 325 | const auto *ND = dyn_cast<NamespaceDecl>(DC); |
| 326 | if (!ND) |
| 327 | return false; |
| 328 | |
| 329 | while (const DeclContext *Parent = ND->getParent()) { |
| 330 | if (!isa<NamespaceDecl>(Parent)) |
| 331 | break; |
| 332 | ND = cast<NamespaceDecl>(Parent); |
| 333 | } |
| 334 | |
| 335 | return ND->isStdNamespace(); |
| 336 | } |
| 337 | |
| 338 | LocationContextManager &AnalysisDeclContext::getLocationContextManager() { |
| 339 | (0) . __assert_fail ("Manager && \"Cannot create LocationContexts without an AnalysisDeclContextManager!\"", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/AnalysisDeclContext.cpp", 340, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Manager && |
| 340 | (0) . __assert_fail ("Manager && \"Cannot create LocationContexts without an AnalysisDeclContextManager!\"", "/home/seafit/code_projects/clang_source/clang/lib/Analysis/AnalysisDeclContext.cpp", 340, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Cannot create LocationContexts without an AnalysisDeclContextManager!"); |
| 341 | return Manager->getLocationContextManager(); |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID, |
| 349 | ContextKind ck, |
| 350 | AnalysisDeclContext *ctx, |
| 351 | const LocationContext *parent, |
| 352 | const void *data) { |
| 353 | ID.AddInteger(ck); |
| 354 | ID.AddPointer(ctx); |
| 355 | ID.AddPointer(parent); |
| 356 | ID.AddPointer(data); |
| 357 | } |
| 358 | |
| 359 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) { |
| 360 | Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index); |
| 361 | } |
| 362 | |
| 363 | void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) { |
| 364 | Profile(ID, getAnalysisDeclContext(), getParent(), Enter); |
| 365 | } |
| 366 | |
| 367 | void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) { |
| 368 | Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData); |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | template <typename LOC, typename DATA> |
| 376 | const LOC* |
| 377 | LocationContextManager::getLocationContext(AnalysisDeclContext *ctx, |
| 378 | const LocationContext *parent, |
| 379 | const DATA *d) { |
| 380 | llvm::FoldingSetNodeID ID; |
| 381 | LOC::Profile(ID, ctx, parent, d); |
| 382 | void *InsertPos; |
| 383 | |
| 384 | LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 385 | |
| 386 | if (!L) { |
| 387 | L = new LOC(ctx, parent, d, ++NewID); |
| 388 | Contexts.InsertNode(L, InsertPos); |
| 389 | } |
| 390 | return L; |
| 391 | } |
| 392 | |
| 393 | const StackFrameContext* |
| 394 | LocationContextManager::getStackFrame(AnalysisDeclContext *ctx, |
| 395 | const LocationContext *parent, |
| 396 | const Stmt *s, |
| 397 | const CFGBlock *blk, unsigned idx) { |
| 398 | llvm::FoldingSetNodeID ID; |
| 399 | StackFrameContext::Profile(ID, ctx, parent, s, blk, idx); |
| 400 | void *InsertPos; |
| 401 | auto *L = |
| 402 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 403 | if (!L) { |
| 404 | L = new StackFrameContext(ctx, parent, s, blk, idx, ++NewID); |
| 405 | Contexts.InsertNode(L, InsertPos); |
| 406 | } |
| 407 | return L; |
| 408 | } |
| 409 | |
| 410 | const ScopeContext * |
| 411 | LocationContextManager::getScope(AnalysisDeclContext *ctx, |
| 412 | const LocationContext *parent, |
| 413 | const Stmt *s) { |
| 414 | return getLocationContext<ScopeContext, Stmt>(ctx, parent, s); |
| 415 | } |
| 416 | |
| 417 | const BlockInvocationContext * |
| 418 | LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx, |
| 419 | const LocationContext *parent, |
| 420 | const BlockDecl *BD, |
| 421 | const void *ContextData) { |
| 422 | llvm::FoldingSetNodeID ID; |
| 423 | BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData); |
| 424 | void *InsertPos; |
| 425 | auto *L = |
| 426 | cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID, |
| 427 | InsertPos)); |
| 428 | if (!L) { |
| 429 | L = new BlockInvocationContext(ctx, parent, BD, ContextData, ++NewID); |
| 430 | Contexts.InsertNode(L, InsertPos); |
| 431 | } |
| 432 | return L; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | const StackFrameContext *LocationContext::getStackFrame() const { |
| 440 | const LocationContext *LC = this; |
| 441 | while (LC) { |
| 442 | if (const auto *SFC = dyn_cast<StackFrameContext>(LC)) |
| 443 | return SFC; |
| 444 | LC = LC->getParent(); |
| 445 | } |
| 446 | return nullptr; |
| 447 | } |
| 448 | |
| 449 | bool LocationContext::inTopFrame() const { |
| 450 | return getStackFrame()->inTopFrame(); |
| 451 | } |
| 452 | |
| 453 | bool LocationContext::isParentOf(const LocationContext *LC) const { |
| 454 | do { |
| 455 | const LocationContext *Parent = LC->getParent(); |
| 456 | if (Parent == this) |
| 457 | return true; |
| 458 | else |
| 459 | LC = Parent; |
| 460 | } while (LC); |
| 461 | |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | static void printLocation(raw_ostream &OS, const SourceManager &SM, |
| 466 | SourceLocation SLoc) { |
| 467 | if (SLoc.isFileID() && SM.isInMainFile(SLoc)) |
| 468 | OS << "line " << SM.getExpansionLineNumber(SLoc); |
| 469 | else |
| 470 | SLoc.print(OS, SM); |
| 471 | } |
| 472 | |
| 473 | void LocationContext::dumpStack( |
| 474 | raw_ostream &OS, StringRef Indent, const char *NL, const char *Sep, |
| 475 | std::function<void(const LocationContext *)> printMoreInfoPerContext) const { |
| 476 | ASTContext &Ctx = getAnalysisDeclContext()->getASTContext(); |
| 477 | PrintingPolicy PP(Ctx.getLangOpts()); |
| 478 | PP.TerseOutput = 1; |
| 479 | |
| 480 | const SourceManager &SM = |
| 481 | getAnalysisDeclContext()->getASTContext().getSourceManager(); |
| 482 | |
| 483 | unsigned Frame = 0; |
| 484 | for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) { |
| 485 | switch (LCtx->getKind()) { |
| 486 | case StackFrame: |
| 487 | OS << Indent << '#' << Frame << ' '; |
| 488 | ++Frame; |
| 489 | if (const auto *D = dyn_cast<NamedDecl>(LCtx->getDecl())) |
| 490 | OS << "Calling " << D->getQualifiedNameAsString(); |
| 491 | else |
| 492 | OS << "Calling anonymous code"; |
| 493 | if (const Stmt *S = cast<StackFrameContext>(LCtx)->getCallSite()) { |
| 494 | OS << " at "; |
| 495 | printLocation(OS, SM, S->getBeginLoc()); |
| 496 | } |
| 497 | break; |
| 498 | case Scope: |
| 499 | OS << "Entering scope"; |
| 500 | break; |
| 501 | case Block: |
| 502 | OS << "Invoking block"; |
| 503 | if (const Decl *D = cast<BlockInvocationContext>(LCtx)->getDecl()) { |
| 504 | OS << " defined at "; |
| 505 | printLocation(OS, SM, D->getBeginLoc()); |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | OS << NL; |
| 510 | |
| 511 | printMoreInfoPerContext(LCtx); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | LLVM_DUMP_METHOD void LocationContext::dumpStack() const { |
| 516 | dumpStack(llvm::errs()); |
| 517 | } |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | namespace { |
| 524 | |
| 525 | class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{ |
| 526 | BumpVector<const VarDecl *> &BEVals; |
| 527 | BumpVectorContext &BC; |
| 528 | llvm::SmallPtrSet<const VarDecl *, 4> Visited; |
| 529 | llvm::SmallPtrSet<const DeclContext *, 4> IgnoredContexts; |
| 530 | |
| 531 | public: |
| 532 | FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals, |
| 533 | BumpVectorContext &bc) |
| 534 | : BEVals(bevals), BC(bc) {} |
| 535 | |
| 536 | void VisitStmt(Stmt *S) { |
| 537 | for (auto *Child : S->children()) |
| 538 | if (Child) |
| 539 | Visit(Child); |
| 540 | } |
| 541 | |
| 542 | void VisitDeclRefExpr(DeclRefExpr *DR) { |
| 543 | |
| 544 | if (const auto *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 545 | if (!VD->hasLocalStorage()) { |
| 546 | if (Visited.insert(VD).second) |
| 547 | BEVals.push_back(VD, BC); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void VisitBlockExpr(BlockExpr *BR) { |
| 553 | |
| 554 | IgnoredContexts.insert(BR->getBlockDecl()); |
| 555 | Visit(BR->getBlockDecl()->getBody()); |
| 556 | } |
| 557 | |
| 558 | void VisitPseudoObjectExpr(PseudoObjectExpr *PE) { |
| 559 | for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(), |
| 560 | et = PE->semantics_end(); it != et; ++it) { |
| 561 | Expr *Semantic = *it; |
| 562 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(Semantic)) |
| 563 | Semantic = OVE->getSourceExpr(); |
| 564 | Visit(Semantic); |
| 565 | } |
| 566 | } |
| 567 | }; |
| 568 | |
| 569 | } |
| 570 | |
| 571 | using DeclVec = BumpVector<const VarDecl *>; |
| 572 | |
| 573 | static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD, |
| 574 | void *&Vec, |
| 575 | llvm::BumpPtrAllocator &A) { |
| 576 | if (Vec) |
| 577 | return (DeclVec*) Vec; |
| 578 | |
| 579 | BumpVectorContext BC(A); |
| 580 | DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>(); |
| 581 | new (BV) DeclVec(BC, 10); |
| 582 | |
| 583 | |
| 584 | for (const auto &CI : BD->captures()) { |
| 585 | BV->push_back(CI.getVariable(), BC); |
| 586 | } |
| 587 | |
| 588 | |
| 589 | FindBlockDeclRefExprsVals F(*BV, BC); |
| 590 | F.Visit(BD->getBody()); |
| 591 | |
| 592 | Vec = BV; |
| 593 | return BV; |
| 594 | } |
| 595 | |
| 596 | llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator> |
| 597 | AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) { |
| 598 | if (!ReferencedBlockVars) |
| 599 | ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>(); |
| 600 | |
| 601 | const DeclVec *V = |
| 602 | LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A); |
| 603 | return llvm::make_range(V->begin(), V->end()); |
| 604 | } |
| 605 | |
| 606 | ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) { |
| 607 | if (!ManagedAnalyses) |
| 608 | ManagedAnalyses = new ManagedAnalysisMap(); |
| 609 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; |
| 610 | return (*M)[tag]; |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | ManagedAnalysis::~ManagedAnalysis() = default; |
| 618 | |
| 619 | AnalysisDeclContext::~AnalysisDeclContext() { |
| 620 | delete forcedBlkExprs; |
| 621 | delete ReferencedBlockVars; |
| 622 | |
| 623 | if (ManagedAnalyses) { |
| 624 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; |
| 625 | llvm::DeleteContainerSeconds(*M); |
| 626 | delete M; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | LocationContext::~LocationContext() = default; |
| 631 | |
| 632 | LocationContextManager::~LocationContextManager() { |
| 633 | clear(); |
| 634 | } |
| 635 | |
| 636 | void LocationContextManager::clear() { |
| 637 | for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(), |
| 638 | E = Contexts.end(); I != E; ) { |
| 639 | LocationContext *LC = &*I; |
| 640 | ++I; |
| 641 | delete LC; |
| 642 | } |
| 643 | Contexts.clear(); |
| 644 | } |
| 645 | |