Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
5.51k
static SMap *AsMap(void *m) { return (SMap*) m; }
23
24
869
CFGStmtMap::~CFGStmtMap() { delete AsMap(M); }
25
26
4.65k
CFGBlock *CFGStmtMap::getBlock(Stmt *S) {
27
4.65k
  SMap *SM = AsMap(M);
28
4.65k
  Stmt *X = S;
29
4.65k
30
4.65k
  // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors
31
4.65k
  // is in the map.
32
4.94k
  while (X) {
33
4.65k
    SMap::iterator I = SM->find(X);
34
4.65k
    if (I != SM->end()) {
35
4.36k
      CFGBlock *B = I->second;
36
4.36k
      // Memoize this lookup.
37
4.36k
      if (X != S)
38
0
        (*SM)[X] = B;
39
4.36k
      return B;
40
4.36k
    }
41
290
42
290
    X = PM->getParentIgnoreParens(X);
43
290
  }
44
4.65k
45
4.65k
  
return nullptr290
;
46
4.65k
}
47
48
3.38k
static void Accumulate(SMap &SM, CFGBlock *B) {
49
3.38k
  // First walk the block-level expressions.
50
25.6k
  for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; 
++I22.2k
) {
51
22.2k
    const CFGElement &CE = *I;
52
22.2k
    Optional<CFGStmt> CS = CE.getAs<CFGStmt>();
53
22.2k
    if (!CS)
54
135
      continue;
55
22.1k
56
22.1k
    CFGBlock *&Entry = SM[CS->getStmt()];
57
22.1k
    // If 'Entry' is already initialized (e.g., a terminator was already),
58
22.1k
    // skip.
59
22.1k
    if (Entry)
60
0
      continue;
61
22.1k
62
22.1k
    Entry = B;
63
22.1k
64
22.1k
  }
65
3.38k
66
3.38k
  // Look at the label of the block.
67
3.38k
  if (Stmt *Label = B->getLabel())
68
7
    SM[Label] = B;
69
3.38k
70
3.38k
  // Finally, look at the terminator.  If the terminator was already added
71
3.38k
  // because it is a block-level expression in another block, overwrite
72
3.38k
  // that mapping.
73
3.38k
  if (Stmt *Term = B->getTerminatorStmt())
74
331
    SM[Term] = B;
75
3.38k
}
76
77
869
CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) {
78
869
  if (!C || !PM)
79
0
    return nullptr;
80
869
81
869
  SMap *SM = new SMap();
82
869
83
869
  // Walk all blocks, accumulating the block-level expressions, labels,
84
869
  // and terminators.
85
4.25k
  for (CFG::iterator I = C->begin(), E = C->end(); I != E; 
++I3.38k
)
86
3.38k
    Accumulate(*SM, *I);
87
869
88
869
  return new CFGStmtMap(PM, SM);
89
869
}
90