optdfa.pas 35 KB

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