BugReporter.cpp 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
  1. //===- BugReporter.cpp - Generate PathDiagnostics for bugs ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines BugReporter, a utility class for generating
  10. // PathDiagnostics.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/DeclBase.h"
  16. #include "clang/AST/DeclObjC.h"
  17. #include "clang/AST/Expr.h"
  18. #include "clang/AST/ExprCXX.h"
  19. #include "clang/AST/ParentMap.h"
  20. #include "clang/AST/Stmt.h"
  21. #include "clang/AST/StmtCXX.h"
  22. #include "clang/AST/StmtObjC.h"
  23. #include "clang/Analysis/AnalysisDeclContext.h"
  24. #include "clang/Analysis/CFG.h"
  25. #include "clang/Analysis/CFGStmtMap.h"
  26. #include "clang/Analysis/ProgramPoint.h"
  27. #include "clang/Basic/LLVM.h"
  28. #include "clang/Basic/SourceLocation.h"
  29. #include "clang/Basic/SourceManager.h"
  30. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  31. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
  32. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  33. #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
  34. #include "clang/StaticAnalyzer/Core/Checker.h"
  35. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  36. #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
  37. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  38. #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
  39. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  40. #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
  41. #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
  42. #include "llvm/ADT/ArrayRef.h"
  43. #include "llvm/ADT/DenseMap.h"
  44. #include "llvm/ADT/DenseSet.h"
  45. #include "llvm/ADT/FoldingSet.h"
  46. #include "llvm/ADT/None.h"
  47. #include "llvm/ADT/Optional.h"
  48. #include "llvm/ADT/STLExtras.h"
  49. #include "llvm/ADT/SmallPtrSet.h"
  50. #include "llvm/ADT/SmallString.h"
  51. #include "llvm/ADT/SmallVector.h"
  52. #include "llvm/ADT/Statistic.h"
  53. #include "llvm/ADT/StringRef.h"
  54. #include "llvm/ADT/iterator_range.h"
  55. #include "llvm/Support/Casting.h"
  56. #include "llvm/Support/Compiler.h"
  57. #include "llvm/Support/ErrorHandling.h"
  58. #include "llvm/Support/MemoryBuffer.h"
  59. #include "llvm/Support/raw_ostream.h"
  60. #include <algorithm>
  61. #include <cassert>
  62. #include <cstddef>
  63. #include <iterator>
  64. #include <memory>
  65. #include <queue>
  66. #include <string>
  67. #include <tuple>
  68. #include <utility>
  69. #include <vector>
  70. using namespace clang;
  71. using namespace ento;
  72. #define DEBUG_TYPE "BugReporter"
  73. STATISTIC(MaxBugClassSize,
  74. "The maximum number of bug reports in the same equivalence class");
  75. STATISTIC(MaxValidBugClassSize,
  76. "The maximum number of bug reports in the same equivalence class "
  77. "where at least one report is valid (not suppressed)");
  78. BugReporterVisitor::~BugReporterVisitor() = default;
  79. void BugReporterContext::anchor() {}
  80. //===----------------------------------------------------------------------===//
  81. // PathDiagnosticBuilder and its associated routines and helper objects.
  82. //===----------------------------------------------------------------------===//
  83. namespace {
  84. /// A (CallPiece, node assiciated with its CallEnter) pair.
  85. using CallWithEntry =
  86. std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>;
  87. using CallWithEntryStack = SmallVector<CallWithEntry, 6>;
  88. /// Map from each node to the diagnostic pieces visitors emit for them.
  89. using VisitorsDiagnosticsTy =
  90. llvm::DenseMap<const ExplodedNode *, std::vector<PathDiagnosticPieceRef>>;
  91. using InterestingExprs = llvm::DenseSet<const Expr *>;
  92. /// A map from PathDiagnosticPiece to the LocationContext of the inlined
  93. /// function call it represents.
  94. using LocationContextMap =
  95. llvm::DenseMap<const PathPieces *, const LocationContext *>;
  96. /// A helper class that contains everything needed to construct a
  97. /// PathDiagnostic object. It does no much more then providing convenient
  98. /// getters and some well placed asserts for extra security.
  99. class PathDiagnosticConstruct {
  100. /// The consumer we're constructing the bug report for.
  101. const PathDiagnosticConsumer *Consumer;
  102. /// Our current position in the bug path, which is owned by
  103. /// PathDiagnosticBuilder.
  104. const ExplodedNode *CurrentNode;
  105. /// A mapping from parts of the bug path (for example, a function call, which
  106. /// would span backwards from a CallExit to a CallEnter with the nodes in
  107. /// between them) with the location contexts it is associated with.
  108. LocationContextMap LCM;
  109. const SourceManager &SM;
  110. public:
  111. /// We keep stack of calls to functions as we're ascending the bug path.
  112. /// TODO: PathDiagnostic has a stack doing the same thing, shouldn't we use
  113. /// that instead?
  114. CallWithEntryStack CallStack;
  115. InterestingExprs IE;
  116. /// The bug report we're constructing. For ease of use, this field is kept
  117. /// public, though some "shortcut" getters are provided for commonly used
  118. /// methods of PathDiagnostic.
  119. std::unique_ptr<PathDiagnostic> PD;
  120. public:
  121. PathDiagnosticConstruct(const PathDiagnosticConsumer *PDC,
  122. const ExplodedNode *ErrorNode, const BugReport *R);
  123. /// \returns the location context associated with the current position in the
  124. /// bug path.
  125. const LocationContext *getCurrLocationContext() const {
  126. assert(CurrentNode && "Already reached the root!");
  127. return CurrentNode->getLocationContext();
  128. }
  129. /// Same as getCurrLocationContext (they should always return the same
  130. /// location context), but works after reaching the root of the bug path as
  131. /// well.
  132. const LocationContext *getLocationContextForActivePath() const {
  133. return LCM.find(&PD->getActivePath())->getSecond();
  134. }
  135. const ExplodedNode *getCurrentNode() const { return CurrentNode; }
  136. /// Steps the current node to its predecessor.
  137. /// \returns whether we reached the root of the bug path.
  138. bool ascendToPrevNode() {
  139. CurrentNode = CurrentNode->getFirstPred();
  140. return static_cast<bool>(CurrentNode);
  141. }
  142. const ParentMap &getParentMap() const {
  143. return getCurrLocationContext()->getParentMap();
  144. }
  145. const SourceManager &getSourceManager() const { return SM; }
  146. const Stmt *getParent(const Stmt *S) const {
  147. return getParentMap().getParent(S);
  148. }
  149. void updateLocCtxMap(const PathPieces *Path, const LocationContext *LC) {
  150. assert(Path && LC);
  151. LCM[Path] = LC;
  152. }
  153. const LocationContext *getLocationContextFor(const PathPieces *Path) const {
  154. assert(LCM.count(Path) &&
  155. "Failed to find the context associated with these pieces!");
  156. return LCM.find(Path)->getSecond();
  157. }
  158. bool isInLocCtxMap(const PathPieces *Path) const { return LCM.count(Path); }
  159. PathPieces &getActivePath() { return PD->getActivePath(); }
  160. PathPieces &getMutablePieces() { return PD->getMutablePieces(); }
  161. bool shouldAddPathEdges() const { return Consumer->shouldAddPathEdges(); }
  162. bool shouldGenerateDiagnostics() const {
  163. return Consumer->shouldGenerateDiagnostics();
  164. }
  165. bool supportsLogicalOpControlFlow() const {
  166. return Consumer->supportsLogicalOpControlFlow();
  167. }
  168. };
  169. /// Contains every contextual information needed for constructing a
  170. /// PathDiagnostic object for a given bug report. This class (and aside from
  171. /// some caching BugReport does in the background) and its fields are immutable,
  172. /// and passes a BugReportConstruct object around during the construction.
  173. class PathDiagnosticBuilder : public BugReporterContext {
  174. /// A linear path from the error node to the root.
  175. std::unique_ptr<const ExplodedGraph> BugPath;
  176. BugReport *R;
  177. /// The leaf of the bug path. This isn't the same as the bug reports error
  178. /// node, which refers to the *original* graph, not the bug path.
  179. const ExplodedNode *const ErrorNode;
  180. /// The diagnostic pieces visitors emitted, which is expected to be collected
  181. /// by the time this builder is constructed.
  182. std::unique_ptr<const VisitorsDiagnosticsTy> VisitorsDiagnostics;
  183. public:
  184. /// Find a non-invalidated report for a given equivalence class, and returns
  185. /// a PathDiagnosticBuilder able to construct bug reports for different
  186. /// consumers. Returns None if no valid report is found.
  187. static Optional<PathDiagnosticBuilder>
  188. findValidReport(ArrayRef<BugReport *> &bugReports, GRBugReporter &Reporter);
  189. PathDiagnosticBuilder(
  190. BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
  191. BugReport *r, const ExplodedNode *ErrorNode,
  192. std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics);
  193. /// This function is responsible for generating diagnostic pieces that are
  194. /// *not* provided by bug report visitors.
  195. /// These diagnostics may differ depending on the consumer's settings,
  196. /// and are therefore constructed separately for each consumer.
  197. ///
  198. /// There are two path diagnostics generation modes: with adding edges (used
  199. /// for plists) and without (used for HTML and text). When edges are added,
  200. /// the path is modified to insert artificially generated edges.
  201. /// Otherwise, more detailed diagnostics is emitted for block edges,
  202. /// explaining the transitions in words.
  203. std::unique_ptr<PathDiagnostic>
  204. generate(const PathDiagnosticConsumer *PDC) const;
  205. private:
  206. void generatePathDiagnosticsForNode(PathDiagnosticConstruct &C,
  207. PathDiagnosticLocation &PrevLoc) const;
  208. void generateMinimalDiagForBlockEdge(PathDiagnosticConstruct &C,
  209. BlockEdge BE) const;
  210. PathDiagnosticPieceRef
  211. generateDiagForGotoOP(const PathDiagnosticConstruct &C, const Stmt *S,
  212. PathDiagnosticLocation &Start) const;
  213. PathDiagnosticPieceRef
  214. generateDiagForSwitchOP(const PathDiagnosticConstruct &C, const CFGBlock *Dst,
  215. PathDiagnosticLocation &Start) const;
  216. PathDiagnosticPieceRef
  217. generateDiagForBinaryOP(const PathDiagnosticConstruct &C, const Stmt *T,
  218. const CFGBlock *Src, const CFGBlock *DstC) const;
  219. PathDiagnosticLocation
  220. ExecutionContinues(const PathDiagnosticConstruct &C) const;
  221. PathDiagnosticLocation
  222. ExecutionContinues(llvm::raw_string_ostream &os,
  223. const PathDiagnosticConstruct &C) const;
  224. BugReport *getBugReport() const { return R; }
  225. };
  226. } // namespace
  227. //===----------------------------------------------------------------------===//
  228. // Helper routines for walking the ExplodedGraph and fetching statements.
  229. //===----------------------------------------------------------------------===//
  230. static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
  231. for (N = N->getFirstPred(); N; N = N->getFirstPred())
  232. if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
  233. return S;
  234. return nullptr;
  235. }
  236. static inline const Stmt*
  237. GetCurrentOrPreviousStmt(const ExplodedNode *N) {
  238. if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
  239. return S;
  240. return GetPreviousStmt(N);
  241. }
  242. //===----------------------------------------------------------------------===//
  243. // Diagnostic cleanup.
  244. //===----------------------------------------------------------------------===//
  245. static PathDiagnosticEventPiece *
  246. eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
  247. PathDiagnosticEventPiece *Y) {
  248. // Prefer diagnostics that come from ConditionBRVisitor over
  249. // those that came from TrackConstraintBRVisitor,
  250. // unless the one from ConditionBRVisitor is
  251. // its generic fallback diagnostic.
  252. const void *tagPreferred = ConditionBRVisitor::getTag();
  253. const void *tagLesser = TrackConstraintBRVisitor::getTag();
  254. if (X->getLocation() != Y->getLocation())
  255. return nullptr;
  256. if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
  257. return ConditionBRVisitor::isPieceMessageGeneric(X) ? Y : X;
  258. if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
  259. return ConditionBRVisitor::isPieceMessageGeneric(Y) ? X : Y;
  260. return nullptr;
  261. }
  262. /// An optimization pass over PathPieces that removes redundant diagnostics
  263. /// generated by both ConditionBRVisitor and TrackConstraintBRVisitor. Both
  264. /// BugReporterVisitors use different methods to generate diagnostics, with
  265. /// one capable of emitting diagnostics in some cases but not in others. This
  266. /// can lead to redundant diagnostic pieces at the same point in a path.
  267. static void removeRedundantMsgs(PathPieces &path) {
  268. unsigned N = path.size();
  269. if (N < 2)
  270. return;
  271. // NOTE: this loop intentionally is not using an iterator. Instead, we
  272. // are streaming the path and modifying it in place. This is done by
  273. // grabbing the front, processing it, and if we decide to keep it append
  274. // it to the end of the path. The entire path is processed in this way.
  275. for (unsigned i = 0; i < N; ++i) {
  276. auto piece = std::move(path.front());
  277. path.pop_front();
  278. switch (piece->getKind()) {
  279. case PathDiagnosticPiece::Call:
  280. removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path);
  281. break;
  282. case PathDiagnosticPiece::Macro:
  283. removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces);
  284. break;
  285. case PathDiagnosticPiece::Event: {
  286. if (i == N-1)
  287. break;
  288. if (auto *nextEvent =
  289. dyn_cast<PathDiagnosticEventPiece>(path.front().get())) {
  290. auto *event = cast<PathDiagnosticEventPiece>(piece.get());
  291. // Check to see if we should keep one of the two pieces. If we
  292. // come up with a preference, record which piece to keep, and consume
  293. // another piece from the path.
  294. if (auto *pieceToKeep =
  295. eventsDescribeSameCondition(event, nextEvent)) {
  296. piece = std::move(pieceToKeep == event ? piece : path.front());
  297. path.pop_front();
  298. ++i;
  299. }
  300. }
  301. break;
  302. }
  303. case PathDiagnosticPiece::ControlFlow:
  304. case PathDiagnosticPiece::Note:
  305. case PathDiagnosticPiece::PopUp:
  306. break;
  307. }
  308. path.push_back(std::move(piece));
  309. }
  310. }
  311. /// Recursively scan through a path and prune out calls and macros pieces
  312. /// that aren't needed. Return true if afterwards the path contains
  313. /// "interesting stuff" which means it shouldn't be pruned from the parent path.
  314. static bool removeUnneededCalls(const PathDiagnosticConstruct &C,
  315. PathPieces &pieces, const BugReport *R,
  316. bool IsInteresting = false) {
  317. bool containsSomethingInteresting = IsInteresting;
  318. const unsigned N = pieces.size();
  319. for (unsigned i = 0 ; i < N ; ++i) {
  320. // Remove the front piece from the path. If it is still something we
  321. // want to keep once we are done, we will push it back on the end.
  322. auto piece = std::move(pieces.front());
  323. pieces.pop_front();
  324. switch (piece->getKind()) {
  325. case PathDiagnosticPiece::Call: {
  326. auto &call = cast<PathDiagnosticCallPiece>(*piece);
  327. // Check if the location context is interesting.
  328. if (!removeUnneededCalls(
  329. C, call.path, R,
  330. R->isInteresting(C.getLocationContextFor(&call.path))))
  331. continue;
  332. containsSomethingInteresting = true;
  333. break;
  334. }
  335. case PathDiagnosticPiece::Macro: {
  336. auto &macro = cast<PathDiagnosticMacroPiece>(*piece);
  337. if (!removeUnneededCalls(C, macro.subPieces, R, IsInteresting))
  338. continue;
  339. containsSomethingInteresting = true;
  340. break;
  341. }
  342. case PathDiagnosticPiece::Event: {
  343. auto &event = cast<PathDiagnosticEventPiece>(*piece);
  344. // We never throw away an event, but we do throw it away wholesale
  345. // as part of a path if we throw the entire path away.
  346. containsSomethingInteresting |= !event.isPrunable();
  347. break;
  348. }
  349. case PathDiagnosticPiece::ControlFlow:
  350. case PathDiagnosticPiece::Note:
  351. case PathDiagnosticPiece::PopUp:
  352. break;
  353. }
  354. pieces.push_back(std::move(piece));
  355. }
  356. return containsSomethingInteresting;
  357. }
  358. /// Same logic as above to remove extra pieces.
  359. static void removePopUpNotes(PathPieces &Path) {
  360. for (unsigned int i = 0; i < Path.size(); ++i) {
  361. auto Piece = std::move(Path.front());
  362. Path.pop_front();
  363. if (!isa<PathDiagnosticPopUpPiece>(*Piece))
  364. Path.push_back(std::move(Piece));
  365. }
  366. }
  367. /// Returns true if the given decl has been implicitly given a body, either by
  368. /// the analyzer or by the compiler proper.
  369. static bool hasImplicitBody(const Decl *D) {
  370. assert(D);
  371. return D->isImplicit() || !D->hasBody();
  372. }
  373. /// Recursively scan through a path and make sure that all call pieces have
  374. /// valid locations.
  375. static void
  376. adjustCallLocations(PathPieces &Pieces,
  377. PathDiagnosticLocation *LastCallLocation = nullptr) {
  378. for (const auto &I : Pieces) {
  379. auto *Call = dyn_cast<PathDiagnosticCallPiece>(I.get());
  380. if (!Call)
  381. continue;
  382. if (LastCallLocation) {
  383. bool CallerIsImplicit = hasImplicitBody(Call->getCaller());
  384. if (CallerIsImplicit || !Call->callEnter.asLocation().isValid())
  385. Call->callEnter = *LastCallLocation;
  386. if (CallerIsImplicit || !Call->callReturn.asLocation().isValid())
  387. Call->callReturn = *LastCallLocation;
  388. }
  389. // Recursively clean out the subclass. Keep this call around if
  390. // it contains any informative diagnostics.
  391. PathDiagnosticLocation *ThisCallLocation;
  392. if (Call->callEnterWithin.asLocation().isValid() &&
  393. !hasImplicitBody(Call->getCallee()))
  394. ThisCallLocation = &Call->callEnterWithin;
  395. else
  396. ThisCallLocation = &Call->callEnter;
  397. assert(ThisCallLocation && "Outermost call has an invalid location");
  398. adjustCallLocations(Call->path, ThisCallLocation);
  399. }
  400. }
  401. /// Remove edges in and out of C++ default initializer expressions. These are
  402. /// for fields that have in-class initializers, as opposed to being initialized
  403. /// explicitly in a constructor or braced list.
  404. static void removeEdgesToDefaultInitializers(PathPieces &Pieces) {
  405. for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
  406. if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
  407. removeEdgesToDefaultInitializers(C->path);
  408. if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
  409. removeEdgesToDefaultInitializers(M->subPieces);
  410. if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) {
  411. const Stmt *Start = CF->getStartLocation().asStmt();
  412. const Stmt *End = CF->getEndLocation().asStmt();
  413. if (Start && isa<CXXDefaultInitExpr>(Start)) {
  414. I = Pieces.erase(I);
  415. continue;
  416. } else if (End && isa<CXXDefaultInitExpr>(End)) {
  417. PathPieces::iterator Next = std::next(I);
  418. if (Next != E) {
  419. if (auto *NextCF =
  420. dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) {
  421. NextCF->setStartLocation(CF->getStartLocation());
  422. }
  423. }
  424. I = Pieces.erase(I);
  425. continue;
  426. }
  427. }
  428. I++;
  429. }
  430. }
  431. /// Remove all pieces with invalid locations as these cannot be serialized.
  432. /// We might have pieces with invalid locations as a result of inlining Body
  433. /// Farm generated functions.
  434. static void removePiecesWithInvalidLocations(PathPieces &Pieces) {
  435. for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
  436. if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
  437. removePiecesWithInvalidLocations(C->path);
  438. if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
  439. removePiecesWithInvalidLocations(M->subPieces);
  440. if (!(*I)->getLocation().isValid() ||
  441. !(*I)->getLocation().asLocation().isValid()) {
  442. I = Pieces.erase(I);
  443. continue;
  444. }
  445. I++;
  446. }
  447. }
  448. PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
  449. const PathDiagnosticConstruct &C) const {
  450. if (const Stmt *S = PathDiagnosticLocation::getNextStmt(C.getCurrentNode()))
  451. return PathDiagnosticLocation(S, getSourceManager(),
  452. C.getCurrLocationContext());
  453. return PathDiagnosticLocation::createDeclEnd(C.getCurrLocationContext(),
  454. getSourceManager());
  455. }
  456. PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
  457. llvm::raw_string_ostream &os, const PathDiagnosticConstruct &C) const {
  458. // Slow, but probably doesn't matter.
  459. if (os.str().empty())
  460. os << ' ';
  461. const PathDiagnosticLocation &Loc = ExecutionContinues(C);
  462. if (Loc.asStmt())
  463. os << "Execution continues on line "
  464. << getSourceManager().getExpansionLineNumber(Loc.asLocation())
  465. << '.';
  466. else {
  467. os << "Execution jumps to the end of the ";
  468. const Decl *D = C.getCurrLocationContext()->getDecl();
  469. if (isa<ObjCMethodDecl>(D))
  470. os << "method";
  471. else if (isa<FunctionDecl>(D))
  472. os << "function";
  473. else {
  474. assert(isa<BlockDecl>(D));
  475. os << "anonymous block";
  476. }
  477. os << '.';
  478. }
  479. return Loc;
  480. }
  481. static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) {
  482. if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
  483. return PM.getParentIgnoreParens(S);
  484. const Stmt *Parent = PM.getParentIgnoreParens(S);
  485. if (!Parent)
  486. return nullptr;
  487. switch (Parent->getStmtClass()) {
  488. case Stmt::ForStmtClass:
  489. case Stmt::DoStmtClass:
  490. case Stmt::WhileStmtClass:
  491. case Stmt::ObjCForCollectionStmtClass:
  492. case Stmt::CXXForRangeStmtClass:
  493. return Parent;
  494. default:
  495. break;
  496. }
  497. return nullptr;
  498. }
  499. static PathDiagnosticLocation
  500. getEnclosingStmtLocation(const Stmt *S, const LocationContext *LC,
  501. bool allowNestedContexts = false) {
  502. if (!S)
  503. return {};
  504. const SourceManager &SMgr = LC->getDecl()->getASTContext().getSourceManager();
  505. while (const Stmt *Parent = getEnclosingParent(S, LC->getParentMap())) {
  506. switch (Parent->getStmtClass()) {
  507. case Stmt::BinaryOperatorClass: {
  508. const auto *B = cast<BinaryOperator>(Parent);
  509. if (B->isLogicalOp())
  510. return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC);
  511. break;
  512. }
  513. case Stmt::CompoundStmtClass:
  514. case Stmt::StmtExprClass:
  515. return PathDiagnosticLocation(S, SMgr, LC);
  516. case Stmt::ChooseExprClass:
  517. // Similar to '?' if we are referring to condition, just have the edge
  518. // point to the entire choose expression.
  519. if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S)
  520. return PathDiagnosticLocation(Parent, SMgr, LC);
  521. else
  522. return PathDiagnosticLocation(S, SMgr, LC);
  523. case Stmt::BinaryConditionalOperatorClass:
  524. case Stmt::ConditionalOperatorClass:
  525. // For '?', if we are referring to condition, just have the edge point
  526. // to the entire '?' expression.
  527. if (allowNestedContexts ||
  528. cast<AbstractConditionalOperator>(Parent)->getCond() == S)
  529. return PathDiagnosticLocation(Parent, SMgr, LC);
  530. else
  531. return PathDiagnosticLocation(S, SMgr, LC);
  532. case Stmt::CXXForRangeStmtClass:
  533. if (cast<CXXForRangeStmt>(Parent)->getBody() == S)
  534. return PathDiagnosticLocation(S, SMgr, LC);
  535. break;
  536. case Stmt::DoStmtClass:
  537. return PathDiagnosticLocation(S, SMgr, LC);
  538. case Stmt::ForStmtClass:
  539. if (cast<ForStmt>(Parent)->getBody() == S)
  540. return PathDiagnosticLocation(S, SMgr, LC);
  541. break;
  542. case Stmt::IfStmtClass:
  543. if (cast<IfStmt>(Parent)->getCond() != S)
  544. return PathDiagnosticLocation(S, SMgr, LC);
  545. break;
  546. case Stmt::ObjCForCollectionStmtClass:
  547. if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
  548. return PathDiagnosticLocation(S, SMgr, LC);
  549. break;
  550. case Stmt::WhileStmtClass:
  551. if (cast<WhileStmt>(Parent)->getCond() != S)
  552. return PathDiagnosticLocation(S, SMgr, LC);
  553. break;
  554. default:
  555. break;
  556. }
  557. S = Parent;
  558. }
  559. assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
  560. return PathDiagnosticLocation(S, SMgr, LC);
  561. }
  562. //===----------------------------------------------------------------------===//
  563. // "Minimal" path diagnostic generation algorithm.
  564. //===----------------------------------------------------------------------===//
  565. /// If the piece contains a special message, add it to all the call pieces on
  566. /// the active stack. For exampler, my_malloc allocated memory, so MallocChecker
  567. /// will construct an event at the call to malloc(), and add a stack hint that
  568. /// an allocated memory was returned. We'll use this hint to construct a message
  569. /// when returning from the call to my_malloc
  570. ///
  571. /// void *my_malloc() { return malloc(sizeof(int)); }
  572. /// void fishy() {
  573. /// void *ptr = my_malloc(); // returned allocated memory
  574. /// } // leak
  575. static void updateStackPiecesWithMessage(PathDiagnosticPiece &P,
  576. const CallWithEntryStack &CallStack) {
  577. if (auto *ep = dyn_cast<PathDiagnosticEventPiece>(&P)) {
  578. if (ep->hasCallStackHint())
  579. for (const auto &I : CallStack) {
  580. PathDiagnosticCallPiece *CP = I.first;
  581. const ExplodedNode *N = I.second;
  582. std::string stackMsg = ep->getCallStackMessage(N);
  583. // The last message on the path to final bug is the most important
  584. // one. Since we traverse the path backwards, do not add the message
  585. // if one has been previously added.
  586. if (!CP->hasCallStackMessage())
  587. CP->setCallStackMessage(stackMsg);
  588. }
  589. }
  590. }
  591. static void CompactMacroExpandedPieces(PathPieces &path,
  592. const SourceManager& SM);
  593. PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForSwitchOP(
  594. const PathDiagnosticConstruct &C, const CFGBlock *Dst,
  595. PathDiagnosticLocation &Start) const {
  596. const SourceManager &SM = getSourceManager();
  597. // Figure out what case arm we took.
  598. std::string sbuf;
  599. llvm::raw_string_ostream os(sbuf);
  600. PathDiagnosticLocation End;
  601. if (const Stmt *S = Dst->getLabel()) {
  602. End = PathDiagnosticLocation(S, SM, C.getCurrLocationContext());
  603. switch (S->getStmtClass()) {
  604. default:
  605. os << "No cases match in the switch statement. "
  606. "Control jumps to line "
  607. << End.asLocation().getExpansionLineNumber();
  608. break;
  609. case Stmt::DefaultStmtClass:
  610. os << "Control jumps to the 'default' case at line "
  611. << End.asLocation().getExpansionLineNumber();
  612. break;
  613. case Stmt::CaseStmtClass: {
  614. os << "Control jumps to 'case ";
  615. const auto *Case = cast<CaseStmt>(S);
  616. const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
  617. // Determine if it is an enum.
  618. bool GetRawInt = true;
  619. if (const auto *DR = dyn_cast<DeclRefExpr>(LHS)) {
  620. // FIXME: Maybe this should be an assertion. Are there cases
  621. // were it is not an EnumConstantDecl?
  622. const auto *D = dyn_cast<EnumConstantDecl>(DR->getDecl());
  623. if (D) {
  624. GetRawInt = false;
  625. os << *D;
  626. }
  627. }
  628. if (GetRawInt)
  629. os << LHS->EvaluateKnownConstInt(getASTContext());
  630. os << ":' at line " << End.asLocation().getExpansionLineNumber();
  631. break;
  632. }
  633. }
  634. } else {
  635. os << "'Default' branch taken. ";
  636. End = ExecutionContinues(os, C);
  637. }
  638. return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
  639. os.str());
  640. }
  641. PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP(
  642. const PathDiagnosticConstruct &C, const Stmt *S,
  643. PathDiagnosticLocation &Start) const {
  644. std::string sbuf;
  645. llvm::raw_string_ostream os(sbuf);
  646. const PathDiagnosticLocation &End =
  647. getEnclosingStmtLocation(S, C.getCurrLocationContext());
  648. os << "Control jumps to line " << End.asLocation().getExpansionLineNumber();
  649. return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str());
  650. }
  651. PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP(
  652. const PathDiagnosticConstruct &C, const Stmt *T, const CFGBlock *Src,
  653. const CFGBlock *Dst) const {
  654. const SourceManager &SM = getSourceManager();
  655. const auto *B = cast<BinaryOperator>(T);
  656. std::string sbuf;
  657. llvm::raw_string_ostream os(sbuf);
  658. os << "Left side of '";
  659. PathDiagnosticLocation Start, End;
  660. if (B->getOpcode() == BO_LAnd) {
  661. os << "&&"
  662. << "' is ";
  663. if (*(Src->succ_begin() + 1) == Dst) {
  664. os << "false";
  665. End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
  666. Start =
  667. PathDiagnosticLocation::createOperatorLoc(B, SM);
  668. } else {
  669. os << "true";
  670. Start =
  671. PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
  672. End = ExecutionContinues(C);
  673. }
  674. } else {
  675. assert(B->getOpcode() == BO_LOr);
  676. os << "||"
  677. << "' is ";
  678. if (*(Src->succ_begin() + 1) == Dst) {
  679. os << "false";
  680. Start =
  681. PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
  682. End = ExecutionContinues(C);
  683. } else {
  684. os << "true";
  685. End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
  686. Start =
  687. PathDiagnosticLocation::createOperatorLoc(B, SM);
  688. }
  689. }
  690. return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
  691. os.str());
  692. }
  693. void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
  694. PathDiagnosticConstruct &C, BlockEdge BE) const {
  695. const SourceManager &SM = getSourceManager();
  696. const LocationContext *LC = C.getCurrLocationContext();
  697. const CFGBlock *Src = BE.getSrc();
  698. const CFGBlock *Dst = BE.getDst();
  699. const Stmt *T = Src->getTerminatorStmt();
  700. if (!T)
  701. return;
  702. auto Start = PathDiagnosticLocation::createBegin(T, SM, LC);
  703. switch (T->getStmtClass()) {
  704. default:
  705. break;
  706. case Stmt::GotoStmtClass:
  707. case Stmt::IndirectGotoStmtClass: {
  708. if (const Stmt *S = PathDiagnosticLocation::getNextStmt(C.getCurrentNode()))
  709. C.getActivePath().push_front(generateDiagForGotoOP(C, S, Start));
  710. break;
  711. }
  712. case Stmt::SwitchStmtClass: {
  713. C.getActivePath().push_front(generateDiagForSwitchOP(C, Dst, Start));
  714. break;
  715. }
  716. case Stmt::BreakStmtClass:
  717. case Stmt::ContinueStmtClass: {
  718. std::string sbuf;
  719. llvm::raw_string_ostream os(sbuf);
  720. PathDiagnosticLocation End = ExecutionContinues(os, C);
  721. C.getActivePath().push_front(
  722. std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
  723. break;
  724. }
  725. // Determine control-flow for ternary '?'.
  726. case Stmt::BinaryConditionalOperatorClass:
  727. case Stmt::ConditionalOperatorClass: {
  728. std::string sbuf;
  729. llvm::raw_string_ostream os(sbuf);
  730. os << "'?' condition is ";
  731. if (*(Src->succ_begin() + 1) == Dst)
  732. os << "false";
  733. else
  734. os << "true";
  735. PathDiagnosticLocation End = ExecutionContinues(C);
  736. if (const Stmt *S = End.asStmt())
  737. End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
  738. C.getActivePath().push_front(
  739. std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
  740. break;
  741. }
  742. // Determine control-flow for short-circuited '&&' and '||'.
  743. case Stmt::BinaryOperatorClass: {
  744. if (!C.supportsLogicalOpControlFlow())
  745. break;
  746. C.getActivePath().push_front(generateDiagForBinaryOP(C, T, Src, Dst));
  747. break;
  748. }
  749. case Stmt::DoStmtClass:
  750. if (*(Src->succ_begin()) == Dst) {
  751. std::string sbuf;
  752. llvm::raw_string_ostream os(sbuf);
  753. os << "Loop condition is true. ";
  754. PathDiagnosticLocation End = ExecutionContinues(os, C);
  755. if (const Stmt *S = End.asStmt())
  756. End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
  757. C.getActivePath().push_front(
  758. std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
  759. os.str()));
  760. } else {
  761. PathDiagnosticLocation End = ExecutionContinues(C);
  762. if (const Stmt *S = End.asStmt())
  763. End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
  764. C.getActivePath().push_front(
  765. std::make_shared<PathDiagnosticControlFlowPiece>(
  766. Start, End, "Loop condition is false. Exiting loop"));
  767. }
  768. break;
  769. case Stmt::WhileStmtClass:
  770. case Stmt::ForStmtClass:
  771. if (*(Src->succ_begin() + 1) == Dst) {
  772. std::string sbuf;
  773. llvm::raw_string_ostream os(sbuf);
  774. os << "Loop condition is false. ";
  775. PathDiagnosticLocation End = ExecutionContinues(os, C);
  776. if (const Stmt *S = End.asStmt())
  777. End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
  778. C.getActivePath().push_front(
  779. std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
  780. os.str()));
  781. } else {
  782. PathDiagnosticLocation End = ExecutionContinues(C);
  783. if (const Stmt *S = End.asStmt())
  784. End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
  785. C.getActivePath().push_front(
  786. std::make_shared<PathDiagnosticControlFlowPiece>(
  787. Start, End, "Loop condition is true. Entering loop body"));
  788. }
  789. break;
  790. case Stmt::IfStmtClass: {
  791. PathDiagnosticLocation End = ExecutionContinues(C);
  792. if (const Stmt *S = End.asStmt())
  793. End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
  794. if (*(Src->succ_begin() + 1) == Dst)
  795. C.getActivePath().push_front(
  796. std::make_shared<PathDiagnosticControlFlowPiece>(
  797. Start, End, "Taking false branch"));
  798. else
  799. C.getActivePath().push_front(
  800. std::make_shared<PathDiagnosticControlFlowPiece>(
  801. Start, End, "Taking true branch"));
  802. break;
  803. }
  804. }
  805. }
  806. // Cone-of-influence: support the reverse propagation of "interesting" symbols
  807. // and values by tracing interesting calculations backwards through evaluated
  808. // expressions along a path. This is probably overly complicated, but the idea
  809. // is that if an expression computed an "interesting" value, the child
  810. // expressions are also likely to be "interesting" as well (which then
  811. // propagates to the values they in turn compute). This reverse propagation
  812. // is needed to track interesting correlations across function call boundaries,
  813. // where formal arguments bind to actual arguments, etc. This is also needed
  814. // because the constraint solver sometimes simplifies certain symbolic values
  815. // into constants when appropriate, and this complicates reasoning about
  816. // interesting values.
  817. static void reversePropagateIntererstingSymbols(BugReport &R,
  818. InterestingExprs &IE,
  819. const ProgramState *State,
  820. const Expr *Ex,
  821. const LocationContext *LCtx) {
  822. SVal V = State->getSVal(Ex, LCtx);
  823. if (!(R.isInteresting(V) || IE.count(Ex)))
  824. return;
  825. switch (Ex->getStmtClass()) {
  826. default:
  827. if (!isa<CastExpr>(Ex))
  828. break;
  829. LLVM_FALLTHROUGH;
  830. case Stmt::BinaryOperatorClass:
  831. case Stmt::UnaryOperatorClass: {
  832. for (const Stmt *SubStmt : Ex->children()) {
  833. if (const auto *child = dyn_cast_or_null<Expr>(SubStmt)) {
  834. IE.insert(child);
  835. SVal ChildV = State->getSVal(child, LCtx);
  836. R.markInteresting(ChildV);
  837. }
  838. }
  839. break;
  840. }
  841. }
  842. R.markInteresting(V);
  843. }
  844. static void reversePropagateInterestingSymbols(BugReport &R,
  845. InterestingExprs &IE,
  846. const ProgramState *State,
  847. const LocationContext *CalleeCtx)
  848. {
  849. // FIXME: Handle non-CallExpr-based CallEvents.
  850. const StackFrameContext *Callee = CalleeCtx->getStackFrame();
  851. const Stmt *CallSite = Callee->getCallSite();
  852. if (const auto *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
  853. if (const auto *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
  854. FunctionDecl::param_const_iterator PI = FD->param_begin(),
  855. PE = FD->param_end();
  856. CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
  857. for (; AI != AE && PI != PE; ++AI, ++PI) {
  858. if (const Expr *ArgE = *AI) {
  859. if (const ParmVarDecl *PD = *PI) {
  860. Loc LV = State->getLValue(PD, CalleeCtx);
  861. if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
  862. IE.insert(ArgE);
  863. }
  864. }
  865. }
  866. }
  867. }
  868. }
  869. //===----------------------------------------------------------------------===//
  870. // Functions for determining if a loop was executed 0 times.
  871. //===----------------------------------------------------------------------===//
  872. static bool isLoop(const Stmt *Term) {
  873. switch (Term->getStmtClass()) {
  874. case Stmt::ForStmtClass:
  875. case Stmt::WhileStmtClass:
  876. case Stmt::ObjCForCollectionStmtClass:
  877. case Stmt::CXXForRangeStmtClass:
  878. return true;
  879. default:
  880. // Note that we intentionally do not include do..while here.
  881. return false;
  882. }
  883. }
  884. static bool isJumpToFalseBranch(const BlockEdge *BE) {
  885. const CFGBlock *Src = BE->getSrc();
  886. assert(Src->succ_size() == 2);
  887. return (*(Src->succ_begin()+1) == BE->getDst());
  888. }
  889. static bool isContainedByStmt(const ParentMap &PM, const Stmt *S,
  890. const Stmt *SubS) {
  891. while (SubS) {
  892. if (SubS == S)
  893. return true;
  894. SubS = PM.getParent(SubS);
  895. }
  896. return false;
  897. }
  898. static const Stmt *getStmtBeforeCond(const ParentMap &PM, const Stmt *Term,
  899. const ExplodedNode *N) {
  900. while (N) {
  901. Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
  902. if (SP) {
  903. const Stmt *S = SP->getStmt();
  904. if (!isContainedByStmt(PM, Term, S))
  905. return S;
  906. }
  907. N = N->getFirstPred();
  908. }
  909. return nullptr;
  910. }
  911. static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term) {
  912. const Stmt *LoopBody = nullptr;
  913. switch (Term->getStmtClass()) {
  914. case Stmt::CXXForRangeStmtClass: {
  915. const auto *FR = cast<CXXForRangeStmt>(Term);
  916. if (isContainedByStmt(PM, FR->getInc(), S))
  917. return true;
  918. if (isContainedByStmt(PM, FR->getLoopVarStmt(), S))
  919. return true;
  920. LoopBody = FR->getBody();
  921. break;
  922. }
  923. case Stmt::ForStmtClass: {
  924. const auto *FS = cast<ForStmt>(Term);
  925. if (isContainedByStmt(PM, FS->getInc(), S))
  926. return true;
  927. LoopBody = FS->getBody();
  928. break;
  929. }
  930. case Stmt::ObjCForCollectionStmtClass: {
  931. const auto *FC = cast<ObjCForCollectionStmt>(Term);
  932. LoopBody = FC->getBody();
  933. break;
  934. }
  935. case Stmt::WhileStmtClass:
  936. LoopBody = cast<WhileStmt>(Term)->getBody();
  937. break;
  938. default:
  939. return false;
  940. }
  941. return isContainedByStmt(PM, LoopBody, S);
  942. }
  943. /// Adds a sanitized control-flow diagnostic edge to a path.
  944. static void addEdgeToPath(PathPieces &path,
  945. PathDiagnosticLocation &PrevLoc,
  946. PathDiagnosticLocation NewLoc) {
  947. if (!NewLoc.isValid())
  948. return;
  949. SourceLocation NewLocL = NewLoc.asLocation();
  950. if (NewLocL.isInvalid())
  951. return;
  952. if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) {
  953. PrevLoc = NewLoc;
  954. return;
  955. }
  956. // Ignore self-edges, which occur when there are multiple nodes at the same
  957. // statement.
  958. if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt())
  959. return;
  960. path.push_front(
  961. std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc));
  962. PrevLoc = NewLoc;
  963. }
  964. /// A customized wrapper for CFGBlock::getTerminatorCondition()
  965. /// which returns the element for ObjCForCollectionStmts.
  966. static const Stmt *getTerminatorCondition(const CFGBlock *B) {
  967. const Stmt *S = B->getTerminatorCondition();
  968. if (const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(S))
  969. return FS->getElement();
  970. return S;
  971. }
  972. llvm::StringLiteral StrEnteringLoop = "Entering loop body";
  973. llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times";
  974. llvm::StringLiteral StrLoopRangeEmpty = "Loop body skipped when range is empty";
  975. llvm::StringLiteral StrLoopCollectionEmpty =
  976. "Loop body skipped when collection is empty";
  977. static std::unique_ptr<FilesToLineNumsMap>
  978. findExecutedLines(const SourceManager &SM, const ExplodedNode *N);
  979. void PathDiagnosticBuilder::generatePathDiagnosticsForNode(
  980. PathDiagnosticConstruct &C, PathDiagnosticLocation &PrevLoc) const {
  981. ProgramPoint P = C.getCurrentNode()->getLocation();
  982. const SourceManager &SM = getSourceManager();
  983. // Have we encountered an entrance to a call? It may be
  984. // the case that we have not encountered a matching
  985. // call exit before this point. This means that the path
  986. // terminated within the call itself.
  987. if (auto CE = P.getAs<CallEnter>()) {
  988. if (C.shouldAddPathEdges()) {
  989. // Add an edge to the start of the function.
  990. const StackFrameContext *CalleeLC = CE->getCalleeContext();
  991. const Decl *D = CalleeLC->getDecl();
  992. // Add the edge only when the callee has body. We jump to the beginning
  993. // of the *declaration*, however we expect it to be followed by the
  994. // body. This isn't the case for autosynthesized property accessors in
  995. // Objective-C. No need for a similar extra check for CallExit points
  996. // because the exit edge comes from a statement (i.e. return),
  997. // not from declaration.
  998. if (D->hasBody())
  999. addEdgeToPath(C.getActivePath(), PrevLoc,
  1000. PathDiagnosticLocation::createBegin(D, SM));
  1001. }
  1002. // Did we visit an entire call?
  1003. bool VisitedEntireCall = C.PD->isWithinCall();
  1004. C.PD->popActivePath();
  1005. PathDiagnosticCallPiece *Call;
  1006. if (VisitedEntireCall) {
  1007. Call = cast<PathDiagnosticCallPiece>(C.getActivePath().front().get());
  1008. } else {
  1009. // The path terminated within a nested location context, create a new
  1010. // call piece to encapsulate the rest of the path pieces.
  1011. const Decl *Caller = CE->getLocationContext()->getDecl();
  1012. Call = PathDiagnosticCallPiece::construct(C.getActivePath(), Caller);
  1013. assert(C.getActivePath().size() == 1 &&
  1014. C.getActivePath().front().get() == Call);
  1015. // Since we just transferred the path over to the call piece, reset the
  1016. // mapping of the active path to the current location context.
  1017. assert(C.isInLocCtxMap(&C.getActivePath()) &&
  1018. "When we ascend to a previously unvisited call, the active path's "
  1019. "address shouldn't change, but rather should be compacted into "
  1020. "a single CallEvent!");
  1021. C.updateLocCtxMap(&C.getActivePath(), C.getCurrLocationContext());
  1022. // Record the location context mapping for the path within the call.
  1023. assert(!C.isInLocCtxMap(&Call->path) &&
  1024. "When we ascend to a previously unvisited call, this must be the "
  1025. "first time we encounter the caller context!");
  1026. C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
  1027. }
  1028. Call->setCallee(*CE, SM);
  1029. // Update the previous location in the active path.
  1030. PrevLoc = Call->getLocation();
  1031. if (!C.CallStack.empty()) {
  1032. assert(C.CallStack.back().first == Call);
  1033. C.CallStack.pop_back();
  1034. }
  1035. return;
  1036. }
  1037. assert(C.getCurrLocationContext() == C.getLocationContextForActivePath() &&
  1038. "The current position in the bug path is out of sync with the "
  1039. "location context associated with the active path!");
  1040. // Have we encountered an exit from a function call?
  1041. if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
  1042. // We are descending into a call (backwards). Construct
  1043. // a new call piece to contain the path pieces for that call.
  1044. auto Call = PathDiagnosticCallPiece::construct(*CE, SM);
  1045. // Record the mapping from call piece to LocationContext.
  1046. assert(!C.isInLocCtxMap(&Call->path) &&
  1047. "We just entered a call, this must've been the first time we "
  1048. "encounter its context!");
  1049. C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
  1050. if (C.shouldAddPathEdges()) {
  1051. const Stmt *S = CE->getCalleeContext()->getCallSite();
  1052. // Propagate the interesting symbols accordingly.
  1053. if (const auto *Ex = dyn_cast_or_null<Expr>(S)) {
  1054. reversePropagateIntererstingSymbols(
  1055. *getBugReport(), C.IE, C.getCurrentNode()->getState().get(), Ex,
  1056. C.getCurrLocationContext());
  1057. }
  1058. // Add the edge to the return site.
  1059. addEdgeToPath(C.getActivePath(), PrevLoc, Call->callReturn);
  1060. PrevLoc.invalidate();
  1061. }
  1062. auto *P = Call.get();
  1063. C.getActivePath().push_front(std::move(Call));
  1064. // Make the contents of the call the active path for now.
  1065. C.PD->pushActivePath(&P->path);
  1066. C.CallStack.push_back(CallWithEntry(P, C.getCurrentNode()));
  1067. return;
  1068. }
  1069. if (auto PS = P.getAs<PostStmt>()) {
  1070. if (!C.shouldAddPathEdges())
  1071. return;
  1072. // For expressions, make sure we propagate the
  1073. // interesting symbols correctly.
  1074. if (const Expr *Ex = PS->getStmtAs<Expr>())
  1075. reversePropagateIntererstingSymbols(*getBugReport(), C.IE,
  1076. C.getCurrentNode()->getState().get(),
  1077. Ex, C.getCurrLocationContext());
  1078. // Add an edge. If this is an ObjCForCollectionStmt do
  1079. // not add an edge here as it appears in the CFG both
  1080. // as a terminator and as a terminator condition.
  1081. if (!isa<ObjCForCollectionStmt>(PS->getStmt())) {
  1082. PathDiagnosticLocation L =
  1083. PathDiagnosticLocation(PS->getStmt(), SM, C.getCurrLocationContext());
  1084. addEdgeToPath(C.getActivePath(), PrevLoc, L);
  1085. }
  1086. } else if (auto BE = P.getAs<BlockEdge>()) {
  1087. if (!C.shouldAddPathEdges()) {
  1088. generateMinimalDiagForBlockEdge(C, *BE);
  1089. return;
  1090. }
  1091. // Does this represent entering a call? If so, look at propagating
  1092. // interesting symbols across call boundaries.
  1093. if (const ExplodedNode *NextNode = C.getCurrentNode()->getFirstPred()) {
  1094. const LocationContext *CallerCtx = NextNode->getLocationContext();
  1095. const LocationContext *CalleeCtx = C.getCurrLocationContext();
  1096. if (CallerCtx != CalleeCtx && C.shouldAddPathEdges()) {
  1097. reversePropagateInterestingSymbols(*getBugReport(), C.IE,
  1098. C.getCurrentNode()->getState().get(),
  1099. CalleeCtx);
  1100. }
  1101. }
  1102. // Are we jumping to the head of a loop? Add a special diagnostic.
  1103. if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
  1104. PathDiagnosticLocation L(Loop, SM, C.getCurrLocationContext());
  1105. const Stmt *Body = nullptr;
  1106. if (const auto *FS = dyn_cast<ForStmt>(Loop))
  1107. Body = FS->getBody();
  1108. else if (const auto *WS = dyn_cast<WhileStmt>(Loop))
  1109. Body = WS->getBody();
  1110. else if (const auto *OFS = dyn_cast<ObjCForCollectionStmt>(Loop)) {
  1111. Body = OFS->getBody();
  1112. } else if (const auto *FRS = dyn_cast<CXXForRangeStmt>(Loop)) {
  1113. Body = FRS->getBody();
  1114. }
  1115. // do-while statements are explicitly excluded here
  1116. auto p = std::make_shared<PathDiagnosticEventPiece>(
  1117. L, "Looping back to the head "
  1118. "of the loop");
  1119. p->setPrunable(true);
  1120. addEdgeToPath(C.getActivePath(), PrevLoc, p->getLocation());
  1121. C.getActivePath().push_front(std::move(p));
  1122. if (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
  1123. addEdgeToPath(C.getActivePath(), PrevLoc,
  1124. PathDiagnosticLocation::createEndBrace(CS, SM));
  1125. }
  1126. }
  1127. const CFGBlock *BSrc = BE->getSrc();
  1128. const ParentMap &PM = C.getParentMap();
  1129. if (const Stmt *Term = BSrc->getTerminatorStmt()) {
  1130. // Are we jumping past the loop body without ever executing the
  1131. // loop (because the condition was false)?
  1132. if (isLoop(Term)) {
  1133. const Stmt *TermCond = getTerminatorCondition(BSrc);
  1134. bool IsInLoopBody = isInLoopBody(
  1135. PM, getStmtBeforeCond(PM, TermCond, C.getCurrentNode()), Term);
  1136. StringRef str;
  1137. if (isJumpToFalseBranch(&*BE)) {
  1138. if (!IsInLoopBody) {
  1139. if (isa<ObjCForCollectionStmt>(Term)) {
  1140. str = StrLoopCollectionEmpty;
  1141. } else if (isa<CXXForRangeStmt>(Term)) {
  1142. str = StrLoopRangeEmpty;
  1143. } else {
  1144. str = StrLoopBodyZero;
  1145. }
  1146. }
  1147. } else {
  1148. str = StrEnteringLoop;
  1149. }
  1150. if (!str.empty()) {
  1151. PathDiagnosticLocation L(TermCond ? TermCond : Term, SM,
  1152. C.getCurrLocationContext());
  1153. auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str);
  1154. PE->setPrunable(true);
  1155. addEdgeToPath(C.getActivePath(), PrevLoc, PE->getLocation());
  1156. C.getActivePath().push_front(std::move(PE));
  1157. }
  1158. } else if (isa<BreakStmt>(Term) || isa<ContinueStmt>(Term) ||
  1159. isa<GotoStmt>(Term)) {
  1160. PathDiagnosticLocation L(Term, SM, C.getCurrLocationContext());
  1161. addEdgeToPath(C.getActivePath(), PrevLoc, L);
  1162. }
  1163. }
  1164. }
  1165. }
  1166. static std::unique_ptr<PathDiagnostic>
  1167. generateEmptyDiagnosticForReport(const BugReport *R, const SourceManager &SM) {
  1168. const BugType &BT = R->getBugType();
  1169. return llvm::make_unique<PathDiagnostic>(
  1170. R->getBugType().getCheckName(), R->getDeclWithIssue(),
  1171. R->getBugType().getName(), R->getDescription(),
  1172. R->getShortDescription(/*UseFallback=*/false), BT.getCategory(),
  1173. R->getUniqueingLocation(), R->getUniqueingDecl(),
  1174. findExecutedLines(SM, R->getErrorNode()));
  1175. }
  1176. static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) {
  1177. if (!S)
  1178. return nullptr;
  1179. while (true) {
  1180. S = PM.getParentIgnoreParens(S);
  1181. if (!S)
  1182. break;
  1183. if (isa<FullExpr>(S) ||
  1184. isa<CXXBindTemporaryExpr>(S) ||
  1185. isa<SubstNonTypeTemplateParmExpr>(S))
  1186. continue;
  1187. break;
  1188. }
  1189. return S;
  1190. }
  1191. static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
  1192. switch (S->getStmtClass()) {
  1193. case Stmt::BinaryOperatorClass: {
  1194. const auto *BO = cast<BinaryOperator>(S);
  1195. if (!BO->isLogicalOp())
  1196. return false;
  1197. return BO->getLHS() == Cond || BO->getRHS() == Cond;
  1198. }
  1199. case Stmt::IfStmtClass:
  1200. return cast<IfStmt>(S)->getCond() == Cond;
  1201. case Stmt::ForStmtClass:
  1202. return cast<ForStmt>(S)->getCond() == Cond;
  1203. case Stmt::WhileStmtClass:
  1204. return cast<WhileStmt>(S)->getCond() == Cond;
  1205. case Stmt::DoStmtClass:
  1206. return cast<DoStmt>(S)->getCond() == Cond;
  1207. case Stmt::ChooseExprClass:
  1208. return cast<ChooseExpr>(S)->getCond() == Cond;
  1209. case Stmt::IndirectGotoStmtClass:
  1210. return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
  1211. case Stmt::SwitchStmtClass:
  1212. return cast<SwitchStmt>(S)->getCond() == Cond;
  1213. case Stmt::BinaryConditionalOperatorClass:
  1214. return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
  1215. case Stmt::ConditionalOperatorClass: {
  1216. const auto *CO = cast<ConditionalOperator>(S);
  1217. return CO->getCond() == Cond ||
  1218. CO->getLHS() == Cond ||
  1219. CO->getRHS() == Cond;
  1220. }
  1221. case Stmt::ObjCForCollectionStmtClass:
  1222. return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
  1223. case Stmt::CXXForRangeStmtClass: {
  1224. const auto *FRS = cast<CXXForRangeStmt>(S);
  1225. return FRS->getCond() == Cond || FRS->getRangeInit() == Cond;
  1226. }
  1227. default:
  1228. return false;
  1229. }
  1230. }
  1231. static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
  1232. if (const auto *FS = dyn_cast<ForStmt>(FL))
  1233. return FS->getInc() == S || FS->getInit() == S;
  1234. if (const auto *FRS = dyn_cast<CXXForRangeStmt>(FL))
  1235. return FRS->getInc() == S || FRS->getRangeStmt() == S ||
  1236. FRS->getLoopVarStmt() || FRS->getRangeInit() == S;
  1237. return false;
  1238. }
  1239. using OptimizedCallsSet = llvm::DenseSet<const PathDiagnosticCallPiece *>;
  1240. /// Adds synthetic edges from top-level statements to their subexpressions.
  1241. ///
  1242. /// This avoids a "swoosh" effect, where an edge from a top-level statement A
  1243. /// points to a sub-expression B.1 that's not at the start of B. In these cases,
  1244. /// we'd like to see an edge from A to B, then another one from B to B.1.
  1245. static void addContextEdges(PathPieces &pieces, const LocationContext *LC) {
  1246. const ParentMap &PM = LC->getParentMap();
  1247. PathPieces::iterator Prev = pieces.end();
  1248. for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
  1249. Prev = I, ++I) {
  1250. auto *Piece = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
  1251. if (!Piece)
  1252. continue;
  1253. PathDiagnosticLocation SrcLoc = Piece->getStartLocation();
  1254. SmallVector<PathDiagnosticLocation, 4> SrcContexts;
  1255. PathDiagnosticLocation NextSrcContext = SrcLoc;
  1256. const Stmt *InnerStmt = nullptr;
  1257. while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) {
  1258. SrcContexts.push_back(NextSrcContext);
  1259. InnerStmt = NextSrcContext.asStmt();
  1260. NextSrcContext = getEnclosingStmtLocation(InnerStmt, LC,
  1261. /*allowNested=*/true);
  1262. }
  1263. // Repeatedly split the edge as necessary.
  1264. // This is important for nested logical expressions (||, &&, ?:) where we
  1265. // want to show all the levels of context.
  1266. while (true) {
  1267. const Stmt *Dst = Piece->getEndLocation().getStmtOrNull();
  1268. // We are looking at an edge. Is the destination within a larger
  1269. // expression?
  1270. PathDiagnosticLocation DstContext =
  1271. getEnclosingStmtLocation(Dst, LC, /*allowNested=*/true);
  1272. if (!DstContext.isValid() || DstContext.asStmt() == Dst)
  1273. break;
  1274. // If the source is in the same context, we're already good.
  1275. if (llvm::find(SrcContexts, DstContext) != SrcContexts.end())
  1276. break;
  1277. // Update the subexpression node to point to the context edge.
  1278. Piece->setStartLocation(DstContext);
  1279. // Try to extend the previous edge if it's at the same level as the source
  1280. // context.
  1281. if (Prev != E) {
  1282. auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get());
  1283. if (PrevPiece) {
  1284. if (const Stmt *PrevSrc =
  1285. PrevPiece->getStartLocation().getStmtOrNull()) {
  1286. const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM);
  1287. if (PrevSrcParent ==
  1288. getStmtParent(DstContext.getStmtOrNull(), PM)) {
  1289. PrevPiece->setEndLocation(DstContext);
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. }
  1295. // Otherwise, split the current edge into a context edge and a
  1296. // subexpression edge. Note that the context statement may itself have
  1297. // context.
  1298. auto P =
  1299. std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext);
  1300. Piece = P.get();
  1301. I = pieces.insert(I, std::move(P));
  1302. }
  1303. }
  1304. }
  1305. /// Move edges from a branch condition to a branch target
  1306. /// when the condition is simple.
  1307. ///
  1308. /// This restructures some of the work of addContextEdges. That function
  1309. /// creates edges this may destroy, but they work together to create a more
  1310. /// aesthetically set of edges around branches. After the call to
  1311. /// addContextEdges, we may have (1) an edge to the branch, (2) an edge from
  1312. /// the branch to the branch condition, and (3) an edge from the branch
  1313. /// condition to the branch target. We keep (1), but may wish to remove (2)
  1314. /// and move the source of (3) to the branch if the branch condition is simple.
  1315. static void simplifySimpleBranches(PathPieces &pieces) {
  1316. for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) {
  1317. const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
  1318. if (!PieceI)
  1319. continue;
  1320. const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
  1321. const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull();
  1322. if (!s1Start || !s1End)
  1323. continue;
  1324. PathPieces::iterator NextI = I; ++NextI;
  1325. if (NextI == E)
  1326. break;
  1327. PathDiagnosticControlFlowPiece *PieceNextI = nullptr;
  1328. while (true) {
  1329. if (NextI == E)
  1330. break;
  1331. const auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
  1332. if (EV) {
  1333. StringRef S = EV->getString();
  1334. if (S == StrEnteringLoop || S == StrLoopBodyZero ||
  1335. S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) {
  1336. ++NextI;
  1337. continue;
  1338. }
  1339. break;
  1340. }
  1341. PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
  1342. break;
  1343. }
  1344. if (!PieceNextI)
  1345. continue;
  1346. const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
  1347. const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull();
  1348. if (!s2Start || !s2End || s1End != s2Start)
  1349. continue;
  1350. // We only perform this transformation for specific branch kinds.
  1351. // We don't want to do this for do..while, for example.
  1352. if (!(isa<ForStmt>(s1Start) || isa<WhileStmt>(s1Start) ||
  1353. isa<IfStmt>(s1Start) || isa<ObjCForCollectionStmt>(s1Start) ||
  1354. isa<CXXForRangeStmt>(s1Start)))
  1355. continue;
  1356. // Is s1End the branch condition?
  1357. if (!isConditionForTerminator(s1Start, s1End))
  1358. continue;
  1359. // Perform the hoisting by eliminating (2) and changing the start
  1360. // location of (3).
  1361. PieceNextI->setStartLocation(PieceI->getStartLocation());
  1362. I = pieces.erase(I);
  1363. }
  1364. }
  1365. /// Returns the number of bytes in the given (character-based) SourceRange.
  1366. ///
  1367. /// If the locations in the range are not on the same line, returns None.
  1368. ///
  1369. /// Note that this does not do a precise user-visible character or column count.
  1370. static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
  1371. SourceRange Range) {
  1372. SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
  1373. SM.getExpansionRange(Range.getEnd()).getEnd());
  1374. FileID FID = SM.getFileID(ExpansionRange.getBegin());
  1375. if (FID != SM.getFileID(ExpansionRange.getEnd()))
  1376. return None;
  1377. bool Invalid;
  1378. const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, &Invalid);
  1379. if (Invalid)
  1380. return None;
  1381. unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
  1382. unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd());
  1383. StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset);
  1384. // We're searching the raw bytes of the buffer here, which might include
  1385. // escaped newlines and such. That's okay; we're trying to decide whether the
  1386. // SourceRange is covering a large or small amount of space in the user's
  1387. // editor.
  1388. if (Snippet.find_first_of("\r\n") != StringRef::npos)
  1389. return None;
  1390. // This isn't Unicode-aware, but it doesn't need to be.
  1391. return Snippet.size();
  1392. }
  1393. /// \sa getLengthOnSingleLine(SourceManager, SourceRange)
  1394. static Optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
  1395. const Stmt *S) {
  1396. return getLengthOnSingleLine(SM, S->getSourceRange());
  1397. }
  1398. /// Eliminate two-edge cycles created by addContextEdges().
  1399. ///
  1400. /// Once all the context edges are in place, there are plenty of cases where
  1401. /// there's a single edge from a top-level statement to a subexpression,
  1402. /// followed by a single path note, and then a reverse edge to get back out to
  1403. /// the top level. If the statement is simple enough, the subexpression edges
  1404. /// just add noise and make it harder to understand what's going on.
  1405. ///
  1406. /// This function only removes edges in pairs, because removing only one edge
  1407. /// might leave other edges dangling.
  1408. ///
  1409. /// This will not remove edges in more complicated situations:
  1410. /// - if there is more than one "hop" leading to or from a subexpression.
  1411. /// - if there is an inlined call between the edges instead of a single event.
  1412. /// - if the whole statement is large enough that having subexpression arrows
  1413. /// might be helpful.
  1414. static void removeContextCycles(PathPieces &Path, const SourceManager &SM) {
  1415. for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
  1416. // Pattern match the current piece and its successor.
  1417. const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
  1418. if (!PieceI) {
  1419. ++I;
  1420. continue;
  1421. }
  1422. const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
  1423. const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull();
  1424. PathPieces::iterator NextI = I; ++NextI;
  1425. if (NextI == E)
  1426. break;
  1427. const auto *PieceNextI =
  1428. dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
  1429. if (!PieceNextI) {
  1430. if (isa<PathDiagnosticEventPiece>(NextI->get())) {
  1431. ++NextI;
  1432. if (NextI == E)
  1433. break;
  1434. PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
  1435. }
  1436. if (!PieceNextI) {
  1437. ++I;
  1438. continue;
  1439. }
  1440. }
  1441. const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
  1442. const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull();
  1443. if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) {
  1444. const size_t MAX_SHORT_LINE_LENGTH = 80;
  1445. Optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start);
  1446. if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) {
  1447. Optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start);
  1448. if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) {
  1449. Path.erase(I);
  1450. I = Path.erase(NextI);
  1451. continue;
  1452. }
  1453. }
  1454. }
  1455. ++I;
  1456. }
  1457. }
  1458. /// Return true if X is contained by Y.
  1459. static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y) {
  1460. while (X) {
  1461. if (X == Y)
  1462. return true;
  1463. X = PM.getParent(X);
  1464. }
  1465. return false;
  1466. }
  1467. // Remove short edges on the same line less than 3 columns in difference.
  1468. static void removePunyEdges(PathPieces &path, const SourceManager &SM,
  1469. const ParentMap &PM) {
  1470. bool erased = false;
  1471. for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
  1472. erased ? I : ++I) {
  1473. erased = false;
  1474. const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
  1475. if (!PieceI)
  1476. continue;
  1477. const Stmt *start = PieceI->getStartLocation().getStmtOrNull();
  1478. const Stmt *end = PieceI->getEndLocation().getStmtOrNull();
  1479. if (!start || !end)
  1480. continue;
  1481. const Stmt *endParent = PM.getParent(end);
  1482. if (!endParent)
  1483. continue;
  1484. if (isConditionForTerminator(end, endParent))
  1485. continue;
  1486. SourceLocation FirstLoc = start->getBeginLoc();
  1487. SourceLocation SecondLoc = end->getBeginLoc();
  1488. if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
  1489. continue;
  1490. if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
  1491. std::swap(SecondLoc, FirstLoc);
  1492. SourceRange EdgeRange(FirstLoc, SecondLoc);
  1493. Optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange);
  1494. // If the statements are on different lines, continue.
  1495. if (!ByteWidth)
  1496. continue;
  1497. const size_t MAX_PUNY_EDGE_LENGTH = 2;
  1498. if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) {
  1499. // FIXME: There are enough /bytes/ between the endpoints of the edge, but
  1500. // there might not be enough /columns/. A proper user-visible column count
  1501. // is probably too expensive, though.
  1502. I = path.erase(I);
  1503. erased = true;
  1504. continue;
  1505. }
  1506. }
  1507. }
  1508. static void removeIdenticalEvents(PathPieces &path) {
  1509. for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) {
  1510. const auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get());
  1511. if (!PieceI)
  1512. continue;
  1513. PathPieces::iterator NextI = I; ++NextI;
  1514. if (NextI == E)
  1515. return;
  1516. const auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
  1517. if (!PieceNextI)
  1518. continue;
  1519. // Erase the second piece if it has the same exact message text.
  1520. if (PieceI->getString() == PieceNextI->getString()) {
  1521. path.erase(NextI);
  1522. }
  1523. }
  1524. }
  1525. static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path,
  1526. OptimizedCallsSet &OCS) {
  1527. bool hasChanges = false;
  1528. const LocationContext *LC = C.getLocationContextFor(&path);
  1529. assert(LC);
  1530. const ParentMap &PM = LC->getParentMap();
  1531. const SourceManager &SM = C.getSourceManager();
  1532. for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
  1533. // Optimize subpaths.
  1534. if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) {
  1535. // Record the fact that a call has been optimized so we only do the
  1536. // effort once.
  1537. if (!OCS.count(CallI)) {
  1538. while (optimizeEdges(C, CallI->path, OCS)) {
  1539. }
  1540. OCS.insert(CallI);
  1541. }
  1542. ++I;
  1543. continue;
  1544. }
  1545. // Pattern match the current piece and its successor.
  1546. auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
  1547. if (!PieceI) {
  1548. ++I;
  1549. continue;
  1550. }
  1551. const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
  1552. const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull();
  1553. const Stmt *level1 = getStmtParent(s1Start, PM);
  1554. const Stmt *level2 = getStmtParent(s1End, PM);
  1555. PathPieces::iterator NextI = I; ++NextI;
  1556. if (NextI == E)
  1557. break;
  1558. const auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
  1559. if (!PieceNextI) {
  1560. ++I;
  1561. continue;
  1562. }
  1563. const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
  1564. const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull();
  1565. const Stmt *level3 = getStmtParent(s2Start, PM);
  1566. const Stmt *level4 = getStmtParent(s2End, PM);
  1567. // Rule I.
  1568. //
  1569. // If we have two consecutive control edges whose end/begin locations
  1570. // are at the same level (e.g. statements or top-level expressions within
  1571. // a compound statement, or siblings share a single ancestor expression),
  1572. // then merge them if they have no interesting intermediate event.
  1573. //
  1574. // For example:
  1575. //
  1576. // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
  1577. // parent is '1'. Here 'x.y.z' represents the hierarchy of statements.
  1578. //
  1579. // NOTE: this will be limited later in cases where we add barriers
  1580. // to prevent this optimization.
  1581. if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
  1582. PieceI->setEndLocation(PieceNextI->getEndLocation());
  1583. path.erase(NextI);
  1584. hasChanges = true;
  1585. continue;
  1586. }
  1587. // Rule II.
  1588. //
  1589. // Eliminate edges between subexpressions and parent expressions
  1590. // when the subexpression is consumed.
  1591. //
  1592. // NOTE: this will be limited later in cases where we add barriers
  1593. // to prevent this optimization.
  1594. if (s1End && s1End == s2Start && level2) {
  1595. bool removeEdge = false;
  1596. // Remove edges into the increment or initialization of a
  1597. // loop that have no interleaving event. This means that
  1598. // they aren't interesting.
  1599. if (isIncrementOrInitInForLoop(s1End, level2))
  1600. removeEdge = true;
  1601. // Next only consider edges that are not anchored on
  1602. // the condition of a terminator. This are intermediate edges
  1603. // that we might want to trim.
  1604. else if (!isConditionForTerminator(level2, s1End)) {
  1605. // Trim edges on expressions that are consumed by
  1606. // the parent expression.
  1607. if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) {
  1608. removeEdge = true;
  1609. }
  1610. // Trim edges where a lexical containment doesn't exist.
  1611. // For example:
  1612. //
  1613. // X -> Y -> Z
  1614. //
  1615. // If 'Z' lexically contains Y (it is an ancestor) and
  1616. // 'X' does not lexically contain Y (it is a descendant OR
  1617. // it has no lexical relationship at all) then trim.
  1618. //
  1619. // This can eliminate edges where we dive into a subexpression
  1620. // and then pop back out, etc.
  1621. else if (s1Start && s2End &&
  1622. lexicalContains(PM, s2Start, s2End) &&
  1623. !lexicalContains(PM, s1End, s1Start)) {
  1624. removeEdge = true;
  1625. }
  1626. // Trim edges from a subexpression back to the top level if the
  1627. // subexpression is on a different line.
  1628. //
  1629. // A.1 -> A -> B
  1630. // becomes
  1631. // A.1 -> B
  1632. //
  1633. // These edges just look ugly and don't usually add anything.
  1634. else if (s1Start && s2End &&
  1635. lexicalContains(PM, s1Start, s1End)) {
  1636. SourceRange EdgeRange(PieceI->getEndLocation().asLocation(),
  1637. PieceI->getStartLocation().asLocation());
  1638. if (!getLengthOnSingleLine(SM, EdgeRange).hasValue())
  1639. removeEdge = true;
  1640. }
  1641. }
  1642. if (removeEdge) {
  1643. PieceI->setEndLocation(PieceNextI->getEndLocation());
  1644. path.erase(NextI);
  1645. hasChanges = true;
  1646. continue;
  1647. }
  1648. }
  1649. // Optimize edges for ObjC fast-enumeration loops.
  1650. //
  1651. // (X -> collection) -> (collection -> element)
  1652. //
  1653. // becomes:
  1654. //
  1655. // (X -> element)
  1656. if (s1End == s2Start) {
  1657. const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(level3);
  1658. if (FS && FS->getCollection()->IgnoreParens() == s2Start &&
  1659. s2End == FS->getElement()) {
  1660. PieceI->setEndLocation(PieceNextI->getEndLocation());
  1661. path.erase(NextI);
  1662. hasChanges = true;
  1663. continue;
  1664. }
  1665. }
  1666. // No changes at this index? Move to the next one.
  1667. ++I;
  1668. }
  1669. if (!hasChanges) {
  1670. // Adjust edges into subexpressions to make them more uniform
  1671. // and aesthetically pleasing.
  1672. addContextEdges(path, LC);
  1673. // Remove "cyclical" edges that include one or more context edges.
  1674. removeContextCycles(path, SM);
  1675. // Hoist edges originating from branch conditions to branches
  1676. // for simple branches.
  1677. simplifySimpleBranches(path);
  1678. // Remove any puny edges left over after primary optimization pass.
  1679. removePunyEdges(path, SM, PM);
  1680. // Remove identical events.
  1681. removeIdenticalEvents(path);
  1682. }
  1683. return hasChanges;
  1684. }
  1685. /// Drop the very first edge in a path, which should be a function entry edge.
  1686. ///
  1687. /// If the first edge is not a function entry edge (say, because the first
  1688. /// statement had an invalid source location), this function does nothing.
  1689. // FIXME: We should just generate invalid edges anyway and have the optimizer
  1690. // deal with them.
  1691. static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C,
  1692. PathPieces &Path) {
  1693. const auto *FirstEdge =
  1694. dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get());
  1695. if (!FirstEdge)
  1696. return;
  1697. const Decl *D = C.getLocationContextFor(&Path)->getDecl();
  1698. PathDiagnosticLocation EntryLoc =
  1699. PathDiagnosticLocation::createBegin(D, C.getSourceManager());
  1700. if (FirstEdge->getStartLocation() != EntryLoc)
  1701. return;
  1702. Path.pop_front();
  1703. }
  1704. /// Populate executes lines with lines containing at least one diagnostics.
  1705. static void updateExecutedLinesWithDiagnosticPieces(PathDiagnostic &PD) {
  1706. PathPieces path = PD.path.flatten(/*ShouldFlattenMacros=*/true);
  1707. FilesToLineNumsMap &ExecutedLines = PD.getExecutedLines();
  1708. for (const auto &P : path) {
  1709. FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc();
  1710. FileID FID = Loc.getFileID();
  1711. unsigned LineNo = Loc.getLineNumber();
  1712. assert(FID.isValid());
  1713. ExecutedLines[FID].insert(LineNo);
  1714. }
  1715. }
  1716. PathDiagnosticConstruct::PathDiagnosticConstruct(
  1717. const PathDiagnosticConsumer *PDC, const ExplodedNode *ErrorNode,
  1718. const BugReport *R)
  1719. : Consumer(PDC), CurrentNode(ErrorNode),
  1720. SM(CurrentNode->getCodeDecl().getASTContext().getSourceManager()),
  1721. PD(generateEmptyDiagnosticForReport(R, getSourceManager())) {
  1722. LCM[&PD->getActivePath()] = ErrorNode->getLocationContext();
  1723. }
  1724. PathDiagnosticBuilder::PathDiagnosticBuilder(
  1725. BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
  1726. BugReport *r, const ExplodedNode *ErrorNode,
  1727. std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics)
  1728. : BugReporterContext(BRC), BugPath(std::move(BugPath)), R(r),
  1729. ErrorNode(ErrorNode),
  1730. VisitorsDiagnostics(std::move(VisitorsDiagnostics)) {}
  1731. std::unique_ptr<PathDiagnostic>
  1732. PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const {
  1733. if (!PDC->shouldGenerateDiagnostics())
  1734. return generateEmptyDiagnosticForReport(R, getSourceManager());
  1735. PathDiagnosticConstruct Construct(PDC, ErrorNode, R);
  1736. const SourceManager &SM = getSourceManager();
  1737. const BugReport *R = getBugReport();
  1738. const AnalyzerOptions &Opts = getAnalyzerOptions();
  1739. // Construct the final (warning) event for the bug report.
  1740. auto EndNotes = VisitorsDiagnostics->find(ErrorNode);
  1741. PathDiagnosticPieceRef LastPiece;
  1742. if (EndNotes != VisitorsDiagnostics->end()) {
  1743. assert(!EndNotes->second.empty());
  1744. LastPiece = EndNotes->second[0];
  1745. } else {
  1746. LastPiece = BugReporterVisitor::getDefaultEndPath(*this, ErrorNode,
  1747. *getBugReport());
  1748. }
  1749. Construct.PD->setEndOfPath(LastPiece);
  1750. PathDiagnosticLocation PrevLoc = Construct.PD->getLocation();
  1751. // From the error node to the root, ascend the bug path and construct the bug
  1752. // report.
  1753. while (Construct.ascendToPrevNode()) {
  1754. generatePathDiagnosticsForNode(Construct, PrevLoc);
  1755. auto VisitorNotes = VisitorsDiagnostics->find(Construct.getCurrentNode());
  1756. if (VisitorNotes == VisitorsDiagnostics->end())
  1757. continue;
  1758. // This is a workaround due to inability to put shared PathDiagnosticPiece
  1759. // into a FoldingSet.
  1760. std::set<llvm::FoldingSetNodeID> DeduplicationSet;
  1761. // Add pieces from custom visitors.
  1762. for (const PathDiagnosticPieceRef &Note : VisitorNotes->second) {
  1763. llvm::FoldingSetNodeID ID;
  1764. Note->Profile(ID);
  1765. if (!DeduplicationSet.insert(ID).second)
  1766. continue;
  1767. if (PDC->shouldAddPathEdges())
  1768. addEdgeToPath(Construct.getActivePath(), PrevLoc, Note->getLocation());
  1769. updateStackPiecesWithMessage(*Note, Construct.CallStack);
  1770. Construct.getActivePath().push_front(Note);
  1771. }
  1772. }
  1773. if (PDC->shouldAddPathEdges()) {
  1774. // Add an edge to the start of the function.
  1775. // We'll prune it out later, but it helps make diagnostics more uniform.
  1776. const StackFrameContext *CalleeLC =
  1777. Construct.getLocationContextForActivePath()->getStackFrame();
  1778. const Decl *D = CalleeLC->getDecl();
  1779. addEdgeToPath(Construct.getActivePath(), PrevLoc,
  1780. PathDiagnosticLocation::createBegin(D, SM));
  1781. }
  1782. // Finally, prune the diagnostic path of uninteresting stuff.
  1783. if (!Construct.PD->path.empty()) {
  1784. if (R->shouldPrunePath() && Opts.ShouldPrunePaths) {
  1785. bool stillHasNotes =
  1786. removeUnneededCalls(Construct, Construct.getMutablePieces(), R);
  1787. assert(stillHasNotes);
  1788. (void)stillHasNotes;
  1789. }
  1790. // Remove pop-up notes if needed.
  1791. if (!Opts.ShouldAddPopUpNotes)
  1792. removePopUpNotes(Construct.getMutablePieces());
  1793. // Redirect all call pieces to have valid locations.
  1794. adjustCallLocations(Construct.getMutablePieces());
  1795. removePiecesWithInvalidLocations(Construct.getMutablePieces());
  1796. if (PDC->shouldAddPathEdges()) {
  1797. // Reduce the number of edges from a very conservative set
  1798. // to an aesthetically pleasing subset that conveys the
  1799. // necessary information.
  1800. OptimizedCallsSet OCS;
  1801. while (optimizeEdges(Construct, Construct.getMutablePieces(), OCS)) {
  1802. }
  1803. // Drop the very first function-entry edge. It's not really necessary
  1804. // for top-level functions.
  1805. dropFunctionEntryEdge(Construct, Construct.getMutablePieces());
  1806. }
  1807. // Remove messages that are basically the same, and edges that may not
  1808. // make sense.
  1809. // We have to do this after edge optimization in the Extensive mode.
  1810. removeRedundantMsgs(Construct.getMutablePieces());
  1811. removeEdgesToDefaultInitializers(Construct.getMutablePieces());
  1812. }
  1813. if (Opts.ShouldDisplayMacroExpansions)
  1814. CompactMacroExpandedPieces(Construct.getMutablePieces(), SM);
  1815. return std::move(Construct.PD);
  1816. }
  1817. //===----------------------------------------------------------------------===//
  1818. // Methods for BugType and subclasses.
  1819. //===----------------------------------------------------------------------===//
  1820. void BugType::anchor() {}
  1821. void BuiltinBug::anchor() {}
  1822. //===----------------------------------------------------------------------===//
  1823. // Methods for BugReport and subclasses.
  1824. //===----------------------------------------------------------------------===//
  1825. void BugReport::NodeResolver::anchor() {}
  1826. void BugReport::addVisitor(std::unique_ptr<BugReporterVisitor> visitor) {
  1827. if (!visitor)
  1828. return;
  1829. llvm::FoldingSetNodeID ID;
  1830. visitor->Profile(ID);
  1831. void *InsertPos = nullptr;
  1832. if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
  1833. return;
  1834. }
  1835. Callbacks.push_back(std::move(visitor));
  1836. }
  1837. void BugReport::clearVisitors() {
  1838. Callbacks.clear();
  1839. }
  1840. const Decl *BugReport::getDeclWithIssue() const {
  1841. if (DeclWithIssue)
  1842. return DeclWithIssue;
  1843. const ExplodedNode *N = getErrorNode();
  1844. if (!N)
  1845. return nullptr;
  1846. const LocationContext *LC = N->getLocationContext();
  1847. return LC->getStackFrame()->getDecl();
  1848. }
  1849. void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
  1850. hash.AddPointer(&BT);
  1851. hash.AddString(Description);
  1852. PathDiagnosticLocation UL = getUniqueingLocation();
  1853. if (UL.isValid()) {
  1854. UL.Profile(hash);
  1855. } else if (Location.isValid()) {
  1856. Location.Profile(hash);
  1857. } else {
  1858. assert(ErrorNode);
  1859. hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
  1860. }
  1861. for (SourceRange range : Ranges) {
  1862. if (!range.isValid())
  1863. continue;
  1864. hash.AddInteger(range.getBegin().getRawEncoding());
  1865. hash.AddInteger(range.getEnd().getRawEncoding());
  1866. }
  1867. }
  1868. void BugReport::markInteresting(SymbolRef sym) {
  1869. if (!sym)
  1870. return;
  1871. InterestingSymbols.insert(sym);
  1872. if (const auto *meta = dyn_cast<SymbolMetadata>(sym))
  1873. InterestingRegions.insert(meta->getRegion());
  1874. }
  1875. void BugReport::markInteresting(const MemRegion *R) {
  1876. if (!R)
  1877. return;
  1878. R = R->getBaseRegion();
  1879. InterestingRegions.insert(R);
  1880. if (const auto *SR = dyn_cast<SymbolicRegion>(R))
  1881. InterestingSymbols.insert(SR->getSymbol());
  1882. }
  1883. void BugReport::markInteresting(SVal V) {
  1884. markInteresting(V.getAsRegion());
  1885. markInteresting(V.getAsSymbol());
  1886. }
  1887. void BugReport::markInteresting(const LocationContext *LC) {
  1888. if (!LC)
  1889. return;
  1890. InterestingLocationContexts.insert(LC);
  1891. }
  1892. bool BugReport::isInteresting(SVal V) const {
  1893. return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
  1894. }
  1895. bool BugReport::isInteresting(SymbolRef sym) const {
  1896. if (!sym)
  1897. return false;
  1898. // We don't currently consider metadata symbols to be interesting
  1899. // even if we know their region is interesting. Is that correct behavior?
  1900. return InterestingSymbols.count(sym);
  1901. }
  1902. bool BugReport::isInteresting(const MemRegion *R) const {
  1903. if (!R)
  1904. return false;
  1905. R = R->getBaseRegion();
  1906. bool b = InterestingRegions.count(R);
  1907. if (b)
  1908. return true;
  1909. if (const auto *SR = dyn_cast<SymbolicRegion>(R))
  1910. return InterestingSymbols.count(SR->getSymbol());
  1911. return false;
  1912. }
  1913. bool BugReport::isInteresting(const LocationContext *LC) const {
  1914. if (!LC)
  1915. return false;
  1916. return InterestingLocationContexts.count(LC);
  1917. }
  1918. const Stmt *BugReport::getStmt() const {
  1919. if (!ErrorNode)
  1920. return nullptr;
  1921. ProgramPoint ProgP = ErrorNode->getLocation();
  1922. const Stmt *S = nullptr;
  1923. if (Optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
  1924. CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
  1925. if (BE->getBlock() == &Exit)
  1926. S = GetPreviousStmt(ErrorNode);
  1927. }
  1928. if (!S)
  1929. S = PathDiagnosticLocation::getStmt(ErrorNode);
  1930. return S;
  1931. }
  1932. llvm::iterator_range<BugReport::ranges_iterator> BugReport::getRanges() {
  1933. // If no custom ranges, add the range of the statement corresponding to
  1934. // the error node.
  1935. if (Ranges.empty()) {
  1936. if (const auto *E = dyn_cast_or_null<Expr>(getStmt()))
  1937. addRange(E->getSourceRange());
  1938. else
  1939. return llvm::make_range(ranges_iterator(), ranges_iterator());
  1940. }
  1941. // User-specified absence of range info.
  1942. if (Ranges.size() == 1 && !Ranges.begin()->isValid())
  1943. return llvm::make_range(ranges_iterator(), ranges_iterator());
  1944. return llvm::make_range(Ranges.begin(), Ranges.end());
  1945. }
  1946. PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
  1947. if (ErrorNode) {
  1948. assert(!Location.isValid() &&
  1949. "Either Location or ErrorNode should be specified but not both.");
  1950. return PathDiagnosticLocation::createEndOfPath(ErrorNode, SM);
  1951. }
  1952. assert(Location.isValid());
  1953. return Location;
  1954. }
  1955. //===----------------------------------------------------------------------===//
  1956. // Methods for BugReporter and subclasses.
  1957. //===----------------------------------------------------------------------===//
  1958. const ExplodedGraph &GRBugReporter::getGraph() const { return Eng.getGraph(); }
  1959. ProgramStateManager&
  1960. GRBugReporter::getStateManager() { return Eng.getStateManager(); }
  1961. ProgramStateManager&
  1962. GRBugReporter::getStateManager() const { return Eng.getStateManager(); }
  1963. BugReporter::~BugReporter() {
  1964. FlushReports();
  1965. // Free the bug reports we are tracking.
  1966. for (const auto I : EQClassesVector)
  1967. delete I;
  1968. }
  1969. void BugReporter::FlushReports() {
  1970. if (BugTypes.isEmpty())
  1971. return;
  1972. // We need to flush reports in deterministic order to ensure the order
  1973. // of the reports is consistent between runs.
  1974. for (const auto EQ : EQClassesVector)
  1975. FlushReport(*EQ);
  1976. // BugReporter owns and deletes only BugTypes created implicitly through
  1977. // EmitBasicReport.
  1978. // FIXME: There are leaks from checkers that assume that the BugTypes they
  1979. // create will be destroyed by the BugReporter.
  1980. llvm::DeleteContainerSeconds(StrBugTypes);
  1981. // Remove all references to the BugType objects.
  1982. BugTypes = F.getEmptySet();
  1983. }
  1984. //===----------------------------------------------------------------------===//
  1985. // PathDiagnostics generation.
  1986. //===----------------------------------------------------------------------===//
  1987. namespace {
  1988. /// A wrapper around an ExplodedGraph that contains a single path from the root
  1989. /// to the error node, and a map that maps the nodes in this path to the ones in
  1990. /// the original ExplodedGraph.
  1991. class BugPathInfo {
  1992. public:
  1993. InterExplodedGraphMap MapToOriginNodes;
  1994. std::unique_ptr<ExplodedGraph> BugPath;
  1995. BugReport *Report;
  1996. const ExplodedNode *ErrorNode;
  1997. };
  1998. /// A wrapper around an ExplodedGraph whose leafs are all error nodes. Can
  1999. /// conveniently retrieve bug paths from a single error node to the root.
  2000. class BugPathGetter {
  2001. std::unique_ptr<ExplodedGraph> TrimmedGraph;
  2002. /// Map from the trimmed graph to the original.
  2003. InterExplodedGraphMap InverseMap;
  2004. using PriorityMapTy = llvm::DenseMap<const ExplodedNode *, unsigned>;
  2005. /// Assign each node with its distance from the root.
  2006. PriorityMapTy PriorityMap;
  2007. /// Since the getErrorNode() or BugReport refers to the original ExplodedGraph,
  2008. /// we need to pair it to the error node of the constructed trimmed graph.
  2009. using ReportNewNodePair = std::pair<BugReport *, const ExplodedNode *>;
  2010. SmallVector<ReportNewNodePair, 32> ReportNodes;
  2011. BugPathInfo CurrentBugPath;
  2012. /// A helper class for sorting ExplodedNodes by priority.
  2013. template <bool Descending>
  2014. class PriorityCompare {
  2015. const PriorityMapTy &PriorityMap;
  2016. public:
  2017. PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
  2018. bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
  2019. PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
  2020. PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
  2021. PriorityMapTy::const_iterator E = PriorityMap.end();
  2022. if (LI == E)
  2023. return Descending;
  2024. if (RI == E)
  2025. return !Descending;
  2026. return Descending ? LI->second > RI->second
  2027. : LI->second < RI->second;
  2028. }
  2029. bool operator()(const ReportNewNodePair &LHS,
  2030. const ReportNewNodePair &RHS) const {
  2031. return (*this)(LHS.second, RHS.second);
  2032. }
  2033. };
  2034. public:
  2035. BugPathGetter(const ExplodedGraph *OriginalGraph,
  2036. ArrayRef<BugReport *> &bugReports);
  2037. BugPathInfo *getNextBugPath();
  2038. };
  2039. } // namespace
  2040. BugPathGetter::BugPathGetter(const ExplodedGraph *OriginalGraph,
  2041. ArrayRef<BugReport *> &bugReports) {
  2042. SmallVector<const ExplodedNode *, 32> Nodes;
  2043. for (const auto I : bugReports) {
  2044. assert(I->isValid() &&
  2045. "We only allow BugReporterVisitors and BugReporter itself to "
  2046. "invalidate reports!");
  2047. Nodes.emplace_back(I->getErrorNode());
  2048. }
  2049. // The trimmed graph is created in the body of the constructor to ensure
  2050. // that the DenseMaps have been initialized already.
  2051. InterExplodedGraphMap ForwardMap;
  2052. TrimmedGraph = OriginalGraph->trim(Nodes, &ForwardMap, &InverseMap);
  2053. // Find the (first) error node in the trimmed graph. We just need to consult
  2054. // the node map which maps from nodes in the original graph to nodes
  2055. // in the new graph.
  2056. llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes;
  2057. for (BugReport *Report : bugReports) {
  2058. const ExplodedNode *NewNode = ForwardMap.lookup(Report->getErrorNode());
  2059. assert(NewNode &&
  2060. "Failed to construct a trimmed graph that contains this error "
  2061. "node!");
  2062. ReportNodes.emplace_back(Report, NewNode);
  2063. RemainingNodes.insert(NewNode);
  2064. }
  2065. assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
  2066. // Perform a forward BFS to find all the shortest paths.
  2067. std::queue<const ExplodedNode *> WS;
  2068. assert(TrimmedGraph->num_roots() == 1);
  2069. WS.push(*TrimmedGraph->roots_begin());
  2070. unsigned Priority = 0;
  2071. while (!WS.empty()) {
  2072. const ExplodedNode *Node = WS.front();
  2073. WS.pop();
  2074. PriorityMapTy::iterator PriorityEntry;
  2075. bool IsNew;
  2076. std::tie(PriorityEntry, IsNew) = PriorityMap.insert({Node, Priority});
  2077. ++Priority;
  2078. if (!IsNew) {
  2079. assert(PriorityEntry->second <= Priority);
  2080. continue;
  2081. }
  2082. if (RemainingNodes.erase(Node))
  2083. if (RemainingNodes.empty())
  2084. break;
  2085. for (const ExplodedNode *Succ : Node->succs())
  2086. WS.push(Succ);
  2087. }
  2088. // Sort the error paths from longest to shortest.
  2089. llvm::sort(ReportNodes, PriorityCompare<true>(PriorityMap));
  2090. }
  2091. BugPathInfo *BugPathGetter::getNextBugPath() {
  2092. if (ReportNodes.empty())
  2093. return nullptr;
  2094. const ExplodedNode *OrigN;
  2095. std::tie(CurrentBugPath.Report, OrigN) = ReportNodes.pop_back_val();
  2096. assert(PriorityMap.find(OrigN) != PriorityMap.end() &&
  2097. "error node not accessible from root");
  2098. // Create a new graph with a single path. This is the graph that will be
  2099. // returned to the caller.
  2100. auto GNew = llvm::make_unique<ExplodedGraph>();
  2101. CurrentBugPath.MapToOriginNodes.clear();
  2102. // Now walk from the error node up the BFS path, always taking the
  2103. // predeccessor with the lowest number.
  2104. ExplodedNode *Succ = nullptr;
  2105. while (true) {
  2106. // Create the equivalent node in the new graph with the same state
  2107. // and location.
  2108. ExplodedNode *NewN = GNew->createUncachedNode(
  2109. OrigN->getLocation(), OrigN->getState(), OrigN->isSink());
  2110. // Store the mapping to the original node.
  2111. InterExplodedGraphMap::const_iterator IMitr = InverseMap.find(OrigN);
  2112. assert(IMitr != InverseMap.end() && "No mapping to original node.");
  2113. CurrentBugPath.MapToOriginNodes[NewN] = IMitr->second;
  2114. // Link up the new node with the previous node.
  2115. if (Succ)
  2116. Succ->addPredecessor(NewN, *GNew);
  2117. else
  2118. CurrentBugPath.ErrorNode = NewN;
  2119. Succ = NewN;
  2120. // Are we at the final node?
  2121. if (OrigN->pred_empty()) {
  2122. GNew->addRoot(NewN);
  2123. break;
  2124. }
  2125. // Find the next predeccessor node. We choose the node that is marked
  2126. // with the lowest BFS number.
  2127. OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
  2128. PriorityCompare<false>(PriorityMap));
  2129. }
  2130. CurrentBugPath.BugPath = std::move(GNew);
  2131. return &CurrentBugPath;
  2132. }
  2133. /// CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic
  2134. /// object and collapses PathDiagosticPieces that are expanded by macros.
  2135. static void CompactMacroExpandedPieces(PathPieces &path,
  2136. const SourceManager& SM) {
  2137. using MacroStackTy = std::vector<
  2138. std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>;
  2139. using PiecesTy = std::vector<PathDiagnosticPieceRef>;
  2140. MacroStackTy MacroStack;
  2141. PiecesTy Pieces;
  2142. for (PathPieces::const_iterator I = path.begin(), E = path.end();
  2143. I != E; ++I) {
  2144. const auto &piece = *I;
  2145. // Recursively compact calls.
  2146. if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) {
  2147. CompactMacroExpandedPieces(call->path, SM);
  2148. }
  2149. // Get the location of the PathDiagnosticPiece.
  2150. const FullSourceLoc Loc = piece->getLocation().asLocation();
  2151. // Determine the instantiation location, which is the location we group
  2152. // related PathDiagnosticPieces.
  2153. SourceLocation InstantiationLoc = Loc.isMacroID() ?
  2154. SM.getExpansionLoc(Loc) :
  2155. SourceLocation();
  2156. if (Loc.isFileID()) {
  2157. MacroStack.clear();
  2158. Pieces.push_back(piece);
  2159. continue;
  2160. }
  2161. assert(Loc.isMacroID());
  2162. // Is the PathDiagnosticPiece within the same macro group?
  2163. if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
  2164. MacroStack.back().first->subPieces.push_back(piece);
  2165. continue;
  2166. }
  2167. // We aren't in the same group. Are we descending into a new macro
  2168. // or are part of an old one?
  2169. std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup;
  2170. SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
  2171. SM.getExpansionLoc(Loc) :
  2172. SourceLocation();
  2173. // Walk the entire macro stack.
  2174. while (!MacroStack.empty()) {
  2175. if (InstantiationLoc == MacroStack.back().second) {
  2176. MacroGroup = MacroStack.back().first;
  2177. break;
  2178. }
  2179. if (ParentInstantiationLoc == MacroStack.back().second) {
  2180. MacroGroup = MacroStack.back().first;
  2181. break;
  2182. }
  2183. MacroStack.pop_back();
  2184. }
  2185. if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
  2186. // Create a new macro group and add it to the stack.
  2187. auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>(
  2188. PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
  2189. if (MacroGroup)
  2190. MacroGroup->subPieces.push_back(NewGroup);
  2191. else {
  2192. assert(InstantiationLoc.isFileID());
  2193. Pieces.push_back(NewGroup);
  2194. }
  2195. MacroGroup = NewGroup;
  2196. MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
  2197. }
  2198. // Finally, add the PathDiagnosticPiece to the group.
  2199. MacroGroup->subPieces.push_back(piece);
  2200. }
  2201. // Now take the pieces and construct a new PathDiagnostic.
  2202. path.clear();
  2203. path.insert(path.end(), Pieces.begin(), Pieces.end());
  2204. }
  2205. /// Generate notes from all visitors.
  2206. /// Notes associated with {@code ErrorNode} are generated using
  2207. /// {@code getEndPath}, and the rest are generated with {@code VisitNode}.
  2208. static std::unique_ptr<VisitorsDiagnosticsTy>
  2209. generateVisitorsDiagnostics(BugReport *R, const ExplodedNode *ErrorNode,
  2210. BugReporterContext &BRC) {
  2211. std::unique_ptr<VisitorsDiagnosticsTy> Notes =
  2212. llvm::make_unique<VisitorsDiagnosticsTy>();
  2213. BugReport::VisitorList visitors;
  2214. // Run visitors on all nodes starting from the node *before* the last one.
  2215. // The last node is reserved for notes generated with {@code getEndPath}.
  2216. const ExplodedNode *NextNode = ErrorNode->getFirstPred();
  2217. while (NextNode) {
  2218. // At each iteration, move all visitors from report to visitor list. This is
  2219. // important, because the Profile() functions of the visitors make sure that
  2220. // a visitor isn't added multiple times for the same node, but it's fine
  2221. // to add the a visitor with Profile() for different nodes (e.g. tracking
  2222. // a region at different points of the symbolic execution).
  2223. for (std::unique_ptr<BugReporterVisitor> &Visitor : R->visitors())
  2224. visitors.push_back(std::move(Visitor));
  2225. R->clearVisitors();
  2226. const ExplodedNode *Pred = NextNode->getFirstPred();
  2227. if (!Pred) {
  2228. PathDiagnosticPieceRef LastPiece;
  2229. for (auto &V : visitors) {
  2230. V->finalizeVisitor(BRC, ErrorNode, *R);
  2231. if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) {
  2232. assert(!LastPiece &&
  2233. "There can only be one final piece in a diagnostic.");
  2234. assert(Piece->getKind() == PathDiagnosticPiece::Kind::Event &&
  2235. "The final piece must contain a message!");
  2236. LastPiece = std::move(Piece);
  2237. (*Notes)[ErrorNode].push_back(LastPiece);
  2238. }
  2239. }
  2240. break;
  2241. }
  2242. for (auto &V : visitors) {
  2243. auto P = V->VisitNode(NextNode, BRC, *R);
  2244. if (P)
  2245. (*Notes)[NextNode].push_back(std::move(P));
  2246. }
  2247. if (!R->isValid())
  2248. break;
  2249. NextNode = Pred;
  2250. }
  2251. return Notes;
  2252. }
  2253. Optional<PathDiagnosticBuilder>
  2254. PathDiagnosticBuilder::findValidReport(ArrayRef<BugReport *> &bugReports,
  2255. GRBugReporter &Reporter) {
  2256. BugPathGetter BugGraph(&Reporter.getGraph(), bugReports);
  2257. while (BugPathInfo *BugPath = BugGraph.getNextBugPath()) {
  2258. // Find the BugReport with the original location.
  2259. BugReport *R = BugPath->Report;
  2260. assert(R && "No original report found for sliced graph.");
  2261. assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
  2262. const ExplodedNode *ErrorNode = BugPath->ErrorNode;
  2263. // Register refutation visitors first, if they mark the bug invalid no
  2264. // further analysis is required
  2265. R->addVisitor(llvm::make_unique<LikelyFalsePositiveSuppressionBRVisitor>());
  2266. // Register additional node visitors.
  2267. R->addVisitor(llvm::make_unique<NilReceiverBRVisitor>());
  2268. R->addVisitor(llvm::make_unique<ConditionBRVisitor>());
  2269. R->addVisitor(llvm::make_unique<TagVisitor>());
  2270. BugReporterContext BRC(Reporter, BugPath->MapToOriginNodes);
  2271. // Run all visitors on a given graph, once.
  2272. std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes =
  2273. generateVisitorsDiagnostics(R, ErrorNode, BRC);
  2274. if (R->isValid()) {
  2275. if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) {
  2276. // If crosscheck is enabled, remove all visitors, add the refutation
  2277. // visitor and check again
  2278. R->clearVisitors();
  2279. R->addVisitor(llvm::make_unique<FalsePositiveRefutationBRVisitor>());
  2280. // We don't overrite the notes inserted by other visitors because the
  2281. // refutation manager does not add any new note to the path
  2282. generateVisitorsDiagnostics(R, BugPath->ErrorNode, BRC);
  2283. }
  2284. // Check if the bug is still valid
  2285. if (R->isValid())
  2286. return PathDiagnosticBuilder(
  2287. std::move(BRC), std::move(BugPath->BugPath), BugPath->Report,
  2288. BugPath->ErrorNode, std::move(visitorNotes));
  2289. }
  2290. }
  2291. return {};
  2292. }
  2293. std::unique_ptr<DiagnosticForConsumerMapTy>
  2294. GRBugReporter::generatePathDiagnostics(
  2295. ArrayRef<PathDiagnosticConsumer *> consumers,
  2296. ArrayRef<BugReport *> &bugReports) {
  2297. assert(!bugReports.empty());
  2298. auto Out = llvm::make_unique<DiagnosticForConsumerMapTy>();
  2299. Optional<PathDiagnosticBuilder> PDB =
  2300. PathDiagnosticBuilder::findValidReport(bugReports, *this);
  2301. if (PDB)
  2302. for (PathDiagnosticConsumer *PC : consumers)
  2303. (*Out)[PC] = PDB->generate(PC);
  2304. return Out;
  2305. }
  2306. void BugReporter::Register(const BugType *BT) {
  2307. BugTypes = F.add(BugTypes, BT);
  2308. }
  2309. void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
  2310. if (const ExplodedNode *E = R->getErrorNode()) {
  2311. // An error node must either be a sink or have a tag, otherwise
  2312. // it could get reclaimed before the path diagnostic is created.
  2313. assert((E->isSink() || E->getLocation().getTag()) &&
  2314. "Error node must either be a sink or have a tag");
  2315. const AnalysisDeclContext *DeclCtx =
  2316. E->getLocationContext()->getAnalysisDeclContext();
  2317. // The source of autosynthesized body can be handcrafted AST or a model
  2318. // file. The locations from handcrafted ASTs have no valid source locations
  2319. // and have to be discarded. Locations from model files should be preserved
  2320. // for processing and reporting.
  2321. if (DeclCtx->isBodyAutosynthesized() &&
  2322. !DeclCtx->isBodyAutosynthesizedFromModelFile())
  2323. return;
  2324. }
  2325. bool ValidSourceLoc = R->getLocation(getSourceManager()).isValid();
  2326. assert(ValidSourceLoc);
  2327. // If we mess up in a release build, we'd still prefer to just drop the bug
  2328. // instead of trying to go on.
  2329. if (!ValidSourceLoc)
  2330. return;
  2331. // Compute the bug report's hash to determine its equivalence class.
  2332. llvm::FoldingSetNodeID ID;
  2333. R->Profile(ID);
  2334. // Lookup the equivance class. If there isn't one, create it.
  2335. const BugType& BT = R->getBugType();
  2336. Register(&BT);
  2337. void *InsertPos;
  2338. BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
  2339. if (!EQ) {
  2340. EQ = new BugReportEquivClass(std::move(R));
  2341. EQClasses.InsertNode(EQ, InsertPos);
  2342. EQClassesVector.push_back(EQ);
  2343. } else
  2344. EQ->AddReport(std::move(R));
  2345. }
  2346. //===----------------------------------------------------------------------===//
  2347. // Emitting reports in equivalence classes.
  2348. //===----------------------------------------------------------------------===//
  2349. namespace {
  2350. struct FRIEC_WLItem {
  2351. const ExplodedNode *N;
  2352. ExplodedNode::const_succ_iterator I, E;
  2353. FRIEC_WLItem(const ExplodedNode *n)
  2354. : N(n), I(N->succ_begin()), E(N->succ_end()) {}
  2355. };
  2356. } // namespace
  2357. static const CFGBlock *findBlockForNode(const ExplodedNode *N) {
  2358. ProgramPoint P = N->getLocation();
  2359. if (auto BEP = P.getAs<BlockEntrance>())
  2360. return BEP->getBlock();
  2361. // Find the node's current statement in the CFG.
  2362. if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
  2363. return N->getLocationContext()->getAnalysisDeclContext()
  2364. ->getCFGStmtMap()->getBlock(S);
  2365. return nullptr;
  2366. }
  2367. // Returns true if by simply looking at the block, we can be sure that it
  2368. // results in a sink during analysis. This is useful to know when the analysis
  2369. // was interrupted, and we try to figure out if it would sink eventually.
  2370. // There may be many more reasons why a sink would appear during analysis
  2371. // (eg. checkers may generate sinks arbitrarily), but here we only consider
  2372. // sinks that would be obvious by looking at the CFG.
  2373. static bool isImmediateSinkBlock(const CFGBlock *Blk) {
  2374. if (Blk->hasNoReturnElement())
  2375. return true;
  2376. // FIXME: Throw-expressions are currently generating sinks during analysis:
  2377. // they're not supported yet, and also often used for actually terminating
  2378. // the program. So we should treat them as sinks in this analysis as well,
  2379. // at least for now, but once we have better support for exceptions,
  2380. // we'd need to carefully handle the case when the throw is being
  2381. // immediately caught.
  2382. if (std::any_of(Blk->begin(), Blk->end(), [](const CFGElement &Elm) {
  2383. if (Optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>())
  2384. if (isa<CXXThrowExpr>(StmtElm->getStmt()))
  2385. return true;
  2386. return false;
  2387. }))
  2388. return true;
  2389. return false;
  2390. }
  2391. // Returns true if by looking at the CFG surrounding the node's program
  2392. // point, we can be sure that any analysis starting from this point would
  2393. // eventually end with a sink. We scan the child CFG blocks in a depth-first
  2394. // manner and see if all paths eventually end up in an immediate sink block.
  2395. static bool isInevitablySinking(const ExplodedNode *N) {
  2396. const CFG &Cfg = N->getCFG();
  2397. const CFGBlock *StartBlk = findBlockForNode(N);
  2398. if (!StartBlk)
  2399. return false;
  2400. if (isImmediateSinkBlock(StartBlk))
  2401. return true;
  2402. llvm::SmallVector<const CFGBlock *, 32> DFSWorkList;
  2403. llvm::SmallPtrSet<const CFGBlock *, 32> Visited;
  2404. DFSWorkList.push_back(StartBlk);
  2405. while (!DFSWorkList.empty()) {
  2406. const CFGBlock *Blk = DFSWorkList.back();
  2407. DFSWorkList.pop_back();
  2408. Visited.insert(Blk);
  2409. // If at least one path reaches the CFG exit, it means that control is
  2410. // returned to the caller. For now, say that we are not sure what
  2411. // happens next. If necessary, this can be improved to analyze
  2412. // the parent StackFrameContext's call site in a similar manner.
  2413. if (Blk == &Cfg.getExit())
  2414. return false;
  2415. for (const auto &Succ : Blk->succs()) {
  2416. if (const CFGBlock *SuccBlk = Succ.getReachableBlock()) {
  2417. if (!isImmediateSinkBlock(SuccBlk) && !Visited.count(SuccBlk)) {
  2418. // If the block has reachable child blocks that aren't no-return,
  2419. // add them to the worklist.
  2420. DFSWorkList.push_back(SuccBlk);
  2421. }
  2422. }
  2423. }
  2424. }
  2425. // Nothing reached the exit. It can only mean one thing: there's no return.
  2426. return true;
  2427. }
  2428. static BugReport *
  2429. FindReportInEquivalenceClass(BugReportEquivClass& EQ,
  2430. SmallVectorImpl<BugReport*> &bugReports) {
  2431. BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
  2432. assert(I != E);
  2433. const BugType& BT = I->getBugType();
  2434. // If we don't need to suppress any of the nodes because they are
  2435. // post-dominated by a sink, simply add all the nodes in the equivalence class
  2436. // to 'Nodes'. Any of the reports will serve as a "representative" report.
  2437. if (!BT.isSuppressOnSink()) {
  2438. BugReport *R = &*I;
  2439. for (auto &I : EQ) {
  2440. const ExplodedNode *N = I.getErrorNode();
  2441. if (N) {
  2442. R = &I;
  2443. bugReports.push_back(R);
  2444. }
  2445. }
  2446. return R;
  2447. }
  2448. // For bug reports that should be suppressed when all paths are post-dominated
  2449. // by a sink node, iterate through the reports in the equivalence class
  2450. // until we find one that isn't post-dominated (if one exists). We use a
  2451. // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
  2452. // this as a recursive function, but we don't want to risk blowing out the
  2453. // stack for very long paths.
  2454. BugReport *exampleReport = nullptr;
  2455. for (; I != E; ++I) {
  2456. const ExplodedNode *errorNode = I->getErrorNode();
  2457. if (!errorNode)
  2458. continue;
  2459. if (errorNode->isSink()) {
  2460. llvm_unreachable(
  2461. "BugType::isSuppressSink() should not be 'true' for sink end nodes");
  2462. }
  2463. // No successors? By definition this nodes isn't post-dominated by a sink.
  2464. if (errorNode->succ_empty()) {
  2465. bugReports.push_back(&*I);
  2466. if (!exampleReport)
  2467. exampleReport = &*I;
  2468. continue;
  2469. }
  2470. // See if we are in a no-return CFG block. If so, treat this similarly
  2471. // to being post-dominated by a sink. This works better when the analysis
  2472. // is incomplete and we have never reached the no-return function call(s)
  2473. // that we'd inevitably bump into on this path.
  2474. if (isInevitablySinking(errorNode))
  2475. continue;
  2476. // At this point we know that 'N' is not a sink and it has at least one
  2477. // successor. Use a DFS worklist to find a non-sink end-of-path node.
  2478. using WLItem = FRIEC_WLItem;
  2479. using DFSWorkList = SmallVector<WLItem, 10>;
  2480. llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
  2481. DFSWorkList WL;
  2482. WL.push_back(errorNode);
  2483. Visited[errorNode] = 1;
  2484. while (!WL.empty()) {
  2485. WLItem &WI = WL.back();
  2486. assert(!WI.N->succ_empty());
  2487. for (; WI.I != WI.E; ++WI.I) {
  2488. const ExplodedNode *Succ = *WI.I;
  2489. // End-of-path node?
  2490. if (Succ->succ_empty()) {
  2491. // If we found an end-of-path node that is not a sink.
  2492. if (!Succ->isSink()) {
  2493. bugReports.push_back(&*I);
  2494. if (!exampleReport)
  2495. exampleReport = &*I;
  2496. WL.clear();
  2497. break;
  2498. }
  2499. // Found a sink? Continue on to the next successor.
  2500. continue;
  2501. }
  2502. // Mark the successor as visited. If it hasn't been explored,
  2503. // enqueue it to the DFS worklist.
  2504. unsigned &mark = Visited[Succ];
  2505. if (!mark) {
  2506. mark = 1;
  2507. WL.push_back(Succ);
  2508. break;
  2509. }
  2510. }
  2511. // The worklist may have been cleared at this point. First
  2512. // check if it is empty before checking the last item.
  2513. if (!WL.empty() && &WL.back() == &WI)
  2514. WL.pop_back();
  2515. }
  2516. }
  2517. // ExampleReport will be NULL if all the nodes in the equivalence class
  2518. // were post-dominated by sinks.
  2519. return exampleReport;
  2520. }
  2521. void BugReporter::FlushReport(BugReportEquivClass& EQ) {
  2522. SmallVector<BugReport*, 10> bugReports;
  2523. BugReport *report = FindReportInEquivalenceClass(EQ, bugReports);
  2524. if (!report)
  2525. return;
  2526. ArrayRef<PathDiagnosticConsumer*> Consumers = getPathDiagnosticConsumers();
  2527. std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
  2528. generateDiagnosticForConsumerMap(report, Consumers, bugReports);
  2529. for (auto &P : *Diagnostics) {
  2530. PathDiagnosticConsumer *Consumer = P.first;
  2531. std::unique_ptr<PathDiagnostic> &PD = P.second;
  2532. // If the path is empty, generate a single step path with the location
  2533. // of the issue.
  2534. if (PD->path.empty()) {
  2535. PathDiagnosticLocation L = report->getLocation(getSourceManager());
  2536. auto piece = llvm::make_unique<PathDiagnosticEventPiece>(
  2537. L, report->getDescription());
  2538. for (SourceRange Range : report->getRanges())
  2539. piece->addRange(Range);
  2540. PD->setEndOfPath(std::move(piece));
  2541. }
  2542. PathPieces &Pieces = PD->getMutablePieces();
  2543. if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) {
  2544. // For path diagnostic consumers that don't support extra notes,
  2545. // we may optionally convert those to path notes.
  2546. for (auto I = report->getNotes().rbegin(),
  2547. E = report->getNotes().rend(); I != E; ++I) {
  2548. PathDiagnosticNotePiece *Piece = I->get();
  2549. auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>(
  2550. Piece->getLocation(), Piece->getString());
  2551. for (const auto &R: Piece->getRanges())
  2552. ConvertedPiece->addRange(R);
  2553. Pieces.push_front(std::move(ConvertedPiece));
  2554. }
  2555. } else {
  2556. for (auto I = report->getNotes().rbegin(),
  2557. E = report->getNotes().rend(); I != E; ++I)
  2558. Pieces.push_front(*I);
  2559. }
  2560. // Get the meta data.
  2561. const BugReport::ExtraTextList &Meta = report->getExtraText();
  2562. for (const auto &i : Meta)
  2563. PD->addMeta(i);
  2564. updateExecutedLinesWithDiagnosticPieces(*PD);
  2565. Consumer->HandlePathDiagnostic(std::move(PD));
  2566. }
  2567. }
  2568. /// Insert all lines participating in the function signature \p Signature
  2569. /// into \p ExecutedLines.
  2570. static void populateExecutedLinesWithFunctionSignature(
  2571. const Decl *Signature, const SourceManager &SM,
  2572. FilesToLineNumsMap &ExecutedLines) {
  2573. SourceRange SignatureSourceRange;
  2574. const Stmt* Body = Signature->getBody();
  2575. if (const auto FD = dyn_cast<FunctionDecl>(Signature)) {
  2576. SignatureSourceRange = FD->getSourceRange();
  2577. } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) {
  2578. SignatureSourceRange = OD->getSourceRange();
  2579. } else {
  2580. return;
  2581. }
  2582. SourceLocation Start = SignatureSourceRange.getBegin();
  2583. SourceLocation End = Body ? Body->getSourceRange().getBegin()
  2584. : SignatureSourceRange.getEnd();
  2585. if (!Start.isValid() || !End.isValid())
  2586. return;
  2587. unsigned StartLine = SM.getExpansionLineNumber(Start);
  2588. unsigned EndLine = SM.getExpansionLineNumber(End);
  2589. FileID FID = SM.getFileID(SM.getExpansionLoc(Start));
  2590. for (unsigned Line = StartLine; Line <= EndLine; Line++)
  2591. ExecutedLines[FID].insert(Line);
  2592. }
  2593. static void populateExecutedLinesWithStmt(
  2594. const Stmt *S, const SourceManager &SM,
  2595. FilesToLineNumsMap &ExecutedLines) {
  2596. SourceLocation Loc = S->getSourceRange().getBegin();
  2597. if (!Loc.isValid())
  2598. return;
  2599. SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
  2600. FileID FID = SM.getFileID(ExpansionLoc);
  2601. unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
  2602. ExecutedLines[FID].insert(LineNo);
  2603. }
  2604. /// \return all executed lines including function signatures on the path
  2605. /// starting from \p N.
  2606. static std::unique_ptr<FilesToLineNumsMap>
  2607. findExecutedLines(const SourceManager &SM, const ExplodedNode *N) {
  2608. auto ExecutedLines = llvm::make_unique<FilesToLineNumsMap>();
  2609. while (N) {
  2610. if (N->getFirstPred() == nullptr) {
  2611. // First node: show signature of the entrance point.
  2612. const Decl *D = N->getLocationContext()->getDecl();
  2613. populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
  2614. } else if (auto CE = N->getLocationAs<CallEnter>()) {
  2615. // Inlined function: show signature.
  2616. const Decl* D = CE->getCalleeContext()->getDecl();
  2617. populateExecutedLinesWithFunctionSignature(D, SM, *ExecutedLines);
  2618. } else if (const Stmt *S = PathDiagnosticLocation::getStmt(N)) {
  2619. populateExecutedLinesWithStmt(S, SM, *ExecutedLines);
  2620. // Show extra context for some parent kinds.
  2621. const Stmt *P = N->getParentMap().getParent(S);
  2622. // The path exploration can die before the node with the associated
  2623. // return statement is generated, but we do want to show the whole
  2624. // return.
  2625. if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) {
  2626. populateExecutedLinesWithStmt(RS, SM, *ExecutedLines);
  2627. P = N->getParentMap().getParent(RS);
  2628. }
  2629. if (P && (isa<SwitchCase>(P) || isa<LabelStmt>(P)))
  2630. populateExecutedLinesWithStmt(P, SM, *ExecutedLines);
  2631. }
  2632. N = N->getFirstPred();
  2633. }
  2634. return ExecutedLines;
  2635. }
  2636. std::unique_ptr<DiagnosticForConsumerMapTy>
  2637. BugReporter::generateDiagnosticForConsumerMap(
  2638. BugReport *report, ArrayRef<PathDiagnosticConsumer *> consumers,
  2639. ArrayRef<BugReport *> bugReports) {
  2640. if (!report->isPathSensitive()) {
  2641. auto Out = llvm::make_unique<DiagnosticForConsumerMapTy>();
  2642. for (auto *Consumer : consumers)
  2643. (*Out)[Consumer] = generateEmptyDiagnosticForReport(report,
  2644. getSourceManager());
  2645. return Out;
  2646. }
  2647. // Generate the full path sensitive diagnostic, using the generation scheme
  2648. // specified by the PathDiagnosticConsumer. Note that we have to generate
  2649. // path diagnostics even for consumers which do not support paths, because
  2650. // the BugReporterVisitors may mark this bug as a false positive.
  2651. assert(!bugReports.empty());
  2652. MaxBugClassSize.updateMax(bugReports.size());
  2653. std::unique_ptr<DiagnosticForConsumerMapTy> Out =
  2654. generatePathDiagnostics(consumers, bugReports);
  2655. if (Out->empty())
  2656. return Out;
  2657. MaxValidBugClassSize.updateMax(bugReports.size());
  2658. // Examine the report and see if the last piece is in a header. Reset the
  2659. // report location to the last piece in the main source file.
  2660. const AnalyzerOptions &Opts = getAnalyzerOptions();
  2661. for (auto const &P : *Out)
  2662. if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
  2663. P.second->resetDiagnosticLocationToMainFile();
  2664. return Out;
  2665. }
  2666. void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
  2667. const CheckerBase *Checker,
  2668. StringRef Name, StringRef Category,
  2669. StringRef Str, PathDiagnosticLocation Loc,
  2670. ArrayRef<SourceRange> Ranges) {
  2671. EmitBasicReport(DeclWithIssue, Checker->getCheckName(), Name, Category, Str,
  2672. Loc, Ranges);
  2673. }
  2674. void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
  2675. CheckName CheckName,
  2676. StringRef name, StringRef category,
  2677. StringRef str, PathDiagnosticLocation Loc,
  2678. ArrayRef<SourceRange> Ranges) {
  2679. // 'BT' is owned by BugReporter.
  2680. BugType *BT = getBugTypeForName(CheckName, name, category);
  2681. auto R = llvm::make_unique<BugReport>(*BT, str, Loc);
  2682. R->setDeclWithIssue(DeclWithIssue);
  2683. for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
  2684. I != E; ++I)
  2685. R->addRange(*I);
  2686. emitReport(std::move(R));
  2687. }
  2688. BugType *BugReporter::getBugTypeForName(CheckName CheckName, StringRef name,
  2689. StringRef category) {
  2690. SmallString<136> fullDesc;
  2691. llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
  2692. << ":" << category;
  2693. BugType *&BT = StrBugTypes[fullDesc];
  2694. if (!BT)
  2695. BT = new BugType(CheckName, name, category);
  2696. return BT;
  2697. }