BugReporter.cpp 126 KB

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