Coverage Report

Created: 2022-05-17 06:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/FlowSensitive/ControlFlowContext.cpp
Line
Count
Source (jump to first uncovered line)
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
14
#include "clang/Analysis/FlowSensitive/ControlFlowContext.h"
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 *>
28
296
buildStmtToBasicBlockMap(const CFG &Cfg) {
29
296
  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
30
1.10k
  for (const CFGBlock *Block : Cfg) {
31
1.10k
    if (Block == nullptr)
32
0
      continue;
33
34
2.90k
    
for (const CFGElement &Element : *Block)1.10k
{
35
2.90k
      auto Stmt = Element.getAs<CFGStmt>();
36
2.90k
      if (!Stmt.hasValue())
37
23
        continue;
38
39
2.88k
      StmtToBlock[Stmt.getValue().getStmt()] = Block;
40
2.88k
    }
41
1.10k
    if (const Stmt *TerminatorStmt = Block->getTerminatorStmt())
42
107
      StmtToBlock[TerminatorStmt] = Block;
43
1.10k
  }
44
296
  return StmtToBlock;
45
296
}
46
47
llvm::Expected<ControlFlowContext>
48
296
ControlFlowContext::build(const Decl *D, Stmt *S, ASTContext *C) {
49
296
  CFG::BuildOptions Options;
50
296
  Options.PruneTriviallyFalseEdges = false;
51
296
  Options.AddImplicitDtors = true;
52
296
  Options.AddTemporaryDtors = true;
53
296
  Options.AddInitializers = true;
54
296
  Options.AddCXXDefaultInitExprInCtors = true;
55
56
  // Ensure that all sub-expressions in basic blocks are evaluated.
57
296
  Options.setAllAlwaysAdd();
58
59
296
  auto Cfg = CFG::buildCFG(D, S, C, Options);
60
296
  if (Cfg == nullptr)
61
0
    return llvm::createStringError(
62
0
        std::make_error_code(std::errc::invalid_argument),
63
0
        "CFG::buildCFG failed");
64
65
296
  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock =
66
296
      buildStmtToBasicBlockMap(*Cfg);
67
296
  return ControlFlowContext(std::move(Cfg), std::move(StmtToBlock));
68
296
}
69
70
} // namespace dataflow
71
} // namespace clang