| 1 |
//===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This file defines the CFGStmtMap class, which defines a mapping from |
| 10 |
// Stmt* to CFGBlock* |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "clang/AST/ParentMap.h" |
| 15 |
#include "clang/Analysis/CFG.h" |
| 16 |
#include "clang/Analysis/CFGStmtMap.h" |
| 17 |
#include <optional> |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
const CFGBlock *CFGStmtMap::getBlock(const Stmt *S) const { |
| 22 |
const Stmt *X = S; |
| 23 |
|
| 24 |
// If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors |
| 25 |
// is in the map. |
| 26 |
while (X) { |
| 27 |
auto I = M.find(X); |
| 28 |
if (I != M.end()) |
| 29 |
return I->second; |
| 30 |
X = PM->getParentIgnoreParens(X); |
| 31 |
} |
| 32 |
|
| 33 |
return nullptr; |
| 34 |
} |
| 35 |
|
| 36 |
CFGStmtMap::CFGStmtMap(const CFG &C, const ParentMap &PM) : PM(&PM) { |
| 37 |
// Walk all blocks, accumulating the block-level expressions, labels, |
| 38 |
// and terminators. |
| 39 |
for (const CFGBlock *B : C) { |
| 40 |
// First walk the block-level expressions. |
| 41 |
for (const CFGElement &CE : *B) { |
| 42 |
if (std::optional<CFGStmt> CS = CE.getAs<CFGStmt>()) |
| 43 |
M.try_emplace(CS->getStmt(), B); |
| 44 |
} |
| 45 |
|
| 46 |
// Look at the label of the block. |
| 47 |
if (const Stmt *Label = B->getLabel()) |
| 48 |
M[Label] = B; |
| 49 |
|
| 50 |
// Finally, look at the terminator. If the terminator was already added |
| 51 |
// because it is a block-level expression in another block, overwrite |
| 52 |
// that mapping. |
| 53 |
if (const Stmt *Term = B->getTerminatorStmt()) |
| 54 |
M[Term] = B; |
| 55 |
} |
| 56 |
} |