Coverage Report

Created: 2020-02-18 08:44

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