optdfa.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. {
  2. DFA
  3. Copyright (c) 2007 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { $define DEBUG_DFA}
  18. { $define EXTDEBUG_DFA}
  19. { this unit implements routines to perform dfa }
  20. unit optdfa;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. node,optutils;
  25. type
  26. TDFABuilder = class
  27. protected
  28. procedure CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  29. public
  30. resultnode : tnode;
  31. nodemap : TIndexedNodeSet;
  32. { reset all dfa info, this is required before creating dfa info
  33. if the tree has been changed without updating dfa }
  34. procedure resetdfainfo(node : tnode);
  35. procedure createdfainfo(node : tnode);
  36. destructor destroy;override;
  37. end;
  38. procedure CheckAndWarn(code : tnode;nodetosearch : tnode);
  39. implementation
  40. uses
  41. globtype,
  42. verbose,
  43. symconst,symdef,symsym,
  44. defutil,
  45. procinfo,
  46. nutils,htypechk,
  47. nbas,nflw,ncal,nset,nld,nadd,
  48. optbase;
  49. (*
  50. function initnodes(var n:tnode; arg: pointer) : foreachnoderesult;
  51. begin
  52. { node worth to add? }
  53. if (node_complexity(n)>1) and (tstoreddef(n.resultdef).is_intregable or tstoreddef(n.resultdef).is_fpuregable) then
  54. begin
  55. plists(arg)^.nodelist.Add(n);
  56. plists(arg)^.locationlist.Add(@n);
  57. result:=fen_false;
  58. end
  59. else
  60. result:=fen_norecurse_false;
  61. end;
  62. *)
  63. {
  64. x:=f; read: [f]
  65. while x do read: []
  66. a:=b; read: [a,b,d] def: [a] life: read*def=[a]
  67. c:=d; read: [a,d] def: [a,c] life: read*def=[a]
  68. e:=a; read: [a] def: [a,c,e] life: read*def=[a]
  69. function f(b,d,x : type) : type;
  70. begin
  71. while x do alive: b,d,x
  72. begin
  73. a:=b; alive: b,d,x
  74. c:=d; alive: a,d,x
  75. e:=a+c; alive: a,c,x
  76. dec(x); alive: c,e,x
  77. end;
  78. result:=c+e; alive: c,e
  79. end; alive: result
  80. }
  81. type
  82. tdfainfo = record
  83. use : PDFASet;
  84. def : PDFASet;
  85. map : TIndexedNodeSet
  86. end;
  87. pdfainfo = ^tdfainfo;
  88. function AddDefUse(var n: tnode; arg: pointer): foreachnoderesult;
  89. begin
  90. case n.nodetype of
  91. tempcreaten:
  92. begin
  93. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  94. begin
  95. pdfainfo(arg)^.map.Add(n);
  96. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index);
  97. end;
  98. end;
  99. temprefn,
  100. loadn:
  101. begin
  102. pdfainfo(arg)^.map.Add(n);
  103. if nf_modify in n.flags then
  104. begin
  105. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  106. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  107. end
  108. else if nf_write in n.flags then
  109. DFASetInclude(pdfainfo(arg)^.def^,n.optinfo^.index)
  110. else
  111. DFASetInclude(pdfainfo(arg)^.use^,n.optinfo^.index);
  112. end;
  113. end;
  114. result:=fen_false;
  115. end;
  116. function ResetProcessing(var n: tnode; arg: pointer): foreachnoderesult;
  117. begin
  118. exclude(n.flags,nf_processing);
  119. { dfa works only on normalized trees, so do not recurse into expressions, because
  120. ResetProcessing eats a signififcant amount of time of CheckAndWarn
  121. the following set contains (hopefully) most of the expression nodes }
  122. if n.nodetype in [calln,inlinen,assignn,callparan,andn,addn,orn,subn,muln,divn,slashn,notn,equaln,unequaln,gtn,ltn,lten,gten,loadn,
  123. typeconvn,vecn,subscriptn,addrn,derefn] then
  124. result:=fen_norecurse_false
  125. else
  126. result:=fen_false;
  127. end;
  128. function ResetDFA(var n: tnode; arg: pointer): foreachnoderesult;
  129. begin
  130. if assigned(n.optinfo) then
  131. begin
  132. with n.optinfo^ do
  133. begin
  134. life:=nil;
  135. def:=nil;
  136. use:=nil;
  137. defsum:=nil;
  138. end;
  139. end;
  140. result:=fen_false;
  141. end;
  142. procedure TDFABuilder.CreateLifeInfo(node : tnode;map : TIndexedNodeSet);
  143. var
  144. changed : boolean;
  145. procedure CreateInfo(node : tnode);
  146. { update life entry of a node with l, set changed if this changes
  147. life info for the node
  148. }
  149. procedure updatelifeinfo(n : tnode;l : TDFASet);
  150. var
  151. b : boolean;
  152. begin
  153. b:=DFASetNotEqual(l,n.optinfo^.life);
  154. {$ifdef DEBUG_DFA}
  155. if not(changed) and b then
  156. begin
  157. writeln('Another DFA pass caused by: ',nodetype2str[n.nodetype],'(',n.fileinfo.line,',',n.fileinfo.column,')');
  158. write(' Life info set was: ');PrintDFASet(Output,n.optinfo^.life);writeln;
  159. write(' Life info set will be: ');PrintDFASet(Output,l);writeln;
  160. end;
  161. {$endif DEBUG_DFA}
  162. changed:=changed or b;
  163. n.optinfo^.life:=l;
  164. end;
  165. procedure calclife(n : tnode);
  166. var
  167. l : TDFASet;
  168. begin
  169. if assigned(n.successor) then
  170. begin
  171. { ensure we can access optinfo }
  172. DFASetDiff(l,n.successor.optinfo^.life,n.optinfo^.def);
  173. DFASetIncludeSet(l,n.optinfo^.use);
  174. DFASetIncludeSet(l,n.optinfo^.life);
  175. end
  176. else
  177. begin
  178. l:=n.optinfo^.use;
  179. DFASetIncludeSet(l,n.optinfo^.life);
  180. end;
  181. updatelifeinfo(n,l);
  182. end;
  183. var
  184. dfainfo : tdfainfo;
  185. l : TDFASet;
  186. save: TDFASet;
  187. i : longint;
  188. counteruse_after_loop : boolean;
  189. begin
  190. if node=nil then
  191. exit;
  192. { ensure we've already optinfo set }
  193. node.allocoptinfo;
  194. if nf_processing in node.flags then
  195. exit;
  196. include(node.flags,nf_processing);
  197. if assigned(node.successor) then
  198. CreateInfo(node.successor);
  199. {$ifdef EXTDEBUG_DFA}
  200. writeln('Handling: ',nodetype2str[node.nodetype],'(',node.fileinfo.line,',',node.fileinfo.column,')');
  201. {$endif EXTDEBUG_DFA}
  202. { life:=succesorlive-definition+use }
  203. case node.nodetype of
  204. whilerepeatn:
  205. begin
  206. { analyze the loop condition }
  207. if not(assigned(node.optinfo^.def)) and
  208. not(assigned(node.optinfo^.use)) then
  209. begin
  210. dfainfo.use:[email protected]^.use;
  211. dfainfo.def:[email protected]^.def;
  212. dfainfo.map:=map;
  213. foreachnodestatic(pm_postprocess,twhilerepeatnode(node).left,@AddDefUse,@dfainfo);
  214. end;
  215. { NB: this node should typically have empty def set }
  216. if assigned(node.successor) then
  217. DFASetDiff(l,node.successor.optinfo^.life,node.optinfo^.def)
  218. else if assigned(resultnode) then
  219. DFASetDiff(l,resultnode.optinfo^.life,node.optinfo^.def)
  220. else
  221. l:=nil;
  222. { for repeat..until, node use set in included at the end of loop }
  223. if not (lnf_testatbegin in twhilerepeatnode(node).loopflags) then
  224. DFASetIncludeSet(l,node.optinfo^.use);
  225. DFASetIncludeSet(l,node.optinfo^.life);
  226. save:=node.optinfo^.life;
  227. { to process body correctly, we need life info in place (because
  228. whilerepeatnode is successor of its body). }
  229. node.optinfo^.life:=l;
  230. { now process the body }
  231. CreateInfo(twhilerepeatnode(node).right);
  232. { restore, to prevent infinite recursion via changed flag }
  233. node.optinfo^.life:=save;
  234. { for while loops, node use set is included at the beginning of loop }
  235. l:=twhilerepeatnode(node).right.optinfo^.life;
  236. if lnf_testatbegin in twhilerepeatnode(node).loopflags then
  237. DFASetIncludeSet(l,node.optinfo^.use);
  238. UpdateLifeInfo(node,l);
  239. { ... and a second iteration for fast convergence }
  240. CreateInfo(twhilerepeatnode(node).right);
  241. end;
  242. forn:
  243. begin
  244. {
  245. left: loopvar
  246. right: from
  247. t1: to
  248. t2: body
  249. }
  250. node.allocoptinfo;
  251. tfornode(node).loopiteration.allocoptinfo;
  252. if not(assigned(node.optinfo^.def)) and
  253. not(assigned(node.optinfo^.use)) then
  254. begin
  255. dfainfo.use:[email protected]^.use;
  256. dfainfo.def:[email protected]^.def;
  257. dfainfo.map:=map;
  258. foreachnodestatic(pm_postprocess,tfornode(node).left,@AddDefUse,@dfainfo);
  259. foreachnodestatic(pm_postprocess,tfornode(node).right,@AddDefUse,@dfainfo);
  260. foreachnodestatic(pm_postprocess,tfornode(node).t1,@AddDefUse,@dfainfo);
  261. end;
  262. { create life for the body }
  263. CreateInfo(tfornode(node).t2);
  264. { is the counter living after the loop?
  265. if left is a record element, it might not be tracked by dfa, so
  266. optinfo might not be assigned
  267. }
  268. counteruse_after_loop:=assigned(tfornode(node).left.optinfo) and assigned(node.successor) and
  269. DFASetIn(node.successor.optinfo^.life,tfornode(node).left.optinfo^.index);
  270. if counteruse_after_loop then
  271. begin
  272. { if yes, then we should warn }
  273. { !!!!!! }
  274. end;
  275. { first update the dummy node }
  276. { get the life of the loop block }
  277. l:=copy(tfornode(node).t2.optinfo^.life);
  278. { take care of the sucessor }
  279. if assigned(node.successor) then
  280. DFASetIncludeSet(l,node.successor.optinfo^.life);
  281. { the counter variable is living as well inside the for loop
  282. if left is a record element, it might not be tracked by dfa, so
  283. optinfo might not be assigned
  284. }
  285. if assigned(tfornode(node).left.optinfo) then
  286. DFASetInclude(l,tfornode(node).left.optinfo^.index);
  287. { force block node life info }
  288. UpdateLifeInfo(tfornode(node).loopiteration,l);
  289. { now update the for node itself }
  290. { get the life of the loop block }
  291. l:=copy(tfornode(node).t2.optinfo^.life);
  292. { take care of the sucessor as it's possible that we don't have one execution of the body }
  293. if (not(tfornode(node).right.nodetype=ordconstn) or not(tfornode(node).t1.nodetype=ordconstn)) and
  294. assigned(node.successor) then
  295. DFASetIncludeSet(l,node.successor.optinfo^.life);
  296. {
  297. the counter variable is not living at the entry of the for node
  298. if left is a record element, it might not be tracked by dfa, so
  299. optinfo might not be assigned
  300. }
  301. if assigned(tfornode(node).left.optinfo) then
  302. DFASetExclude(l,tfornode(node).left.optinfo^.index);
  303. { ... but it could be that left/right use it, so do this after
  304. removing the def of the counter variable }
  305. DFASetIncludeSet(l,node.optinfo^.use);
  306. UpdateLifeInfo(node,l);
  307. { ... and a second iteration for fast convergence }
  308. CreateInfo(tfornode(node).t2);
  309. end;
  310. temprefn,
  311. loadn,
  312. typeconvn,
  313. derefn,
  314. assignn:
  315. begin
  316. if not(assigned(node.optinfo^.def)) and
  317. not(assigned(node.optinfo^.use)) then
  318. begin
  319. dfainfo.use:[email protected]^.use;
  320. dfainfo.def:[email protected]^.def;
  321. dfainfo.map:=map;
  322. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  323. end;
  324. calclife(node);
  325. end;
  326. statementn:
  327. begin
  328. { nested statement }
  329. CreateInfo(tstatementnode(node).statement);
  330. { inherit info }
  331. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  332. end;
  333. blockn:
  334. begin
  335. CreateInfo(tblocknode(node).statements);
  336. { ensure that we don't remove life info }
  337. l:=node.optinfo^.life;
  338. if assigned(node.successor) then
  339. DFASetIncludeSet(l,node.successor.optinfo^.life);
  340. UpdateLifeInfo(node,l);
  341. end;
  342. ifn:
  343. begin
  344. { get information from cond. expression }
  345. if not(assigned(node.optinfo^.def)) and
  346. not(assigned(node.optinfo^.use)) then
  347. begin
  348. dfainfo.use:[email protected]^.use;
  349. dfainfo.def:[email protected]^.def;
  350. dfainfo.map:=map;
  351. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  352. end;
  353. { create life info for then and else node }
  354. CreateInfo(tifnode(node).right);
  355. CreateInfo(tifnode(node).t1);
  356. { ensure that we don't remove life info }
  357. l:=node.optinfo^.life;
  358. { get life info from then branch }
  359. if assigned(tifnode(node).right) then
  360. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  361. { get life info from else branch }
  362. if assigned(tifnode(node).t1) then
  363. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  364. else if assigned(node.successor) then
  365. DFASetIncludeSet(l,node.successor.optinfo^.life);
  366. { remove def info from the cond. expression }
  367. DFASetExcludeSet(l,tifnode(node).optinfo^.def);
  368. { add use info from the cond. expression }
  369. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  370. { finally, update the life info of the node }
  371. UpdateLifeInfo(node,l);
  372. end;
  373. casen:
  374. begin
  375. { get information from "case" expression }
  376. if not(assigned(node.optinfo^.def)) and
  377. not(assigned(node.optinfo^.use)) then
  378. begin
  379. dfainfo.use:[email protected]^.use;
  380. dfainfo.def:[email protected]^.def;
  381. dfainfo.map:=map;
  382. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  383. end;
  384. { create life info for block and else nodes }
  385. for i:=0 to tcasenode(node).blocks.count-1 do
  386. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  387. CreateInfo(tcasenode(node).elseblock);
  388. { ensure that we don't remove life info }
  389. l:=node.optinfo^.life;
  390. { get life info from case branches }
  391. for i:=0 to tcasenode(node).blocks.count-1 do
  392. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  393. { get life info from else branch or the succesor }
  394. if assigned(tcasenode(node).elseblock) then
  395. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  396. else if assigned(node.successor) then
  397. DFASetIncludeSet(l,node.successor.optinfo^.life);
  398. { add use info from the "case" expression }
  399. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  400. { finally, update the life info of the node }
  401. UpdateLifeInfo(node,l);
  402. end;
  403. exitn:
  404. begin
  405. if not(is_void(current_procinfo.procdef.returndef)) then
  406. begin
  407. if not(assigned(node.optinfo^.def)) and
  408. not(assigned(node.optinfo^.use)) then
  409. begin
  410. if assigned(texitnode(node).left) then
  411. begin
  412. node.optinfo^.def:=resultnode.optinfo^.def;
  413. dfainfo.use:[email protected]^.use;
  414. dfainfo.def:[email protected]^.def;
  415. dfainfo.map:=map;
  416. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  417. calclife(node);
  418. end
  419. else
  420. begin
  421. { get info from faked resultnode }
  422. node.optinfo^.use:=resultnode.optinfo^.use;
  423. node.optinfo^.life:=node.optinfo^.use;
  424. changed:=true;
  425. end;
  426. end;
  427. end;
  428. end;
  429. {$ifdef JVM}
  430. { all other platforms except jvm translate raise nodes into call nodes during pass_1 }
  431. raisen,
  432. {$endif JVM}
  433. asn,
  434. inlinen,
  435. calln:
  436. begin
  437. if not(assigned(node.optinfo^.def)) and
  438. not(assigned(node.optinfo^.use)) then
  439. begin
  440. dfainfo.use:[email protected]^.use;
  441. dfainfo.def:[email protected]^.def;
  442. dfainfo.map:=map;
  443. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  444. end;
  445. calclife(node);
  446. end;
  447. labeln:
  448. begin
  449. calclife(node);
  450. if assigned(tlabelnode(node).left) then
  451. begin
  452. l:=node.optinfo^.life;
  453. DFASetIncludeSet(l,tlabelnode(node).optinfo^.life);
  454. UpdateLifeInfo(node,l);
  455. end;
  456. end;
  457. tempcreaten,
  458. tempdeleten,
  459. nothingn,
  460. continuen,
  461. goton,
  462. breakn:
  463. begin
  464. calclife(node);
  465. end;
  466. else
  467. internalerror(2007050502);
  468. end;
  469. end;
  470. var
  471. runs : integer;
  472. begin
  473. runs:=0;
  474. repeat
  475. inc(runs);
  476. changed:=false;
  477. CreateInfo(node);
  478. foreachnodestatic(pm_postprocess,node,@ResetProcessing,nil);
  479. { the result node is not reached by foreachnodestatic }
  480. exclude(resultnode.flags,nf_processing);
  481. {$ifdef DEBUG_DFA}
  482. PrintIndexedNodeSet(output,map);
  483. PrintDFAInfo(output,node);
  484. {$endif DEBUG_DFA}
  485. until not(changed);
  486. {$ifdef DEBUG_DFA}
  487. writeln('DFA solver iterations: ',runs);
  488. {$endif DEBUG_DFA}
  489. end;
  490. { reset all dfa info, this is required before creating dfa info
  491. if the tree has been changed without updating dfa }
  492. procedure TDFABuilder.resetdfainfo(node : tnode);
  493. begin
  494. foreachnodestatic(pm_postprocess,node,@ResetDFA,nil);
  495. end;
  496. procedure TDFABuilder.createdfainfo(node : tnode);
  497. var
  498. dfarec : tdfainfo;
  499. begin
  500. if not(assigned(nodemap)) then
  501. nodemap:=TIndexedNodeSet.Create;
  502. { create a fake node using the result which will be the last node }
  503. if not(is_void(current_procinfo.procdef.returndef)) then
  504. begin
  505. if current_procinfo.procdef.proctypeoption=potype_constructor then
  506. resultnode:=load_self_node
  507. else
  508. resultnode:=load_result_node;
  509. resultnode.allocoptinfo;
  510. dfarec.use:[email protected]^.use;
  511. dfarec.def:[email protected]^.def;
  512. dfarec.map:=nodemap;
  513. AddDefUse(resultnode,@dfarec);
  514. resultnode.optinfo^.life:=resultnode.optinfo^.use;
  515. end
  516. else
  517. begin
  518. resultnode:=cnothingnode.create;
  519. resultnode.allocoptinfo;
  520. end;
  521. { add controll flow information }
  522. SetNodeSucessors(node,resultnode);
  523. { now, collect life information }
  524. CreateLifeInfo(node,nodemap);
  525. end;
  526. destructor TDFABuilder.Destroy;
  527. begin
  528. Resultnode.free;
  529. nodemap.free;
  530. inherited destroy;
  531. end;
  532. var
  533. { we have to pass the address of SearchNode in a call inside of SearchNode:
  534. @SearchNode does not work because the compiler thinks we take the address of the result
  535. so store the address from outside }
  536. SearchNodeProcPointer : function(var n: tnode; arg: pointer): foreachnoderesult;
  537. type
  538. { helper structure to be able to pass more than one variable to the iterator function }
  539. TSearchNodeInfo = record
  540. nodetosearch : tnode;
  541. { this contains a list of all file locations where a warning was thrown already,
  542. the same location might appear multiple times because nodes might have been copied }
  543. warnedfilelocs : array of tfileposinfo;
  544. end;
  545. PSearchNodeInfo = ^TSearchNodeInfo;
  546. { searches for a given node n and warns if the node is found as being uninitialized. If a node is
  547. found, searching is stopped so each call issues only one warning/hint }
  548. function SearchNode(var n: tnode; arg: pointer): foreachnoderesult;
  549. function WarnedForLocation(f : tfileposinfo) : boolean;
  550. var
  551. i : longint;
  552. begin
  553. result:=true;
  554. for i:=0 to high(PSearchNodeInfo(arg)^.warnedfilelocs) do
  555. with PSearchNodeInfo(arg)^.warnedfilelocs[i] do
  556. begin
  557. if (f.column=column) and (f.fileindex=fileindex) and (f.line=line) and (f.moduleindex=moduleindex) then
  558. exit;
  559. end;
  560. result:=false;
  561. end;
  562. procedure AddFilepos(const f : tfileposinfo);
  563. begin
  564. Setlength(PSearchNodeInfo(arg)^.warnedfilelocs,length(PSearchNodeInfo(arg)^.warnedfilelocs)+1);
  565. PSearchNodeInfo(arg)^.warnedfilelocs[high(PSearchNodeInfo(arg)^.warnedfilelocs)]:=f;
  566. end;
  567. { Checks if the symbol is a candidate for a warning.
  568. Emit warning/note for living locals, result and parameters, but only about the current
  569. symtables }
  570. function SymbolCandidateForWarningOrHint(sym : tabstractnormalvarsym) : Boolean;
  571. begin
  572. Result:=(((sym.owner=current_procinfo.procdef.localst) and
  573. (current_procinfo.procdef.localst.symtablelevel=sym.owner.symtablelevel)
  574. ) or
  575. ((sym.owner=current_procinfo.procdef.parast) and
  576. (sym.typ=paravarsym) and
  577. (current_procinfo.procdef.parast.symtablelevel=sym.owner.symtablelevel) and
  578. { all parameters except out parameters are initialized by the caller }
  579. (tparavarsym(sym).varspez=vs_out)
  580. ) or
  581. ((vo_is_funcret in sym.varoptions) and
  582. (current_procinfo.procdef.parast.symtablelevel=sym.owner.symtablelevel)
  583. )
  584. ) and not(vo_is_external in sym.varoptions)
  585. end;
  586. var
  587. varsym : tabstractnormalvarsym;
  588. methodpointer,
  589. hpt : tnode;
  590. begin
  591. result:=fen_false;
  592. case n.nodetype of
  593. callparan:
  594. begin
  595. { do not warn about variables passed by var, just issue a hint, this
  596. is a workaround for old code e.g. using fillchar }
  597. if assigned(tcallparanode(n).parasym) and (tcallparanode(n).parasym.varspez in [vs_var,vs_out]) then
  598. begin
  599. hpt:=tcallparanode(n).left;
  600. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn,typeconvn]) do
  601. hpt:=tunarynode(hpt).left;
  602. if assigned(hpt) and (hpt.nodetype=loadn) and not(WarnedForLocation(hpt.fileinfo)) and
  603. SymbolCandidateForWarningOrHint(tabstractnormalvarsym(tloadnode(hpt).symtableentry)) and
  604. PSearchNodeInfo(arg)^.nodetosearch.isequal(hpt) then
  605. begin
  606. { issue only a hint for var, when encountering the node passed as out, we need only to stop searching }
  607. if tcallparanode(n).parasym.varspez=vs_var then
  608. UninitializedVariableMessage(hpt.fileinfo,false,
  609. tloadnode(hpt).symtable.symtabletype=localsymtable,
  610. is_managed_type(tloadnode(hpt).resultdef),
  611. tloadnode(hpt).symtableentry.RealName);
  612. AddFilepos(hpt.fileinfo);
  613. result:=fen_norecurse_true;
  614. end
  615. end;
  616. end;
  617. orn,
  618. andn:
  619. begin
  620. { take care of short boolean evaluation: if the expression to be search is found in left,
  621. we do not need to search right }
  622. if foreachnodestatic(pm_postprocess,taddnode(n).left,SearchNodeProcPointer,arg) or
  623. foreachnodestatic(pm_postprocess,taddnode(n).right,SearchNodeProcPointer,arg) then
  624. result:=fen_norecurse_true
  625. else
  626. result:=fen_norecurse_false;
  627. end;
  628. calln:
  629. begin
  630. methodpointer:=tcallnode(n).methodpointer;
  631. if assigned(methodpointer) and (methodpointer.nodetype<>typen) then
  632. begin
  633. { Remove all postfix operators }
  634. hpt:=methodpointer;
  635. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  636. hpt:=tunarynode(hpt).left;
  637. { skip (absolute and other simple) type conversions -- only now,
  638. because the checks above have to take type conversions into
  639. e.g. class reference types account }
  640. hpt:=actualtargetnode(@hpt)^;
  641. { R.Init then R will be initialized by the constructor,
  642. Also allow it for simple loads }
  643. if (tcallnode(n).procdefinition.proctypeoption=potype_constructor) or
  644. (PSearchNodeInfo(arg)^.nodetosearch.isequal(hpt) and
  645. (((methodpointer.resultdef.typ=objectdef) and
  646. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  647. (methodpointer.resultdef.typ=recorddef)
  648. )
  649. ) then
  650. begin
  651. { don't warn about the method pointer }
  652. AddFilepos(hpt.fileinfo);
  653. if not(foreachnodestatic(pm_postprocess,tcallnode(n).left,SearchNodeProcPointer,arg)) then
  654. foreachnodestatic(pm_postprocess,tcallnode(n).right,SearchNodeProcPointer,arg);
  655. result:=fen_norecurse_true
  656. end;
  657. end;
  658. end;
  659. loadn:
  660. begin
  661. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  662. PSearchNodeInfo(arg)^.nodetosearch.isequal(n) and ((nf_modify in n.flags) or not(nf_write in n.flags)) then
  663. begin
  664. varsym:=tabstractnormalvarsym(tloadnode(n).symtableentry);
  665. if assigned(varsym.owner) and SymbolCandidateForWarningOrHint(varsym) then
  666. begin
  667. if (vo_is_funcret in varsym.varoptions) and not(WarnedForLocation(n.fileinfo)) then
  668. begin
  669. if is_managed_type(varsym.vardef) then
  670. MessagePos(n.fileinfo,sym_w_managed_function_result_uninitialized)
  671. else
  672. MessagePos(n.fileinfo,sym_w_function_result_uninitialized);
  673. AddFilepos(n.fileinfo);
  674. result:=fen_norecurse_true;
  675. end
  676. else
  677. begin
  678. { typed consts are initialized, further, warn only once per location }
  679. if not (vo_is_typed_const in varsym.varoptions) and not(WarnedForLocation(n.fileinfo)) then
  680. begin
  681. UninitializedVariableMessage(n.fileinfo,true,varsym.typ=localvarsym,is_managed_type(varsym.vardef),varsym.realname);
  682. AddFilepos(n.fileinfo);
  683. result:=fen_norecurse_true;
  684. end;
  685. end;
  686. end
  687. {$ifdef dummy}
  688. { if a the variable we are looking for is passed as a var parameter, we stop searching }
  689. else if assigned(varsym.owner) and
  690. (varsym.owner=current_procinfo.procdef.parast) and
  691. (varsym.typ=paravarsym) and
  692. (current_procinfo.procdef.parast.symtablelevel=varsym.owner.symtablelevel) and
  693. (tparavarsym(varsym).varspez=vs_var) then
  694. result:=fen_norecurse_true;
  695. {$endif dummy}
  696. end;
  697. end;
  698. end;
  699. end;
  700. procedure CheckAndWarn(code : tnode;nodetosearch : tnode);
  701. var
  702. SearchNodeInfo : TSearchNodeInfo;
  703. function DoCheck(node : tnode) : boolean;
  704. var
  705. i : longint;
  706. touchesnode : Boolean;
  707. procedure MaybeDoCheck(n : tnode);inline;
  708. begin
  709. Result:=Result or DoCheck(n);
  710. end;
  711. procedure MaybeSearchIn(n : tnode);
  712. begin
  713. if touchesnode then
  714. Result:=Result or foreachnodestatic(pm_postprocess,n,@SearchNode,@SearchNodeInfo);
  715. end;
  716. begin
  717. result:=false;
  718. if node=nil then
  719. exit;
  720. if nf_processing in node.flags then
  721. exit;
  722. include(node.flags,nf_processing);
  723. if not(DFASetIn(node.optinfo^.life,nodetosearch.optinfo^.index)) then
  724. exit;
  725. { we do not need this info always, so try to safe some time here, CheckAndWarn
  726. takes a lot of time anyways }
  727. if not(node.nodetype in [statementn,blockn]) then
  728. touchesnode:=DFASetIn(node.optinfo^.use,nodetosearch.optinfo^.index) or
  729. DFASetIn(node.optinfo^.def,nodetosearch.optinfo^.index)
  730. else
  731. touchesnode:=false;
  732. case node.nodetype of
  733. whilerepeatn:
  734. begin
  735. MaybeSearchIn(twhilerepeatnode(node).left);
  736. MaybeDoCheck(twhilerepeatnode(node).right);
  737. end;
  738. forn:
  739. begin
  740. MaybeSearchIn(tfornode(node).right);
  741. MaybeSearchIn(tfornode(node).t1);
  742. MaybeDoCheck(tfornode(node).t2);
  743. end;
  744. statementn:
  745. MaybeDoCheck(tstatementnode(node).statement);
  746. blockn:
  747. MaybeDoCheck(tblocknode(node).statements);
  748. ifn:
  749. begin
  750. MaybeSearchIn(tifnode(node).left);
  751. MaybeDoCheck(tifnode(node).right);
  752. MaybeDoCheck(tifnode(node).t1);
  753. end;
  754. casen:
  755. begin
  756. MaybeSearchIn(tcasenode(node).left);
  757. for i:=0 to tcasenode(node).blocks.count-1 do
  758. MaybeDoCheck(pcaseblock(tcasenode(node).blocks[i])^.statement);
  759. MaybeDoCheck(tcasenode(node).elseblock);
  760. end;
  761. labeln:
  762. MaybeDoCheck(tlabelnode(node).left);
  763. { we are aware of the following nodes so if new node types are added to the compiler
  764. and pop up in the search, the ie below kicks in as a reminder }
  765. exitn:
  766. begin
  767. MaybeSearchIn(texitnode(node).left);
  768. { exit uses the resultnode implicitly, so searching for a matching node is
  769. useless, if we reach the exit node and found the living node not in left, then
  770. it can be only the resultnode }
  771. if not(Result) and not(is_void(current_procinfo.procdef.returndef)) and
  772. not(assigned(texitnode(node).resultexpr)) and
  773. { don't warn about constructors }
  774. not(current_procinfo.procdef.proctypeoption in [potype_class_constructor,potype_constructor]) then
  775. begin
  776. if is_managed_type(current_procinfo.procdef.returndef) then
  777. MessagePos(node.fileinfo,sym_w_managed_function_result_uninitialized)
  778. else
  779. MessagePos(node.fileinfo,sym_w_function_result_uninitialized);
  780. Setlength(SearchNodeInfo.warnedfilelocs,length(SearchNodeInfo.warnedfilelocs)+1);
  781. SearchNodeInfo.warnedfilelocs[high(SearchNodeInfo.warnedfilelocs)]:=node.fileinfo;
  782. end
  783. end;
  784. { could be the implicitly generated load node for the result }
  785. {$ifdef JVM}
  786. { all other platforms except jvm translate raise nodes into call nodes during pass_1 }
  787. raisen,
  788. {$endif JVM}
  789. loadn,
  790. assignn,
  791. calln,
  792. temprefn,
  793. typeconvn,
  794. inlinen,
  795. tempcreaten,
  796. tempdeleten:
  797. MaybeSearchIn(node);
  798. nothingn,
  799. continuen,
  800. goton,
  801. breakn:
  802. ;
  803. else
  804. internalerror(2013111301);
  805. end;
  806. { if already a warning has been issued, then stop }
  807. if Result then
  808. exit;
  809. if assigned(node.successor) then
  810. MaybeDoCheck(node.successor);
  811. end;
  812. begin
  813. SearchNodeInfo.nodetosearch:=nodetosearch;
  814. DoCheck(code);
  815. foreachnodestatic(pm_postprocess,code,@ResetProcessing,nil);
  816. end;
  817. begin
  818. SearchNodeProcPointer:=@SearchNode;
  819. end.