optdfa.pas 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. assignn:
  314. begin
  315. if not(assigned(node.optinfo^.def)) and
  316. not(assigned(node.optinfo^.use)) then
  317. begin
  318. dfainfo.use:[email protected]^.use;
  319. dfainfo.def:[email protected]^.def;
  320. dfainfo.map:=map;
  321. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  322. end;
  323. calclife(node);
  324. end;
  325. statementn:
  326. begin
  327. { nested statement }
  328. CreateInfo(tstatementnode(node).statement);
  329. { inherit info }
  330. node.optinfo^.life:=tstatementnode(node).statement.optinfo^.life;
  331. end;
  332. blockn:
  333. begin
  334. CreateInfo(tblocknode(node).statements);
  335. { ensure that we don't remove life info }
  336. l:=node.optinfo^.life;
  337. if assigned(node.successor) then
  338. DFASetIncludeSet(l,node.successor.optinfo^.life);
  339. UpdateLifeInfo(node,l);
  340. end;
  341. ifn:
  342. begin
  343. { get information from cond. expression }
  344. if not(assigned(node.optinfo^.def)) and
  345. not(assigned(node.optinfo^.use)) then
  346. begin
  347. dfainfo.use:[email protected]^.use;
  348. dfainfo.def:[email protected]^.def;
  349. dfainfo.map:=map;
  350. foreachnodestatic(pm_postprocess,tifnode(node).left,@AddDefUse,@dfainfo);
  351. end;
  352. { create life info for then and else node }
  353. CreateInfo(tifnode(node).right);
  354. CreateInfo(tifnode(node).t1);
  355. { ensure that we don't remove life info }
  356. l:=node.optinfo^.life;
  357. { get life info from then branch }
  358. if assigned(tifnode(node).right) then
  359. DFASetIncludeSet(l,tifnode(node).right.optinfo^.life);
  360. { get life info from else branch }
  361. if assigned(tifnode(node).t1) then
  362. DFASetIncludeSet(l,tifnode(node).t1.optinfo^.life)
  363. else if assigned(node.successor) then
  364. DFASetIncludeSet(l,node.successor.optinfo^.life);
  365. { remove def info from the cond. expression }
  366. DFASetExcludeSet(l,tifnode(node).optinfo^.def);
  367. { add use info from the cond. expression }
  368. DFASetIncludeSet(l,tifnode(node).optinfo^.use);
  369. { finally, update the life info of the node }
  370. UpdateLifeInfo(node,l);
  371. end;
  372. casen:
  373. begin
  374. { get information from "case" expression }
  375. if not(assigned(node.optinfo^.def)) and
  376. not(assigned(node.optinfo^.use)) then
  377. begin
  378. dfainfo.use:[email protected]^.use;
  379. dfainfo.def:[email protected]^.def;
  380. dfainfo.map:=map;
  381. foreachnodestatic(pm_postprocess,tcasenode(node).left,@AddDefUse,@dfainfo);
  382. end;
  383. { create life info for block and else nodes }
  384. for i:=0 to tcasenode(node).blocks.count-1 do
  385. CreateInfo(pcaseblock(tcasenode(node).blocks[i])^.statement);
  386. CreateInfo(tcasenode(node).elseblock);
  387. { ensure that we don't remove life info }
  388. l:=node.optinfo^.life;
  389. { get life info from case branches }
  390. for i:=0 to tcasenode(node).blocks.count-1 do
  391. DFASetIncludeSet(l,pcaseblock(tcasenode(node).blocks[i])^.statement.optinfo^.life);
  392. { get life info from else branch or the succesor }
  393. if assigned(tcasenode(node).elseblock) then
  394. DFASetIncludeSet(l,tcasenode(node).elseblock.optinfo^.life)
  395. else if assigned(node.successor) then
  396. DFASetIncludeSet(l,node.successor.optinfo^.life);
  397. { add use info from the "case" expression }
  398. DFASetIncludeSet(l,tcasenode(node).optinfo^.use);
  399. { finally, update the life info of the node }
  400. UpdateLifeInfo(node,l);
  401. end;
  402. exitn:
  403. begin
  404. if not(is_void(current_procinfo.procdef.returndef)) then
  405. begin
  406. if not(assigned(node.optinfo^.def)) and
  407. not(assigned(node.optinfo^.use)) then
  408. begin
  409. if assigned(texitnode(node).left) then
  410. begin
  411. node.optinfo^.def:=resultnode.optinfo^.def;
  412. dfainfo.use:[email protected]^.use;
  413. dfainfo.def:[email protected]^.def;
  414. dfainfo.map:=map;
  415. foreachnodestatic(pm_postprocess,texitnode(node).left,@AddDefUse,@dfainfo);
  416. calclife(node);
  417. end
  418. else
  419. begin
  420. { get info from faked resultnode }
  421. node.optinfo^.use:=resultnode.optinfo^.use;
  422. node.optinfo^.life:=node.optinfo^.use;
  423. changed:=true;
  424. end;
  425. end;
  426. end;
  427. end;
  428. {$ifdef JVM}
  429. { all other platforms except jvm translate raise nodes into call nodes during pass_1 }
  430. raisen,
  431. {$endif JVM}
  432. asn,
  433. inlinen,
  434. calln:
  435. begin
  436. if not(assigned(node.optinfo^.def)) and
  437. not(assigned(node.optinfo^.use)) then
  438. begin
  439. dfainfo.use:[email protected]^.use;
  440. dfainfo.def:[email protected]^.def;
  441. dfainfo.map:=map;
  442. foreachnodestatic(pm_postprocess,node,@AddDefUse,@dfainfo);
  443. end;
  444. calclife(node);
  445. end;
  446. labeln:
  447. begin
  448. calclife(node);
  449. if assigned(tlabelnode(node).left) then
  450. begin
  451. l:=node.optinfo^.life;
  452. DFASetIncludeSet(l,tlabelnode(node).optinfo^.life);
  453. UpdateLifeInfo(node,l);
  454. end;
  455. end;
  456. tempcreaten,
  457. tempdeleten,
  458. nothingn,
  459. continuen,
  460. goton,
  461. breakn:
  462. begin
  463. calclife(node);
  464. end;
  465. else
  466. internalerror(2007050502);
  467. end;
  468. end;
  469. var
  470. runs : integer;
  471. begin
  472. runs:=0;
  473. repeat
  474. inc(runs);
  475. changed:=false;
  476. CreateInfo(node);
  477. foreachnodestatic(pm_postprocess,node,@ResetProcessing,nil);
  478. { the result node is not reached by foreachnodestatic }
  479. exclude(resultnode.flags,nf_processing);
  480. {$ifdef DEBUG_DFA}
  481. PrintIndexedNodeSet(output,map);
  482. PrintDFAInfo(output,node);
  483. {$endif DEBUG_DFA}
  484. until not(changed);
  485. {$ifdef DEBUG_DFA}
  486. writeln('DFA solver iterations: ',runs);
  487. {$endif DEBUG_DFA}
  488. end;
  489. { reset all dfa info, this is required before creating dfa info
  490. if the tree has been changed without updating dfa }
  491. procedure TDFABuilder.resetdfainfo(node : tnode);
  492. begin
  493. foreachnodestatic(pm_postprocess,node,@ResetDFA,nil);
  494. end;
  495. procedure TDFABuilder.createdfainfo(node : tnode);
  496. var
  497. dfarec : tdfainfo;
  498. begin
  499. if not(assigned(nodemap)) then
  500. nodemap:=TIndexedNodeSet.Create;
  501. { create a fake node using the result which will be the last node }
  502. if not(is_void(current_procinfo.procdef.returndef)) then
  503. begin
  504. if current_procinfo.procdef.proctypeoption=potype_constructor then
  505. resultnode:=load_self_node
  506. else
  507. resultnode:=load_result_node;
  508. resultnode.allocoptinfo;
  509. dfarec.use:[email protected]^.use;
  510. dfarec.def:[email protected]^.def;
  511. dfarec.map:=nodemap;
  512. AddDefUse(resultnode,@dfarec);
  513. resultnode.optinfo^.life:=resultnode.optinfo^.use;
  514. end
  515. else
  516. begin
  517. resultnode:=cnothingnode.create;
  518. resultnode.allocoptinfo;
  519. end;
  520. { add controll flow information }
  521. SetNodeSucessors(node,resultnode);
  522. { now, collect life information }
  523. CreateLifeInfo(node,nodemap);
  524. end;
  525. destructor TDFABuilder.Destroy;
  526. begin
  527. Resultnode.free;
  528. nodemap.free;
  529. inherited destroy;
  530. end;
  531. var
  532. { we have to pass the address of SearchNode in a call inside of SearchNode:
  533. @SearchNode does not work because the compiler thinks we take the address of the result
  534. so store the address from outside }
  535. SearchNodeProcPointer : function(var n: tnode; arg: pointer): foreachnoderesult;
  536. type
  537. { helper structure to be able to pass more than one variable to the iterator function }
  538. TSearchNodeInfo = record
  539. nodetosearch : tnode;
  540. { this contains a list of all file locations where a warning was thrown already,
  541. the same location might appear multiple times because nodes might have been copied }
  542. warnedfilelocs : array of tfileposinfo;
  543. end;
  544. PSearchNodeInfo = ^TSearchNodeInfo;
  545. { searches for a given node n and warns if the node is found as being uninitialized. If a node is
  546. found, searching is stopped so each call issues only one warning/hint }
  547. function SearchNode(var n: tnode; arg: pointer): foreachnoderesult;
  548. function WarnedForLocation(f : tfileposinfo) : boolean;
  549. var
  550. i : longint;
  551. begin
  552. result:=true;
  553. for i:=0 to high(PSearchNodeInfo(arg)^.warnedfilelocs) do
  554. with PSearchNodeInfo(arg)^.warnedfilelocs[i] do
  555. begin
  556. if (f.column=column) and (f.fileindex=fileindex) and (f.line=line) and (f.moduleindex=moduleindex) then
  557. exit;
  558. end;
  559. result:=false;
  560. end;
  561. procedure AddFilepos(const f : tfileposinfo);
  562. begin
  563. Setlength(PSearchNodeInfo(arg)^.warnedfilelocs,length(PSearchNodeInfo(arg)^.warnedfilelocs)+1);
  564. PSearchNodeInfo(arg)^.warnedfilelocs[high(PSearchNodeInfo(arg)^.warnedfilelocs)]:=f;
  565. end;
  566. { Checks if the symbol is a candidate for a warning.
  567. Emit warning/note for living locals, result and parameters, but only about the current
  568. symtables }
  569. function SymbolCandidateForWarningOrHint(sym : tabstractnormalvarsym) : Boolean;
  570. begin
  571. Result:=(((sym.owner=current_procinfo.procdef.localst) and
  572. (current_procinfo.procdef.localst.symtablelevel=sym.owner.symtablelevel)
  573. ) or
  574. ((sym.owner=current_procinfo.procdef.parast) and
  575. (sym.typ=paravarsym) and
  576. (current_procinfo.procdef.parast.symtablelevel=sym.owner.symtablelevel) and
  577. { all parameters except out parameters are initialized by the caller }
  578. (tparavarsym(sym).varspez=vs_out)
  579. ) or
  580. ((vo_is_funcret in sym.varoptions) and
  581. (current_procinfo.procdef.parast.symtablelevel=sym.owner.symtablelevel)
  582. )
  583. ) and not(vo_is_external in sym.varoptions)
  584. end;
  585. var
  586. varsym : tabstractnormalvarsym;
  587. methodpointer,
  588. hpt : tnode;
  589. begin
  590. result:=fen_false;
  591. case n.nodetype of
  592. callparan:
  593. begin
  594. { do not warn about variables passed by var, just issue a hint, this
  595. is a workaround for old code e.g. using fillchar }
  596. if assigned(tcallparanode(n).parasym) and (tcallparanode(n).parasym.varspez in [vs_var,vs_out]) then
  597. begin
  598. hpt:=tcallparanode(n).left;
  599. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn,typeconvn]) do
  600. hpt:=tunarynode(hpt).left;
  601. if assigned(hpt) and (hpt.nodetype=loadn) and not(WarnedForLocation(hpt.fileinfo)) and
  602. SymbolCandidateForWarningOrHint(tabstractnormalvarsym(tloadnode(hpt).symtableentry)) and
  603. PSearchNodeInfo(arg)^.nodetosearch.isequal(hpt) then
  604. begin
  605. { issue only a hint for var, when encountering the node passed as out, we need only to stop searching }
  606. if tcallparanode(n).parasym.varspez=vs_var then
  607. UninitializedVariableMessage(hpt.fileinfo,false,
  608. tloadnode(hpt).symtable.symtabletype=localsymtable,
  609. is_managed_type(tloadnode(hpt).resultdef),
  610. tloadnode(hpt).symtableentry.RealName);
  611. AddFilepos(hpt.fileinfo);
  612. result:=fen_norecurse_true;
  613. end
  614. end;
  615. end;
  616. orn,
  617. andn:
  618. begin
  619. { take care of short boolean evaluation: if the expression to be search is found in left,
  620. we do not need to search right }
  621. if foreachnodestatic(pm_postprocess,taddnode(n).left,SearchNodeProcPointer,arg) or
  622. foreachnodestatic(pm_postprocess,taddnode(n).right,SearchNodeProcPointer,arg) then
  623. result:=fen_norecurse_true
  624. else
  625. result:=fen_norecurse_false;
  626. end;
  627. calln:
  628. begin
  629. methodpointer:=tcallnode(n).methodpointer;
  630. if assigned(methodpointer) and (methodpointer.nodetype<>typen) then
  631. begin
  632. { Remove all postfix operators }
  633. hpt:=methodpointer;
  634. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  635. hpt:=tunarynode(hpt).left;
  636. { skip (absolute and other simple) type conversions -- only now,
  637. because the checks above have to take type conversions into
  638. e.g. class reference types account }
  639. hpt:=actualtargetnode(@hpt)^;
  640. { R.Init then R will be initialized by the constructor,
  641. Also allow it for simple loads }
  642. if (tcallnode(n).procdefinition.proctypeoption=potype_constructor) or
  643. (PSearchNodeInfo(arg)^.nodetosearch.isequal(hpt) and
  644. (((methodpointer.resultdef.typ=objectdef) and
  645. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)) or
  646. (methodpointer.resultdef.typ=recorddef)
  647. )
  648. ) then
  649. begin
  650. { don't warn about the method pointer }
  651. AddFilepos(hpt.fileinfo);
  652. if not(foreachnodestatic(pm_postprocess,tcallnode(n).left,SearchNodeProcPointer,arg)) then
  653. foreachnodestatic(pm_postprocess,tcallnode(n).right,SearchNodeProcPointer,arg);
  654. result:=fen_norecurse_true
  655. end;
  656. end;
  657. end;
  658. loadn:
  659. begin
  660. if (tloadnode(n).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) and
  661. PSearchNodeInfo(arg)^.nodetosearch.isequal(n) and ((nf_modify in n.flags) or not(nf_write in n.flags)) then
  662. begin
  663. varsym:=tabstractnormalvarsym(tloadnode(n).symtableentry);
  664. if assigned(varsym.owner) and SymbolCandidateForWarningOrHint(varsym) then
  665. begin
  666. if (vo_is_funcret in varsym.varoptions) and not(WarnedForLocation(n.fileinfo)) then
  667. begin
  668. if is_managed_type(varsym.vardef) then
  669. MessagePos(n.fileinfo,sym_w_managed_function_result_uninitialized)
  670. else
  671. MessagePos(n.fileinfo,sym_w_function_result_uninitialized);
  672. AddFilepos(n.fileinfo);
  673. result:=fen_norecurse_true;
  674. end
  675. else
  676. begin
  677. { typed consts are initialized, further, warn only once per location }
  678. if not (vo_is_typed_const in varsym.varoptions) and not(WarnedForLocation(n.fileinfo)) then
  679. begin
  680. UninitializedVariableMessage(n.fileinfo,true,varsym.typ=localvarsym,is_managed_type(varsym.vardef),varsym.realname);
  681. AddFilepos(n.fileinfo);
  682. result:=fen_norecurse_true;
  683. end;
  684. end;
  685. end
  686. {$ifdef dummy}
  687. { if a the variable we are looking for is passed as a var parameter, we stop searching }
  688. else if assigned(varsym.owner) and
  689. (varsym.owner=current_procinfo.procdef.parast) and
  690. (varsym.typ=paravarsym) and
  691. (current_procinfo.procdef.parast.symtablelevel=varsym.owner.symtablelevel) and
  692. (tparavarsym(varsym).varspez=vs_var) then
  693. result:=fen_norecurse_true;
  694. {$endif dummy}
  695. end;
  696. end;
  697. end;
  698. end;
  699. procedure CheckAndWarn(code : tnode;nodetosearch : tnode);
  700. var
  701. SearchNodeInfo : TSearchNodeInfo;
  702. function DoCheck(node : tnode) : boolean;
  703. var
  704. i : longint;
  705. touchesnode : Boolean;
  706. procedure MaybeDoCheck(n : tnode);inline;
  707. begin
  708. Result:=Result or DoCheck(n);
  709. end;
  710. procedure MaybeSearchIn(n : tnode);
  711. begin
  712. if touchesnode then
  713. Result:=Result or foreachnodestatic(pm_postprocess,n,@SearchNode,@SearchNodeInfo);
  714. end;
  715. begin
  716. result:=false;
  717. if node=nil then
  718. exit;
  719. if nf_processing in node.flags then
  720. exit;
  721. include(node.flags,nf_processing);
  722. if not(DFASetIn(node.optinfo^.life,nodetosearch.optinfo^.index)) then
  723. exit;
  724. { we do not need this info always, so try to safe some time here, CheckAndWarn
  725. takes a lot of time anyways }
  726. if not(node.nodetype in [statementn,blockn]) then
  727. touchesnode:=DFASetIn(node.optinfo^.use,nodetosearch.optinfo^.index) or
  728. DFASetIn(node.optinfo^.def,nodetosearch.optinfo^.index)
  729. else
  730. touchesnode:=false;
  731. case node.nodetype of
  732. whilerepeatn:
  733. begin
  734. MaybeSearchIn(twhilerepeatnode(node).left);
  735. MaybeDoCheck(twhilerepeatnode(node).right);
  736. end;
  737. forn:
  738. begin
  739. MaybeSearchIn(tfornode(node).right);
  740. MaybeSearchIn(tfornode(node).t1);
  741. MaybeDoCheck(tfornode(node).t2);
  742. end;
  743. statementn:
  744. MaybeDoCheck(tstatementnode(node).statement);
  745. blockn:
  746. MaybeDoCheck(tblocknode(node).statements);
  747. ifn:
  748. begin
  749. MaybeSearchIn(tifnode(node).left);
  750. MaybeDoCheck(tifnode(node).right);
  751. MaybeDoCheck(tifnode(node).t1);
  752. end;
  753. casen:
  754. begin
  755. MaybeSearchIn(tcasenode(node).left);
  756. for i:=0 to tcasenode(node).blocks.count-1 do
  757. MaybeDoCheck(pcaseblock(tcasenode(node).blocks[i])^.statement);
  758. MaybeDoCheck(tcasenode(node).elseblock);
  759. end;
  760. labeln:
  761. MaybeDoCheck(tlabelnode(node).left);
  762. { we are aware of the following nodes so if new node types are added to the compiler
  763. and pop up in the search, the ie below kicks in as a reminder }
  764. exitn:
  765. begin
  766. MaybeSearchIn(texitnode(node).left);
  767. { exit uses the resultnode implicitly, so searching for a matching node is
  768. useless, if we reach the exit node and found the living node not in left, then
  769. it can be only the resultnode }
  770. if not(Result) and not(is_void(current_procinfo.procdef.returndef)) and
  771. not(assigned(texitnode(node).resultexpr)) and
  772. { don't warn about constructors }
  773. not(current_procinfo.procdef.proctypeoption in [potype_class_constructor,potype_constructor]) then
  774. begin
  775. if is_managed_type(current_procinfo.procdef.returndef) then
  776. MessagePos(node.fileinfo,sym_w_managed_function_result_uninitialized)
  777. else
  778. MessagePos(node.fileinfo,sym_w_function_result_uninitialized);
  779. Setlength(SearchNodeInfo.warnedfilelocs,length(SearchNodeInfo.warnedfilelocs)+1);
  780. SearchNodeInfo.warnedfilelocs[high(SearchNodeInfo.warnedfilelocs)]:=node.fileinfo;
  781. end
  782. end;
  783. { could be the implicitly generated load node for the result }
  784. {$ifdef JVM}
  785. { all other platforms except jvm translate raise nodes into call nodes during pass_1 }
  786. raisen,
  787. {$endif JVM}
  788. loadn,
  789. assignn,
  790. calln,
  791. temprefn,
  792. typeconvn,
  793. inlinen,
  794. tempcreaten,
  795. tempdeleten:
  796. MaybeSearchIn(node);
  797. nothingn,
  798. continuen,
  799. goton,
  800. breakn:
  801. ;
  802. else
  803. internalerror(2013111301);
  804. end;
  805. { if already a warning has been issued, then stop }
  806. if Result then
  807. exit;
  808. if assigned(node.successor) then
  809. MaybeDoCheck(node.successor);
  810. end;
  811. begin
  812. SearchNodeInfo.nodetosearch:=nodetosearch;
  813. DoCheck(code);
  814. foreachnodestatic(pm_postprocess,code,@ResetProcessing,nil);
  815. end;
  816. begin
  817. SearchNodeProcPointer:=@SearchNode;
  818. end.