1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
15 | #include "clang/AST/Decl.h" |
16 | #include "clang/AST/DeclBase.h" |
17 | #include "clang/AST/DeclObjC.h" |
18 | #include "clang/AST/Expr.h" |
19 | #include "clang/AST/ExprCXX.h" |
20 | #include "clang/AST/ParentMap.h" |
21 | #include "clang/AST/Stmt.h" |
22 | #include "clang/AST/StmtCXX.h" |
23 | #include "clang/AST/StmtObjC.h" |
24 | #include "clang/Analysis/AnalysisDeclContext.h" |
25 | #include "clang/Analysis/CFG.h" |
26 | #include "clang/Analysis/CFGStmtMap.h" |
27 | #include "clang/Analysis/ProgramPoint.h" |
28 | #include "clang/Basic/LLVM.h" |
29 | #include "clang/Basic/SourceLocation.h" |
30 | #include "clang/Basic/SourceManager.h" |
31 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
32 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" |
33 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
34 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
35 | #include "clang/StaticAnalyzer/Core/Checker.h" |
36 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
37 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
38 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
39 | #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" |
40 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
41 | #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" |
42 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
43 | #include "llvm/ADT/ArrayRef.h" |
44 | #include "llvm/ADT/DenseMap.h" |
45 | #include "llvm/ADT/DenseSet.h" |
46 | #include "llvm/ADT/FoldingSet.h" |
47 | #include "llvm/ADT/None.h" |
48 | #include "llvm/ADT/Optional.h" |
49 | #include "llvm/ADT/STLExtras.h" |
50 | #include "llvm/ADT/SmallPtrSet.h" |
51 | #include "llvm/ADT/SmallString.h" |
52 | #include "llvm/ADT/SmallVector.h" |
53 | #include "llvm/ADT/Statistic.h" |
54 | #include "llvm/ADT/StringRef.h" |
55 | #include "llvm/ADT/iterator_range.h" |
56 | #include "llvm/Support/Casting.h" |
57 | #include "llvm/Support/Compiler.h" |
58 | #include "llvm/Support/ErrorHandling.h" |
59 | #include "llvm/Support/MemoryBuffer.h" |
60 | #include "llvm/Support/raw_ostream.h" |
61 | #include <algorithm> |
62 | #include <cassert> |
63 | #include <cstddef> |
64 | #include <iterator> |
65 | #include <memory> |
66 | #include <queue> |
67 | #include <string> |
68 | #include <tuple> |
69 | #include <utility> |
70 | #include <vector> |
71 | |
72 | using namespace clang; |
73 | using namespace ento; |
74 | |
75 | #define DEBUG_TYPE "BugReporter" |
76 | |
77 | STATISTIC(MaxBugClassSize, |
78 | "The maximum number of bug reports in the same equivalence class"); |
79 | STATISTIC(MaxValidBugClassSize, |
80 | "The maximum number of bug reports in the same equivalence class " |
81 | "where at least one report is valid (not suppressed)"); |
82 | |
83 | BugReporterVisitor::~BugReporterVisitor() = default; |
84 | |
85 | void BugReporterContext::anchor() {} |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | static const Stmt *GetPreviousStmt(const ExplodedNode *N) { |
92 | for (N = N->getFirstPred(); N; N = N->getFirstPred()) |
93 | if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) |
94 | return S; |
95 | |
96 | return nullptr; |
97 | } |
98 | |
99 | static inline const Stmt* |
100 | GetCurrentOrPreviousStmt(const ExplodedNode *N) { |
101 | if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) |
102 | return S; |
103 | |
104 | return GetPreviousStmt(N); |
105 | } |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | static PathDiagnosticEventPiece * |
112 | eventsDescribeSameCondition(PathDiagnosticEventPiece *X, |
113 | PathDiagnosticEventPiece *Y) { |
114 | |
115 | |
116 | |
117 | |
118 | const void *tagPreferred = ConditionBRVisitor::getTag(); |
119 | const void *tagLesser = TrackConstraintBRVisitor::getTag(); |
120 | |
121 | if (X->getLocation() != Y->getLocation()) |
122 | return nullptr; |
123 | |
124 | if (X->getTag() == tagPreferred && Y->getTag() == tagLesser) |
125 | return ConditionBRVisitor::isPieceMessageGeneric(X) ? Y : X; |
126 | |
127 | if (Y->getTag() == tagPreferred && X->getTag() == tagLesser) |
128 | return ConditionBRVisitor::isPieceMessageGeneric(Y) ? X : Y; |
129 | |
130 | return nullptr; |
131 | } |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | static void removeRedundantMsgs(PathPieces &path) { |
139 | unsigned N = path.size(); |
140 | if (N < 2) |
141 | return; |
142 | |
143 | |
144 | |
145 | |
146 | for (unsigned i = 0; i < N; ++i) { |
147 | auto piece = std::move(path.front()); |
148 | path.pop_front(); |
149 | |
150 | switch (piece->getKind()) { |
151 | case PathDiagnosticPiece::Call: |
152 | removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path); |
153 | break; |
154 | case PathDiagnosticPiece::Macro: |
155 | removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces); |
156 | break; |
157 | case PathDiagnosticPiece::ControlFlow: |
158 | break; |
159 | case PathDiagnosticPiece::Event: { |
160 | if (i == N-1) |
161 | break; |
162 | |
163 | if (auto *nextEvent = |
164 | dyn_cast<PathDiagnosticEventPiece>(path.front().get())) { |
165 | auto *event = cast<PathDiagnosticEventPiece>(piece.get()); |
166 | |
167 | |
168 | |
169 | if (auto *pieceToKeep = |
170 | eventsDescribeSameCondition(event, nextEvent)) { |
171 | piece = std::move(pieceToKeep == event ? piece : path.front()); |
172 | path.pop_front(); |
173 | ++i; |
174 | } |
175 | } |
176 | break; |
177 | } |
178 | case PathDiagnosticPiece::Note: |
179 | break; |
180 | } |
181 | path.push_back(std::move(piece)); |
182 | } |
183 | } |
184 | |
185 | |
186 | |
187 | using LocationContextMap = |
188 | llvm::DenseMap<const PathPieces *, const LocationContext *>; |
189 | |
190 | |
191 | |
192 | |
193 | static bool removeUnneededCalls(PathPieces &pieces, BugReport *R, |
194 | LocationContextMap &LCM, |
195 | bool IsInteresting = false) { |
196 | bool containsSomethingInteresting = IsInteresting; |
197 | const unsigned N = pieces.size(); |
198 | |
199 | for (unsigned i = 0 ; i < N ; ++i) { |
200 | |
201 | |
202 | auto piece = std::move(pieces.front()); |
203 | pieces.pop_front(); |
204 | |
205 | switch (piece->getKind()) { |
206 | case PathDiagnosticPiece::Call: { |
207 | auto &call = cast<PathDiagnosticCallPiece>(*piece); |
208 | |
209 | assert(LCM.count(&call.path)); |
210 | if (!removeUnneededCalls(call.path, R, LCM, |
211 | R->isInteresting(LCM[&call.path]))) |
212 | continue; |
213 | |
214 | containsSomethingInteresting = true; |
215 | break; |
216 | } |
217 | case PathDiagnosticPiece::Macro: { |
218 | auto ¯o = cast<PathDiagnosticMacroPiece>(*piece); |
219 | if (!removeUnneededCalls(macro.subPieces, R, LCM, IsInteresting)) |
220 | continue; |
221 | containsSomethingInteresting = true; |
222 | break; |
223 | } |
224 | case PathDiagnosticPiece::Event: { |
225 | auto &event = cast<PathDiagnosticEventPiece>(*piece); |
226 | |
227 | |
228 | |
229 | containsSomethingInteresting |= !event.isPrunable(); |
230 | break; |
231 | } |
232 | case PathDiagnosticPiece::ControlFlow: |
233 | break; |
234 | |
235 | case PathDiagnosticPiece::Note: |
236 | break; |
237 | } |
238 | |
239 | pieces.push_back(std::move(piece)); |
240 | } |
241 | |
242 | return containsSomethingInteresting; |
243 | } |
244 | |
245 | |
246 | |
247 | static bool hasImplicitBody(const Decl *D) { |
248 | assert(D); |
249 | return D->isImplicit() || !D->hasBody(); |
250 | } |
251 | |
252 | |
253 | |
254 | static void |
255 | adjustCallLocations(PathPieces &Pieces, |
256 | PathDiagnosticLocation *LastCallLocation = nullptr) { |
257 | for (const auto &I : Pieces) { |
258 | auto *Call = dyn_cast<PathDiagnosticCallPiece>(I.get()); |
259 | |
260 | if (!Call) |
261 | continue; |
262 | |
263 | if (LastCallLocation) { |
264 | bool CallerIsImplicit = hasImplicitBody(Call->getCaller()); |
265 | if (CallerIsImplicit || !Call->callEnter.asLocation().isValid()) |
266 | Call->callEnter = *LastCallLocation; |
267 | if (CallerIsImplicit || !Call->callReturn.asLocation().isValid()) |
268 | Call->callReturn = *LastCallLocation; |
269 | } |
270 | |
271 | |
272 | |
273 | PathDiagnosticLocation *ThisCallLocation; |
274 | if (Call->callEnterWithin.asLocation().isValid() && |
275 | !hasImplicitBody(Call->getCallee())) |
276 | ThisCallLocation = &Call->callEnterWithin; |
277 | else |
278 | ThisCallLocation = &Call->callEnter; |
279 | |
280 | (0) . __assert_fail ("ThisCallLocation && \"Outermost call has an invalid location\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 280, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(ThisCallLocation && "Outermost call has an invalid location"); |
281 | adjustCallLocations(Call->path, ThisCallLocation); |
282 | } |
283 | } |
284 | |
285 | |
286 | |
287 | |
288 | static void removeEdgesToDefaultInitializers(PathPieces &Pieces) { |
289 | for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) { |
290 | if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get())) |
291 | removeEdgesToDefaultInitializers(C->path); |
292 | |
293 | if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get())) |
294 | removeEdgesToDefaultInitializers(M->subPieces); |
295 | |
296 | if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) { |
297 | const Stmt *Start = CF->getStartLocation().asStmt(); |
298 | const Stmt *End = CF->getEndLocation().asStmt(); |
299 | if (Start && isa<CXXDefaultInitExpr>(Start)) { |
300 | I = Pieces.erase(I); |
301 | continue; |
302 | } else if (End && isa<CXXDefaultInitExpr>(End)) { |
303 | PathPieces::iterator Next = std::next(I); |
304 | if (Next != E) { |
305 | if (auto *NextCF = |
306 | dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) { |
307 | NextCF->setStartLocation(CF->getStartLocation()); |
308 | } |
309 | } |
310 | I = Pieces.erase(I); |
311 | continue; |
312 | } |
313 | } |
314 | |
315 | I++; |
316 | } |
317 | } |
318 | |
319 | |
320 | |
321 | |
322 | static void removePiecesWithInvalidLocations(PathPieces &Pieces) { |
323 | for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) { |
324 | if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get())) |
325 | removePiecesWithInvalidLocations(C->path); |
326 | |
327 | if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get())) |
328 | removePiecesWithInvalidLocations(M->subPieces); |
329 | |
330 | if (!(*I)->getLocation().isValid() || |
331 | !(*I)->getLocation().asLocation().isValid()) { |
332 | I = Pieces.erase(I); |
333 | continue; |
334 | } |
335 | I++; |
336 | } |
337 | } |
338 | |
339 | |
340 | |
341 | |
342 | |
343 | namespace { |
344 | |
345 | class PathDiagnosticBuilder : public BugReporterContext { |
346 | BugReport *R; |
347 | PathDiagnosticConsumer *PDC; |
348 | |
349 | public: |
350 | const LocationContext *LC; |
351 | |
352 | PathDiagnosticBuilder(GRBugReporter &br, |
353 | BugReport *r, InterExplodedGraphMap &Backmap, |
354 | PathDiagnosticConsumer *pdc) |
355 | : BugReporterContext(br, Backmap), R(r), PDC(pdc), |
356 | LC(r->getErrorNode()->getLocationContext()) {} |
357 | |
358 | PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N); |
359 | |
360 | PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os, |
361 | const ExplodedNode *N); |
362 | |
363 | BugReport *getBugReport() { return R; } |
364 | |
365 | Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); } |
366 | |
367 | ParentMap& getParentMap() { return LC->getParentMap(); } |
368 | |
369 | const Stmt *getParent(const Stmt *S) { |
370 | return getParentMap().getParent(S); |
371 | } |
372 | |
373 | PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); |
374 | |
375 | PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const { |
376 | return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Minimal; |
377 | } |
378 | |
379 | bool supportsLogicalOpControlFlow() const { |
380 | return PDC ? PDC->supportsLogicalOpControlFlow() : true; |
381 | } |
382 | }; |
383 | |
384 | } |
385 | |
386 | PathDiagnosticLocation |
387 | PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) { |
388 | if (const Stmt *S = PathDiagnosticLocation::getNextStmt(N)) |
389 | return PathDiagnosticLocation(S, getSourceManager(), LC); |
390 | |
391 | return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(), |
392 | getSourceManager()); |
393 | } |
394 | |
395 | PathDiagnosticLocation |
396 | PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os, |
397 | const ExplodedNode *N) { |
398 | |
399 | if (os.str().empty()) |
400 | os << ' '; |
401 | |
402 | const PathDiagnosticLocation &Loc = ExecutionContinues(N); |
403 | |
404 | if (Loc.asStmt()) |
405 | os << "Execution continues on line " |
406 | << getSourceManager().getExpansionLineNumber(Loc.asLocation()) |
407 | << '.'; |
408 | else { |
409 | os << "Execution jumps to the end of the "; |
410 | const Decl *D = N->getLocationContext()->getDecl(); |
411 | if (isa<ObjCMethodDecl>(D)) |
412 | os << "method"; |
413 | else if (isa<FunctionDecl>(D)) |
414 | os << "function"; |
415 | else { |
416 | (D)", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 416, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(isa<BlockDecl>(D)); |
417 | os << "anonymous block"; |
418 | } |
419 | os << '.'; |
420 | } |
421 | |
422 | return Loc; |
423 | } |
424 | |
425 | static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) { |
426 | if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S))) |
427 | return PM.getParentIgnoreParens(S); |
428 | |
429 | const Stmt *Parent = PM.getParentIgnoreParens(S); |
430 | if (!Parent) |
431 | return nullptr; |
432 | |
433 | switch (Parent->getStmtClass()) { |
434 | case Stmt::ForStmtClass: |
435 | case Stmt::DoStmtClass: |
436 | case Stmt::WhileStmtClass: |
437 | case Stmt::ObjCForCollectionStmtClass: |
438 | case Stmt::CXXForRangeStmtClass: |
439 | return Parent; |
440 | default: |
441 | break; |
442 | } |
443 | |
444 | return nullptr; |
445 | } |
446 | |
447 | static PathDiagnosticLocation |
448 | getEnclosingStmtLocation(const Stmt *S, SourceManager &SMgr, const ParentMap &P, |
449 | const LocationContext *LC, bool allowNestedContexts) { |
450 | if (!S) |
451 | return {}; |
452 | |
453 | while (const Stmt *Parent = getEnclosingParent(S, P)) { |
454 | switch (Parent->getStmtClass()) { |
455 | case Stmt::BinaryOperatorClass: { |
456 | const auto *B = cast<BinaryOperator>(Parent); |
457 | if (B->isLogicalOp()) |
458 | return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC); |
459 | break; |
460 | } |
461 | case Stmt::CompoundStmtClass: |
462 | case Stmt::StmtExprClass: |
463 | return PathDiagnosticLocation(S, SMgr, LC); |
464 | case Stmt::ChooseExprClass: |
465 | |
466 | |
467 | if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S) |
468 | return PathDiagnosticLocation(Parent, SMgr, LC); |
469 | else |
470 | return PathDiagnosticLocation(S, SMgr, LC); |
471 | case Stmt::BinaryConditionalOperatorClass: |
472 | case Stmt::ConditionalOperatorClass: |
473 | |
474 | |
475 | if (allowNestedContexts || |
476 | cast<AbstractConditionalOperator>(Parent)->getCond() == S) |
477 | return PathDiagnosticLocation(Parent, SMgr, LC); |
478 | else |
479 | return PathDiagnosticLocation(S, SMgr, LC); |
480 | case Stmt::CXXForRangeStmtClass: |
481 | if (cast<CXXForRangeStmt>(Parent)->getBody() == S) |
482 | return PathDiagnosticLocation(S, SMgr, LC); |
483 | break; |
484 | case Stmt::DoStmtClass: |
485 | return PathDiagnosticLocation(S, SMgr, LC); |
486 | case Stmt::ForStmtClass: |
487 | if (cast<ForStmt>(Parent)->getBody() == S) |
488 | return PathDiagnosticLocation(S, SMgr, LC); |
489 | break; |
490 | case Stmt::IfStmtClass: |
491 | if (cast<IfStmt>(Parent)->getCond() != S) |
492 | return PathDiagnosticLocation(S, SMgr, LC); |
493 | break; |
494 | case Stmt::ObjCForCollectionStmtClass: |
495 | if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) |
496 | return PathDiagnosticLocation(S, SMgr, LC); |
497 | break; |
498 | case Stmt::WhileStmtClass: |
499 | if (cast<WhileStmt>(Parent)->getCond() != S) |
500 | return PathDiagnosticLocation(S, SMgr, LC); |
501 | break; |
502 | default: |
503 | break; |
504 | } |
505 | |
506 | S = Parent; |
507 | } |
508 | |
509 | (0) . __assert_fail ("S && \"Cannot have null Stmt for PathDiagnosticLocation\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 509, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); |
510 | |
511 | return PathDiagnosticLocation(S, SMgr, LC); |
512 | } |
513 | |
514 | PathDiagnosticLocation |
515 | PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { |
516 | (0) . __assert_fail ("S && \"Null Stmt passed to getEnclosingStmtLocation\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 516, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(S && "Null Stmt passed to getEnclosingStmtLocation"); |
517 | return ::getEnclosingStmtLocation(S, getSourceManager(), getParentMap(), LC, |
518 | ); |
519 | } |
520 | |
521 | |
522 | |
523 | |
524 | using StackDiagPair = |
525 | std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>; |
526 | using StackDiagVector = SmallVector<StackDiagPair, 6>; |
527 | |
528 | static void updateStackPiecesWithMessage(PathDiagnosticPiece &P, |
529 | StackDiagVector &CallStack) { |
530 | |
531 | |
532 | if (auto *ep = dyn_cast<PathDiagnosticEventPiece>(&P)) { |
533 | if (ep->hasCallStackHint()) |
534 | for (const auto &I : CallStack) { |
535 | PathDiagnosticCallPiece *CP = I.first; |
536 | const ExplodedNode *N = I.second; |
537 | std::string stackMsg = ep->getCallStackMessage(N); |
538 | |
539 | |
540 | |
541 | |
542 | if (!CP->hasCallStackMessage()) |
543 | CP->setCallStackMessage(stackMsg); |
544 | } |
545 | } |
546 | } |
547 | |
548 | static void CompactMacroExpandedPieces(PathPieces &path, |
549 | const SourceManager& SM); |
550 | |
551 | |
552 | std::shared_ptr<PathDiagnosticControlFlowPiece> generateDiagForSwitchOP( |
553 | const ExplodedNode *N, |
554 | const CFGBlock *Dst, |
555 | const SourceManager &SM, |
556 | const LocationContext *LC, |
557 | PathDiagnosticBuilder &PDB, |
558 | PathDiagnosticLocation &Start |
559 | ) { |
560 | |
561 | std::string sbuf; |
562 | llvm::raw_string_ostream os(sbuf); |
563 | PathDiagnosticLocation End; |
564 | |
565 | if (const Stmt *S = Dst->getLabel()) { |
566 | End = PathDiagnosticLocation(S, SM, LC); |
567 | |
568 | switch (S->getStmtClass()) { |
569 | default: |
570 | os << "No cases match in the switch statement. " |
571 | "Control jumps to line " |
572 | << End.asLocation().getExpansionLineNumber(); |
573 | break; |
574 | case Stmt::DefaultStmtClass: |
575 | os << "Control jumps to the 'default' case at line " |
576 | << End.asLocation().getExpansionLineNumber(); |
577 | break; |
578 | |
579 | case Stmt::CaseStmtClass: { |
580 | os << "Control jumps to 'case "; |
581 | const auto *Case = cast<CaseStmt>(S); |
582 | const Expr *LHS = Case->getLHS()->IgnoreParenCasts(); |
583 | |
584 | |
585 | bool GetRawInt = true; |
586 | |
587 | if (const auto *DR = dyn_cast<DeclRefExpr>(LHS)) { |
588 | |
589 | |
590 | const auto *D = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
591 | |
592 | if (D) { |
593 | GetRawInt = false; |
594 | os << *D; |
595 | } |
596 | } |
597 | |
598 | if (GetRawInt) |
599 | os << LHS->EvaluateKnownConstInt(PDB.getASTContext()); |
600 | |
601 | os << ":' at line " << End.asLocation().getExpansionLineNumber(); |
602 | break; |
603 | } |
604 | } |
605 | } else { |
606 | os << "'Default' branch taken. "; |
607 | End = PDB.ExecutionContinues(os, N); |
608 | } |
609 | return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, |
610 | os.str()); |
611 | } |
612 | |
613 | |
614 | std::shared_ptr<PathDiagnosticControlFlowPiece> generateDiagForGotoOP( |
615 | const Stmt *S, |
616 | PathDiagnosticBuilder &PDB, |
617 | PathDiagnosticLocation &Start) { |
618 | std::string sbuf; |
619 | llvm::raw_string_ostream os(sbuf); |
620 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
621 | os << "Control jumps to line " << End.asLocation().getExpansionLineNumber(); |
622 | return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()); |
623 | |
624 | } |
625 | |
626 | std::shared_ptr<PathDiagnosticControlFlowPiece> generateDiagForBinaryOP( |
627 | const ExplodedNode *N, |
628 | const Stmt *T, |
629 | const CFGBlock *Src, |
630 | const CFGBlock *Dst, |
631 | const SourceManager &SM, |
632 | PathDiagnosticBuilder &PDB, |
633 | const LocationContext *LC) { |
634 | const auto *B = cast<BinaryOperator>(T); |
635 | std::string sbuf; |
636 | llvm::raw_string_ostream os(sbuf); |
637 | os << "Left side of '"; |
638 | PathDiagnosticLocation Start, End; |
639 | |
640 | if (B->getOpcode() == BO_LAnd) { |
641 | os << "&&" |
642 | << "' is "; |
643 | |
644 | if (*(Src->succ_begin() + 1) == Dst) { |
645 | os << "false"; |
646 | End = PathDiagnosticLocation(B->getLHS(), SM, LC); |
647 | Start = |
648 | PathDiagnosticLocation::createOperatorLoc(B, SM); |
649 | } else { |
650 | os << "true"; |
651 | Start = PathDiagnosticLocation(B->getLHS(), SM, LC); |
652 | End = PDB.ExecutionContinues(N); |
653 | } |
654 | } else { |
655 | getOpcode() == BO_LOr", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 655, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(B->getOpcode() == BO_LOr); |
656 | os << "||" |
657 | << "' is "; |
658 | |
659 | if (*(Src->succ_begin() + 1) == Dst) { |
660 | os << "false"; |
661 | Start = PathDiagnosticLocation(B->getLHS(), SM, LC); |
662 | End = PDB.ExecutionContinues(N); |
663 | } else { |
664 | os << "true"; |
665 | End = PathDiagnosticLocation(B->getLHS(), SM, LC); |
666 | Start = |
667 | PathDiagnosticLocation::createOperatorLoc(B, SM); |
668 | } |
669 | } |
670 | return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, |
671 | os.str()); |
672 | } |
673 | |
674 | void generateMinimalDiagForBlockEdge(const ExplodedNode *N, BlockEdge BE, |
675 | const SourceManager &SM, |
676 | PathDiagnosticBuilder &PDB, |
677 | PathDiagnostic &PD) { |
678 | const LocationContext *LC = N->getLocationContext(); |
679 | const CFGBlock *Src = BE.getSrc(); |
680 | const CFGBlock *Dst = BE.getDst(); |
681 | const Stmt *T = Src->getTerminator(); |
682 | if (!T) |
683 | return; |
684 | |
685 | auto Start = PathDiagnosticLocation::createBegin(T, SM, LC); |
686 | switch (T->getStmtClass()) { |
687 | default: |
688 | break; |
689 | |
690 | case Stmt::GotoStmtClass: |
691 | case Stmt::IndirectGotoStmtClass: { |
692 | if (const Stmt *S = PathDiagnosticLocation::getNextStmt(N)) |
693 | PD.getActivePath().push_front(generateDiagForGotoOP(S, PDB, Start)); |
694 | break; |
695 | } |
696 | |
697 | case Stmt::SwitchStmtClass: { |
698 | PD.getActivePath().push_front( |
699 | generateDiagForSwitchOP(N, Dst, SM, LC, PDB, Start)); |
700 | break; |
701 | } |
702 | |
703 | case Stmt::BreakStmtClass: |
704 | case Stmt::ContinueStmtClass: { |
705 | std::string sbuf; |
706 | llvm::raw_string_ostream os(sbuf); |
707 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
708 | PD.getActivePath().push_front( |
709 | std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str())); |
710 | break; |
711 | } |
712 | |
713 | |
714 | case Stmt::BinaryConditionalOperatorClass: |
715 | case Stmt::ConditionalOperatorClass: { |
716 | std::string sbuf; |
717 | llvm::raw_string_ostream os(sbuf); |
718 | os << "'?' condition is "; |
719 | |
720 | if (*(Src->succ_begin() + 1) == Dst) |
721 | os << "false"; |
722 | else |
723 | os << "true"; |
724 | |
725 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
726 | |
727 | if (const Stmt *S = End.asStmt()) |
728 | End = PDB.getEnclosingStmtLocation(S); |
729 | |
730 | PD.getActivePath().push_front( |
731 | std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str())); |
732 | break; |
733 | } |
734 | |
735 | |
736 | case Stmt::BinaryOperatorClass: { |
737 | if (!PDB.supportsLogicalOpControlFlow()) |
738 | break; |
739 | |
740 | std::shared_ptr<PathDiagnosticControlFlowPiece> Diag = |
741 | generateDiagForBinaryOP(N, T, Src, Dst, SM, PDB, LC); |
742 | PD.getActivePath().push_front(Diag); |
743 | break; |
744 | } |
745 | |
746 | case Stmt::DoStmtClass: |
747 | if (*(Src->succ_begin()) == Dst) { |
748 | std::string sbuf; |
749 | llvm::raw_string_ostream os(sbuf); |
750 | |
751 | os << "Loop condition is true. "; |
752 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
753 | |
754 | if (const Stmt *S = End.asStmt()) |
755 | End = PDB.getEnclosingStmtLocation(S); |
756 | |
757 | PD.getActivePath().push_front( |
758 | std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, |
759 | os.str())); |
760 | } else { |
761 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
762 | |
763 | if (const Stmt *S = End.asStmt()) |
764 | End = PDB.getEnclosingStmtLocation(S); |
765 | |
766 | PD.getActivePath().push_front( |
767 | std::make_shared<PathDiagnosticControlFlowPiece>( |
768 | Start, End, "Loop condition is false. Exiting loop")); |
769 | } |
770 | break; |
771 | |
772 | case Stmt::WhileStmtClass: |
773 | case Stmt::ForStmtClass: |
774 | if (*(Src->succ_begin() + 1) == Dst) { |
775 | std::string sbuf; |
776 | llvm::raw_string_ostream os(sbuf); |
777 | |
778 | os << "Loop condition is false. "; |
779 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
780 | if (const Stmt *S = End.asStmt()) |
781 | End = PDB.getEnclosingStmtLocation(S); |
782 | |
783 | PD.getActivePath().push_front( |
784 | std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, |
785 | os.str())); |
786 | } else { |
787 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
788 | if (const Stmt *S = End.asStmt()) |
789 | End = PDB.getEnclosingStmtLocation(S); |
790 | |
791 | PD.getActivePath().push_front( |
792 | std::make_shared<PathDiagnosticControlFlowPiece>( |
793 | Start, End, "Loop condition is true. Entering loop body")); |
794 | } |
795 | |
796 | break; |
797 | |
798 | case Stmt::IfStmtClass: { |
799 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
800 | |
801 | if (const Stmt *S = End.asStmt()) |
802 | End = PDB.getEnclosingStmtLocation(S); |
803 | |
804 | if (*(Src->succ_begin() + 1) == Dst) |
805 | PD.getActivePath().push_front( |
806 | std::make_shared<PathDiagnosticControlFlowPiece>( |
807 | Start, End, "Taking false branch")); |
808 | else |
809 | PD.getActivePath().push_front( |
810 | std::make_shared<PathDiagnosticControlFlowPiece>( |
811 | Start, End, "Taking true branch")); |
812 | |
813 | break; |
814 | } |
815 | } |
816 | } |
817 | |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | |
824 | |
825 | |
826 | |
827 | |
828 | |
829 | using InterestingExprs = llvm::DenseSet<const Expr *>; |
830 | |
831 | static void reversePropagateIntererstingSymbols(BugReport &R, |
832 | InterestingExprs &IE, |
833 | const ProgramState *State, |
834 | const Expr *Ex, |
835 | const LocationContext *LCtx) { |
836 | SVal V = State->getSVal(Ex, LCtx); |
837 | if (!(R.isInteresting(V) || IE.count(Ex))) |
838 | return; |
839 | |
840 | switch (Ex->getStmtClass()) { |
841 | default: |
842 | if (!isa<CastExpr>(Ex)) |
843 | break; |
844 | LLVM_FALLTHROUGH; |
845 | case Stmt::BinaryOperatorClass: |
846 | case Stmt::UnaryOperatorClass: { |
847 | for (const Stmt *SubStmt : Ex->children()) { |
848 | if (const auto *child = dyn_cast_or_null<Expr>(SubStmt)) { |
849 | IE.insert(child); |
850 | SVal ChildV = State->getSVal(child, LCtx); |
851 | R.markInteresting(ChildV); |
852 | } |
853 | } |
854 | break; |
855 | } |
856 | } |
857 | |
858 | R.markInteresting(V); |
859 | } |
860 | |
861 | static void reversePropagateInterestingSymbols(BugReport &R, |
862 | InterestingExprs &IE, |
863 | const ProgramState *State, |
864 | const LocationContext *CalleeCtx) |
865 | { |
866 | |
867 | const StackFrameContext *Callee = CalleeCtx->getStackFrame(); |
868 | const Stmt *CallSite = Callee->getCallSite(); |
869 | if (const auto *CE = dyn_cast_or_null<CallExpr>(CallSite)) { |
870 | if (const auto *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) { |
871 | FunctionDecl::param_const_iterator PI = FD->param_begin(), |
872 | PE = FD->param_end(); |
873 | CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end(); |
874 | for (; AI != AE && PI != PE; ++AI, ++PI) { |
875 | if (const Expr *ArgE = *AI) { |
876 | if (const ParmVarDecl *PD = *PI) { |
877 | Loc LV = State->getLValue(PD, CalleeCtx); |
878 | if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV))) |
879 | IE.insert(ArgE); |
880 | } |
881 | } |
882 | } |
883 | } |
884 | } |
885 | } |
886 | |
887 | |
888 | |
889 | |
890 | |
891 | static bool isLoop(const Stmt *Term) { |
892 | switch (Term->getStmtClass()) { |
893 | case Stmt::ForStmtClass: |
894 | case Stmt::WhileStmtClass: |
895 | case Stmt::ObjCForCollectionStmtClass: |
896 | case Stmt::CXXForRangeStmtClass: |
897 | return true; |
898 | default: |
899 | |
900 | return false; |
901 | } |
902 | } |
903 | |
904 | static bool isJumpToFalseBranch(const BlockEdge *BE) { |
905 | const CFGBlock *Src = BE->getSrc(); |
906 | succ_size() == 2", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 906, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Src->succ_size() == 2); |
907 | return (*(Src->succ_begin()+1) == BE->getDst()); |
908 | } |
909 | |
910 | static bool isContainedByStmt(ParentMap &PM, const Stmt *S, const Stmt *SubS) { |
911 | while (SubS) { |
912 | if (SubS == S) |
913 | return true; |
914 | SubS = PM.getParent(SubS); |
915 | } |
916 | return false; |
917 | } |
918 | |
919 | static const Stmt *getStmtBeforeCond(ParentMap &PM, const Stmt *Term, |
920 | const ExplodedNode *N) { |
921 | while (N) { |
922 | Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>(); |
923 | if (SP) { |
924 | const Stmt *S = SP->getStmt(); |
925 | if (!isContainedByStmt(PM, Term, S)) |
926 | return S; |
927 | } |
928 | N = N->getFirstPred(); |
929 | } |
930 | return nullptr; |
931 | } |
932 | |
933 | static bool isInLoopBody(ParentMap &PM, const Stmt *S, const Stmt *Term) { |
934 | const Stmt *LoopBody = nullptr; |
935 | switch (Term->getStmtClass()) { |
936 | case Stmt::CXXForRangeStmtClass: { |
937 | const auto *FR = cast<CXXForRangeStmt>(Term); |
938 | if (isContainedByStmt(PM, FR->getInc(), S)) |
939 | return true; |
940 | if (isContainedByStmt(PM, FR->getLoopVarStmt(), S)) |
941 | return true; |
942 | LoopBody = FR->getBody(); |
943 | break; |
944 | } |
945 | case Stmt::ForStmtClass: { |
946 | const auto *FS = cast<ForStmt>(Term); |
947 | if (isContainedByStmt(PM, FS->getInc(), S)) |
948 | return true; |
949 | LoopBody = FS->getBody(); |
950 | break; |
951 | } |
952 | case Stmt::ObjCForCollectionStmtClass: { |
953 | const auto *FC = cast<ObjCForCollectionStmt>(Term); |
954 | LoopBody = FC->getBody(); |
955 | break; |
956 | } |
957 | case Stmt::WhileStmtClass: |
958 | LoopBody = cast<WhileStmt>(Term)->getBody(); |
959 | break; |
960 | default: |
961 | return false; |
962 | } |
963 | return isContainedByStmt(PM, LoopBody, S); |
964 | } |
965 | |
966 | |
967 | static void addEdgeToPath(PathPieces &path, |
968 | PathDiagnosticLocation &PrevLoc, |
969 | PathDiagnosticLocation NewLoc) { |
970 | if (!NewLoc.isValid()) |
971 | return; |
972 | |
973 | SourceLocation NewLocL = NewLoc.asLocation(); |
974 | if (NewLocL.isInvalid()) |
975 | return; |
976 | |
977 | if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) { |
978 | PrevLoc = NewLoc; |
979 | return; |
980 | } |
981 | |
982 | |
983 | |
984 | if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt()) |
985 | return; |
986 | |
987 | path.push_front( |
988 | std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc)); |
989 | PrevLoc = NewLoc; |
990 | } |
991 | |
992 | |
993 | |
994 | static const Stmt *getTerminatorCondition(const CFGBlock *B) { |
995 | const Stmt *S = B->getTerminatorCondition(); |
996 | if (const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(S)) |
997 | return FS->getElement(); |
998 | return S; |
999 | } |
1000 | |
1001 | static const char StrEnteringLoop[] = "Entering loop body"; |
1002 | static const char StrLoopBodyZero[] = "Loop body executed 0 times"; |
1003 | static const char StrLoopRangeEmpty[] = |
1004 | "Loop body skipped when range is empty"; |
1005 | static const char StrLoopCollectionEmpty[] = |
1006 | "Loop body skipped when collection is empty"; |
1007 | |
1008 | static std::unique_ptr<FilesToLineNumsMap> |
1009 | findExecutedLines(SourceManager &SM, const ExplodedNode *N); |
1010 | |
1011 | |
1012 | |
1013 | |
1014 | |
1015 | static void generatePathDiagnosticsForNode(const ExplodedNode *N, |
1016 | PathDiagnostic &PD, |
1017 | PathDiagnosticLocation &PrevLoc, |
1018 | PathDiagnosticBuilder &PDB, |
1019 | LocationContextMap &LCM, |
1020 | StackDiagVector &CallStack, |
1021 | InterestingExprs &IE, |
1022 | bool AddPathEdges) { |
1023 | ProgramPoint P = N->getLocation(); |
1024 | const SourceManager& SM = PDB.getSourceManager(); |
1025 | |
1026 | |
1027 | |
1028 | |
1029 | |
1030 | if (auto CE = P.getAs<CallEnter>()) { |
1031 | |
1032 | if (AddPathEdges) { |
1033 | |
1034 | const StackFrameContext *CalleeLC = CE->getCalleeContext(); |
1035 | const Decl *D = CalleeLC->getDecl(); |
1036 | |
1037 | |
1038 | |
1039 | |
1040 | |
1041 | |
1042 | if (D->hasBody()) |
1043 | addEdgeToPath(PD.getActivePath(), PrevLoc, |
1044 | PathDiagnosticLocation::createBegin(D, SM)); |
1045 | } |
1046 | |
1047 | |
1048 | bool VisitedEntireCall = PD.isWithinCall(); |
1049 | PD.popActivePath(); |
1050 | |
1051 | PathDiagnosticCallPiece *C; |
1052 | if (VisitedEntireCall) { |
1053 | C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front().get()); |
1054 | } else { |
1055 | const Decl *Caller = CE->getLocationContext()->getDecl(); |
1056 | C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller); |
1057 | |
1058 | if (AddPathEdges) { |
1059 | |
1060 | |
1061 | assert(PD.getActivePath().size() == 1 && |
1062 | PD.getActivePath().front().get() == C); |
1063 | LCM[&PD.getActivePath()] = nullptr; |
1064 | } |
1065 | |
1066 | |
1067 | |
1068 | path] == nullptr || LCM[&C->path] == CE->getCalleeContext()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 1069, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(LCM[&C->path] == nullptr || |
1069 | path] == nullptr || LCM[&C->path] == CE->getCalleeContext()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 1069, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> LCM[&C->path] == CE->getCalleeContext()); |
1070 | LCM[&C->path] = CE->getCalleeContext(); |
1071 | |
1072 | |
1073 | |
1074 | const LocationContext *&NewLC = LCM[&PD.getActivePath()]; |
1075 | if (!NewLC) |
1076 | NewLC = N->getLocationContext(); |
1077 | |
1078 | PDB.LC = NewLC; |
1079 | } |
1080 | C->setCallee(*CE, SM); |
1081 | |
1082 | |
1083 | PrevLoc = C->getLocation(); |
1084 | |
1085 | if (!CallStack.empty()) { |
1086 | assert(CallStack.back().first == C); |
1087 | CallStack.pop_back(); |
1088 | } |
1089 | return; |
1090 | } |
1091 | |
1092 | |
1093 | if (AddPathEdges) { |
1094 | |
1095 | |
1096 | PDB.LC = N->getLocationContext(); |
1097 | |
1098 | |
1099 | |
1100 | assert(!LCM[&PD.getActivePath()] || LCM[&PD.getActivePath()] == PDB.LC); |
1101 | LCM[&PD.getActivePath()] = PDB.LC; |
1102 | } |
1103 | |
1104 | |
1105 | if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) { |
1106 | |
1107 | |
1108 | |
1109 | auto C = PathDiagnosticCallPiece::construct(*CE, SM); |
1110 | |
1111 | LCM[&C->path] = CE->getCalleeContext(); |
1112 | |
1113 | if (AddPathEdges) { |
1114 | const Stmt *S = CE->getCalleeContext()->getCallSite(); |
1115 | |
1116 | if (const auto *Ex = dyn_cast_or_null<Expr>(S)) { |
1117 | reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE, |
1118 | N->getState().get(), Ex, |
1119 | N->getLocationContext()); |
1120 | } |
1121 | |
1122 | addEdgeToPath(PD.getActivePath(), PrevLoc, C->callReturn); |
1123 | PrevLoc.invalidate(); |
1124 | } |
1125 | |
1126 | auto *P = C.get(); |
1127 | PD.getActivePath().push_front(std::move(C)); |
1128 | |
1129 | |
1130 | PD.pushActivePath(&P->path); |
1131 | CallStack.push_back(StackDiagPair(P, N)); |
1132 | return; |
1133 | } |
1134 | |
1135 | if (auto PS = P.getAs<PostStmt>()) { |
1136 | if (!AddPathEdges) |
1137 | return; |
1138 | |
1139 | |
1140 | |
1141 | if (const Expr *Ex = PS->getStmtAs<Expr>()) |
1142 | reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE, |
1143 | N->getState().get(), Ex, |
1144 | N->getLocationContext()); |
1145 | |
1146 | |
1147 | |
1148 | |
1149 | if (!isa<ObjCForCollectionStmt>(PS->getStmt())) { |
1150 | PathDiagnosticLocation L = |
1151 | PathDiagnosticLocation(PS->getStmt(), SM, PDB.LC); |
1152 | addEdgeToPath(PD.getActivePath(), PrevLoc, L); |
1153 | } |
1154 | |
1155 | } else if (auto BE = P.getAs<BlockEdge>()) { |
1156 | |
1157 | if (!AddPathEdges) { |
1158 | generateMinimalDiagForBlockEdge(N, *BE, SM, PDB, PD); |
1159 | return; |
1160 | } |
1161 | |
1162 | |
1163 | |
1164 | if (const ExplodedNode *NextNode = N->getFirstPred()) { |
1165 | const LocationContext *CallerCtx = NextNode->getLocationContext(); |
1166 | const LocationContext *CalleeCtx = PDB.LC; |
1167 | if (CallerCtx != CalleeCtx && AddPathEdges) { |
1168 | reversePropagateInterestingSymbols(*PDB.getBugReport(), IE, |
1169 | N->getState().get(), CalleeCtx); |
1170 | } |
1171 | } |
1172 | |
1173 | |
1174 | if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) { |
1175 | PathDiagnosticLocation L(Loop, SM, PDB.LC); |
1176 | const Stmt *Body = nullptr; |
1177 | |
1178 | if (const auto *FS = dyn_cast<ForStmt>(Loop)) |
1179 | Body = FS->getBody(); |
1180 | else if (const auto *WS = dyn_cast<WhileStmt>(Loop)) |
1181 | Body = WS->getBody(); |
1182 | else if (const auto *OFS = dyn_cast<ObjCForCollectionStmt>(Loop)) { |
1183 | Body = OFS->getBody(); |
1184 | } else if (const auto *FRS = dyn_cast<CXXForRangeStmt>(Loop)) { |
1185 | Body = FRS->getBody(); |
1186 | } |
1187 | |
1188 | |
1189 | auto p = std::make_shared<PathDiagnosticEventPiece>( |
1190 | L, "Looping back to the head " |
1191 | "of the loop"); |
1192 | p->setPrunable(true); |
1193 | |
1194 | addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation()); |
1195 | PD.getActivePath().push_front(std::move(p)); |
1196 | |
1197 | if (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) { |
1198 | addEdgeToPath(PD.getActivePath(), PrevLoc, |
1199 | PathDiagnosticLocation::createEndBrace(CS, SM)); |
1200 | } |
1201 | } |
1202 | |
1203 | const CFGBlock *BSrc = BE->getSrc(); |
1204 | ParentMap &PM = PDB.getParentMap(); |
1205 | |
1206 | if (const Stmt *Term = BSrc->getTerminator()) { |
1207 | |
1208 | |
1209 | if (isLoop(Term)) { |
1210 | const Stmt *TermCond = getTerminatorCondition(BSrc); |
1211 | bool IsInLoopBody = |
1212 | isInLoopBody(PM, getStmtBeforeCond(PM, TermCond, N), Term); |
1213 | |
1214 | const char *str = nullptr; |
1215 | |
1216 | if (isJumpToFalseBranch(&*BE)) { |
1217 | if (!IsInLoopBody) { |
1218 | if (isa<ObjCForCollectionStmt>(Term)) { |
1219 | str = StrLoopCollectionEmpty; |
1220 | } else if (isa<CXXForRangeStmt>(Term)) { |
1221 | str = StrLoopRangeEmpty; |
1222 | } else { |
1223 | str = StrLoopBodyZero; |
1224 | } |
1225 | } |
1226 | } else { |
1227 | str = StrEnteringLoop; |
1228 | } |
1229 | |
1230 | if (str) { |
1231 | PathDiagnosticLocation L(TermCond ? TermCond : Term, SM, PDB.LC); |
1232 | auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str); |
1233 | PE->setPrunable(true); |
1234 | addEdgeToPath(PD.getActivePath(), PrevLoc, |
1235 | PE->getLocation()); |
1236 | PD.getActivePath().push_front(std::move(PE)); |
1237 | } |
1238 | } else if (isa<BreakStmt>(Term) || isa<ContinueStmt>(Term) || |
1239 | isa<GotoStmt>(Term)) { |
1240 | PathDiagnosticLocation L(Term, SM, PDB.LC); |
1241 | addEdgeToPath(PD.getActivePath(), PrevLoc, L); |
1242 | } |
1243 | } |
1244 | } |
1245 | } |
1246 | |
1247 | static std::unique_ptr<PathDiagnostic> |
1248 | generateEmptyDiagnosticForReport(BugReport *R, SourceManager &SM) { |
1249 | const BugType &BT = R->getBugType(); |
1250 | return llvm::make_unique<PathDiagnostic>( |
1251 | R->getBugType().getCheckName(), R->getDeclWithIssue(), |
1252 | R->getBugType().getName(), R->getDescription(), |
1253 | R->getShortDescription(), BT.getCategory(), |
1254 | R->getUniqueingLocation(), R->getUniqueingDecl(), |
1255 | findExecutedLines(SM, R->getErrorNode())); |
1256 | } |
1257 | |
1258 | static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) { |
1259 | if (!S) |
1260 | return nullptr; |
1261 | |
1262 | while (true) { |
1263 | S = PM.getParentIgnoreParens(S); |
1264 | |
1265 | if (!S) |
1266 | break; |
1267 | |
1268 | if (isa<FullExpr>(S) || |
1269 | isa<CXXBindTemporaryExpr>(S) || |
1270 | isa<SubstNonTypeTemplateParmExpr>(S)) |
1271 | continue; |
1272 | |
1273 | break; |
1274 | } |
1275 | |
1276 | return S; |
1277 | } |
1278 | |
1279 | static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) { |
1280 | switch (S->getStmtClass()) { |
1281 | case Stmt::BinaryOperatorClass: { |
1282 | const auto *BO = cast<BinaryOperator>(S); |
1283 | if (!BO->isLogicalOp()) |
1284 | return false; |
1285 | return BO->getLHS() == Cond || BO->getRHS() == Cond; |
1286 | } |
1287 | case Stmt::IfStmtClass: |
1288 | return cast<IfStmt>(S)->getCond() == Cond; |
1289 | case Stmt::ForStmtClass: |
1290 | return cast<ForStmt>(S)->getCond() == Cond; |
1291 | case Stmt::WhileStmtClass: |
1292 | return cast<WhileStmt>(S)->getCond() == Cond; |
1293 | case Stmt::DoStmtClass: |
1294 | return cast<DoStmt>(S)->getCond() == Cond; |
1295 | case Stmt::ChooseExprClass: |
1296 | return cast<ChooseExpr>(S)->getCond() == Cond; |
1297 | case Stmt::IndirectGotoStmtClass: |
1298 | return cast<IndirectGotoStmt>(S)->getTarget() == Cond; |
1299 | case Stmt::SwitchStmtClass: |
1300 | return cast<SwitchStmt>(S)->getCond() == Cond; |
1301 | case Stmt::BinaryConditionalOperatorClass: |
1302 | return cast<BinaryConditionalOperator>(S)->getCond() == Cond; |
1303 | case Stmt::ConditionalOperatorClass: { |
1304 | const auto *CO = cast<ConditionalOperator>(S); |
1305 | return CO->getCond() == Cond || |
1306 | CO->getLHS() == Cond || |
1307 | CO->getRHS() == Cond; |
1308 | } |
1309 | case Stmt::ObjCForCollectionStmtClass: |
1310 | return cast<ObjCForCollectionStmt>(S)->getElement() == Cond; |
1311 | case Stmt::CXXForRangeStmtClass: { |
1312 | const auto *FRS = cast<CXXForRangeStmt>(S); |
1313 | return FRS->getCond() == Cond || FRS->getRangeInit() == Cond; |
1314 | } |
1315 | default: |
1316 | return false; |
1317 | } |
1318 | } |
1319 | |
1320 | static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) { |
1321 | if (const auto *FS = dyn_cast<ForStmt>(FL)) |
1322 | return FS->getInc() == S || FS->getInit() == S; |
1323 | if (const auto *FRS = dyn_cast<CXXForRangeStmt>(FL)) |
1324 | return FRS->getInc() == S || FRS->getRangeStmt() == S || |
1325 | FRS->getLoopVarStmt() || FRS->getRangeInit() == S; |
1326 | return false; |
1327 | } |
1328 | |
1329 | using OptimizedCallsSet = llvm::DenseSet<const PathDiagnosticCallPiece *>; |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | static void addContextEdges(PathPieces &pieces, SourceManager &SM, |
1337 | const ParentMap &PM, const LocationContext *LCtx) { |
1338 | PathPieces::iterator Prev = pieces.end(); |
1339 | for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E; |
1340 | Prev = I, ++I) { |
1341 | auto *Piece = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); |
1342 | |
1343 | if (!Piece) |
1344 | continue; |
1345 | |
1346 | PathDiagnosticLocation SrcLoc = Piece->getStartLocation(); |
1347 | SmallVector<PathDiagnosticLocation, 4> SrcContexts; |
1348 | |
1349 | PathDiagnosticLocation NextSrcContext = SrcLoc; |
1350 | const Stmt *InnerStmt = nullptr; |
1351 | while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) { |
1352 | SrcContexts.push_back(NextSrcContext); |
1353 | InnerStmt = NextSrcContext.asStmt(); |
1354 | NextSrcContext = getEnclosingStmtLocation(InnerStmt, SM, PM, LCtx, |
1355 | ); |
1356 | } |
1357 | |
1358 | |
1359 | |
1360 | |
1361 | while (true) { |
1362 | const Stmt *Dst = Piece->getEndLocation().getStmtOrNull(); |
1363 | |
1364 | |
1365 | |
1366 | PathDiagnosticLocation DstContext = |
1367 | getEnclosingStmtLocation(Dst, SM, PM, LCtx, ); |
1368 | if (!DstContext.isValid() || DstContext.asStmt() == Dst) |
1369 | break; |
1370 | |
1371 | |
1372 | if (std::find(SrcContexts.begin(), SrcContexts.end(), DstContext) != |
1373 | SrcContexts.end()) |
1374 | break; |
1375 | |
1376 | |
1377 | Piece->setStartLocation(DstContext); |
1378 | |
1379 | |
1380 | |
1381 | if (Prev != E) { |
1382 | auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get()); |
1383 | |
1384 | if (PrevPiece) { |
1385 | if (const Stmt *PrevSrc = |
1386 | PrevPiece->getStartLocation().getStmtOrNull()) { |
1387 | const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM); |
1388 | if (PrevSrcParent == |
1389 | getStmtParent(DstContext.getStmtOrNull(), PM)) { |
1390 | PrevPiece->setEndLocation(DstContext); |
1391 | break; |
1392 | } |
1393 | } |
1394 | } |
1395 | } |
1396 | |
1397 | |
1398 | |
1399 | |
1400 | auto P = |
1401 | std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext); |
1402 | Piece = P.get(); |
1403 | I = pieces.insert(I, std::move(P)); |
1404 | } |
1405 | } |
1406 | } |
1407 | |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | static void simplifySimpleBranches(PathPieces &pieces) { |
1419 | for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) { |
1420 | const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); |
1421 | |
1422 | if (!PieceI) |
1423 | continue; |
1424 | |
1425 | const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull(); |
1426 | const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull(); |
1427 | |
1428 | if (!s1Start || !s1End) |
1429 | continue; |
1430 | |
1431 | PathPieces::iterator NextI = I; ++NextI; |
1432 | if (NextI == E) |
1433 | break; |
1434 | |
1435 | PathDiagnosticControlFlowPiece *PieceNextI = nullptr; |
1436 | |
1437 | while (true) { |
1438 | if (NextI == E) |
1439 | break; |
1440 | |
1441 | const auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get()); |
1442 | if (EV) { |
1443 | StringRef S = EV->getString(); |
1444 | if (S == StrEnteringLoop || S == StrLoopBodyZero || |
1445 | S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) { |
1446 | ++NextI; |
1447 | continue; |
1448 | } |
1449 | break; |
1450 | } |
1451 | |
1452 | PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); |
1453 | break; |
1454 | } |
1455 | |
1456 | if (!PieceNextI) |
1457 | continue; |
1458 | |
1459 | const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull(); |
1460 | const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull(); |
1461 | |
1462 | if (!s2Start || !s2End || s1End != s2Start) |
1463 | continue; |
1464 | |
1465 | |
1466 | |
1467 | if (!(isa<ForStmt>(s1Start) || isa<WhileStmt>(s1Start) || |
1468 | isa<IfStmt>(s1Start) || isa<ObjCForCollectionStmt>(s1Start) || |
1469 | isa<CXXForRangeStmt>(s1Start))) |
1470 | continue; |
1471 | |
1472 | |
1473 | if (!isConditionForTerminator(s1Start, s1End)) |
1474 | continue; |
1475 | |
1476 | |
1477 | |
1478 | PieceNextI->setStartLocation(PieceI->getStartLocation()); |
1479 | I = pieces.erase(I); |
1480 | } |
1481 | } |
1482 | |
1483 | |
1484 | |
1485 | |
1486 | |
1487 | |
1488 | static Optional<size_t> getLengthOnSingleLine(SourceManager &SM, |
1489 | SourceRange Range) { |
1490 | SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()), |
1491 | SM.getExpansionRange(Range.getEnd()).getEnd()); |
1492 | |
1493 | FileID FID = SM.getFileID(ExpansionRange.getBegin()); |
1494 | if (FID != SM.getFileID(ExpansionRange.getEnd())) |
1495 | return None; |
1496 | |
1497 | bool Invalid; |
1498 | const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, &Invalid); |
1499 | if (Invalid) |
1500 | return None; |
1501 | |
1502 | unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin()); |
1503 | unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd()); |
1504 | StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset); |
1505 | |
1506 | |
1507 | |
1508 | |
1509 | |
1510 | if (Snippet.find_first_of("\r\n") != StringRef::npos) |
1511 | return None; |
1512 | |
1513 | |
1514 | return Snippet.size(); |
1515 | } |
1516 | |
1517 | |
1518 | static Optional<size_t> getLengthOnSingleLine(SourceManager &SM, |
1519 | const Stmt *S) { |
1520 | return getLengthOnSingleLine(SM, S->getSourceRange()); |
1521 | } |
1522 | |
1523 | |
1524 | |
1525 | |
1526 | |
1527 | |
1528 | |
1529 | |
1530 | |
1531 | |
1532 | |
1533 | |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | |
1539 | static void removeContextCycles(PathPieces &Path, SourceManager &SM) { |
1540 | for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) { |
1541 | |
1542 | const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); |
1543 | |
1544 | if (!PieceI) { |
1545 | ++I; |
1546 | continue; |
1547 | } |
1548 | |
1549 | const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull(); |
1550 | const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull(); |
1551 | |
1552 | PathPieces::iterator NextI = I; ++NextI; |
1553 | if (NextI == E) |
1554 | break; |
1555 | |
1556 | const auto *PieceNextI = |
1557 | dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); |
1558 | |
1559 | if (!PieceNextI) { |
1560 | if (isa<PathDiagnosticEventPiece>(NextI->get())) { |
1561 | ++NextI; |
1562 | if (NextI == E) |
1563 | break; |
1564 | PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); |
1565 | } |
1566 | |
1567 | if (!PieceNextI) { |
1568 | ++I; |
1569 | continue; |
1570 | } |
1571 | } |
1572 | |
1573 | const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull(); |
1574 | const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull(); |
1575 | |
1576 | if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) { |
1577 | const size_t MAX_SHORT_LINE_LENGTH = 80; |
1578 | Optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start); |
1579 | if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) { |
1580 | Optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start); |
1581 | if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) { |
1582 | Path.erase(I); |
1583 | I = Path.erase(NextI); |
1584 | continue; |
1585 | } |
1586 | } |
1587 | } |
1588 | |
1589 | ++I; |
1590 | } |
1591 | } |
1592 | |
1593 | |
1594 | static bool lexicalContains(ParentMap &PM, const Stmt *X, const Stmt *Y) { |
1595 | while (X) { |
1596 | if (X == Y) |
1597 | return true; |
1598 | X = PM.getParent(X); |
1599 | } |
1600 | return false; |
1601 | } |
1602 | |
1603 | |
1604 | static void removePunyEdges(PathPieces &path, SourceManager &SM, |
1605 | ParentMap &PM) { |
1606 | bool erased = false; |
1607 | |
1608 | for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; |
1609 | erased ? I : ++I) { |
1610 | erased = false; |
1611 | |
1612 | const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); |
1613 | |
1614 | if (!PieceI) |
1615 | continue; |
1616 | |
1617 | const Stmt *start = PieceI->getStartLocation().getStmtOrNull(); |
1618 | const Stmt *end = PieceI->getEndLocation().getStmtOrNull(); |
1619 | |
1620 | if (!start || !end) |
1621 | continue; |
1622 | |
1623 | const Stmt *endParent = PM.getParent(end); |
1624 | if (!endParent) |
1625 | continue; |
1626 | |
1627 | if (isConditionForTerminator(end, endParent)) |
1628 | continue; |
1629 | |
1630 | SourceLocation FirstLoc = start->getBeginLoc(); |
1631 | SourceLocation SecondLoc = end->getBeginLoc(); |
1632 | |
1633 | if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc)) |
1634 | continue; |
1635 | if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc)) |
1636 | std::swap(SecondLoc, FirstLoc); |
1637 | |
1638 | SourceRange EdgeRange(FirstLoc, SecondLoc); |
1639 | Optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange); |
1640 | |
1641 | |
1642 | if (!ByteWidth) |
1643 | continue; |
1644 | |
1645 | const size_t MAX_PUNY_EDGE_LENGTH = 2; |
1646 | if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) { |
1647 | |
1648 | |
1649 | |
1650 | I = path.erase(I); |
1651 | erased = true; |
1652 | continue; |
1653 | } |
1654 | } |
1655 | } |
1656 | |
1657 | static void removeIdenticalEvents(PathPieces &path) { |
1658 | for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) { |
1659 | const auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get()); |
1660 | |
1661 | if (!PieceI) |
1662 | continue; |
1663 | |
1664 | PathPieces::iterator NextI = I; ++NextI; |
1665 | if (NextI == E) |
1666 | return; |
1667 | |
1668 | const auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get()); |
1669 | |
1670 | if (!PieceNextI) |
1671 | continue; |
1672 | |
1673 | |
1674 | if (PieceI->getString() == PieceNextI->getString()) { |
1675 | path.erase(NextI); |
1676 | } |
1677 | } |
1678 | } |
1679 | |
1680 | static bool optimizeEdges(PathPieces &path, SourceManager &SM, |
1681 | OptimizedCallsSet &OCS, |
1682 | LocationContextMap &LCM) { |
1683 | bool hasChanges = false; |
1684 | const LocationContext *LC = LCM[&path]; |
1685 | assert(LC); |
1686 | ParentMap &PM = LC->getParentMap(); |
1687 | |
1688 | for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) { |
1689 | |
1690 | if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) { |
1691 | |
1692 | |
1693 | if (!OCS.count(CallI)) { |
1694 | while (optimizeEdges(CallI->path, SM, OCS, LCM)) {} |
1695 | OCS.insert(CallI); |
1696 | } |
1697 | ++I; |
1698 | continue; |
1699 | } |
1700 | |
1701 | |
1702 | auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get()); |
1703 | |
1704 | if (!PieceI) { |
1705 | ++I; |
1706 | continue; |
1707 | } |
1708 | |
1709 | const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull(); |
1710 | const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull(); |
1711 | const Stmt *level1 = getStmtParent(s1Start, PM); |
1712 | const Stmt *level2 = getStmtParent(s1End, PM); |
1713 | |
1714 | PathPieces::iterator NextI = I; ++NextI; |
1715 | if (NextI == E) |
1716 | break; |
1717 | |
1718 | const auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get()); |
1719 | |
1720 | if (!PieceNextI) { |
1721 | ++I; |
1722 | continue; |
1723 | } |
1724 | |
1725 | const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull(); |
1726 | const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull(); |
1727 | const Stmt *level3 = getStmtParent(s2Start, PM); |
1728 | const Stmt *level4 = getStmtParent(s2End, PM); |
1729 | |
1730 | |
1731 | |
1732 | |
1733 | |
1734 | |
1735 | |
1736 | |
1737 | |
1738 | |
1739 | |
1740 | |
1741 | |
1742 | |
1743 | |
1744 | if (level1 && level1 == level2 && level1 == level3 && level1 == level4) { |
1745 | PieceI->setEndLocation(PieceNextI->getEndLocation()); |
1746 | path.erase(NextI); |
1747 | hasChanges = true; |
1748 | continue; |
1749 | } |
1750 | |
1751 | |
1752 | |
1753 | |
1754 | |
1755 | |
1756 | |
1757 | |
1758 | if (s1End && s1End == s2Start && level2) { |
1759 | bool removeEdge = false; |
1760 | |
1761 | |
1762 | |
1763 | if (isIncrementOrInitInForLoop(s1End, level2)) |
1764 | removeEdge = true; |
1765 | |
1766 | |
1767 | |
1768 | else if (!isConditionForTerminator(level2, s1End)) { |
1769 | |
1770 | |
1771 | if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) { |
1772 | removeEdge = true; |
1773 | } |
1774 | |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | |
1785 | else if (s1Start && s2End && |
1786 | lexicalContains(PM, s2Start, s2End) && |
1787 | !lexicalContains(PM, s1End, s1Start)) { |
1788 | removeEdge = true; |
1789 | } |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | |
1795 | |
1796 | |
1797 | |
1798 | else if (s1Start && s2End && |
1799 | lexicalContains(PM, s1Start, s1End)) { |
1800 | SourceRange EdgeRange(PieceI->getEndLocation().asLocation(), |
1801 | PieceI->getStartLocation().asLocation()); |
1802 | if (!getLengthOnSingleLine(SM, EdgeRange).hasValue()) |
1803 | removeEdge = true; |
1804 | } |
1805 | } |
1806 | |
1807 | if (removeEdge) { |
1808 | PieceI->setEndLocation(PieceNextI->getEndLocation()); |
1809 | path.erase(NextI); |
1810 | hasChanges = true; |
1811 | continue; |
1812 | } |
1813 | } |
1814 | |
1815 | |
1816 | |
1817 | |
1818 | |
1819 | |
1820 | |
1821 | |
1822 | if (s1End == s2Start) { |
1823 | const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(level3); |
1824 | if (FS && FS->getCollection()->IgnoreParens() == s2Start && |
1825 | s2End == FS->getElement()) { |
1826 | PieceI->setEndLocation(PieceNextI->getEndLocation()); |
1827 | path.erase(NextI); |
1828 | hasChanges = true; |
1829 | continue; |
1830 | } |
1831 | } |
1832 | |
1833 | |
1834 | ++I; |
1835 | } |
1836 | |
1837 | if (!hasChanges) { |
1838 | |
1839 | |
1840 | addContextEdges(path, SM, PM, LC); |
1841 | |
1842 | removeContextCycles(path, SM); |
1843 | |
1844 | |
1845 | simplifySimpleBranches(path); |
1846 | |
1847 | removePunyEdges(path, SM, PM); |
1848 | |
1849 | removeIdenticalEvents(path); |
1850 | } |
1851 | |
1852 | return hasChanges; |
1853 | } |
1854 | |
1855 | |
1856 | |
1857 | |
1858 | |
1859 | |
1860 | |
1861 | static void dropFunctionEntryEdge(PathPieces &Path, LocationContextMap &LCM, |
1862 | SourceManager &SM) { |
1863 | const auto *FirstEdge = |
1864 | dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get()); |
1865 | if (!FirstEdge) |
1866 | return; |
1867 | |
1868 | const Decl *D = LCM[&Path]->getDecl(); |
1869 | PathDiagnosticLocation EntryLoc = PathDiagnosticLocation::createBegin(D, SM); |
1870 | if (FirstEdge->getStartLocation() != EntryLoc) |
1871 | return; |
1872 | |
1873 | Path.pop_front(); |
1874 | } |
1875 | |
1876 | using VisitorsDiagnosticsTy = llvm::DenseMap<const ExplodedNode *, |
1877 | std::vector<std::shared_ptr<PathDiagnosticPiece>>>; |
1878 | |
1879 | |
1880 | static void updateExecutedLinesWithDiagnosticPieces( |
1881 | PathDiagnostic &PD) { |
1882 | |
1883 | PathPieces path = PD.path.flatten(); |
1884 | FilesToLineNumsMap &ExecutedLines = PD.getExecutedLines(); |
1885 | |
1886 | for (const auto &P : path) { |
1887 | FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc(); |
1888 | FileID FID = Loc.getFileID(); |
1889 | unsigned LineNo = Loc.getLineNumber(); |
1890 | assert(FID.isValid()); |
1891 | ExecutedLines[FID].insert(LineNo); |
1892 | } |
1893 | } |
1894 | |
1895 | |
1896 | |
1897 | |
1898 | |
1899 | |
1900 | |
1901 | |
1902 | |
1903 | |
1904 | |
1905 | |
1906 | |
1907 | static std::unique_ptr<PathDiagnostic> generatePathDiagnosticForConsumer( |
1908 | PathDiagnosticConsumer::PathGenerationScheme ActiveScheme, |
1909 | PathDiagnosticBuilder &PDB, |
1910 | const ExplodedNode *ErrorNode, |
1911 | const VisitorsDiagnosticsTy &VisitorsDiagnostics) { |
1912 | |
1913 | bool GenerateDiagnostics = (ActiveScheme != PathDiagnosticConsumer::None); |
1914 | bool AddPathEdges = (ActiveScheme == PathDiagnosticConsumer::Extensive); |
1915 | SourceManager &SM = PDB.getSourceManager(); |
1916 | BugReport *R = PDB.getBugReport(); |
1917 | AnalyzerOptions &Opts = PDB.getBugReporter().getAnalyzerOptions(); |
1918 | StackDiagVector CallStack; |
1919 | InterestingExprs IE; |
1920 | LocationContextMap LCM; |
1921 | std::unique_ptr<PathDiagnostic> PD = generateEmptyDiagnosticForReport(R, SM); |
1922 | |
1923 | if (GenerateDiagnostics) { |
1924 | auto EndNotes = VisitorsDiagnostics.find(ErrorNode); |
1925 | std::shared_ptr<PathDiagnosticPiece> LastPiece; |
1926 | if (EndNotes != VisitorsDiagnostics.end()) { |
1927 | second.empty()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 1927, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!EndNotes->second.empty()); |
1928 | LastPiece = EndNotes->second[0]; |
1929 | } else { |
1930 | LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, ErrorNode, *R); |
1931 | } |
1932 | PD->setEndOfPath(LastPiece); |
1933 | } |
1934 | |
1935 | PathDiagnosticLocation PrevLoc = PD->getLocation(); |
1936 | const ExplodedNode *NextNode = ErrorNode->getFirstPred(); |
1937 | while (NextNode) { |
1938 | if (GenerateDiagnostics) |
1939 | generatePathDiagnosticsForNode( |
1940 | NextNode, *PD, PrevLoc, PDB, LCM, CallStack, IE, AddPathEdges); |
1941 | |
1942 | auto VisitorNotes = VisitorsDiagnostics.find(NextNode); |
1943 | NextNode = NextNode->getFirstPred(); |
1944 | if (!GenerateDiagnostics || VisitorNotes == VisitorsDiagnostics.end()) |
1945 | continue; |
1946 | |
1947 | |
1948 | |
1949 | std::set<llvm::FoldingSetNodeID> DeduplicationSet; |
1950 | |
1951 | |
1952 | for (const auto &Note : VisitorNotes->second) { |
1953 | llvm::FoldingSetNodeID ID; |
1954 | Note->Profile(ID); |
1955 | auto P = DeduplicationSet.insert(ID); |
1956 | if (!P.second) |
1957 | continue; |
1958 | |
1959 | if (AddPathEdges) |
1960 | addEdgeToPath(PD->getActivePath(), PrevLoc, Note->getLocation()); |
1961 | updateStackPiecesWithMessage(*Note, CallStack); |
1962 | PD->getActivePath().push_front(Note); |
1963 | } |
1964 | } |
1965 | |
1966 | if (AddPathEdges) { |
1967 | |
1968 | |
1969 | const StackFrameContext *CalleeLC = PDB.LC->getStackFrame(); |
1970 | const Decl *D = CalleeLC->getDecl(); |
1971 | addEdgeToPath(PD->getActivePath(), PrevLoc, |
1972 | PathDiagnosticLocation::createBegin(D, SM)); |
1973 | } |
1974 | |
1975 | |
1976 | |
1977 | if (!PD->path.empty()) { |
1978 | if (R->shouldPrunePath() && Opts.ShouldPrunePaths) { |
1979 | bool stillHasNotes = |
1980 | removeUnneededCalls(PD->getMutablePieces(), R, LCM); |
1981 | assert(stillHasNotes); |
1982 | (void)stillHasNotes; |
1983 | } |
1984 | |
1985 | |
1986 | adjustCallLocations(PD->getMutablePieces()); |
1987 | removePiecesWithInvalidLocations(PD->getMutablePieces()); |
1988 | |
1989 | if (AddPathEdges) { |
1990 | |
1991 | |
1992 | |
1993 | |
1994 | OptimizedCallsSet OCS; |
1995 | while (optimizeEdges(PD->getMutablePieces(), SM, OCS, LCM)) {} |
1996 | |
1997 | |
1998 | |
1999 | dropFunctionEntryEdge(PD->getMutablePieces(), LCM, SM); |
2000 | } |
2001 | |
2002 | |
2003 | |
2004 | |
2005 | removeRedundantMsgs(PD->getMutablePieces()); |
2006 | removeEdgesToDefaultInitializers(PD->getMutablePieces()); |
2007 | } |
2008 | |
2009 | if (GenerateDiagnostics && Opts.ShouldDisplayMacroExpansions) |
2010 | CompactMacroExpandedPieces(PD->getMutablePieces(), SM); |
2011 | |
2012 | return PD; |
2013 | } |
2014 | |
2015 | |
2016 | |
2017 | |
2018 | |
2019 | |
2020 | void BugType::anchor() {} |
2021 | |
2022 | void BuiltinBug::anchor() {} |
2023 | |
2024 | |
2025 | |
2026 | |
2027 | |
2028 | void BugReport::NodeResolver::anchor() {} |
2029 | |
2030 | void BugReport::addVisitor(std::unique_ptr<BugReporterVisitor> visitor) { |
2031 | if (!visitor) |
2032 | return; |
2033 | |
2034 | llvm::FoldingSetNodeID ID; |
2035 | visitor->Profile(ID); |
2036 | |
2037 | void *InsertPos = nullptr; |
2038 | if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) { |
2039 | return; |
2040 | } |
2041 | |
2042 | Callbacks.push_back(std::move(visitor)); |
2043 | } |
2044 | |
2045 | void BugReport::clearVisitors() { |
2046 | Callbacks.clear(); |
2047 | } |
2048 | |
2049 | BugReport::~BugReport() { |
2050 | while (!interestingSymbols.empty()) { |
2051 | popInterestingSymbolsAndRegions(); |
2052 | } |
2053 | } |
2054 | |
2055 | const Decl *BugReport::getDeclWithIssue() const { |
2056 | if (DeclWithIssue) |
2057 | return DeclWithIssue; |
2058 | |
2059 | const ExplodedNode *N = getErrorNode(); |
2060 | if (!N) |
2061 | return nullptr; |
2062 | |
2063 | const LocationContext *LC = N->getLocationContext(); |
2064 | return LC->getStackFrame()->getDecl(); |
2065 | } |
2066 | |
2067 | void BugReport::Profile(llvm::FoldingSetNodeID& hash) const { |
2068 | hash.AddPointer(&BT); |
2069 | hash.AddString(Description); |
2070 | PathDiagnosticLocation UL = getUniqueingLocation(); |
2071 | if (UL.isValid()) { |
2072 | UL.Profile(hash); |
2073 | } else if (Location.isValid()) { |
2074 | Location.Profile(hash); |
2075 | } else { |
2076 | assert(ErrorNode); |
2077 | hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode)); |
2078 | } |
2079 | |
2080 | for (SourceRange range : Ranges) { |
2081 | if (!range.isValid()) |
2082 | continue; |
2083 | hash.AddInteger(range.getBegin().getRawEncoding()); |
2084 | hash.AddInteger(range.getEnd().getRawEncoding()); |
2085 | } |
2086 | } |
2087 | |
2088 | void BugReport::markInteresting(SymbolRef sym) { |
2089 | if (!sym) |
2090 | return; |
2091 | |
2092 | getInterestingSymbols().insert(sym); |
2093 | |
2094 | if (const auto *meta = dyn_cast<SymbolMetadata>(sym)) |
2095 | getInterestingRegions().insert(meta->getRegion()); |
2096 | } |
2097 | |
2098 | void BugReport::markInteresting(const MemRegion *R) { |
2099 | if (!R) |
2100 | return; |
2101 | |
2102 | R = R->getBaseRegion(); |
2103 | getInterestingRegions().insert(R); |
2104 | |
2105 | if (const auto *SR = dyn_cast<SymbolicRegion>(R)) |
2106 | getInterestingSymbols().insert(SR->getSymbol()); |
2107 | } |
2108 | |
2109 | void BugReport::markInteresting(SVal V) { |
2110 | markInteresting(V.getAsRegion()); |
2111 | markInteresting(V.getAsSymbol()); |
2112 | } |
2113 | |
2114 | void BugReport::markInteresting(const LocationContext *LC) { |
2115 | if (!LC) |
2116 | return; |
2117 | InterestingLocationContexts.insert(LC); |
2118 | } |
2119 | |
2120 | bool BugReport::isInteresting(SVal V) { |
2121 | return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol()); |
2122 | } |
2123 | |
2124 | bool BugReport::isInteresting(SymbolRef sym) { |
2125 | if (!sym) |
2126 | return false; |
2127 | |
2128 | |
2129 | return getInterestingSymbols().count(sym); |
2130 | } |
2131 | |
2132 | bool BugReport::isInteresting(const MemRegion *R) { |
2133 | if (!R) |
2134 | return false; |
2135 | R = R->getBaseRegion(); |
2136 | bool b = getInterestingRegions().count(R); |
2137 | if (b) |
2138 | return true; |
2139 | if (const auto *SR = dyn_cast<SymbolicRegion>(R)) |
2140 | return getInterestingSymbols().count(SR->getSymbol()); |
2141 | return false; |
2142 | } |
2143 | |
2144 | bool BugReport::isInteresting(const LocationContext *LC) { |
2145 | if (!LC) |
2146 | return false; |
2147 | return InterestingLocationContexts.count(LC); |
2148 | } |
2149 | |
2150 | void BugReport::lazyInitializeInterestingSets() { |
2151 | if (interestingSymbols.empty()) { |
2152 | interestingSymbols.push_back(new Symbols()); |
2153 | interestingRegions.push_back(new Regions()); |
2154 | } |
2155 | } |
2156 | |
2157 | BugReport::Symbols &BugReport::getInterestingSymbols() { |
2158 | lazyInitializeInterestingSets(); |
2159 | return *interestingSymbols.back(); |
2160 | } |
2161 | |
2162 | BugReport::Regions &BugReport::getInterestingRegions() { |
2163 | lazyInitializeInterestingSets(); |
2164 | return *interestingRegions.back(); |
2165 | } |
2166 | |
2167 | void BugReport::pushInterestingSymbolsAndRegions() { |
2168 | interestingSymbols.push_back(new Symbols(getInterestingSymbols())); |
2169 | interestingRegions.push_back(new Regions(getInterestingRegions())); |
2170 | } |
2171 | |
2172 | void BugReport::popInterestingSymbolsAndRegions() { |
2173 | delete interestingSymbols.pop_back_val(); |
2174 | delete interestingRegions.pop_back_val(); |
2175 | } |
2176 | |
2177 | const Stmt *BugReport::getStmt() const { |
2178 | if (!ErrorNode) |
2179 | return nullptr; |
2180 | |
2181 | ProgramPoint ProgP = ErrorNode->getLocation(); |
2182 | const Stmt *S = nullptr; |
2183 | |
2184 | if (Optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) { |
2185 | CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit(); |
2186 | if (BE->getBlock() == &Exit) |
2187 | S = GetPreviousStmt(ErrorNode); |
2188 | } |
2189 | if (!S) |
2190 | S = PathDiagnosticLocation::getStmt(ErrorNode); |
2191 | |
2192 | return S; |
2193 | } |
2194 | |
2195 | llvm::iterator_range<BugReport::ranges_iterator> BugReport::getRanges() { |
2196 | |
2197 | |
2198 | if (Ranges.empty()) { |
2199 | if (const auto *E = dyn_cast_or_null<Expr>(getStmt())) |
2200 | addRange(E->getSourceRange()); |
2201 | else |
2202 | return llvm::make_range(ranges_iterator(), ranges_iterator()); |
2203 | } |
2204 | |
2205 | |
2206 | if (Ranges.size() == 1 && !Ranges.begin()->isValid()) |
2207 | return llvm::make_range(ranges_iterator(), ranges_iterator()); |
2208 | |
2209 | return llvm::make_range(Ranges.begin(), Ranges.end()); |
2210 | } |
2211 | |
2212 | PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const { |
2213 | if (ErrorNode) { |
2214 | (0) . __assert_fail ("!Location.isValid() && \"Either Location or ErrorNode should be specified but not both.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2215, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!Location.isValid() && |
2215 | (0) . __assert_fail ("!Location.isValid() && \"Either Location or ErrorNode should be specified but not both.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2215, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Either Location or ErrorNode should be specified but not both."); |
2216 | return PathDiagnosticLocation::createEndOfPath(ErrorNode, SM); |
2217 | } |
2218 | |
2219 | assert(Location.isValid()); |
2220 | return Location; |
2221 | } |
2222 | |
2223 | |
2224 | |
2225 | |
2226 | |
2227 | BugReportEquivClass::~BugReportEquivClass() = default; |
2228 | |
2229 | GRBugReporter::~GRBugReporter() = default; |
2230 | |
2231 | BugReporterData::~BugReporterData() = default; |
2232 | |
2233 | ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); } |
2234 | |
2235 | ProgramStateManager& |
2236 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
2237 | |
2238 | BugReporter::~BugReporter() { |
2239 | FlushReports(); |
2240 | |
2241 | |
2242 | for (const auto I : EQClassesVector) |
2243 | delete I; |
2244 | } |
2245 | |
2246 | void BugReporter::FlushReports() { |
2247 | if (BugTypes.isEmpty()) |
2248 | return; |
2249 | |
2250 | |
2251 | |
2252 | for (const auto EQ : EQClassesVector) |
2253 | FlushReport(*EQ); |
2254 | |
2255 | |
2256 | |
2257 | |
2258 | |
2259 | llvm::DeleteContainerSeconds(StrBugTypes); |
2260 | |
2261 | |
2262 | BugTypes = F.getEmptySet(); |
2263 | } |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | |
2269 | namespace { |
2270 | |
2271 | |
2272 | |
2273 | class ReportGraph { |
2274 | public: |
2275 | InterExplodedGraphMap BackMap; |
2276 | std::unique_ptr<ExplodedGraph> Graph; |
2277 | const ExplodedNode *ErrorNode; |
2278 | size_t Index; |
2279 | }; |
2280 | |
2281 | |
2282 | class TrimmedGraph { |
2283 | InterExplodedGraphMap InverseMap; |
2284 | |
2285 | using PriorityMapTy = llvm::DenseMap<const ExplodedNode *, unsigned>; |
2286 | |
2287 | PriorityMapTy PriorityMap; |
2288 | |
2289 | using NodeIndexPair = std::pair<const ExplodedNode *, size_t>; |
2290 | |
2291 | SmallVector<NodeIndexPair, 32> ReportNodes; |
2292 | |
2293 | std::unique_ptr<ExplodedGraph> G; |
2294 | |
2295 | |
2296 | template <bool Descending> |
2297 | class PriorityCompare { |
2298 | const PriorityMapTy &PriorityMap; |
2299 | |
2300 | public: |
2301 | PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {} |
2302 | |
2303 | bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const { |
2304 | PriorityMapTy::const_iterator LI = PriorityMap.find(LHS); |
2305 | PriorityMapTy::const_iterator RI = PriorityMap.find(RHS); |
2306 | PriorityMapTy::const_iterator E = PriorityMap.end(); |
2307 | |
2308 | if (LI == E) |
2309 | return Descending; |
2310 | if (RI == E) |
2311 | return !Descending; |
2312 | |
2313 | return Descending ? LI->second > RI->second |
2314 | : LI->second < RI->second; |
2315 | } |
2316 | |
2317 | bool operator()(const NodeIndexPair &LHS, const NodeIndexPair &RHS) const { |
2318 | return (*this)(LHS.first, RHS.first); |
2319 | } |
2320 | }; |
2321 | |
2322 | public: |
2323 | TrimmedGraph(const ExplodedGraph *OriginalGraph, |
2324 | ArrayRef<const ExplodedNode *> Nodes); |
2325 | |
2326 | bool popNextReportGraph(ReportGraph &GraphWrapper); |
2327 | }; |
2328 | |
2329 | } |
2330 | |
2331 | TrimmedGraph::TrimmedGraph(const ExplodedGraph *OriginalGraph, |
2332 | ArrayRef<const ExplodedNode *> Nodes) { |
2333 | |
2334 | |
2335 | InterExplodedGraphMap ForwardMap; |
2336 | G = OriginalGraph->trim(Nodes, &ForwardMap, &InverseMap); |
2337 | |
2338 | |
2339 | |
2340 | |
2341 | llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes; |
2342 | |
2343 | for (unsigned i = 0, count = Nodes.size(); i < count; ++i) { |
2344 | if (const ExplodedNode *NewNode = ForwardMap.lookup(Nodes[i])) { |
2345 | ReportNodes.push_back(std::make_pair(NewNode, i)); |
2346 | RemainingNodes.insert(NewNode); |
2347 | } |
2348 | } |
2349 | |
2350 | (0) . __assert_fail ("!RemainingNodes.empty() && \"No error node found in the trimmed graph\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2350, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!RemainingNodes.empty() && "No error node found in the trimmed graph"); |
2351 | |
2352 | |
2353 | std::queue<const ExplodedNode *> WS; |
2354 | |
2355 | num_roots() == 1", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2355, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(G->num_roots() == 1); |
2356 | WS.push(*G->roots_begin()); |
2357 | unsigned Priority = 0; |
2358 | |
2359 | while (!WS.empty()) { |
2360 | const ExplodedNode *Node = WS.front(); |
2361 | WS.pop(); |
2362 | |
2363 | PriorityMapTy::iterator PriorityEntry; |
2364 | bool IsNew; |
2365 | std::tie(PriorityEntry, IsNew) = |
2366 | PriorityMap.insert(std::make_pair(Node, Priority)); |
2367 | ++Priority; |
2368 | |
2369 | if (!IsNew) { |
2370 | second <= Priority", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2370, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(PriorityEntry->second <= Priority); |
2371 | continue; |
2372 | } |
2373 | |
2374 | if (RemainingNodes.erase(Node)) |
2375 | if (RemainingNodes.empty()) |
2376 | break; |
2377 | |
2378 | for (ExplodedNode::const_pred_iterator I = Node->succ_begin(), |
2379 | E = Node->succ_end(); |
2380 | I != E; ++I) |
2381 | WS.push(*I); |
2382 | } |
2383 | |
2384 | |
2385 | llvm::sort(ReportNodes, PriorityCompare<true>(PriorityMap)); |
2386 | } |
2387 | |
2388 | bool TrimmedGraph::popNextReportGraph(ReportGraph &GraphWrapper) { |
2389 | if (ReportNodes.empty()) |
2390 | return false; |
2391 | |
2392 | const ExplodedNode *OrigN; |
2393 | std::tie(OrigN, GraphWrapper.Index) = ReportNodes.pop_back_val(); |
2394 | (0) . __assert_fail ("PriorityMap.find(OrigN) != PriorityMap.end() && \"error node not accessible from root\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2395, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(PriorityMap.find(OrigN) != PriorityMap.end() && |
2395 | (0) . __assert_fail ("PriorityMap.find(OrigN) != PriorityMap.end() && \"error node not accessible from root\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2395, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "error node not accessible from root"); |
2396 | |
2397 | |
2398 | |
2399 | auto GNew = llvm::make_unique<ExplodedGraph>(); |
2400 | GraphWrapper.BackMap.clear(); |
2401 | |
2402 | |
2403 | |
2404 | ExplodedNode *Succ = nullptr; |
2405 | while (true) { |
2406 | |
2407 | |
2408 | ExplodedNode *NewN = GNew->createUncachedNode(OrigN->getLocation(), OrigN->getState(), |
2409 | OrigN->isSink()); |
2410 | |
2411 | |
2412 | InterExplodedGraphMap::const_iterator IMitr = InverseMap.find(OrigN); |
2413 | (0) . __assert_fail ("IMitr != InverseMap.end() && \"No mapping to original node.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2413, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(IMitr != InverseMap.end() && "No mapping to original node."); |
2414 | GraphWrapper.BackMap[NewN] = IMitr->second; |
2415 | |
2416 | |
2417 | if (Succ) |
2418 | Succ->addPredecessor(NewN, *GNew); |
2419 | else |
2420 | GraphWrapper.ErrorNode = NewN; |
2421 | |
2422 | Succ = NewN; |
2423 | |
2424 | |
2425 | if (OrigN->pred_empty()) { |
2426 | GNew->addRoot(NewN); |
2427 | break; |
2428 | } |
2429 | |
2430 | |
2431 | |
2432 | OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(), |
2433 | PriorityCompare<false>(PriorityMap)); |
2434 | } |
2435 | |
2436 | GraphWrapper.Graph = std::move(GNew); |
2437 | |
2438 | return true; |
2439 | } |
2440 | |
2441 | |
2442 | |
2443 | static void CompactMacroExpandedPieces(PathPieces &path, |
2444 | const SourceManager& SM) { |
2445 | using MacroStackTy = |
2446 | std::vector< |
2447 | std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>; |
2448 | |
2449 | using PiecesTy = std::vector<std::shared_ptr<PathDiagnosticPiece>>; |
2450 | |
2451 | MacroStackTy MacroStack; |
2452 | PiecesTy Pieces; |
2453 | |
2454 | for (PathPieces::const_iterator I = path.begin(), E = path.end(); |
2455 | I != E; ++I) { |
2456 | const auto &piece = *I; |
2457 | |
2458 | |
2459 | if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) { |
2460 | CompactMacroExpandedPieces(call->path, SM); |
2461 | } |
2462 | |
2463 | |
2464 | const FullSourceLoc Loc = piece->getLocation().asLocation(); |
2465 | |
2466 | |
2467 | |
2468 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
2469 | SM.getExpansionLoc(Loc) : |
2470 | SourceLocation(); |
2471 | |
2472 | if (Loc.isFileID()) { |
2473 | MacroStack.clear(); |
2474 | Pieces.push_back(piece); |
2475 | continue; |
2476 | } |
2477 | |
2478 | assert(Loc.isMacroID()); |
2479 | |
2480 | |
2481 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
2482 | MacroStack.back().first->subPieces.push_back(piece); |
2483 | continue; |
2484 | } |
2485 | |
2486 | |
2487 | |
2488 | std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup; |
2489 | |
2490 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
2491 | SM.getExpansionLoc(Loc) : |
2492 | SourceLocation(); |
2493 | |
2494 | |
2495 | while (!MacroStack.empty()) { |
2496 | if (InstantiationLoc == MacroStack.back().second) { |
2497 | MacroGroup = MacroStack.back().first; |
2498 | break; |
2499 | } |
2500 | |
2501 | if (ParentInstantiationLoc == MacroStack.back().second) { |
2502 | MacroGroup = MacroStack.back().first; |
2503 | break; |
2504 | } |
2505 | |
2506 | MacroStack.pop_back(); |
2507 | } |
2508 | |
2509 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
2510 | |
2511 | auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>( |
2512 | PathDiagnosticLocation::createSingleLocation(piece->getLocation())); |
2513 | |
2514 | if (MacroGroup) |
2515 | MacroGroup->subPieces.push_back(NewGroup); |
2516 | else { |
2517 | assert(InstantiationLoc.isFileID()); |
2518 | Pieces.push_back(NewGroup); |
2519 | } |
2520 | |
2521 | MacroGroup = NewGroup; |
2522 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
2523 | } |
2524 | |
2525 | |
2526 | MacroGroup->subPieces.push_back(piece); |
2527 | } |
2528 | |
2529 | |
2530 | path.clear(); |
2531 | |
2532 | path.insert(path.end(), Pieces.begin(), Pieces.end()); |
2533 | } |
2534 | |
2535 | |
2536 | |
2537 | |
2538 | std::unique_ptr<VisitorsDiagnosticsTy> |
2539 | generateVisitorsDiagnostics(BugReport *R, const ExplodedNode *ErrorNode, |
2540 | BugReporterContext &BRC) { |
2541 | auto Notes = llvm::make_unique<VisitorsDiagnosticsTy>(); |
2542 | BugReport::VisitorList visitors; |
2543 | |
2544 | |
2545 | |
2546 | const ExplodedNode *NextNode = ErrorNode->getFirstPred(); |
2547 | while (NextNode) { |
2548 | |
2549 | |
2550 | for (BugReport::visitor_iterator I = R->visitor_begin(), |
2551 | E = R->visitor_end(); |
2552 | I != E; ++I) { |
2553 | visitors.push_back(std::move(*I)); |
2554 | } |
2555 | R->clearVisitors(); |
2556 | |
2557 | const ExplodedNode *Pred = NextNode->getFirstPred(); |
2558 | if (!Pred) { |
2559 | std::shared_ptr<PathDiagnosticPiece> LastPiece; |
2560 | for (auto &V : visitors) { |
2561 | V->finalizeVisitor(BRC, ErrorNode, *R); |
2562 | |
2563 | if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) { |
2564 | (0) . __assert_fail ("!LastPiece && \"There can only be one final piece in a diagnostic.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2565, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!LastPiece && |
2565 | (0) . __assert_fail ("!LastPiece && \"There can only be one final piece in a diagnostic.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2565, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "There can only be one final piece in a diagnostic."); |
2566 | LastPiece = std::move(Piece); |
2567 | (*Notes)[ErrorNode].push_back(LastPiece); |
2568 | } |
2569 | } |
2570 | break; |
2571 | } |
2572 | |
2573 | for (auto &V : visitors) { |
2574 | auto P = V->VisitNode(NextNode, BRC, *R); |
2575 | if (P) |
2576 | (*Notes)[NextNode].push_back(std::move(P)); |
2577 | } |
2578 | |
2579 | if (!R->isValid()) |
2580 | break; |
2581 | |
2582 | NextNode = Pred; |
2583 | } |
2584 | |
2585 | return Notes; |
2586 | } |
2587 | |
2588 | |
2589 | |
2590 | |
2591 | static |
2592 | std::pair<BugReport*, std::unique_ptr<VisitorsDiagnosticsTy>> findValidReport( |
2593 | TrimmedGraph &TrimG, |
2594 | ReportGraph &ErrorGraph, |
2595 | ArrayRef<BugReport *> &bugReports, |
2596 | AnalyzerOptions &Opts, |
2597 | GRBugReporter &Reporter) { |
2598 | |
2599 | while (TrimG.popNextReportGraph(ErrorGraph)) { |
2600 | |
2601 | assert(ErrorGraph.Index < bugReports.size()); |
2602 | BugReport *R = bugReports[ErrorGraph.Index]; |
2603 | (0) . __assert_fail ("R && \"No original report found for sliced graph.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2603, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(R && "No original report found for sliced graph."); |
2604 | (0) . __assert_fail ("R->isValid() && \"Report selected by trimmed graph marked invalid.\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2604, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(R->isValid() && "Report selected by trimmed graph marked invalid."); |
2605 | const ExplodedNode *ErrorNode = ErrorGraph.ErrorNode; |
2606 | |
2607 | |
2608 | |
2609 | R->addVisitor(llvm::make_unique<LikelyFalsePositiveSuppressionBRVisitor>()); |
2610 | |
2611 | |
2612 | R->addVisitor(llvm::make_unique<NilReceiverBRVisitor>()); |
2613 | R->addVisitor(llvm::make_unique<ConditionBRVisitor>()); |
2614 | R->addVisitor(llvm::make_unique<CXXSelfAssignmentBRVisitor>()); |
2615 | |
2616 | BugReporterContext BRC(Reporter, ErrorGraph.BackMap); |
2617 | |
2618 | |
2619 | std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes = |
2620 | generateVisitorsDiagnostics(R, ErrorNode, BRC); |
2621 | |
2622 | if (R->isValid()) { |
2623 | if (Opts.ShouldCrosscheckWithZ3) { |
2624 | |
2625 | |
2626 | R->clearVisitors(); |
2627 | R->addVisitor(llvm::make_unique<FalsePositiveRefutationBRVisitor>()); |
2628 | |
2629 | |
2630 | |
2631 | generateVisitorsDiagnostics(R, ErrorGraph.ErrorNode, BRC); |
2632 | } |
2633 | |
2634 | |
2635 | if (R->isValid()) |
2636 | return std::make_pair(R, std::move(visitorNotes)); |
2637 | } |
2638 | } |
2639 | |
2640 | return std::make_pair(nullptr, llvm::make_unique<VisitorsDiagnosticsTy>()); |
2641 | } |
2642 | |
2643 | std::unique_ptr<DiagnosticForConsumerMapTy> |
2644 | GRBugReporter::generatePathDiagnostics( |
2645 | ArrayRef<PathDiagnosticConsumer *> consumers, |
2646 | ArrayRef<BugReport *> &bugReports) { |
2647 | assert(!bugReports.empty()); |
2648 | |
2649 | auto Out = llvm::make_unique<DiagnosticForConsumerMapTy>(); |
2650 | bool HasValid = false; |
2651 | SmallVector<const ExplodedNode *, 32> errorNodes; |
2652 | for (const auto I : bugReports) { |
2653 | if (I->isValid()) { |
2654 | HasValid = true; |
2655 | errorNodes.push_back(I->getErrorNode()); |
2656 | } else { |
2657 | |
2658 | errorNodes.push_back(nullptr); |
2659 | } |
2660 | } |
2661 | |
2662 | |
2663 | |
2664 | if (!HasValid) |
2665 | return Out; |
2666 | |
2667 | TrimmedGraph TrimG(&getGraph(), errorNodes); |
2668 | ReportGraph ErrorGraph; |
2669 | auto ReportInfo = findValidReport(TrimG, ErrorGraph, bugReports, |
2670 | getAnalyzerOptions(), *this); |
2671 | BugReport *R = ReportInfo.first; |
2672 | |
2673 | if (R && R->isValid()) { |
2674 | const ExplodedNode *ErrorNode = ErrorGraph.ErrorNode; |
2675 | for (PathDiagnosticConsumer *PC : consumers) { |
2676 | PathDiagnosticBuilder PDB(*this, R, ErrorGraph.BackMap, PC); |
2677 | std::unique_ptr<PathDiagnostic> PD = generatePathDiagnosticForConsumer( |
2678 | PC->getGenerationScheme(), PDB, ErrorNode, *ReportInfo.second); |
2679 | (*Out)[PC] = std::move(PD); |
2680 | } |
2681 | } |
2682 | |
2683 | return Out; |
2684 | } |
2685 | |
2686 | void BugReporter::Register(const BugType *BT) { |
2687 | BugTypes = F.add(BugTypes, BT); |
2688 | } |
2689 | |
2690 | void BugReporter::emitReport(std::unique_ptr<BugReport> R) { |
2691 | if (const ExplodedNode *E = R->getErrorNode()) { |
2692 | |
2693 | |
2694 | (0) . __assert_fail ("(E->isSink() || E->getLocation().getTag()) && \"Error node must either be a sink or have a tag\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2695, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert((E->isSink() || E->getLocation().getTag()) && |
2695 | (0) . __assert_fail ("(E->isSink() || E->getLocation().getTag()) && \"Error node must either be a sink or have a tag\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2695, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true"> "Error node must either be a sink or have a tag"); |
2696 | |
2697 | const AnalysisDeclContext *DeclCtx = |
2698 | E->getLocationContext()->getAnalysisDeclContext(); |
2699 | |
2700 | |
2701 | |
2702 | |
2703 | if (DeclCtx->isBodyAutosynthesized() && |
2704 | !DeclCtx->isBodyAutosynthesizedFromModelFile()) |
2705 | return; |
2706 | } |
2707 | |
2708 | bool ValidSourceLoc = R->getLocation(getSourceManager()).isValid(); |
2709 | assert(ValidSourceLoc); |
2710 | |
2711 | |
2712 | if (!ValidSourceLoc) |
2713 | return; |
2714 | |
2715 | |
2716 | llvm::FoldingSetNodeID ID; |
2717 | R->Profile(ID); |
2718 | |
2719 | |
2720 | const BugType& BT = R->getBugType(); |
2721 | Register(&BT); |
2722 | void *InsertPos; |
2723 | BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
2724 | |
2725 | if (!EQ) { |
2726 | EQ = new BugReportEquivClass(std::move(R)); |
2727 | EQClasses.InsertNode(EQ, InsertPos); |
2728 | EQClassesVector.push_back(EQ); |
2729 | } else |
2730 | EQ->AddReport(std::move(R)); |
2731 | } |
2732 | |
2733 | |
2734 | |
2735 | |
2736 | |
2737 | namespace { |
2738 | |
2739 | struct FRIEC_WLItem { |
2740 | const ExplodedNode *N; |
2741 | ExplodedNode::const_succ_iterator I, E; |
2742 | |
2743 | FRIEC_WLItem(const ExplodedNode *n) |
2744 | : N(n), I(N->succ_begin()), E(N->succ_end()) {} |
2745 | }; |
2746 | |
2747 | } |
2748 | |
2749 | static const CFGBlock *findBlockForNode(const ExplodedNode *N) { |
2750 | ProgramPoint P = N->getLocation(); |
2751 | if (auto BEP = P.getAs<BlockEntrance>()) |
2752 | return BEP->getBlock(); |
2753 | |
2754 | |
2755 | if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) |
2756 | return N->getLocationContext()->getAnalysisDeclContext() |
2757 | ->getCFGStmtMap()->getBlock(S); |
2758 | |
2759 | return nullptr; |
2760 | } |
2761 | |
2762 | |
2763 | |
2764 | |
2765 | |
2766 | |
2767 | |
2768 | static bool isImmediateSinkBlock(const CFGBlock *Blk) { |
2769 | if (Blk->hasNoReturnElement()) |
2770 | return true; |
2771 | |
2772 | |
2773 | |
2774 | |
2775 | |
2776 | |
2777 | |
2778 | if (std::any_of(Blk->begin(), Blk->end(), [](const CFGElement &Elm) { |
2779 | if (Optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>()) |
2780 | if (isa<CXXThrowExpr>(StmtElm->getStmt())) |
2781 | return true; |
2782 | return false; |
2783 | })) |
2784 | return true; |
2785 | |
2786 | return false; |
2787 | } |
2788 | |
2789 | |
2790 | |
2791 | |
2792 | |
2793 | static bool isInevitablySinking(const ExplodedNode *N) { |
2794 | const CFG &Cfg = N->getCFG(); |
2795 | |
2796 | const CFGBlock *StartBlk = findBlockForNode(N); |
2797 | if (!StartBlk) |
2798 | return false; |
2799 | if (isImmediateSinkBlock(StartBlk)) |
2800 | return true; |
2801 | |
2802 | llvm::SmallVector<const CFGBlock *, 32> DFSWorkList; |
2803 | llvm::SmallPtrSet<const CFGBlock *, 32> Visited; |
2804 | |
2805 | DFSWorkList.push_back(StartBlk); |
2806 | while (!DFSWorkList.empty()) { |
2807 | const CFGBlock *Blk = DFSWorkList.back(); |
2808 | DFSWorkList.pop_back(); |
2809 | Visited.insert(Blk); |
2810 | |
2811 | |
2812 | |
2813 | |
2814 | |
2815 | if (Blk == &Cfg.getExit()) |
2816 | return false; |
2817 | |
2818 | for (const auto &Succ : Blk->succs()) { |
2819 | if (const CFGBlock *SuccBlk = Succ.getReachableBlock()) { |
2820 | if (!isImmediateSinkBlock(SuccBlk) && !Visited.count(SuccBlk)) { |
2821 | |
2822 | |
2823 | DFSWorkList.push_back(SuccBlk); |
2824 | } |
2825 | } |
2826 | } |
2827 | } |
2828 | |
2829 | |
2830 | return true; |
2831 | } |
2832 | |
2833 | static BugReport * |
2834 | FindReportInEquivalenceClass(BugReportEquivClass& EQ, |
2835 | SmallVectorImpl<BugReport*> &bugReports) { |
2836 | BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end(); |
2837 | assert(I != E); |
2838 | const BugType& BT = I->getBugType(); |
2839 | |
2840 | |
2841 | |
2842 | |
2843 | if (!BT.isSuppressOnSink()) { |
2844 | BugReport *R = &*I; |
2845 | for (auto &I : EQ) { |
2846 | const ExplodedNode *N = I.getErrorNode(); |
2847 | if (N) { |
2848 | R = &I; |
2849 | bugReports.push_back(R); |
2850 | } |
2851 | } |
2852 | return R; |
2853 | } |
2854 | |
2855 | |
2856 | |
2857 | |
2858 | |
2859 | |
2860 | |
2861 | BugReport *exampleReport = nullptr; |
2862 | |
2863 | for (; I != E; ++I) { |
2864 | const ExplodedNode *errorNode = I->getErrorNode(); |
2865 | |
2866 | if (!errorNode) |
2867 | continue; |
2868 | if (errorNode->isSink()) { |
2869 | llvm_unreachable( |
2870 | "BugType::isSuppressSink() should not be 'true' for sink end nodes"); |
2871 | } |
2872 | |
2873 | if (errorNode->succ_empty()) { |
2874 | bugReports.push_back(&*I); |
2875 | if (!exampleReport) |
2876 | exampleReport = &*I; |
2877 | continue; |
2878 | } |
2879 | |
2880 | |
2881 | |
2882 | |
2883 | |
2884 | if (isInevitablySinking(errorNode)) |
2885 | continue; |
2886 | |
2887 | |
2888 | |
2889 | using WLItem = FRIEC_WLItem; |
2890 | using DFSWorkList = SmallVector<WLItem, 10>; |
2891 | |
2892 | llvm::DenseMap<const ExplodedNode *, unsigned> Visited; |
2893 | |
2894 | DFSWorkList WL; |
2895 | WL.push_back(errorNode); |
2896 | Visited[errorNode] = 1; |
2897 | |
2898 | while (!WL.empty()) { |
2899 | WLItem &WI = WL.back(); |
2900 | succ_empty()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/BugReporter.cpp", 2900, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!WI.N->succ_empty()); |
2901 | |
2902 | for (; WI.I != WI.E; ++WI.I) { |
2903 | const ExplodedNode *Succ = *WI.I; |
2904 | |
2905 | if (Succ->succ_empty()) { |
2906 | |
2907 | if (!Succ->isSink()) { |
2908 | bugReports.push_back(&*I); |
2909 | if (!exampleReport) |
2910 | exampleReport = &*I; |
2911 | WL.clear(); |
2912 | break; |
2913 | } |
2914 | |
2915 | continue; |
2916 | } |
2917 | |
2918 | |
2919 | unsigned &mark = Visited[Succ]; |
2920 | if (!mark) { |
2921 | mark = 1; |
2922 | WL.push_back(Succ); |
2923 | break; |
2924 | } |
2925 | } |
2926 | |
2927 | |
2928 | |
2929 | if (!WL.empty() && &WL.back() == &WI) |
2930 | WL.pop_back(); |
2931 | } |
2932 | } |
2933 | |
2934 | |
2935 | |
2936 | return exampleReport; |
2937 | } |
2938 | |
2939 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
2940 | SmallVector<BugReport*, 10> bugReports; |
2941 | BugReport *report = FindReportInEquivalenceClass(EQ, bugReports); |
2942 | if (!report) |
2943 | return; |
2944 | |
2945 | ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers(); |
2946 | std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics = |
2947 | generateDiagnosticForConsumerMap(report, Consumers, bugReports); |
2948 | |
2949 | for (auto &P : *Diagnostics) { |
2950 | PathDiagnosticConsumer *Consumer = P.first; |
2951 | std::unique_ptr<PathDiagnostic> &PD = P.second; |
2952 | |
2953 | |
2954 | |
2955 | if (PD->path.empty()) { |
2956 | PathDiagnosticLocation L = report->getLocation(getSourceManager()); |
2957 | auto piece = llvm::make_unique<PathDiagnosticEventPiece>( |
2958 | L, report->getDescription()); |
2959 | for (SourceRange Range : report->getRanges()) |
2960 | piece->addRange(Range); |
2961 | PD->setEndOfPath(std::move(piece)); |
2962 | } |
2963 | |
2964 | PathPieces &Pieces = PD->getMutablePieces(); |
2965 | if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) { |
2966 | |
2967 | |
2968 | for (auto I = report->getNotes().rbegin(), |
2969 | E = report->getNotes().rend(); I != E; ++I) { |
2970 | PathDiagnosticNotePiece *Piece = I->get(); |
2971 | auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>( |
2972 | Piece->getLocation(), Piece->getString()); |
2973 | for (const auto &R: Piece->getRanges()) |
2974 | ConvertedPiece->addRange(R); |
2975 | |
2976 | Pieces.push_front(std::move(ConvertedPiece)); |
2977 | } |
2978 | } else { |
2979 | for (auto I = report->getNotes().rbegin(), |
2980 | E = report->getNotes().rend(); I != E; ++I) |
2981 | Pieces.push_front(*I); |
2982 | } |
2983 | |
2984 | |
2985 | const BugReport::ExtraTextList &Meta = report->getExtraText(); |
2986 | for (const auto &i : Meta) |
2987 | PD->addMeta(i); |
2988 | |
2989 | updateExecutedLinesWithDiagnosticPieces(*PD); |
2990 | Consumer->HandlePathDiagnostic(std::move(PD)); |
2991 | } |
2992 | } |
2993 | |
2994 | |
2995 | |
2996 | static void populateExecutedLinesWithFunctionSignature( |
2997 | const Decl *Signature, SourceManager &SM, |
2998 | FilesToLineNumsMap &ExecutedLines) { |
2999 | SourceRange SignatureSourceRange; |
3000 | const Stmt* Body = Signature->getBody(); |
3001 | if (const auto FD = dyn_cast<FunctionDecl>(Signature)) { |
3002 | SignatureSourceRange = FD->getSourceRange(); |
3003 | } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) { |
3004 | SignatureSourceRange = OD->getSourceRange(); |
3005 | } else { |
3006 | return; |
3007 | } |
3008 | SourceLocation Start = SignatureSourceRange.getBegin(); |
3009 | SourceLocation End = Body ? Body->getSourceRange().getBegin() |
3010 | : SignatureSourceRange.getEnd(); |
3011 | if (!Start.isValid() || !End.isValid()) |
3012 | return; |
3013 | unsigned StartLine = SM.getExpansionLineNumber(Start); |
3014 | unsigned EndLine = SM.getExpansionLineNumber(End); |
3015 | |
3016 | FileID FID = SM.getFileID(SM.getExpansionLoc(Start)); |
3017 | for (unsigned Line = StartLine; Line <= EndLine; Line++) |
3018 | ExecutedLines[FID].insert(Line); |
3019 | } |
3020 | |
3021 | static void populateExecutedLinesWithStmt( |
3022 | const Stmt *S, SourceManager &SM, |
3023 | FilesToLineNumsMap &ExecutedLines) { |
3024 | SourceLocation Loc = S->getSourceRange().getBegin(); |
3025 | if (!Loc.isValid()) |
3026 | return; |
3027 | SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); |
3028 | FileID FID = SM.getFileID(ExpansionLoc); |
3029 | unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc); |
3030 | ExecutedLines[FID].insert(LineNo); |
3031 | } |
3032 | |
3033 | |
3034 | |
3035 | static std::unique_ptr<FilesToLineNumsMap> |
3036 | findExecutedLines(SourceManager &SM, const ExplodedNode *N) { |
3037 | auto ExecutedLines = llvm::make_unique<FilesToLineNumsMap>(); |
3038 | |
3039 | while (N) { |
3040 | if (N->getFirstPred() == nullptr) { |
3041 | |
3042 | const Decl *D = N->getLocationContext()->getDecl(); |
3043 | populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines); |
3044 | } else if (auto CE = N->getLocationAs<CallEnter>()) { |
3045 | |
3046 | const Decl* D = CE->getCalleeContext()->getDecl(); |
3047 | populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines); |
3048 | } else if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) { |
3049 | populateExecutedLinesWithStmt(S, SM, *ExecutedLines); |
3050 | |
3051 | |
3052 | const Stmt *P = N->getParentMap().getParent(S); |
3053 | |
3054 | |
3055 | |
3056 | |
3057 | if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) { |
3058 | populateExecutedLinesWithStmt(RS, SM, *ExecutedLines); |
3059 | P = N->getParentMap().getParent(RS); |
3060 | } |
3061 | |
3062 | if (P && (isa<SwitchCase>(P) || isa<LabelStmt>(P))) |
3063 | populateExecutedLinesWithStmt(P, SM, *ExecutedLines); |
3064 | } |
3065 | |
3066 | N = N->getFirstPred(); |
3067 | } |
3068 | return ExecutedLines; |
3069 | } |
3070 | |
3071 | std::unique_ptr<DiagnosticForConsumerMapTy> |
3072 | BugReporter::generateDiagnosticForConsumerMap( |
3073 | BugReport *report, ArrayRef<PathDiagnosticConsumer *> consumers, |
3074 | ArrayRef<BugReport *> bugReports) { |
3075 | |
3076 | if (!report->isPathSensitive()) { |
3077 | auto Out = llvm::make_unique<DiagnosticForConsumerMapTy>(); |
3078 | for (auto *Consumer : consumers) |
3079 | (*Out)[Consumer] = generateEmptyDiagnosticForReport(report, |
3080 | getSourceManager()); |
3081 | return Out; |
3082 | } |
3083 | |
3084 | |
3085 | |
3086 | |
3087 | |
3088 | assert(!bugReports.empty()); |
3089 | MaxBugClassSize.updateMax(bugReports.size()); |
3090 | std::unique_ptr<DiagnosticForConsumerMapTy> Out = |
3091 | generatePathDiagnostics(consumers, bugReports); |
3092 | |
3093 | if (Out->empty()) |
3094 | return Out; |
3095 | |
3096 | MaxValidBugClassSize.updateMax(bugReports.size()); |
3097 | |
3098 | |
3099 | |
3100 | AnalyzerOptions &Opts = getAnalyzerOptions(); |
3101 | for (auto const &P : *Out) |
3102 | if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll) |
3103 | P.second->resetDiagnosticLocationToMainFile(); |
3104 | |
3105 | return Out; |
3106 | } |
3107 | |
3108 | void BugReporter::EmitBasicReport(const Decl *DeclWithIssue, |
3109 | const CheckerBase *Checker, |
3110 | StringRef Name, StringRef Category, |
3111 | StringRef Str, PathDiagnosticLocation Loc, |
3112 | ArrayRef<SourceRange> Ranges) { |
3113 | EmitBasicReport(DeclWithIssue, Checker->getCheckName(), Name, Category, Str, |
3114 | Loc, Ranges); |
3115 | } |
3116 | |
3117 | void BugReporter::EmitBasicReport(const Decl *DeclWithIssue, |
3118 | CheckName CheckName, |
3119 | StringRef name, StringRef category, |
3120 | StringRef str, PathDiagnosticLocation Loc, |
3121 | ArrayRef<SourceRange> Ranges) { |
3122 | |
3123 | BugType *BT = getBugTypeForName(CheckName, name, category); |
3124 | auto R = llvm::make_unique<BugReport>(*BT, str, Loc); |
3125 | R->setDeclWithIssue(DeclWithIssue); |
3126 | for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end(); |
3127 | I != E; ++I) |
3128 | R->addRange(*I); |
3129 | emitReport(std::move(R)); |
3130 | } |
3131 | |
3132 | BugType *BugReporter::getBugTypeForName(CheckName CheckName, StringRef name, |
3133 | StringRef category) { |
3134 | SmallString<136> fullDesc; |
3135 | llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name |
3136 | << ":" << category; |
3137 | BugType *&BT = StrBugTypes[fullDesc]; |
3138 | if (!BT) |
3139 | BT = new BugType(CheckName, name, category); |
3140 | return BT; |
3141 | } |
3142 | |