xref: /openbsd-src/gnu/llvm/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1*12c85518Srobert //===- ControlFlowContext.cpp ---------------------------------------------===//
2*12c85518Srobert //
3*12c85518Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*12c85518Srobert // See https://llvm.org/LICENSE.txt for license information.
5*12c85518Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*12c85518Srobert //
7*12c85518Srobert //===----------------------------------------------------------------------===//
8*12c85518Srobert //
9*12c85518Srobert //  This file defines a ControlFlowContext class that is used by dataflow
10*12c85518Srobert //  analyses that run over Control-Flow Graphs (CFGs).
11*12c85518Srobert //
12*12c85518Srobert //===----------------------------------------------------------------------===//
13*12c85518Srobert 
14*12c85518Srobert #include "clang/Analysis/FlowSensitive/ControlFlowContext.h"
15*12c85518Srobert #include "clang/AST/ASTContext.h"
16*12c85518Srobert #include "clang/AST/Decl.h"
17*12c85518Srobert #include "clang/AST/Stmt.h"
18*12c85518Srobert #include "clang/Analysis/CFG.h"
19*12c85518Srobert #include "llvm/ADT/DenseMap.h"
20*12c85518Srobert #include "llvm/Support/Error.h"
21*12c85518Srobert #include <utility>
22*12c85518Srobert 
23*12c85518Srobert namespace clang {
24*12c85518Srobert namespace dataflow {
25*12c85518Srobert 
26*12c85518Srobert /// Returns a map from statements to basic blocks that contain them.
27*12c85518Srobert static llvm::DenseMap<const Stmt *, const CFGBlock *>
buildStmtToBasicBlockMap(const CFG & Cfg)28*12c85518Srobert buildStmtToBasicBlockMap(const CFG &Cfg) {
29*12c85518Srobert   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
30*12c85518Srobert   for (const CFGBlock *Block : Cfg) {
31*12c85518Srobert     if (Block == nullptr)
32*12c85518Srobert       continue;
33*12c85518Srobert 
34*12c85518Srobert     for (const CFGElement &Element : *Block) {
35*12c85518Srobert       auto Stmt = Element.getAs<CFGStmt>();
36*12c85518Srobert       if (!Stmt)
37*12c85518Srobert         continue;
38*12c85518Srobert 
39*12c85518Srobert       StmtToBlock[Stmt->getStmt()] = Block;
40*12c85518Srobert     }
41*12c85518Srobert     if (const Stmt *TerminatorStmt = Block->getTerminatorStmt())
42*12c85518Srobert       StmtToBlock[TerminatorStmt] = Block;
43*12c85518Srobert   }
44*12c85518Srobert   return StmtToBlock;
45*12c85518Srobert }
46*12c85518Srobert 
47*12c85518Srobert llvm::Expected<ControlFlowContext>
build(const Decl * D,Stmt & S,ASTContext & C)48*12c85518Srobert ControlFlowContext::build(const Decl *D, Stmt &S, ASTContext &C) {
49*12c85518Srobert   CFG::BuildOptions Options;
50*12c85518Srobert   Options.PruneTriviallyFalseEdges = false;
51*12c85518Srobert   Options.AddImplicitDtors = true;
52*12c85518Srobert   Options.AddTemporaryDtors = true;
53*12c85518Srobert   Options.AddInitializers = true;
54*12c85518Srobert   Options.AddCXXDefaultInitExprInCtors = true;
55*12c85518Srobert 
56*12c85518Srobert   // Ensure that all sub-expressions in basic blocks are evaluated.
57*12c85518Srobert   Options.setAllAlwaysAdd();
58*12c85518Srobert 
59*12c85518Srobert   auto Cfg = CFG::buildCFG(D, &S, &C, Options);
60*12c85518Srobert   if (Cfg == nullptr)
61*12c85518Srobert     return llvm::createStringError(
62*12c85518Srobert         std::make_error_code(std::errc::invalid_argument),
63*12c85518Srobert         "CFG::buildCFG failed");
64*12c85518Srobert 
65*12c85518Srobert   llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock =
66*12c85518Srobert       buildStmtToBasicBlockMap(*Cfg);
67*12c85518Srobert   return ControlFlowContext(D, std::move(Cfg), std::move(StmtToBlock));
68*12c85518Srobert }
69*12c85518Srobert 
70*12c85518Srobert } // namespace dataflow
71*12c85518Srobert } // namespace clang
72