Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/CFGStmtMap.cpp
Line
Count
Source (jump to first uncovered line)
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 "llvm/ADT/DenseMap.h"
15
#include "clang/AST/ParentMap.h"
16
#include "clang/Analysis/CFG.h"
17
#include "clang/Analysis/CFGStmtMap.h"
18
19
using namespace clang;
20
21
typedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap;
22
170k
static SMap *AsMap(void *m) { return (SMap*) m; }
23
24
3.84k
CFGStmtMap::~CFGStmtMap() { delete AsMap(M); }
25
26
166k
CFGBlock *CFGStmtMap::getBlock(Stmt *S) {
27
166k
  SMap *SM = AsMap(M);
28
166k
  Stmt *X = S;
29
30
  // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors
31
  // is in the map.
32
166k
  while (X) {
33
166k
    SMap::iterator I = SM->find(X);
34
166k
    if (I != SM->end()) {
35
166k
      CFGBlock *B = I->second;
36
      // Memoize this lookup.
37
166k
      if (X != S)
38
0
        (*SM)[X] = B;
39
166k
      return B;
40
166k
    }
41
42
292
    X = PM->getParentIgnoreParens(X);
43
292
  }
44
45
292
  return nullptr;
46
166k
}
47
48
15.8k
static void Accumulate(SMap &SM, CFGBlock *B) {
49
  // First walk the block-level expressions.
50
108k
  for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; 
++I92.4k
) {
51
92.4k
    const CFGElement &CE = *I;
52
92.4k
    Optional<CFGStmt> CS = CE.getAs<CFGStmt>();
53
92.4k
    if (!CS)
54
852
      continue;
55
56
91.5k
    CFGBlock *&Entry = SM[CS->getStmt()];
57
    // If 'Entry' is already initialized (e.g., a terminator was already),
58
    // skip.
59
91.5k
    if (Entry)
60
261
      continue;
61
62
91.3k
    Entry = B;
63
64
91.3k
  }
65
66
  // Look at the label of the block.
67
15.8k
  if (Stmt *Label = B->getLabel())
68
114
    SM[Label] = B;
69
70
  // Finally, look at the terminator.  If the terminator was already added
71
  // because it is a block-level expression in another block, overwrite
72
  // that mapping.
73
15.8k
  if (Stmt *Term = B->getTerminatorStmt())
74
2.26k
    SM[Term] = B;
75
15.8k
}
76
77
3.84k
CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) {
78
3.84k
  if (!C || !PM)
79
0
    return nullptr;
80
81
3.84k
  SMap *SM = new SMap();
82
83
  // Walk all blocks, accumulating the block-level expressions, labels,
84
  // and terminators.
85
19.7k
  for (CFG::iterator I = C->begin(), E = C->end(); I != E; 
++I15.8k
)
86
15.8k
    Accumulate(*SM, *I);
87
88
3.84k
  return new CFGStmtMap(PM, SM);
89
3.84k
}
90