Release coccinelle-0.1.8
[bpt/coccinelle.git] / parsing_c / token_helpers.ml
1 (* Yoann Padioleau
2 *
3 * Copyright (C) 2007, 2008 Ecole des Mines de Nantes
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License (GPL)
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * file license.txt for more details.
13 *)
14
15
16 open Common
17
18 open Parser_c
19
20 (*****************************************************************************)
21 (* Is_xxx, categories *)
22 (*****************************************************************************)
23
24 (* could define a type token_class = Comment | Ident | Operator | ...
25 * update: now token_c can maybe do that.
26 * but still, sometimes tokens belon to multiple classes. Could maybe
27 * return then a set of classes.
28 *)
29
30 let is_space = function
31 | TCommentSpace _ -> true
32 | TCommentNewline _ -> true
33 | _ -> false
34
35 let is_whitespace = is_space
36
37 let is_just_comment_or_space = function
38 | TComment _ -> true
39 | TCommentSpace _ -> true
40 | TCommentNewline _ -> true
41 | _ -> false
42 let is_real_comment = is_just_comment_or_space
43
44 let is_just_comment = function
45 | TComment _ -> true
46 | _ -> false
47
48
49
50
51 let is_comment = function
52 | TComment _
53 | TCommentSpace _ | TCommentNewline _
54 | TCommentCpp _
55 | TCommentMisc _ -> true
56 | _ -> false
57
58 (* coupling with comment_annotater_c.ml.
59 * In fact more tokens than comments are not in the ast, but
60 * they were usually temporally created by ocamllex and removed
61 * in parsing_hacks.
62 *)
63 let is_not_in_ast = is_comment
64
65 let is_fake_comment = function
66 | TCommentCpp _ | TCommentMisc _
67 -> true
68 | _ -> false
69
70 let is_not_comment x =
71 not (is_comment x)
72
73
74 (* ---------------------------------------------------------------------- *)
75
76 let is_cpp_instruction = function
77 | TInclude _
78 | TDefine _
79 | TIfdef _ | TIfdefelse _ | TIfdefelif _ | TEndif _
80 | TIfdefBool _ | TIfdefMisc _ | TIfdefVersion _
81 | TUndef _
82 | TCppDirectiveOther _
83 -> true
84 | _ -> false
85
86
87 let is_gcc_token = function
88 | Tasm _
89 | Tinline _
90 | Tattribute _
91 | Ttypeof _
92 -> true
93 | _ -> false
94
95
96
97
98 (* ---------------------------------------------------------------------- *)
99 let is_opar = function
100 | TOPar _ | TOParDefine _ -> true
101 | _ -> false
102
103 let is_cpar = function
104 | TCPar _ | TCParEOL _ -> true
105 | _ -> false
106
107
108 let is_obrace = function
109 | TOBrace _ | TOBraceDefineInit _ -> true
110 | _ -> false
111
112 let is_cbrace = function
113 | TCBrace _ -> true
114 | _ -> false
115
116
117
118
119 (* ---------------------------------------------------------------------- *)
120
121 (* end of file *)
122 let is_eof = function
123 | EOF x -> true
124 | _ -> false
125
126
127 (* end of macro *)
128 let is_eom = function
129 | TDefEOL _ -> true
130 | _ -> false
131
132 let is_statement = function
133 | Tfor _ | Tdo _ | Tif _ | Twhile _ | Treturn _
134 | Tbreak _ | Telse _ | Tswitch _ | Tcase _ | Tcontinue _
135 | Tgoto _
136 | TPtVirg _
137 | TMacroIterator _
138 -> true
139 | _ -> false
140
141 (* is_start_of_something is used in parse_c for error recovery, to find
142 * a synchronisation token.
143 *
144 * Would like to put TIdent or TDefine, TIfdef but they can be in the
145 * middle of a function, for instance with label:.
146 *
147 * Could put Typedefident but fired ? it would work in error recovery
148 * on the already_passed tokens, which has been already gone in the
149 * Parsing_hacks.lookahead machinery, but it will not work on the
150 * "next" tokens. But because the namespace for labels is different
151 * from namespace for ident/typedef, we can use the name for a typedef
152 * for a label and so dangerous to put Typedefident at true here.
153 *
154 * Can look in parser_c.output to know what can be at toplevel
155 * at the very beginning.
156 *)
157
158 let is_start_of_something = function
159 | Tchar _ | Tshort _ | Tint _ | Tdouble _ | Tfloat _ | Tlong _
160 | Tunsigned _ | Tsigned _ | Tvoid _
161 | Tauto _ | Tregister _ | Textern _ | Tstatic _
162 | Tconst _ | Tvolatile _
163 | Ttypedef _
164 | Tstruct _ | Tunion _ | Tenum _
165 -> true
166 | _ -> false
167
168
169
170 let is_binary_operator = function
171 | TOrLog _ | TAndLog _ | TOr _ | TXor _ | TAnd _
172 | TEqEq _ | TNotEq _ | TInf _ | TSup _ | TInfEq _ | TSupEq _
173 | TShl _ | TShr _
174 | TPlus _ | TMinus _ | TMul _ | TDiv _ | TMod _
175 -> true
176 | _ -> false
177
178 let is_stuff_taking_parenthized = function
179 | Tif _
180 | Twhile _
181 | Tswitch _
182 | Ttypeof _
183 | TMacroIterator _
184 -> true
185 | _ -> false
186
187
188 (* used in the algorithms for "10 most problematic errors" *)
189 let is_ident_like = function
190 | TIdent _
191 | TypedefIdent _
192 | TIdentDefine _
193 | TDefParamVariadic _
194
195 | TUnknown _
196
197 | TMacroAttr _
198 | TMacroAttrStorage _
199 | TMacroStmt _
200 | TMacroString _
201 | TMacroDecl _
202 | TMacroDeclConst _
203 | TMacroIterator _
204 -> true
205
206 | _ -> false
207
208
209 (*****************************************************************************)
210 (* Visitors *)
211 (*****************************************************************************)
212
213 (* Because ocamlyacc force us to do it that way. The ocamlyacc token
214 * cant be a pair of a sum type, it must be directly a sum type.
215 *)
216 let info_of_tok = function
217 | TString ((string, isWchar), i) -> i
218 | TChar ((string, isWchar), i) -> i
219 | TFloat ((string, floatType), i) -> i
220
221 | TAssign (assignOp, i) -> i
222
223 | TIdent (s, i) -> i
224 | TypedefIdent (s, i) -> i
225
226 | TInt (s, i) -> i
227
228 | TDefine (ii) -> ii
229 | TInclude (includes, filename, inifdef, i1) -> i1
230
231 | TUndef (s, ii) -> ii
232 | TCppDirectiveOther (ii) -> ii
233
234 | TIncludeStart (i1, inifdef) -> i1
235 | TIncludeFilename (s, i1) -> i1
236
237 | TDefEOL (i1) -> i1
238 | TOParDefine (i1) -> i1
239 | TIdentDefine (s, i) -> i
240 | TCppEscapedNewline (ii) -> ii
241 | TDefParamVariadic (s, i1) -> i1
242
243 | TCppConcatOp (ii) -> ii
244
245 | TOBraceDefineInit (i1) -> i1
246
247 | TUnknown (i) -> i
248
249 | TMacroIdentBuilder (s, i) -> i
250 | TMacroAttr (s, i) -> i
251 | TMacroAttrStorage (s, i) -> i
252 | TMacroStmt (s, i) -> i
253 | TMacroString (s, i) -> i
254 | TMacroDecl (s, i) -> i
255 | TMacroDeclConst (i) -> i
256 | TMacroIterator (s,i) -> i
257 (* | TMacroTop (s,i) -> i *)
258 | TCParEOL (i1) -> i1
259
260 | TAction (i) -> i
261
262 | TComment (i) -> i
263 | TCommentSpace (i) -> i
264 | TCommentNewline (i) -> i
265 | TCommentCpp (cppkind, i) -> i
266 | TCommentMisc (i) -> i
267
268 | TCommentSkipTagStart (i) -> i
269 | TCommentSkipTagEnd (i) -> i
270
271 | TIfdef (_, i) -> i
272 | TIfdefelse (_, i) -> i
273 | TIfdefelif (_, i) -> i
274 | TEndif (_, i) -> i
275 | TIfdefBool (b, _, i) -> i
276 | TIfdefMisc (b, _, i) -> i
277 | TIfdefVersion (b, _, i) -> i
278
279 | TOPar (i) -> i
280 | TCPar (i) -> i
281 | TOBrace (i) -> i
282 | TCBrace (i) -> i
283 | TOCro (i) -> i
284 | TCCro (i) -> i
285 | TDot (i) -> i
286 | TComma (i) -> i
287 | TPtrOp (i) -> i
288 | TInc (i) -> i
289 | TDec (i) -> i
290 | TEq (i) -> i
291 | TWhy (i) -> i
292 | TTilde (i) -> i
293 | TBang (i) -> i
294 | TEllipsis (i) -> i
295 | TDotDot (i) -> i
296 | TPtVirg (i) -> i
297 | TOrLog (i) -> i
298 | TAndLog (i) -> i
299 | TOr (i) -> i
300 | TXor (i) -> i
301 | TAnd (i) -> i
302 | TEqEq (i) -> i
303 | TNotEq (i) -> i
304 | TInf (i) -> i
305 | TSup (i) -> i
306 | TInfEq (i) -> i
307 | TSupEq (i) -> i
308 | TShl (i) -> i
309 | TShr (i) -> i
310 | TPlus (i) -> i
311 | TMinus (i) -> i
312 | TMul (i) -> i
313 | TDiv (i) -> i
314 | TMod (i) -> i
315
316 | Tchar (i) -> i
317 | Tshort (i) -> i
318 | Tint (i) -> i
319 | Tdouble (i) -> i
320 | Tfloat (i) -> i
321 | Tlong (i) -> i
322 | Tunsigned (i) -> i
323 | Tsigned (i) -> i
324 | Tvoid (i) -> i
325 | Tauto (i) -> i
326 | Tregister (i) -> i
327 | Textern (i) -> i
328 | Tstatic (i) -> i
329 | Tconst (i) -> i
330 | Tvolatile (i) -> i
331
332 | Trestrict (i) -> i
333
334 | Tstruct (i) -> i
335 | Tenum (i) -> i
336 | Ttypedef (i) -> i
337 | Tunion (i) -> i
338 | Tbreak (i) -> i
339 | Telse (i) -> i
340 | Tswitch (i) -> i
341 | Tcase (i) -> i
342 | Tcontinue (i) -> i
343 | Tfor (i) -> i
344 | Tdo (i) -> i
345 | Tif (i) -> i
346 | Twhile (i) -> i
347 | Treturn (i) -> i
348 | Tgoto (i) -> i
349 | Tdefault (i) -> i
350 | Tsizeof (i) -> i
351 | Tasm (i) -> i
352 | Tattribute (i) -> i
353 | Tinline (i) -> i
354 | Ttypeof (i) -> i
355
356 | EOF (i) -> i
357
358
359
360
361 (* used by tokens to complete the parse_info with filename, line, col infos *)
362 let visitor_info_of_tok f = function
363 | TString ((s, isWchar), i) -> TString ((s, isWchar), f i)
364 | TChar ((s, isWchar), i) -> TChar ((s, isWchar), f i)
365 | TFloat ((s, floatType), i) -> TFloat ((s, floatType), f i)
366 | TAssign (assignOp, i) -> TAssign (assignOp, f i)
367
368 | TIdent (s, i) -> TIdent (s, f i)
369 | TypedefIdent (s, i) -> TypedefIdent (s, f i)
370 | TInt (s, i) -> TInt (s, f i)
371
372 | TDefine (i1) -> TDefine(f i1)
373
374 | TUndef (s,i1) -> TUndef(s, f i1)
375 | TCppDirectiveOther (i1) -> TCppDirectiveOther(f i1)
376
377 | TInclude (includes, filename, inifdef, i1) ->
378 TInclude (includes, filename, inifdef, f i1)
379
380 | TIncludeStart (i1, inifdef) -> TIncludeStart (f i1, inifdef)
381 | TIncludeFilename (s, i1) -> TIncludeFilename (s, f i1)
382
383 | TCppEscapedNewline (i1) -> TCppEscapedNewline (f i1)
384 | TDefEOL (i1) -> TDefEOL (f i1)
385
386 | TCppConcatOp (ii) -> TCppConcatOp (f ii)
387
388 | TOParDefine (i1) -> TOParDefine (f i1)
389 | TIdentDefine (s, i) -> TIdentDefine (s, f i)
390
391 | TDefParamVariadic (s, i1) -> TDefParamVariadic (s, f i1)
392
393 | TOBraceDefineInit (i1) -> TOBraceDefineInit (f i1)
394
395
396 | TUnknown (i) -> TUnknown (f i)
397
398 | TMacroIdentBuilder (s, i) -> TMacroIdentBuilder (s, f i)
399 | TMacroAttr (s, i) -> TMacroAttr (s, f i)
400 | TMacroAttrStorage (s, i) -> TMacroAttrStorage (s, f i)
401 | TMacroStmt (s, i) -> TMacroStmt (s, f i)
402 | TMacroString (s, i) -> TMacroString (s, f i)
403 | TMacroDecl (s, i) -> TMacroDecl (s, f i)
404 | TMacroDeclConst (i) -> TMacroDeclConst (f i)
405 | TMacroIterator (s, i) -> TMacroIterator (s, f i)
406 (* | TMacroTop (s,i) -> TMacroTop (s,f i) *)
407 | TCParEOL (i) -> TCParEOL (f i)
408
409
410 | TAction (i) -> TAction (f i)
411
412 | TComment (i) -> TComment (f i)
413 | TCommentSpace (i) -> TCommentSpace (f i)
414 | TCommentNewline (i) -> TCommentNewline (f i)
415 | TCommentCpp (cppkind, i) -> TCommentCpp (cppkind, f i)
416 | TCommentMisc (i) -> TCommentMisc (f i)
417
418 | TCommentSkipTagStart (i) -> TCommentSkipTagStart (f i)
419 | TCommentSkipTagEnd (i) -> TCommentSkipTagEnd (f i)
420
421 | TIfdef (t, i) -> TIfdef (t, f i)
422 | TIfdefelse (t, i) -> TIfdefelse (t, f i)
423 | TIfdefelif (t, i) -> TIfdefelif (t, f i)
424 | TEndif (t, i) -> TEndif (t, f i)
425 | TIfdefBool (b, t, i) -> TIfdefBool (b, t, f i)
426 | TIfdefMisc (b, t, i) -> TIfdefMisc (b, t, f i)
427 | TIfdefVersion (b, t, i) -> TIfdefVersion (b, t, f i)
428
429 | TOPar (i) -> TOPar (f i)
430 | TCPar (i) -> TCPar (f i)
431 | TOBrace (i) -> TOBrace (f i)
432 | TCBrace (i) -> TCBrace (f i)
433 | TOCro (i) -> TOCro (f i)
434 | TCCro (i) -> TCCro (f i)
435 | TDot (i) -> TDot (f i)
436 | TComma (i) -> TComma (f i)
437 | TPtrOp (i) -> TPtrOp (f i)
438 | TInc (i) -> TInc (f i)
439 | TDec (i) -> TDec (f i)
440 | TEq (i) -> TEq (f i)
441 | TWhy (i) -> TWhy (f i)
442 | TTilde (i) -> TTilde (f i)
443 | TBang (i) -> TBang (f i)
444 | TEllipsis (i) -> TEllipsis (f i)
445 | TDotDot (i) -> TDotDot (f i)
446 | TPtVirg (i) -> TPtVirg (f i)
447 | TOrLog (i) -> TOrLog (f i)
448 | TAndLog (i) -> TAndLog (f i)
449 | TOr (i) -> TOr (f i)
450 | TXor (i) -> TXor (f i)
451 | TAnd (i) -> TAnd (f i)
452 | TEqEq (i) -> TEqEq (f i)
453 | TNotEq (i) -> TNotEq (f i)
454 | TInf (i) -> TInf (f i)
455 | TSup (i) -> TSup (f i)
456 | TInfEq (i) -> TInfEq (f i)
457 | TSupEq (i) -> TSupEq (f i)
458 | TShl (i) -> TShl (f i)
459 | TShr (i) -> TShr (f i)
460 | TPlus (i) -> TPlus (f i)
461 | TMinus (i) -> TMinus (f i)
462 | TMul (i) -> TMul (f i)
463 | TDiv (i) -> TDiv (f i)
464 | TMod (i) -> TMod (f i)
465 | Tchar (i) -> Tchar (f i)
466 | Tshort (i) -> Tshort (f i)
467 | Tint (i) -> Tint (f i)
468 | Tdouble (i) -> Tdouble (f i)
469 | Tfloat (i) -> Tfloat (f i)
470 | Tlong (i) -> Tlong (f i)
471 | Tunsigned (i) -> Tunsigned (f i)
472 | Tsigned (i) -> Tsigned (f i)
473 | Tvoid (i) -> Tvoid (f i)
474 | Tauto (i) -> Tauto (f i)
475 | Tregister (i) -> Tregister (f i)
476 | Textern (i) -> Textern (f i)
477 | Tstatic (i) -> Tstatic (f i)
478 | Tconst (i) -> Tconst (f i)
479 | Tvolatile (i) -> Tvolatile (f i)
480
481 | Trestrict (i) -> Trestrict (f i)
482
483 | Tstruct (i) -> Tstruct (f i)
484 | Tenum (i) -> Tenum (f i)
485 | Ttypedef (i) -> Ttypedef (f i)
486 | Tunion (i) -> Tunion (f i)
487 | Tbreak (i) -> Tbreak (f i)
488 | Telse (i) -> Telse (f i)
489 | Tswitch (i) -> Tswitch (f i)
490 | Tcase (i) -> Tcase (f i)
491 | Tcontinue (i) -> Tcontinue (f i)
492 | Tfor (i) -> Tfor (f i)
493 | Tdo (i) -> Tdo (f i)
494 | Tif (i) -> Tif (f i)
495 | Twhile (i) -> Twhile (f i)
496 | Treturn (i) -> Treturn (f i)
497 | Tgoto (i) -> Tgoto (f i)
498 | Tdefault (i) -> Tdefault (f i)
499 | Tsizeof (i) -> Tsizeof (f i)
500 | Tasm (i) -> Tasm (f i)
501 | Tattribute (i) -> Tattribute (f i)
502 | Tinline (i) -> Tinline (f i)
503 | Ttypeof (i) -> Ttypeof (f i)
504 | EOF (i) -> EOF (f i)
505
506
507 (*****************************************************************************)
508 (* Accessors *)
509 (*****************************************************************************)
510
511 let linecol_of_tok tok =
512 let info = info_of_tok tok in
513 Ast_c.line_of_info info, Ast_c.col_of_info info
514
515 let col_of_tok x = snd (linecol_of_tok x)
516 let line_of_tok x = fst (linecol_of_tok x)
517 let pos_of_tok x = Ast_c.opos_of_info (info_of_tok x)
518 let str_of_tok x = Ast_c.str_of_info (info_of_tok x)
519 let file_of_tok x = Ast_c.file_of_info (info_of_tok x)
520 let pinfo_of_tok x = Ast_c.pinfo_of_info (info_of_tok x)
521
522 let is_origin x =
523 match pinfo_of_tok x with Ast_c.OriginTok _ -> true | _ -> false
524 let is_expanded x =
525 match pinfo_of_tok x with Ast_c.ExpandedTok _ -> true | _ -> false
526 let is_fake x =
527 match pinfo_of_tok x with Ast_c.FakeTok _ -> true | _ -> false
528 let is_abstract x =
529 match pinfo_of_tok x with Ast_c.AbstractLineTok _ -> true | _ -> false
530
531 (*****************************************************************************)
532 (* Helpers *)
533 (*****************************************************************************)
534 let is_same_line_or_close line tok =
535 line_of_tok tok =|= line ||
536 line_of_tok tok =|= line - 1 ||
537 line_of_tok tok =|= line - 2
538