Path: blob/main/contrib/llvm-project/clang/lib/Analysis/CFGStmtMap.cpp
35233 views
//===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the CFGStmtMap class, which defines a mapping from9// Stmt* to CFGBlock*10//11//===----------------------------------------------------------------------===//1213#include "llvm/ADT/DenseMap.h"14#include "clang/AST/ParentMap.h"15#include "clang/Analysis/CFG.h"16#include "clang/Analysis/CFGStmtMap.h"17#include <optional>1819using namespace clang;2021typedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap;22static SMap *AsMap(void *m) { return (SMap*) m; }2324CFGStmtMap::~CFGStmtMap() { delete AsMap(M); }2526CFGBlock *CFGStmtMap::getBlock(Stmt *S) {27SMap *SM = AsMap(M);28Stmt *X = S;2930// If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors31// is in the map.32while (X) {33SMap::iterator I = SM->find(X);34if (I != SM->end()) {35CFGBlock *B = I->second;36// Memoize this lookup.37if (X != S)38(*SM)[X] = B;39return B;40}4142X = PM->getParentIgnoreParens(X);43}4445return nullptr;46}4748static void Accumulate(SMap &SM, CFGBlock *B) {49// First walk the block-level expressions.50for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) {51const CFGElement &CE = *I;52std::optional<CFGStmt> CS = CE.getAs<CFGStmt>();53if (!CS)54continue;5556CFGBlock *&Entry = SM[CS->getStmt()];57// If 'Entry' is already initialized (e.g., a terminator was already),58// skip.59if (Entry)60continue;6162Entry = B;6364}6566// Look at the label of the block.67if (Stmt *Label = B->getLabel())68SM[Label] = B;6970// Finally, look at the terminator. If the terminator was already added71// because it is a block-level expression in another block, overwrite72// that mapping.73if (Stmt *Term = B->getTerminatorStmt())74SM[Term] = B;75}7677CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) {78if (!C || !PM)79return nullptr;8081SMap *SM = new SMap();8283// Walk all blocks, accumulating the block-level expressions, labels,84// and terminators.85for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I)86Accumulate(*SM, *I);8788return new CFGStmtMap(PM, SM);89}90919293