optdfa.pas 35 KB

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