optdfa.pas 36 KB

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