clang 22.0.0git
CFGStmtMap.cpp
Go to the documentation of this file.
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"
17#include <optional>
18
19using namespace clang;
20
21const 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
36CFGStmtMap::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}
#define X(type, name)
Definition Value.h:97
Represents a single basic block in a source-level CFG.
Definition CFG.h:605
Represents a top-level expression in a basic block.
Definition CFG.h:55
const CFGBlock * getBlock(const Stmt *S) const
Returns the CFGBlock the specified Stmt* appears in.
CFGStmtMap(const CFG &C, const ParentMap &PM)
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1218
Stmt - This represents one statement.
Definition Stmt.h:85
The JSON file list parser is used to communicate input to InstallAPI.