]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/StaticAnalyzer/Core/PathDiagnostic.cpp
MFV r325013,r325034: 640 number_to_scaled_string is duplicated in several commands
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / StaticAnalyzer / Core / PathDiagnostic.cpp
1 //===--- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the PathDiagnostic-related interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclCXX.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/StmtCXX.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Support/raw_ostream.h"
27
28 using namespace clang;
29 using namespace ento;
30
31 bool PathDiagnosticMacroPiece::containsEvent() const {
32   for (auto &P : subPieces) {
33     if (isa<PathDiagnosticEventPiece>(*P))
34       return true;
35     if (auto *MP = dyn_cast<PathDiagnosticMacroPiece>(P.get()))
36       if (MP->containsEvent())
37         return true;
38   }
39   return false;
40 }
41
42 static StringRef StripTrailingDots(StringRef s) {
43   for (StringRef::size_type i = s.size(); i != 0; --i)
44     if (s[i - 1] != '.')
45       return s.substr(0, i);
46   return "";
47 }
48
49 PathDiagnosticPiece::PathDiagnosticPiece(StringRef s,
50                                          Kind k, DisplayHint hint)
51   : str(StripTrailingDots(s)), kind(k), Hint(hint),
52     LastInMainSourceFile(false) {}
53
54 PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
55   : kind(k), Hint(hint), LastInMainSourceFile(false) {}
56
57 PathDiagnosticPiece::~PathDiagnosticPiece() {}
58 PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
59 PathDiagnosticCallPiece::~PathDiagnosticCallPiece() {}
60 PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
61 PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {}
62 PathDiagnosticNotePiece::~PathDiagnosticNotePiece() {}
63
64 void PathPieces::flattenTo(PathPieces &Primary, PathPieces &Current,
65                            bool ShouldFlattenMacros) const {
66   for (auto &Piece : *this) {
67     switch (Piece->getKind()) {
68     case PathDiagnosticPiece::Call: {
69       auto &Call = cast<PathDiagnosticCallPiece>(*Piece);
70       if (auto CallEnter = Call.getCallEnterEvent())
71         Current.push_back(std::move(CallEnter));
72       Call.path.flattenTo(Primary, Primary, ShouldFlattenMacros);
73       if (auto callExit = Call.getCallExitEvent())
74         Current.push_back(std::move(callExit));
75       break;
76     }
77     case PathDiagnosticPiece::Macro: {
78       auto &Macro = cast<PathDiagnosticMacroPiece>(*Piece);
79       if (ShouldFlattenMacros) {
80         Macro.subPieces.flattenTo(Primary, Primary, ShouldFlattenMacros);
81       } else {
82         Current.push_back(Piece);
83         PathPieces NewPath;
84         Macro.subPieces.flattenTo(Primary, NewPath, ShouldFlattenMacros);
85         // FIXME: This probably shouldn't mutate the original path piece.
86         Macro.subPieces = NewPath;
87       }
88       break;
89     }
90     case PathDiagnosticPiece::Event:
91     case PathDiagnosticPiece::ControlFlow:
92     case PathDiagnosticPiece::Note:
93       Current.push_back(Piece);
94       break;
95     }
96   }
97 }
98
99 PathDiagnostic::~PathDiagnostic() {}
100
101 PathDiagnostic::PathDiagnostic(StringRef CheckName, const Decl *declWithIssue,
102                                StringRef bugtype, StringRef verboseDesc,
103                                StringRef shortDesc, StringRef category,
104                                PathDiagnosticLocation LocationToUnique,
105                                const Decl *DeclToUnique)
106   : CheckName(CheckName),
107     DeclWithIssue(declWithIssue),
108     BugType(StripTrailingDots(bugtype)),
109     VerboseDesc(StripTrailingDots(verboseDesc)),
110     ShortDesc(StripTrailingDots(shortDesc)),
111     Category(StripTrailingDots(category)),
112     UniqueingLoc(LocationToUnique),
113     UniqueingDecl(DeclToUnique),
114     path(pathImpl) {}
115
116 static PathDiagnosticCallPiece *
117 getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP,
118                                 const SourceManager &SMgr) {
119   SourceLocation CallLoc = CP->callEnter.asLocation();
120
121   // If the call is within a macro, don't do anything (for now).
122   if (CallLoc.isMacroID())
123     return nullptr;
124
125   assert(SMgr.isInMainFile(CallLoc) &&
126          "The call piece should be in the main file.");
127
128   // Check if CP represents a path through a function outside of the main file.
129   if (!SMgr.isInMainFile(CP->callEnterWithin.asLocation()))
130     return CP;
131
132   const PathPieces &Path = CP->path;
133   if (Path.empty())
134     return nullptr;
135
136   // Check if the last piece in the callee path is a call to a function outside
137   // of the main file.
138   if (PathDiagnosticCallPiece *CPInner =
139           dyn_cast<PathDiagnosticCallPiece>(Path.back().get())) {
140     return getFirstStackedCallToHeaderFile(CPInner, SMgr);
141   }
142
143   // Otherwise, the last piece is in the main file.
144   return nullptr;
145 }
146
147 void PathDiagnostic::resetDiagnosticLocationToMainFile() {
148   if (path.empty())
149     return;
150
151   PathDiagnosticPiece *LastP = path.back().get();
152   assert(LastP);
153   const SourceManager &SMgr = LastP->getLocation().getManager();
154
155   // We only need to check if the report ends inside headers, if the last piece
156   // is a call piece.
157   if (PathDiagnosticCallPiece *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
158     CP = getFirstStackedCallToHeaderFile(CP, SMgr);
159     if (CP) {
160       // Mark the piece.
161        CP->setAsLastInMainSourceFile();
162
163       // Update the path diagnostic message.
164       const NamedDecl *ND = dyn_cast<NamedDecl>(CP->getCallee());
165       if (ND) {
166         SmallString<200> buf;
167         llvm::raw_svector_ostream os(buf);
168         os << " (within a call to '" << ND->getDeclName() << "')";
169         appendToDesc(os.str());
170       }
171
172       // Reset the report containing declaration and location.
173       DeclWithIssue = CP->getCaller();
174       Loc = CP->getLocation();
175
176       return;
177     }
178   }
179 }
180
181 void PathDiagnosticConsumer::anchor() { }
182
183 PathDiagnosticConsumer::~PathDiagnosticConsumer() {
184   // Delete the contents of the FoldingSet if it isn't empty already.
185   for (llvm::FoldingSet<PathDiagnostic>::iterator it =
186        Diags.begin(), et = Diags.end() ; it != et ; ++it) {
187     delete &*it;
188   }
189 }
190
191 void PathDiagnosticConsumer::HandlePathDiagnostic(
192     std::unique_ptr<PathDiagnostic> D) {
193   if (!D || D->path.empty())
194     return;
195
196   // We need to flatten the locations (convert Stmt* to locations) because
197   // the referenced statements may be freed by the time the diagnostics
198   // are emitted.
199   D->flattenLocations();
200
201   // If the PathDiagnosticConsumer does not support diagnostics that
202   // cross file boundaries, prune out such diagnostics now.
203   if (!supportsCrossFileDiagnostics()) {
204     // Verify that the entire path is from the same FileID.
205     FileID FID;
206     const SourceManager &SMgr = D->path.front()->getLocation().getManager();
207     SmallVector<const PathPieces *, 5> WorkList;
208     WorkList.push_back(&D->path);
209     SmallString<128> buf;
210     llvm::raw_svector_ostream warning(buf);
211     warning << "warning: Path diagnostic report is not generated. Current "
212             << "output format does not support diagnostics that cross file "
213             << "boundaries. Refer to --analyzer-output for valid output "
214             << "formats\n";
215
216     while (!WorkList.empty()) {
217       const PathPieces &path = *WorkList.pop_back_val();
218
219       for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E;
220            ++I) {
221         const PathDiagnosticPiece *piece = I->get();
222         FullSourceLoc L = piece->getLocation().asLocation().getExpansionLoc();
223
224         if (FID.isInvalid()) {
225           FID = SMgr.getFileID(L);
226         } else if (SMgr.getFileID(L) != FID) {
227           llvm::errs() << warning.str();
228           return;
229         }
230
231         // Check the source ranges.
232         ArrayRef<SourceRange> Ranges = piece->getRanges();
233         for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
234                                              E = Ranges.end(); I != E; ++I) {
235           SourceLocation L = SMgr.getExpansionLoc(I->getBegin());
236           if (!L.isFileID() || SMgr.getFileID(L) != FID) {
237             llvm::errs() << warning.str();
238             return;
239           }
240           L = SMgr.getExpansionLoc(I->getEnd());
241           if (!L.isFileID() || SMgr.getFileID(L) != FID) {
242             llvm::errs() << warning.str();
243             return;
244           }
245         }
246
247         if (const PathDiagnosticCallPiece *call =
248             dyn_cast<PathDiagnosticCallPiece>(piece)) {
249           WorkList.push_back(&call->path);
250         }
251         else if (const PathDiagnosticMacroPiece *macro =
252                  dyn_cast<PathDiagnosticMacroPiece>(piece)) {
253           WorkList.push_back(&macro->subPieces);
254         }
255       }
256     }
257
258     if (FID.isInvalid())
259       return; // FIXME: Emit a warning?
260   }
261
262   // Profile the node to see if we already have something matching it
263   llvm::FoldingSetNodeID profile;
264   D->Profile(profile);
265   void *InsertPos = nullptr;
266
267   if (PathDiagnostic *orig = Diags.FindNodeOrInsertPos(profile, InsertPos)) {
268     // Keep the PathDiagnostic with the shorter path.
269     // Note, the enclosing routine is called in deterministic order, so the
270     // results will be consistent between runs (no reason to break ties if the
271     // size is the same).
272     const unsigned orig_size = orig->full_size();
273     const unsigned new_size = D->full_size();
274     if (orig_size <= new_size)
275       return;
276
277     assert(orig != D.get());
278     Diags.RemoveNode(orig);
279     delete orig;
280   }
281
282   Diags.InsertNode(D.release());
283 }
284
285 static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y);
286
287 static Optional<bool>
288 compareControlFlow(const PathDiagnosticControlFlowPiece &X,
289                    const PathDiagnosticControlFlowPiece &Y) {
290   FullSourceLoc XSL = X.getStartLocation().asLocation();
291   FullSourceLoc YSL = Y.getStartLocation().asLocation();
292   if (XSL != YSL)
293     return XSL.isBeforeInTranslationUnitThan(YSL);
294   FullSourceLoc XEL = X.getEndLocation().asLocation();
295   FullSourceLoc YEL = Y.getEndLocation().asLocation();
296   if (XEL != YEL)
297     return XEL.isBeforeInTranslationUnitThan(YEL);
298   return None;
299 }
300
301 static Optional<bool> compareMacro(const PathDiagnosticMacroPiece &X,
302                                    const PathDiagnosticMacroPiece &Y) {
303   return comparePath(X.subPieces, Y.subPieces);
304 }
305
306 static Optional<bool> compareCall(const PathDiagnosticCallPiece &X,
307                                   const PathDiagnosticCallPiece &Y) {
308   FullSourceLoc X_CEL = X.callEnter.asLocation();
309   FullSourceLoc Y_CEL = Y.callEnter.asLocation();
310   if (X_CEL != Y_CEL)
311     return X_CEL.isBeforeInTranslationUnitThan(Y_CEL);
312   FullSourceLoc X_CEWL = X.callEnterWithin.asLocation();
313   FullSourceLoc Y_CEWL = Y.callEnterWithin.asLocation();
314   if (X_CEWL != Y_CEWL)
315     return X_CEWL.isBeforeInTranslationUnitThan(Y_CEWL);
316   FullSourceLoc X_CRL = X.callReturn.asLocation();
317   FullSourceLoc Y_CRL = Y.callReturn.asLocation();
318   if (X_CRL != Y_CRL)
319     return X_CRL.isBeforeInTranslationUnitThan(Y_CRL);
320   return comparePath(X.path, Y.path);
321 }
322
323 static Optional<bool> comparePiece(const PathDiagnosticPiece &X,
324                                    const PathDiagnosticPiece &Y) {
325   if (X.getKind() != Y.getKind())
326     return X.getKind() < Y.getKind();
327
328   FullSourceLoc XL = X.getLocation().asLocation();
329   FullSourceLoc YL = Y.getLocation().asLocation();
330   if (XL != YL)
331     return XL.isBeforeInTranslationUnitThan(YL);
332
333   if (X.getString() != Y.getString())
334     return X.getString() < Y.getString();
335
336   if (X.getRanges().size() != Y.getRanges().size())
337     return X.getRanges().size() < Y.getRanges().size();
338
339   const SourceManager &SM = XL.getManager();
340
341   for (unsigned i = 0, n = X.getRanges().size(); i < n; ++i) {
342     SourceRange XR = X.getRanges()[i];
343     SourceRange YR = Y.getRanges()[i];
344     if (XR != YR) {
345       if (XR.getBegin() != YR.getBegin())
346         return SM.isBeforeInTranslationUnit(XR.getBegin(), YR.getBegin());
347       return SM.isBeforeInTranslationUnit(XR.getEnd(), YR.getEnd());
348     }
349   }
350
351   switch (X.getKind()) {
352     case PathDiagnosticPiece::ControlFlow:
353       return compareControlFlow(cast<PathDiagnosticControlFlowPiece>(X),
354                                 cast<PathDiagnosticControlFlowPiece>(Y));
355     case PathDiagnosticPiece::Event:
356     case PathDiagnosticPiece::Note:
357       return None;
358     case PathDiagnosticPiece::Macro:
359       return compareMacro(cast<PathDiagnosticMacroPiece>(X),
360                           cast<PathDiagnosticMacroPiece>(Y));
361     case PathDiagnosticPiece::Call:
362       return compareCall(cast<PathDiagnosticCallPiece>(X),
363                          cast<PathDiagnosticCallPiece>(Y));
364   }
365   llvm_unreachable("all cases handled");
366 }
367
368 static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y) {
369   if (X.size() != Y.size())
370     return X.size() < Y.size();
371
372   PathPieces::const_iterator X_I = X.begin(), X_end = X.end();
373   PathPieces::const_iterator Y_I = Y.begin(), Y_end = Y.end();
374
375   for ( ; X_I != X_end && Y_I != Y_end; ++X_I, ++Y_I) {
376     Optional<bool> b = comparePiece(**X_I, **Y_I);
377     if (b.hasValue())
378       return b.getValue();
379   }
380
381   return None;
382 }
383
384 static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) {
385   FullSourceLoc XL = X.getLocation().asLocation();
386   FullSourceLoc YL = Y.getLocation().asLocation();
387   if (XL != YL)
388     return XL.isBeforeInTranslationUnitThan(YL);
389   if (X.getBugType() != Y.getBugType())
390     return X.getBugType() < Y.getBugType();
391   if (X.getCategory() != Y.getCategory())
392     return X.getCategory() < Y.getCategory();
393   if (X.getVerboseDescription() != Y.getVerboseDescription())
394     return X.getVerboseDescription() < Y.getVerboseDescription();
395   if (X.getShortDescription() != Y.getShortDescription())
396     return X.getShortDescription() < Y.getShortDescription();
397   if (X.getDeclWithIssue() != Y.getDeclWithIssue()) {
398     const Decl *XD = X.getDeclWithIssue();
399     if (!XD)
400       return true;
401     const Decl *YD = Y.getDeclWithIssue();
402     if (!YD)
403       return false;
404     SourceLocation XDL = XD->getLocation();
405     SourceLocation YDL = YD->getLocation();
406     if (XDL != YDL) {
407       const SourceManager &SM = XL.getManager();
408       return SM.isBeforeInTranslationUnit(XDL, YDL);
409     }
410   }
411   PathDiagnostic::meta_iterator XI = X.meta_begin(), XE = X.meta_end();
412   PathDiagnostic::meta_iterator YI = Y.meta_begin(), YE = Y.meta_end();
413   if (XE - XI != YE - YI)
414     return (XE - XI) < (YE - YI);
415   for ( ; XI != XE ; ++XI, ++YI) {
416     if (*XI != *YI)
417       return (*XI) < (*YI);
418   }
419   Optional<bool> b = comparePath(X.path, Y.path);
420   assert(b.hasValue());
421   return b.getValue();
422 }
423
424 void PathDiagnosticConsumer::FlushDiagnostics(
425                                      PathDiagnosticConsumer::FilesMade *Files) {
426   if (flushed)
427     return;
428
429   flushed = true;
430
431   std::vector<const PathDiagnostic *> BatchDiags;
432   for (llvm::FoldingSet<PathDiagnostic>::iterator it = Diags.begin(),
433        et = Diags.end(); it != et; ++it) {
434     const PathDiagnostic *D = &*it;
435     BatchDiags.push_back(D);
436   }
437
438   // Sort the diagnostics so that they are always emitted in a deterministic
439   // order.
440   int (*Comp)(const PathDiagnostic *const *, const PathDiagnostic *const *) =
441       [](const PathDiagnostic *const *X, const PathDiagnostic *const *Y) {
442         assert(*X != *Y && "PathDiagnostics not uniqued!");
443         if (compare(**X, **Y))
444           return -1;
445         assert(compare(**Y, **X) && "Not a total order!");
446         return 1;
447       };
448   array_pod_sort(BatchDiags.begin(), BatchDiags.end(), Comp);
449
450   FlushDiagnosticsImpl(BatchDiags, Files);
451
452   // Delete the flushed diagnostics.
453   for (std::vector<const PathDiagnostic *>::iterator it = BatchDiags.begin(),
454        et = BatchDiags.end(); it != et; ++it) {
455     const PathDiagnostic *D = *it;
456     delete D;
457   }
458
459   // Clear out the FoldingSet.
460   Diags.clear();
461 }
462
463 PathDiagnosticConsumer::FilesMade::~FilesMade() {
464   for (PDFileEntry &Entry : Set)
465     Entry.~PDFileEntry();
466 }
467
468 void PathDiagnosticConsumer::FilesMade::addDiagnostic(const PathDiagnostic &PD,
469                                                       StringRef ConsumerName,
470                                                       StringRef FileName) {
471   llvm::FoldingSetNodeID NodeID;
472   NodeID.Add(PD);
473   void *InsertPos;
474   PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos);
475   if (!Entry) {
476     Entry = Alloc.Allocate<PDFileEntry>();
477     Entry = new (Entry) PDFileEntry(NodeID);
478     Set.InsertNode(Entry, InsertPos);
479   }
480
481   // Allocate persistent storage for the file name.
482   char *FileName_cstr = (char*) Alloc.Allocate(FileName.size(), 1);
483   memcpy(FileName_cstr, FileName.data(), FileName.size());
484
485   Entry->files.push_back(std::make_pair(ConsumerName,
486                                         StringRef(FileName_cstr,
487                                                   FileName.size())));
488 }
489
490 PathDiagnosticConsumer::PDFileEntry::ConsumerFiles *
491 PathDiagnosticConsumer::FilesMade::getFiles(const PathDiagnostic &PD) {
492   llvm::FoldingSetNodeID NodeID;
493   NodeID.Add(PD);
494   void *InsertPos;
495   PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos);
496   if (!Entry)
497     return nullptr;
498   return &Entry->files;
499 }
500
501 //===----------------------------------------------------------------------===//
502 // PathDiagnosticLocation methods.
503 //===----------------------------------------------------------------------===//
504
505 static SourceLocation getValidSourceLocation(const Stmt* S,
506                                              LocationOrAnalysisDeclContext LAC,
507                                              bool UseEnd = false) {
508   SourceLocation L = UseEnd ? S->getLocEnd() : S->getLocStart();
509   assert(!LAC.isNull() && "A valid LocationContext or AnalysisDeclContext should "
510                           "be passed to PathDiagnosticLocation upon creation.");
511
512   // S might be a temporary statement that does not have a location in the
513   // source code, so find an enclosing statement and use its location.
514   if (!L.isValid()) {
515     AnalysisDeclContext *ADC;
516     if (LAC.is<const LocationContext*>())
517       ADC = LAC.get<const LocationContext*>()->getAnalysisDeclContext();
518     else
519       ADC = LAC.get<AnalysisDeclContext*>();
520
521     ParentMap &PM = ADC->getParentMap();
522
523     const Stmt *Parent = S;
524     do {
525       Parent = PM.getParent(Parent);
526
527       // In rare cases, we have implicit top-level expressions,
528       // such as arguments for implicit member initializers.
529       // In this case, fall back to the start of the body (even if we were
530       // asked for the statement end location).
531       if (!Parent) {
532         const Stmt *Body = ADC->getBody();
533         if (Body)
534           L = Body->getLocStart();
535         else
536           L = ADC->getDecl()->getLocEnd();
537         break;
538       }
539
540       L = UseEnd ? Parent->getLocEnd() : Parent->getLocStart();
541     } while (!L.isValid());
542   }
543
544   return L;
545 }
546
547 static PathDiagnosticLocation
548 getLocationForCaller(const StackFrameContext *SFC,
549                      const LocationContext *CallerCtx,
550                      const SourceManager &SM) {
551   const CFGBlock &Block = *SFC->getCallSiteBlock();
552   CFGElement Source = Block[SFC->getIndex()];
553
554   switch (Source.getKind()) {
555   case CFGElement::Statement:
556     return PathDiagnosticLocation(Source.castAs<CFGStmt>().getStmt(),
557                                   SM, CallerCtx);
558   case CFGElement::Initializer: {
559     const CFGInitializer &Init = Source.castAs<CFGInitializer>();
560     return PathDiagnosticLocation(Init.getInitializer()->getInit(),
561                                   SM, CallerCtx);
562   }
563   case CFGElement::AutomaticObjectDtor: {
564     const CFGAutomaticObjDtor &Dtor = Source.castAs<CFGAutomaticObjDtor>();
565     return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(),
566                                              SM, CallerCtx);
567   }
568   case CFGElement::DeleteDtor: {
569     const CFGDeleteDtor &Dtor = Source.castAs<CFGDeleteDtor>();
570     return PathDiagnosticLocation(Dtor.getDeleteExpr(), SM, CallerCtx);
571   }
572   case CFGElement::BaseDtor:
573   case CFGElement::MemberDtor: {
574     const AnalysisDeclContext *CallerInfo = CallerCtx->getAnalysisDeclContext();
575     if (const Stmt *CallerBody = CallerInfo->getBody())
576       return PathDiagnosticLocation::createEnd(CallerBody, SM, CallerCtx);
577     return PathDiagnosticLocation::create(CallerInfo->getDecl(), SM);
578   }
579   case CFGElement::TemporaryDtor:
580   case CFGElement::NewAllocator:
581   case CFGElement::LifetimeEnds:
582     llvm_unreachable("not yet implemented!");
583   }
584
585   llvm_unreachable("Unknown CFGElement kind");
586 }
587
588 PathDiagnosticLocation
589 PathDiagnosticLocation::createBegin(const Decl *D,
590                                     const SourceManager &SM) {
591   return PathDiagnosticLocation(D->getLocStart(), SM, SingleLocK);
592 }
593
594 PathDiagnosticLocation
595 PathDiagnosticLocation::createBegin(const Stmt *S,
596                                     const SourceManager &SM,
597                                     LocationOrAnalysisDeclContext LAC) {
598   return PathDiagnosticLocation(getValidSourceLocation(S, LAC),
599                                 SM, SingleLocK);
600 }
601
602 PathDiagnosticLocation
603 PathDiagnosticLocation::createEnd(const Stmt *S,
604                                   const SourceManager &SM,
605                                   LocationOrAnalysisDeclContext LAC) {
606   if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S))
607     return createEndBrace(CS, SM);
608   return PathDiagnosticLocation(getValidSourceLocation(S, LAC, /*End=*/true),
609                                 SM, SingleLocK);
610 }
611
612 PathDiagnosticLocation
613 PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO,
614                                           const SourceManager &SM) {
615   return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK);
616 }
617
618 PathDiagnosticLocation
619 PathDiagnosticLocation::createConditionalColonLoc(
620                                             const ConditionalOperator *CO,
621                                             const SourceManager &SM) {
622   return PathDiagnosticLocation(CO->getColonLoc(), SM, SingleLocK);
623 }
624
625
626 PathDiagnosticLocation
627 PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME,
628                                         const SourceManager &SM) {
629   return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK);
630 }
631
632 PathDiagnosticLocation
633 PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS,
634                                          const SourceManager &SM) {
635   SourceLocation L = CS->getLBracLoc();
636   return PathDiagnosticLocation(L, SM, SingleLocK);
637 }
638
639 PathDiagnosticLocation
640 PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS,
641                                        const SourceManager &SM) {
642   SourceLocation L = CS->getRBracLoc();
643   return PathDiagnosticLocation(L, SM, SingleLocK);
644 }
645
646 PathDiagnosticLocation
647 PathDiagnosticLocation::createDeclBegin(const LocationContext *LC,
648                                         const SourceManager &SM) {
649   // FIXME: Should handle CXXTryStmt if analyser starts supporting C++.
650   if (const CompoundStmt *CS =
651         dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody()))
652     if (!CS->body_empty()) {
653       SourceLocation Loc = (*CS->body_begin())->getLocStart();
654       return PathDiagnosticLocation(Loc, SM, SingleLocK);
655     }
656
657   return PathDiagnosticLocation();
658 }
659
660 PathDiagnosticLocation
661 PathDiagnosticLocation::createDeclEnd(const LocationContext *LC,
662                                       const SourceManager &SM) {
663   SourceLocation L = LC->getDecl()->getBodyRBrace();
664   return PathDiagnosticLocation(L, SM, SingleLocK);
665 }
666
667 PathDiagnosticLocation
668 PathDiagnosticLocation::create(const ProgramPoint& P,
669                                const SourceManager &SMng) {
670   const Stmt* S = nullptr;
671   if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
672     const CFGBlock *BSrc = BE->getSrc();
673     S = BSrc->getTerminatorCondition();
674   } else if (Optional<StmtPoint> SP = P.getAs<StmtPoint>()) {
675     S = SP->getStmt();
676     if (P.getAs<PostStmtPurgeDeadSymbols>())
677       return PathDiagnosticLocation::createEnd(S, SMng, P.getLocationContext());
678   } else if (Optional<PostInitializer> PIP = P.getAs<PostInitializer>()) {
679     return PathDiagnosticLocation(PIP->getInitializer()->getSourceLocation(),
680                                   SMng);
681   } else if (Optional<PostImplicitCall> PIE = P.getAs<PostImplicitCall>()) {
682     return PathDiagnosticLocation(PIE->getLocation(), SMng);
683   } else if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
684     return getLocationForCaller(CE->getCalleeContext(),
685                                 CE->getLocationContext(),
686                                 SMng);
687   } else if (Optional<CallExitEnd> CEE = P.getAs<CallExitEnd>()) {
688     return getLocationForCaller(CEE->getCalleeContext(),
689                                 CEE->getLocationContext(),
690                                 SMng);
691   } else {
692     llvm_unreachable("Unexpected ProgramPoint");
693   }
694
695   return PathDiagnosticLocation(S, SMng, P.getLocationContext());
696 }
697
698 static const LocationContext *
699 findTopAutosynthesizedParentContext(const LocationContext *LC) {
700   assert(LC->getAnalysisDeclContext()->isBodyAutosynthesized());
701   const LocationContext *ParentLC = LC->getParent();
702   assert(ParentLC && "We don't start analysis from autosynthesized code");
703   while (ParentLC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
704     LC = ParentLC;
705     ParentLC = LC->getParent();
706     assert(ParentLC && "We don't start analysis from autosynthesized code");
707   }
708   return LC;
709 }
710
711 const Stmt *PathDiagnosticLocation::getStmt(const ExplodedNode *N) {
712   // We cannot place diagnostics on autosynthesized code.
713   // Put them onto the call site through which we jumped into autosynthesized
714   // code for the first time.
715   const LocationContext *LC = N->getLocationContext();
716   if (LC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
717     // It must be a stack frame because we only autosynthesize functions.
718     return cast<StackFrameContext>(findTopAutosynthesizedParentContext(LC))
719         ->getCallSite();
720   }
721   // Otherwise, see if the node's program point directly points to a statement.
722   ProgramPoint P = N->getLocation();
723   if (Optional<StmtPoint> SP = P.getAs<StmtPoint>())
724     return SP->getStmt();
725   if (Optional<BlockEdge> BE = P.getAs<BlockEdge>())
726     return BE->getSrc()->getTerminator();
727   if (Optional<CallEnter> CE = P.getAs<CallEnter>())
728     return CE->getCallExpr();
729   if (Optional<CallExitEnd> CEE = P.getAs<CallExitEnd>())
730     return CEE->getCalleeContext()->getCallSite();
731   if (Optional<PostInitializer> PIPP = P.getAs<PostInitializer>())
732     return PIPP->getInitializer()->getInit();
733
734   return nullptr;
735 }
736
737 const Stmt *PathDiagnosticLocation::getNextStmt(const ExplodedNode *N) {
738   for (N = N->getFirstSucc(); N; N = N->getFirstSucc()) {
739     if (const Stmt *S = getStmt(N)) {
740       // Check if the statement is '?' or '&&'/'||'.  These are "merges",
741       // not actual statement points.
742       switch (S->getStmtClass()) {
743         case Stmt::ChooseExprClass:
744         case Stmt::BinaryConditionalOperatorClass:
745         case Stmt::ConditionalOperatorClass:
746           continue;
747         case Stmt::BinaryOperatorClass: {
748           BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
749           if (Op == BO_LAnd || Op == BO_LOr)
750             continue;
751           break;
752         }
753         default:
754           break;
755       }
756       // We found the statement, so return it.
757       return S;
758     }
759   }
760
761   return nullptr;
762 }
763
764 PathDiagnosticLocation
765   PathDiagnosticLocation::createEndOfPath(const ExplodedNode *N,
766                                           const SourceManager &SM) {
767   assert(N && "Cannot create a location with a null node.");
768   const Stmt *S = getStmt(N);
769
770   if (!S) {
771     // If this is an implicit call, return the implicit call point location.
772     if (Optional<PreImplicitCall> PIE = N->getLocationAs<PreImplicitCall>())
773       return PathDiagnosticLocation(PIE->getLocation(), SM);
774     S = getNextStmt(N);
775   }
776
777   if (S) {
778     ProgramPoint P = N->getLocation();
779     const LocationContext *LC = N->getLocationContext();
780
781     // For member expressions, return the location of the '.' or '->'.
782     if (const MemberExpr *ME = dyn_cast<MemberExpr>(S))
783       return PathDiagnosticLocation::createMemberLoc(ME, SM);
784
785     // For binary operators, return the location of the operator.
786     if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S))
787       return PathDiagnosticLocation::createOperatorLoc(B, SM);
788
789     if (P.getAs<PostStmtPurgeDeadSymbols>())
790       return PathDiagnosticLocation::createEnd(S, SM, LC);
791
792     if (S->getLocStart().isValid())
793       return PathDiagnosticLocation(S, SM, LC);
794     return PathDiagnosticLocation(getValidSourceLocation(S, LC), SM);
795   }
796
797   return createDeclEnd(N->getLocationContext(), SM);
798 }
799
800 PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation(
801                                            const PathDiagnosticLocation &PDL) {
802   FullSourceLoc L = PDL.asLocation();
803   return PathDiagnosticLocation(L, L.getManager(), SingleLocK);
804 }
805
806 FullSourceLoc
807   PathDiagnosticLocation::genLocation(SourceLocation L,
808                                       LocationOrAnalysisDeclContext LAC) const {
809   assert(isValid());
810   // Note that we want a 'switch' here so that the compiler can warn us in
811   // case we add more cases.
812   switch (K) {
813     case SingleLocK:
814     case RangeK:
815       break;
816     case StmtK:
817       // Defensive checking.
818       if (!S)
819         break;
820       return FullSourceLoc(getValidSourceLocation(S, LAC),
821                            const_cast<SourceManager&>(*SM));
822     case DeclK:
823       // Defensive checking.
824       if (!D)
825         break;
826       return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM));
827   }
828
829   return FullSourceLoc(L, const_cast<SourceManager&>(*SM));
830 }
831
832 PathDiagnosticRange
833   PathDiagnosticLocation::genRange(LocationOrAnalysisDeclContext LAC) const {
834   assert(isValid());
835   // Note that we want a 'switch' here so that the compiler can warn us in
836   // case we add more cases.
837   switch (K) {
838     case SingleLocK:
839       return PathDiagnosticRange(SourceRange(Loc,Loc), true);
840     case RangeK:
841       break;
842     case StmtK: {
843       const Stmt *S = asStmt();
844       switch (S->getStmtClass()) {
845         default:
846           break;
847         case Stmt::DeclStmtClass: {
848           const DeclStmt *DS = cast<DeclStmt>(S);
849           if (DS->isSingleDecl()) {
850             // Should always be the case, but we'll be defensive.
851             return SourceRange(DS->getLocStart(),
852                                DS->getSingleDecl()->getLocation());
853           }
854           break;
855         }
856           // FIXME: Provide better range information for different
857           //  terminators.
858         case Stmt::IfStmtClass:
859         case Stmt::WhileStmtClass:
860         case Stmt::DoStmtClass:
861         case Stmt::ForStmtClass:
862         case Stmt::ChooseExprClass:
863         case Stmt::IndirectGotoStmtClass:
864         case Stmt::SwitchStmtClass:
865         case Stmt::BinaryConditionalOperatorClass:
866         case Stmt::ConditionalOperatorClass:
867         case Stmt::ObjCForCollectionStmtClass: {
868           SourceLocation L = getValidSourceLocation(S, LAC);
869           return SourceRange(L, L);
870         }
871       }
872       SourceRange R = S->getSourceRange();
873       if (R.isValid())
874         return R;
875       break;
876     }
877     case DeclK:
878       if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
879         return MD->getSourceRange();
880       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
881         if (Stmt *Body = FD->getBody())
882           return Body->getSourceRange();
883       }
884       else {
885         SourceLocation L = D->getLocation();
886         return PathDiagnosticRange(SourceRange(L, L), true);
887       }
888   }
889
890   return SourceRange(Loc,Loc);
891 }
892
893 void PathDiagnosticLocation::flatten() {
894   if (K == StmtK) {
895     K = RangeK;
896     S = nullptr;
897     D = nullptr;
898   }
899   else if (K == DeclK) {
900     K = SingleLocK;
901     S = nullptr;
902     D = nullptr;
903   }
904 }
905
906 //===----------------------------------------------------------------------===//
907 // Manipulation of PathDiagnosticCallPieces.
908 //===----------------------------------------------------------------------===//
909
910 std::shared_ptr<PathDiagnosticCallPiece>
911 PathDiagnosticCallPiece::construct(const ExplodedNode *N, const CallExitEnd &CE,
912                                    const SourceManager &SM) {
913   const Decl *caller = CE.getLocationContext()->getDecl();
914   PathDiagnosticLocation pos = getLocationForCaller(CE.getCalleeContext(),
915                                                     CE.getLocationContext(),
916                                                     SM);
917   return std::shared_ptr<PathDiagnosticCallPiece>(
918       new PathDiagnosticCallPiece(caller, pos));
919 }
920
921 PathDiagnosticCallPiece *
922 PathDiagnosticCallPiece::construct(PathPieces &path,
923                                    const Decl *caller) {
924   std::shared_ptr<PathDiagnosticCallPiece> C(
925       new PathDiagnosticCallPiece(path, caller));
926   path.clear();
927   auto *R = C.get();
928   path.push_front(std::move(C));
929   return R;
930 }
931
932 void PathDiagnosticCallPiece::setCallee(const CallEnter &CE,
933                                         const SourceManager &SM) {
934   const StackFrameContext *CalleeCtx = CE.getCalleeContext();
935   Callee = CalleeCtx->getDecl();
936
937   callEnterWithin = PathDiagnosticLocation::createBegin(Callee, SM);
938   callEnter = getLocationForCaller(CalleeCtx, CE.getLocationContext(), SM);
939
940   // Autosynthesized property accessors are special because we'd never
941   // pop back up to non-autosynthesized code until we leave them.
942   // This is not generally true for autosynthesized callees, which may call
943   // non-autosynthesized callbacks.
944   // Unless set here, the IsCalleeAnAutosynthesizedPropertyAccessor flag
945   // defaults to false.
946   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(Callee))
947     IsCalleeAnAutosynthesizedPropertyAccessor = (
948         MD->isPropertyAccessor() &&
949         CalleeCtx->getAnalysisDeclContext()->isBodyAutosynthesized());
950 }
951
952 static inline void describeClass(raw_ostream &Out, const CXXRecordDecl *D,
953                                  StringRef Prefix = StringRef()) {
954   if (!D->getIdentifier())
955     return;
956   Out << Prefix << '\'' << *D << '\'';
957 }
958
959 static bool describeCodeDecl(raw_ostream &Out, const Decl *D,
960                              bool ExtendedDescription,
961                              StringRef Prefix = StringRef()) {
962   if (!D)
963     return false;
964
965   if (isa<BlockDecl>(D)) {
966     if (ExtendedDescription)
967       Out << Prefix << "anonymous block";
968     return ExtendedDescription;
969   }
970
971   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
972     Out << Prefix;
973     if (ExtendedDescription && !MD->isUserProvided()) {
974       if (MD->isExplicitlyDefaulted())
975         Out << "defaulted ";
976       else
977         Out << "implicit ";
978     }
979
980     if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(MD)) {
981       if (CD->isDefaultConstructor())
982         Out << "default ";
983       else if (CD->isCopyConstructor())
984         Out << "copy ";
985       else if (CD->isMoveConstructor())
986         Out << "move ";
987
988       Out << "constructor";
989       describeClass(Out, MD->getParent(), " for ");
990
991     } else if (isa<CXXDestructorDecl>(MD)) {
992       if (!MD->isUserProvided()) {
993         Out << "destructor";
994         describeClass(Out, MD->getParent(), " for ");
995       } else {
996         // Use ~Foo for explicitly-written destructors.
997         Out << "'" << *MD << "'";
998       }
999
1000     } else if (MD->isCopyAssignmentOperator()) {
1001         Out << "copy assignment operator";
1002         describeClass(Out, MD->getParent(), " for ");
1003
1004     } else if (MD->isMoveAssignmentOperator()) {
1005         Out << "move assignment operator";
1006         describeClass(Out, MD->getParent(), " for ");
1007
1008     } else {
1009       if (MD->getParent()->getIdentifier())
1010         Out << "'" << *MD->getParent() << "::" << *MD << "'";
1011       else
1012         Out << "'" << *MD << "'";
1013     }
1014
1015     return true;
1016   }
1017
1018   Out << Prefix << '\'' << cast<NamedDecl>(*D) << '\'';
1019   return true;
1020 }
1021
1022 std::shared_ptr<PathDiagnosticEventPiece>
1023 PathDiagnosticCallPiece::getCallEnterEvent() const {
1024   // We do not produce call enters and call exits for autosynthesized property
1025   // accessors. We do generally produce them for other functions coming from
1026   // the body farm because they may call callbacks that bring us back into
1027   // visible code.
1028   if (!Callee || IsCalleeAnAutosynthesizedPropertyAccessor)
1029     return nullptr;
1030
1031   SmallString<256> buf;
1032   llvm::raw_svector_ostream Out(buf);
1033
1034   Out << "Calling ";
1035   describeCodeDecl(Out, Callee, /*ExtendedDescription=*/true);
1036
1037   assert(callEnter.asLocation().isValid());
1038   return std::make_shared<PathDiagnosticEventPiece>(callEnter, Out.str());
1039 }
1040
1041 std::shared_ptr<PathDiagnosticEventPiece>
1042 PathDiagnosticCallPiece::getCallEnterWithinCallerEvent() const {
1043   if (!callEnterWithin.asLocation().isValid())
1044     return nullptr;
1045   if (Callee->isImplicit() || !Callee->hasBody())
1046     return nullptr;
1047   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee))
1048     if (MD->isDefaulted())
1049       return nullptr;
1050
1051   SmallString<256> buf;
1052   llvm::raw_svector_ostream Out(buf);
1053
1054   Out << "Entered call";
1055   describeCodeDecl(Out, Caller, /*ExtendedDescription=*/false, " from ");
1056
1057   return std::make_shared<PathDiagnosticEventPiece>(callEnterWithin, Out.str());
1058 }
1059
1060 std::shared_ptr<PathDiagnosticEventPiece>
1061 PathDiagnosticCallPiece::getCallExitEvent() const {
1062   // We do not produce call enters and call exits for autosynthesized property
1063   // accessors. We do generally produce them for other functions coming from
1064   // the body farm because they may call callbacks that bring us back into
1065   // visible code.
1066   if (NoExit || IsCalleeAnAutosynthesizedPropertyAccessor)
1067     return nullptr;
1068
1069   SmallString<256> buf;
1070   llvm::raw_svector_ostream Out(buf);
1071
1072   if (!CallStackMessage.empty()) {
1073     Out << CallStackMessage;
1074   } else {
1075     bool DidDescribe = describeCodeDecl(Out, Callee,
1076                                         /*ExtendedDescription=*/false,
1077                                         "Returning from ");
1078     if (!DidDescribe)
1079       Out << "Returning to caller";
1080   }
1081
1082   assert(callReturn.asLocation().isValid());
1083   return std::make_shared<PathDiagnosticEventPiece>(callReturn, Out.str());
1084 }
1085
1086 static void compute_path_size(const PathPieces &pieces, unsigned &size) {
1087   for (PathPieces::const_iterator it = pieces.begin(),
1088                                   et = pieces.end(); it != et; ++it) {
1089     const PathDiagnosticPiece *piece = it->get();
1090     if (const PathDiagnosticCallPiece *cp =
1091         dyn_cast<PathDiagnosticCallPiece>(piece)) {
1092       compute_path_size(cp->path, size);
1093     }
1094     else
1095       ++size;
1096   }
1097 }
1098
1099 unsigned PathDiagnostic::full_size() {
1100   unsigned size = 0;
1101   compute_path_size(path, size);
1102   return size;
1103 }
1104
1105 //===----------------------------------------------------------------------===//
1106 // FoldingSet profiling methods.
1107 //===----------------------------------------------------------------------===//
1108
1109 void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const {
1110   ID.AddInteger(Range.getBegin().getRawEncoding());
1111   ID.AddInteger(Range.getEnd().getRawEncoding());
1112   ID.AddInteger(Loc.getRawEncoding());
1113 }
1114
1115 void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const {
1116   ID.AddInteger((unsigned) getKind());
1117   ID.AddString(str);
1118   // FIXME: Add profiling support for code hints.
1119   ID.AddInteger((unsigned) getDisplayHint());
1120   ArrayRef<SourceRange> Ranges = getRanges();
1121   for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
1122                                         I != E; ++I) {
1123     ID.AddInteger(I->getBegin().getRawEncoding());
1124     ID.AddInteger(I->getEnd().getRawEncoding());
1125   }
1126 }
1127
1128 void PathDiagnosticCallPiece::Profile(llvm::FoldingSetNodeID &ID) const {
1129   PathDiagnosticPiece::Profile(ID);
1130   for (PathPieces::const_iterator it = path.begin(),
1131        et = path.end(); it != et; ++it) {
1132     ID.Add(**it);
1133   }
1134 }
1135
1136 void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const {
1137   PathDiagnosticPiece::Profile(ID);
1138   ID.Add(Pos);
1139 }
1140
1141 void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const {
1142   PathDiagnosticPiece::Profile(ID);
1143   for (const_iterator I = begin(), E = end(); I != E; ++I)
1144     ID.Add(*I);
1145 }
1146
1147 void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const {
1148   PathDiagnosticSpotPiece::Profile(ID);
1149   for (PathPieces::const_iterator I = subPieces.begin(), E = subPieces.end();
1150        I != E; ++I)
1151     ID.Add(**I);
1152 }
1153
1154 void PathDiagnosticNotePiece::Profile(llvm::FoldingSetNodeID &ID) const {
1155   PathDiagnosticSpotPiece::Profile(ID);
1156 }
1157
1158 void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const {
1159   ID.Add(getLocation());
1160   ID.AddString(BugType);
1161   ID.AddString(VerboseDesc);
1162   ID.AddString(Category);
1163 }
1164
1165 void PathDiagnostic::FullProfile(llvm::FoldingSetNodeID &ID) const {
1166   Profile(ID);
1167   for (PathPieces::const_iterator I = path.begin(), E = path.end(); I != E; ++I)
1168     ID.Add(**I);
1169   for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I)
1170     ID.AddString(*I);
1171 }
1172
1173 StackHintGenerator::~StackHintGenerator() {}
1174
1175 std::string StackHintGeneratorForSymbol::getMessage(const ExplodedNode *N){
1176   ProgramPoint P = N->getLocation();
1177   CallExitEnd CExit = P.castAs<CallExitEnd>();
1178
1179   // FIXME: Use CallEvent to abstract this over all calls.
1180   const Stmt *CallSite = CExit.getCalleeContext()->getCallSite();
1181   const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite);
1182   if (!CE)
1183     return "";
1184
1185   if (!N)
1186     return getMessageForSymbolNotFound();
1187
1188   // Check if one of the parameters are set to the interesting symbol.
1189   ProgramStateRef State = N->getState();
1190   const LocationContext *LCtx = N->getLocationContext();
1191   unsigned ArgIndex = 0;
1192   for (CallExpr::const_arg_iterator I = CE->arg_begin(),
1193                                     E = CE->arg_end(); I != E; ++I, ++ArgIndex){
1194     SVal SV = State->getSVal(*I, LCtx);
1195
1196     // Check if the variable corresponding to the symbol is passed by value.
1197     SymbolRef AS = SV.getAsLocSymbol();
1198     if (AS == Sym) {
1199       return getMessageForArg(*I, ArgIndex);
1200     }
1201
1202     // Check if the parameter is a pointer to the symbol.
1203     if (Optional<loc::MemRegionVal> Reg = SV.getAs<loc::MemRegionVal>()) {
1204       SVal PSV = State->getSVal(Reg->getRegion());
1205       SymbolRef AS = PSV.getAsLocSymbol();
1206       if (AS == Sym) {
1207         return getMessageForArg(*I, ArgIndex);
1208       }
1209     }
1210   }
1211
1212   // Check if we are returning the interesting symbol.
1213   SVal SV = State->getSVal(CE, LCtx);
1214   SymbolRef RetSym = SV.getAsLocSymbol();
1215   if (RetSym == Sym) {
1216     return getMessageForReturn(CE);
1217   }
1218
1219   return getMessageForSymbolNotFound();
1220 }
1221
1222 std::string StackHintGeneratorForSymbol::getMessageForArg(const Expr *ArgE,
1223                                                           unsigned ArgIndex) {
1224   // Printed parameters start at 1, not 0.
1225   ++ArgIndex;
1226
1227   SmallString<200> buf;
1228   llvm::raw_svector_ostream os(buf);
1229
1230   os << Msg << " via " << ArgIndex << llvm::getOrdinalSuffix(ArgIndex)
1231      << " parameter";
1232
1233   return os.str();
1234 }