clang  16.0.0git
ControlFlowContext.cpp
Go to the documentation of this file.
1 //===- ControlFlowContext.cpp ---------------------------------------------===//
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 a ControlFlowContext class that is used by dataflow
10 // analyses that run over Control-Flow Graphs (CFGs).
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/Stmt.h"
18 #include "clang/Analysis/CFG.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Error.h"
21 #include <utility>
22 
23 namespace clang {
24 namespace dataflow {
25 
26 /// Returns a map from statements to basic blocks that contain them.
27 static llvm::DenseMap<const Stmt *, const CFGBlock *>
29  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
30  for (const CFGBlock *Block : Cfg) {
31  if (Block == nullptr)
32  continue;
33 
34  for (const CFGElement &Element : *Block) {
35  auto Stmt = Element.getAs<CFGStmt>();
36  if (!Stmt)
37  continue;
38 
39  StmtToBlock[Stmt.value().getStmt()] = Block;
40  }
41  if (const Stmt *TerminatorStmt = Block->getTerminatorStmt())
42  StmtToBlock[TerminatorStmt] = Block;
43  }
44  return StmtToBlock;
45 }
46 
49  CFG::BuildOptions Options;
50  Options.PruneTriviallyFalseEdges = false;
51  Options.AddImplicitDtors = true;
52  Options.AddTemporaryDtors = true;
53  Options.AddInitializers = true;
54  Options.AddCXXDefaultInitExprInCtors = true;
55 
56  // Ensure that all sub-expressions in basic blocks are evaluated.
57  Options.setAllAlwaysAdd();
58 
59  auto Cfg = CFG::buildCFG(D, &S, &C, Options);
60  if (Cfg == nullptr)
61  return llvm::createStringError(
62  std::make_error_code(std::errc::invalid_argument),
63  "CFG::buildCFG failed");
64 
65  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock =
67  return ControlFlowContext(D, std::move(Cfg), std::move(StmtToBlock));
68 }
69 
72  assert(S != nullptr);
73  assert(C != nullptr);
74  return build(D, *S, *C);
75 }
76 
77 } // namespace dataflow
78 } // namespace clang
llvm::Expected
Definition: LLVM.h:41
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition: CFG.h:1226
Decl.h
clang::CFGBlock
Represents a single basic block in a source-level CFG.
Definition: CFG.h:577
clang::CFG::BuildOptions
Definition: CFG.h:1232
clang::CFG::buildCFG
static std::unique_ptr< CFG > buildCFG(const Decl *D, Stmt *AST, ASTContext *C, const BuildOptions &BO)
Builds a CFG from an AST.
Definition: CFG.cpp:5232
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::dataflow::buildStmtToBasicBlockMap
static llvm::DenseMap< const Stmt *, const CFGBlock * > buildStmtToBasicBlockMap(const CFG &Cfg)
Returns a map from statements to basic blocks that contain them.
Definition: ControlFlowContext.cpp:28
clang::dataflow::ControlFlowContext::build
static llvm::Expected< ControlFlowContext > build(const Decl *D, Stmt &S, ASTContext &C)
Builds a ControlFlowContext from an AST node.
Definition: ControlFlowContext.cpp:48
ASTContext.h
clang::CFGStmt
Definition: CFG.h:132
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
clang::dataflow::ControlFlowContext
Holds CFG and other derived context that is needed to perform dataflow analysis.
Definition: ControlFlowContext.h:31
clang::CFGElement
Represents a top-level expression in a basic block.
Definition: CFG.h:55
ControlFlowContext.h
clang
Definition: CalledOnceCheck.h:17
CFG.h
clang::DeclaratorContext::Block
@ Block
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:70
Stmt.h
clang::format::make_error_code
std::error_code make_error_code(ParseError e)
Definition: Format.cpp:1010