Debian Codegraph @

gcc/m2/gm2-compiler/M2Students.mod

raw
1 (* M2Students.mod checks for new programmer errors.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2Students ;
23
24
25 FROM SymbolTable IMPORT FinalSymbol, IsVar, IsProcedure, IsModule,
26 GetMainModule, IsType, NulSym, IsRecord, GetSymName, GetNth, GetNthProcedure, GetDeclaredMod, NoOfParam ;
27 FROM NameKey IMPORT GetKey, WriteKey, MakeKey, IsSameExcludingCase, NulName, makekey, KeyToCharStar ;
28 FROM M2MetaError IMPORT MetaErrorString0, MetaError2 ;
29 FROM Lists IMPORT List, InitList, IsItemInList, IncludeItemIntoList ;
30 FROM M2Reserved IMPORT IsReserved, toktype ;
31 FROM DynamicStrings IMPORT String, InitString, KillString, ToUpper, InitStringCharStar, string, Mark, ToUpper, Dup ;
32 FROM FormatStrings IMPORT Sprintf0, Sprintf1, Sprintf2 ;
33 FROM M2LexBuf IMPORT GetTokenNo ;
34 FROM ASCII IMPORT nul ;
35 FROM M2Options IMPORT StyleChecking ;
36
37
38 VAR
39 ErrantNames,
40 ErrantSymbols: List ;
41
42
43 (*
44 IsNotADuplicate - returns TRUE if either s1 or s2 have not been reported before.
45 *)
46
47 PROCEDURE IsNotADuplicate (s1, s2: CARDINAL) : BOOLEAN ;
48 BEGIN
49 IF (NOT IsItemInList(ErrantSymbols, s1)) AND (NOT IsItemInList(ErrantSymbols, s2))
50 THEN
51 IncludeItemIntoList(ErrantSymbols, s1) ;
52 IncludeItemIntoList(ErrantSymbols, s2) ;
53 RETURN( TRUE )
54 ELSE
55 RETURN( FALSE )
56 END
57 END IsNotADuplicate ;
58
59
60 (*
61 IsNotADuplicateName - returns TRUE if name has not been reported before.
62 *)
63
64 PROCEDURE IsNotADuplicateName (name: Name) : BOOLEAN ;
65 BEGIN
66 IF NOT IsItemInList(ErrantNames, name)
67 THEN
68 IncludeItemIntoList(ErrantNames, name) ;
69 RETURN( TRUE )
70 ELSE
71 RETURN( FALSE )
72 END
73 END IsNotADuplicateName ;
74
75
76 (*
77 CheckVariableAgainstKeyword - checks for a identifier that looks the same
78 as a keyword except for its case.
79 *)
80
81 PROCEDURE CheckVariableAgainstKeyword (name: Name) ;
82 BEGIN
83 IF StyleChecking
84 THEN
85 PerformVariableKeywordCheck (name)
86 END
87 END CheckVariableAgainstKeyword ;
88
89
90 (*
91 PerformVariableKeywordCheck - performs the check and constructs the metaerror notes if appropriate.
92 *)
93
94 PROCEDURE PerformVariableKeywordCheck (name: Name) ;
95 VAR
96 upper : Name ;
97 token : toktype ;
98 orig,
99 upperS: String ;
100 BEGIN
101 orig := InitStringCharStar (KeyToCharStar (name)) ;
102 upperS := ToUpper (Dup (orig)) ;
103 upper := makekey (string (upperS)) ;
104 IF IsReserved (upper, token)
105 THEN
106 IF IsNotADuplicateName (name)
107 THEN
108 MetaErrorString0 (Sprintf2 (Mark (InitString ('either the identifier has the same name as a keyword or alternatively a keyword has the wrong case ({%%K%s} and {!%%O:{%%K%s}})')),
109 upperS, orig)) ;
110 MetaErrorString0 (Sprintf1 (Mark (InitString ('the symbol name {!%%O:{%%K%s}} is legal as an identifier, however as such it might cause confusion and is considered bad programming practice')), orig))
111 END
112 END ;
113 upperS := KillString (upperS) ;
114 orig := KillString (orig)
115 END PerformVariableKeywordCheck ;
116
117
118 (*
119 CheckAsciiName - checks to see whether ascii names, s1, and, s2, are similar.
120 *)
121
122 PROCEDURE CheckAsciiName (previous, s1, newblock, s2: CARDINAL) ;
123 VAR
124 a1, a2: Name ;
125 BEGIN
126 a1 := GetSymName (s1) ;
127 a2 := GetSymName (s2) ;
128 IF (a1 = a2) AND (a1 # NulName)
129 THEN
130 IF IsNotADuplicate (s1, s2)
131 THEN
132 MetaError2 ('identical symbol name in two different scopes, scope {%1Oad} has symbol {%2Mad}', previous, s1) ;
133 MetaError2 ('identical symbol name in two different scopes, scope {%1Oad} has symbol {%2Mad}', newblock, s2)
134 END
135 ELSIF IsSameExcludingCase (a1, a2)
136 THEN
137 IF IsNotADuplicate (s1, s2)
138 THEN
139 MetaError2 ('very similar symbol names (different case) in two different scopes, scope {%1ORad} has symbol {%2Mad}', previous, s1) ;
140 MetaError2 ('very similar symbol names (different case) in two different scopes, scope {%1OCad} has symbol {%2Mad}', newblock, s2)
141 END
142 END
143 END CheckAsciiName ;
144
145
146 (*
147 CheckProcedure - checks the procedure, p, for symbols which look like, s.
148 *)
149
150 PROCEDURE CheckProcedure (m, p: CARDINAL) ;
151 VAR
152 i, n1,
153 j, n2: CARDINAL ;
154 BEGIN
155 IF p#NulSym
156 THEN
157 i := 1 ; (* I would have used NoOfParam(p)+1 but Stuart wants parameters checked as well - maybe he is right. *)
158 REPEAT
159 n1 := GetNth(p, i) ;
160 IF n1#NulSym
161 THEN
162 IF IsVar(n1) OR IsType(n1) OR IsProcedure(n1) OR IsRecord(n1)
163 THEN
164 j := 1 ;
165 REPEAT
166 n2 := GetNth(m, j) ;
167 IF n2#NulSym
168 THEN
169 IF IsVar(n2) OR IsType(n2) OR IsProcedure(n2) OR IsRecord(n2)
170 THEN
171 CheckAsciiName(m, n2, p, n1)
172 END
173 END ;
174 INC(j)
175 UNTIL n2=NulSym
176 END
177 END ;
178 INC(i)
179 UNTIL n1=NulSym
180 END
181 END CheckProcedure ;
182
183
184 (*
185 CheckModule - checks the module, m, for symbols which look like, s.
186 *)
187
188 PROCEDURE CheckModule (m, s: CARDINAL) ;
189 VAR
190 i, n: CARDINAL ;
191 BEGIN
192 IF m#NulSym
193 THEN
194 i := 1 ;
195 REPEAT
196 n := GetNth(m, i) ;
197 IF n#NulSym
198 THEN
199 IF (n#NulSym) AND (n#s)
200 THEN
201 IF IsVar(n) OR IsType(n) OR IsProcedure(n) OR IsRecord(n)
202 THEN
203 CheckAsciiName(m, s, m, n)
204 END
205 END
206 END ;
207 INC(i)
208 UNTIL n=NulSym
209 END
210 END CheckModule ;
211
212
213 (*
214 StudentVariableCheck - checks to see that variables are quite different from keywords and
215 issues an message if they are not. It ignores case so to catch
216 1st and 2nd semester programming errors.
217 *)
218
219 PROCEDURE StudentVariableCheck ;
220 VAR
221 i, n, m: CARDINAL ;
222 BEGIN
223 m := GetMainModule() ;
224 (* first check global scope *)
225 i := 1 ;
226 REPEAT
227 n := GetNth(m, i) ;
228 IF n#NulSym
229 THEN
230 IF IsVar(n) OR IsType(n) OR IsProcedure(n) OR IsRecord(n)
231 THEN
232 CheckModule(m, n)
233 END
234 END ;
235 INC(i)
236 UNTIL n=NulSym ;
237 (* now check local scope *)
238 i := 1 ;
239 REPEAT
240 n := GetNthProcedure(m, i) ;
241 IF n#NulSym
242 THEN
243 IF IsProcedure(n)
244 THEN
245 CheckProcedure(m, n)
246 END
247 END ;
248 INC(i)
249 UNTIL n=NulSym
250 END StudentVariableCheck ;
251
252
253 BEGIN
254 InitList(ErrantSymbols) ;
255 InitList(ErrantNames)
256 END M2Students.