aboutsummaryrefslogtreecommitdiff
path: root/include/clang/StaticAnalyzer/Core/PathSensitive/FunctionSummary.h
blob: cf4a6929a3aa71d240daeeef2eb0b32091a989b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//== FunctionSummary.h - Stores summaries of functions. ------------*- C++ -*-//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines a summary of a function gathered/used by static analyzes.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_GR_FUNCTIONSUMMARY_H
#define LLVM_CLANG_GR_FUNCTIONSUMMARY_H

#include <deque>
#include "clang/AST/Decl.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/BitVector.h"

namespace clang {
namespace ento {
typedef std::deque<Decl*> SetOfDecls;
typedef llvm::DenseSet<const Decl*> SetOfConstDecls;

class FunctionSummariesTy {
  struct FunctionSummary {
    /// True if this function has reached a max block count while inlined from
    /// at least one call site.
    bool MayReachMaxBlockCount;

    /// Total number of blocks in the function.
    unsigned TotalBasicBlocks;

    /// Marks the IDs of the basic blocks visited during the analyzes.
    llvm::BitVector VisitedBasicBlocks;

    FunctionSummary() :
      MayReachMaxBlockCount(false),
      TotalBasicBlocks(0),
      VisitedBasicBlocks(0) {}
  };

  typedef llvm::DenseMap<const Decl*, FunctionSummary*> MapTy;
  MapTy Map;

public:
  ~FunctionSummariesTy();

  MapTy::iterator findOrInsertSummary(const Decl *D) {
    MapTy::iterator I = Map.find(D);
    if (I != Map.end())
      return I;
    FunctionSummary *DS = new FunctionSummary();
    I = Map.insert(std::pair<const Decl*, FunctionSummary*>(D, DS)).first;
    assert(I != Map.end());
    return I;
  }

  void markReachedMaxBlockCount(const Decl* D) {
    MapTy::iterator I = findOrInsertSummary(D);
    I->second->MayReachMaxBlockCount = true;
  }

  bool hasReachedMaxBlockCount(const Decl* D) {
  MapTy::const_iterator I = Map.find(D);
    if (I != Map.end())
      return I->second->MayReachMaxBlockCount;
    return false;
  }

  void markVisitedBasicBlock(unsigned ID, const Decl* D, unsigned TotalIDs) {
    MapTy::iterator I = findOrInsertSummary(D);
    llvm::BitVector &Blocks = I->second->VisitedBasicBlocks;
    assert(ID < TotalIDs);
    if (TotalIDs > Blocks.size()) {
      Blocks.resize(TotalIDs);
      I->second->TotalBasicBlocks = TotalIDs;
    }
    Blocks[ID] = true;
  }

  unsigned getNumVisitedBasicBlocks(const Decl* D) {
    MapTy::const_iterator I = Map.find(D);
      if (I != Map.end())
        return I->second->VisitedBasicBlocks.count();
    return 0;
  }

  /// Get the percentage of the reachable blocks.
  unsigned getPercentBlocksReachable(const Decl *D) {
    MapTy::const_iterator I = Map.find(D);
      if (I != Map.end())
        return ((I->second->VisitedBasicBlocks.count() * 100) /
                 I->second->TotalBasicBlocks);
    return 0;
  }

  unsigned getTotalNumBasicBlocks();
  unsigned getTotalNumVisitedBasicBlocks();

};

}} // end clang ento namespaces

#endif