1 unit WarnEmptyBlock;
2 
3 { AFS 30 Dec 2002
4  warn of an enmpty block, one of
5  begin..end, try..except, try..finally, except..end, finally..end
6 }
7 
8 
9 {(*}
10 (*------------------------------------------------------------------------------
11  Delphi Code formatter source code
12 
13 The Original Code is WarnEmptyBlock, released May 2003.
14 The Initial Developer of the Original Code is Anthony Steele.
15 Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
16 All Rights Reserved.
17 Contributor(s): Anthony Steele.
18 
19 The contents of this file are subject to the Mozilla Public License Version 1.1
20 (the "License"). you may not use this file except in compliance with the License.
21 You may obtain a copy of the License at http://www.mozilla.org/NPL/
22 
23 Software distributed under the License is distributed on an "AS IS" basis,
24 WITHOUT WARRANTY OF ANY KIND, either express or implied.
25 See the License for the specific language governing rights and limitations
26 under the License.
27 
28 Alternatively, the contents of this file may be used under the terms of
29 the GNU General Public License Version 2 or later (the "GPL")
30 See http://www.gnu.org/licenses/gpl.html
31 ------------------------------------------------------------------------------*)
32 {*)}
33 
34 {$I JcfGlobal.inc}
35 
36 interface
37 
38 uses Warning;
39 
40 type
41 
42   TWarnEmptyBlock = class(TWarning)
43   public
44     constructor Create; override;
45 
46     procedure PreVisitParseTreeNode(const pcNode: TObject); override;
47   end;
48 
49 implementation
50 
51 uses ParseTreeNode, ParseTreeNodeType;
52 
53 constructor TWarnEmptyBlock.Create;
54 begin
55   inherited;
56 
57   HasPreVisit := True;
58   HasPostVisit := False;
59   HasSourceTokenVisit := False;
60 end;
61 
62 procedure TWarnEmptyBlock.PreVisitParseTreeNode(const pcNode: TObject);
63 var
64   lcNode: TParseTreeNode;
65   liSolidChildCount: integer;
66 begin
67   lcNode := TParseTreeNode(pcNode);
68 
69   // only look in statements
70   if not lcNode.HasParentNode(nBlock) then
71     exit;
72 
73   { looking for nodes with 2 solid tokens under them
74     e.g. 'begin' and 'end'
75   }
76   liSolidChildCount := lcNode.SolidChildCount;
77 
78   if liSolidChildCount = 2 then
79   begin
80     if lcNode.NodeType = nCompoundStatement then
81     begin
82       SendWarning(lcNode, 'Empty begin..end block');
83     end;
84 
85     if lcNode.NodeType = nFinallyBlock then
86     begin
87       SendWarning(lcNode, 'Empty finally..end block');
88     end;
89 
90     if lcNode.NodeType = nExceptBlock then
91     begin
92       SendWarning(lcNode, 'Empty except..end block');
93     end;
94   end
95   else if liSolidChildCount = 1 then
96   begin
97     if lcNode.NodeType = nTryBlock then
98     begin
99       SendWarning(lcNode, 'Empty try block');
100     end;
101   end;
102 
103 end;
104 
105 end.
106