Coverage Report

Created: 2022-01-18 06:27

/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
68
buildStmtToBasicBlockMap(const CFG &Cfg) {
29
68
  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock;
30
268
  for (const CFGBlock *Block : Cfg) {
31
268
    if (Block == nullptr)
32
0
      continue;
33
34
470
    
for (const CFGElement &Element : *Block)268
{
35
470
      auto Stmt = Element.getAs<CFGStmt>();
36
470
      if (!Stmt.hasValue())
37
13
        continue;
38
39
457
      StmtToBlock[Stmt.getValue().getStmt()] = Block;
40
457
    }
41
268
  }
42
68
  return StmtToBlock;
43
68
}
44
45
llvm::Expected<ControlFlowContext>
46
68
ControlFlowContext::build(const Decl *D, Stmt *S, ASTContext *C) {
47
68
  CFG::BuildOptions Options;
48
68
  Options.PruneTriviallyFalseEdges = false;
49
68
  Options.AddImplicitDtors = true;
50
68
  Options.AddTemporaryDtors = true;
51
68
  Options.AddInitializers = true;
52
68
  Options.AddCXXDefaultInitExprInCtors = true;
53
54
  // Ensure that all sub-expressions in basic blocks are evaluated.
55
68
  Options.setAllAlwaysAdd();
56
57
68
  auto Cfg = CFG::buildCFG(D, S, C, Options);
58
68
  if (Cfg == nullptr)
59
0
    return llvm::createStringError(
60
0
        std::make_error_code(std::errc::invalid_argument),
61
0
        "CFG::buildCFG failed");
62
63
68
  llvm::DenseMap<const Stmt *, const CFGBlock *> StmtToBlock =
64
68
      buildStmtToBasicBlockMap(*Cfg);
65
68
  return ControlFlowContext(std::move(Cfg), std::move(StmtToBlock));
66
68
}
67
68
} // namespace dataflow
69
} // namespace clang