Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/ProgramPoint.cpp
Line
Count
Source (jump to first uncovered line)
1
//==- ProgramPoint.cpp - Program Points for Path-Sensitive Analysis -*- 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 interface ProgramPoint, which identifies a
10
//  distinct location in a function.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Analysis/ProgramPoint.h"
15
#include "clang/Basic/JsonSupport.h"
16
17
using namespace clang;
18
19
25.0k
ProgramPointTag::~ProgramPointTag() {}
20
21
ProgramPoint ProgramPoint::getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
22
                                           const LocationContext *LC,
23
4.56M
                                           const ProgramPointTag *tag){
24
4.56M
  switch (K) {
25
0
    default:
26
0
      llvm_unreachable("Unhandled ProgramPoint kind");
27
557k
    case ProgramPoint::PreStmtKind:
28
557k
      return PreStmt(S, LC, tag);
29
1.28M
    case ProgramPoint::PostStmtKind:
30
1.28M
      return PostStmt(S, LC, tag);
31
427k
    case ProgramPoint::PreLoadKind:
32
427k
      return PreLoad(S, LC, tag);
33
179k
    case ProgramPoint::PostLoadKind:
34
179k
      return PostLoad(S, LC, tag);
35
76.2k
    case ProgramPoint::PreStoreKind:
36
76.2k
      return PreStore(S, LC, tag);
37
321k
    case ProgramPoint::PostLValueKind:
38
321k
      return PostLValue(S, LC, tag);
39
303k
    case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
40
303k
      return PostStmtPurgeDeadSymbols(S, LC, tag);
41
1.41M
    case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
42
1.41M
      return PreStmtPurgeDeadSymbols(S, LC, tag);
43
4.56M
  }
44
4.56M
}
45
46
0
LLVM_DUMP_METHOD void ProgramPoint::dump() const {
47
0
  return printJson(llvm::errs());
48
0
}
49
50
177
void ProgramPoint::printJson(llvm::raw_ostream &Out, const char *NL) const {
51
177
  const ASTContext &Context =
52
177
      getLocationContext()->getAnalysisDeclContext()->getASTContext();
53
177
  const SourceManager &SM = Context.getSourceManager();
54
177
  const PrintingPolicy &PP = Context.getPrintingPolicy();
55
177
  const bool AddQuotes = true;
56
177
57
177
  Out << "\"kind\": \"";
58
177
  switch (getKind()) {
59
11
  case ProgramPoint::BlockEntranceKind:
60
11
    Out << "BlockEntrance\""
61
11
        << ", \"block_id\": "
62
11
        << castAs<BlockEntrance>().getBlock()->getBlockID();
63
11
    break;
64
0
65
0
  case ProgramPoint::FunctionExitKind: {
66
0
    auto FEP = getAs<FunctionExitPoint>();
67
0
    Out << "FunctionExit\""
68
0
        << ", \"block_id\": " << FEP->getBlock()->getBlockID()
69
0
        << ", \"stmt_id\": ";
70
0
71
0
    if (const ReturnStmt *RS = FEP->getStmt()) {
72
0
      Out << RS->getID(Context) << ", \"stmt\": ";
73
0
      RS->printJson(Out, nullptr, PP, AddQuotes);
74
0
    } else {
75
0
      Out << "null, \"stmt\": null";
76
0
    }
77
0
    break;
78
0
  }
79
0
  case ProgramPoint::BlockExitKind:
80
0
    llvm_unreachable("BlockExitKind");
81
0
    break;
82
8
  case ProgramPoint::CallEnterKind:
83
8
    Out << "CallEnter\"";
84
8
    break;
85
8
  case ProgramPoint::CallExitBeginKind:
86
8
    Out << "CallExitBegin\"";
87
8
    break;
88
8
  case ProgramPoint::CallExitEndKind:
89
8
    Out << "CallExitEnd\"";
90
8
    break;
91
0
  case ProgramPoint::EpsilonKind:
92
0
    Out << "EpsilonPoint\"";
93
0
    break;
94
0
95
0
  case ProgramPoint::LoopExitKind:
96
0
    Out << "LoopExit\", \"stmt\": \""
97
0
        << castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
98
0
    break;
99
0
100
0
  case ProgramPoint::PreImplicitCallKind: {
101
0
    ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
102
0
    Out << "PreCall\", \"decl\": \""
103
0
        << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
104
0
        << "\", \"location\": ";
105
0
    printSourceLocationAsJson(Out, PC.getLocation(), SM);
106
0
    break;
107
0
  }
108
0
109
1
  case ProgramPoint::PostImplicitCallKind: {
110
1
    ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
111
1
    Out << "PostCall\", \"decl\": \""
112
1
        << PC.getDecl()->getAsFunction()->getQualifiedNameAsString()
113
1
        << "\", \"location\": ";
114
1
    printSourceLocationAsJson(Out, PC.getLocation(), SM);
115
1
    break;
116
0
  }
117
0
118
2
  case ProgramPoint::PostInitializerKind: {
119
2
    Out << "PostInitializer\", ";
120
2
    const CXXCtorInitializer *Init = castAs<PostInitializer>().getInitializer();
121
2
    if (const FieldDecl *FD = Init->getAnyMember()) {
122
2
      Out << "\"field_decl\": \"" << *FD << '\"';
123
2
    } else {
124
0
      Out << "\"type\": \"";
125
0
      QualType Ty = Init->getTypeSourceInfo()->getType();
126
0
      Ty = Ty.getLocalUnqualifiedType();
127
0
      Ty.print(Out, Context.getLangOpts());
128
0
      Out << '\"';
129
0
    }
130
2
    break;
131
0
  }
132
0
133
25
  case ProgramPoint::BlockEdgeKind: {
134
25
    const BlockEdge &E = castAs<BlockEdge>();
135
25
    const Stmt *T = E.getSrc()->getTerminatorStmt();
136
25
    Out << "Edge\", \"src_id\": " << E.getSrc()->getBlockID()
137
25
        << ", \"dst_id\": " << E.getDst()->getBlockID() << ", \"terminator\": ";
138
25
139
25
    if (!T) {
140
25
      Out << "null, \"term_kind\": null";
141
25
      break;
142
25
    }
143
0
144
0
    E.getSrc()->printTerminatorJson(Out, Context.getLangOpts(),
145
0
                                    /*AddQuotes=*/true);
146
0
    Out << ", \"location\": ";
147
0
    printSourceLocationAsJson(Out, T->getBeginLoc(), SM);
148
0
149
0
    Out << ", \"term_kind\": \"";
150
0
    if (isa<SwitchStmt>(T)) {
151
0
      Out << "SwitchStmt\", \"case\": ";
152
0
      if (const Stmt *Label = E.getDst()->getLabel()) {
153
0
        if (const auto *C = dyn_cast<CaseStmt>(Label)) {
154
0
          Out << "{ \"lhs\": ";
155
0
          if (const Stmt *LHS = C->getLHS()) {
156
0
            LHS->printJson(Out, nullptr, PP, AddQuotes);
157
0
          } else {
158
0
            Out << "null";
159
0
    }
160
0
161
0
          Out << ", \"rhs\": ";
162
0
          if (const Stmt *RHS = C->getRHS()) {
163
0
            RHS->printJson(Out, nullptr, PP, AddQuotes);
164
0
          } else {
165
0
            Out << "null";
166
0
          }
167
0
          Out << " }";
168
0
        } else {
169
0
          assert(isa<DefaultStmt>(Label));
170
0
          Out << "\"default\"";
171
0
        }
172
0
      } else {
173
0
        Out << "\"implicit default\"";
174
0
      }
175
0
    } else if (isa<IndirectGotoStmt>(T)) {
176
0
      // FIXME: More info.
177
0
      Out << "IndirectGotoStmt\"";
178
0
    } else {
179
0
      Out << "Condition\", \"value\": "
180
0
          << (*E.getSrc()->succ_begin() == E.getDst() ? "true" : "false");
181
0
    }
182
0
    break;
183
0
  }
184
0
185
114
  default: {
186
114
    const Stmt *S = castAs<StmtPoint>().getStmt();
187
114
    assert(S != nullptr && "Expecting non-null Stmt");
188
114
189
114
    Out << "Statement\", \"stmt_kind\": \"" << S->getStmtClassName()
190
114
        << "\", \"stmt_id\": " << S->getID(Context)
191
114
        << ", \"pointer\": \"" << (const void *)S << "\", ";
192
114
    if (const auto *CS = dyn_cast<CastExpr>(S))
193
15
      Out << "\"cast_kind\": \"" << CS->getCastKindName() << "\", ";
194
114
195
114
    Out << "\"pretty\": ";
196
114
197
114
    S->printJson(Out, nullptr, PP, AddQuotes);
198
114
199
114
    Out << ", \"location\": ";
200
114
    printSourceLocationAsJson(Out, S->getBeginLoc(), SM);
201
114
202
114
    Out << ", \"stmt_point_kind\": \"";
203
114
    if (getAs<PreLoad>())
204
2
      Out << "PreLoad";
205
112
    else if (getAs<PreStore>())
206
0
      Out << "PreStore";
207
112
    else if (getAs<PostAllocatorCall>())
208
0
      Out << "PostAllocatorCall";
209
112
    else if (getAs<PostCondition>())
210
0
      Out << "PostCondition";
211
112
    else if (getAs<PostLoad>())
212
2
      Out << "PostLoad";
213
110
    else if (getAs<PostLValue>())
214
2
      Out << "PostLValue";
215
108
    else if (getAs<PostStore>())
216
10
      Out << "PostStore";
217
98
    else if (getAs<PostStmt>())
218
50
      Out << "PostStmt";
219
48
    else if (getAs<PostStmtPurgeDeadSymbols>())
220
10
      Out << "PostStmtPurgeDeadSymbols";
221
38
    else if (getAs<PreStmtPurgeDeadSymbols>())
222
26
      Out << "PreStmtPurgeDeadSymbols";
223
12
    else if (getAs<PreStmt>())
224
12
      Out << "PreStmt";
225
0
    else {
226
0
      Out << "\nKind: '" << getKind();
227
0
      llvm_unreachable("' is unhandled StmtPoint kind!");
228
0
    }
229
114
230
114
    Out << '\"';
231
114
    break;
232
114
  }
233
177
  }
234
177
}
235
236
SimpleProgramPointTag::SimpleProgramPointTag(StringRef MsgProvider,
237
                                             StringRef Msg)
238
5.11k
  : Desc((MsgProvider + " : " + Msg).str()) {}
239
240
658
StringRef SimpleProgramPointTag::getTagDescription() const {
241
658
  return Desc;
242
658
}