3 * Copyright (C) 2010, University of Copenhagen DIKU and INRIA.
4 * Copyright (C) 2007, 2008 Ecole des Mines de Nantes
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License (GPL)
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * file license.txt for more details.
18 module TH
= Token_helpers
19 module TV
= Token_views_c
20 module LP
= Lexer_parser
22 module Stat
= Parsing_stat
28 (*****************************************************************************)
29 (* Some debugging functions *)
30 (*****************************************************************************)
32 let pr2, pr2_once
= Common.mk_pr2_wrappers
Flag_parsing_c.verbose_parsing
35 if !Flag_parsing_c.debug_cpp
36 then Common.pr2_once
("CPP-" ^ s
)
39 let msg_gen cond is_known printer s
=
42 if not
(!Flag_parsing_c.filter_msg
)
48 (* In the following, there are some harcoded names of types or macros
49 * but they are not used by our heuristics! They are just here to
50 * enable to detect false positive by printing only the typedef/macros
51 * that we don't know yet. If we print everything, then we can easily
52 * get lost with too much verbose tracing information. So those
53 * functions "filter" some messages. So our heuristics are still good,
54 * there is no more (or not that much) hardcoded linux stuff.
60 | "u_char" | "u_short" | "u_int" | "u_long"
61 | "u8" | "u16" | "u32" | "u64"
62 | "s8" | "s16" | "s32" | "s64"
63 | "__u8" | "__u16" | "__u32" | "__u64"
74 | s
when s
=~
".*_t$" -> true
79 (* note: cant use partial application with let msg_typedef =
80 * because it would compute msg_typedef at compile time when
81 * the flag debug_typedef is always false
83 let msg_typedef s ii n
=
84 incr
Stat.nTypedefInfer
;
85 msg_gen (!Flag_parsing_c.debug_typedef
)
89 (Printf.sprintf
"TYPEDEF: promoting:(%d) %s on line %d" n s
90 (Ast_c.line_of_info ii
))
91 (*(Printf.sprintf "TYPEDEF: promoting: %s on line %d" s
92 (Ast_c.line_of_info ii))*)
96 let msg_maybe_dangereous_typedef s
=
97 if not
(is_known_typdef s
)
100 ("PB MAYBE: dangerous typedef inference, maybe not a typedef: " ^ s
)
104 let msg_declare_macro s
=
105 incr
Stat.nMacroDecl
;
106 msg_gen (!Flag_parsing_c.debug_cpp
)
109 | "DECLARE_MUTEX" | "DECLARE_COMPLETION" | "DECLARE_RWSEM"
110 | "DECLARE_WAITQUEUE" | "DECLARE_WAIT_QUEUE_HEAD"
111 | "DEFINE_SPINLOCK" | "DEFINE_TIMER"
112 | "DEVICE_ATTR" | "CLASS_DEVICE_ATTR" | "DRIVER_ATTR"
113 | "SENSOR_DEVICE_ATTR"
115 | "DECLARE_WORK" | "DECLARE_TASKLET"
116 | "PORT_ATTR_RO" | "PORT_PMA_ATTR"
121 | s when s =~ "^DECLARE_.*" -> true
122 | s when s =~ ".*_ATTR$" -> true
123 | s when s =~ "^DEFINE_.*" -> true
129 (fun s
-> pr2_cpp ("MACRO: found declare-macro: " ^ s
))
134 incr
Stat.nIteratorHeuristic
;
135 pr2_cpp ("MACRO: found foreach: " ^ s
)
139 let msg_debug_macro s =
140 pr2_cpp ("MACRO: found debug-macro: " ^ s)
144 let msg_macro_noptvirg s
=
145 incr
Stat.nMacroStmt
;
146 pr2_cpp ("MACRO: found macro with param noptvirg: " ^ s
)
148 let msg_macro_toplevel_noptvirg s
=
149 incr
Stat.nMacroStmt
;
150 pr2_cpp ("MACRO: found toplevel macro noptvirg: " ^ s
)
152 let msg_macro_noptvirg_single s
=
153 incr
Stat.nMacroStmt
;
154 pr2_cpp ("MACRO: found single-macro noptvirg: " ^ s
)
159 let msg_macro_higher_order s
=
160 incr
Stat.nMacroHigherOrder
;
161 msg_gen (!Flag_parsing_c.debug_cpp
)
171 (fun s
-> pr2_cpp ("MACRO: found higher ordre macro : " ^ s
))
175 let msg_stringification s
=
176 incr
Stat.nMacroString
;
177 msg_gen (!Flag_parsing_c.debug_cpp
)
185 (* s when s =~ ".*STR.*" -> true *)
189 (fun s
-> pr2_cpp ("MACRO: found string-macro " ^ s
))
192 let msg_stringification_params s
=
193 incr
Stat.nMacroString
;
194 pr2_cpp ("MACRO: string-macro with params : " ^ s
)
198 let msg_apply_known_macro s
=
199 incr
Stat.nMacroExpand
;
200 pr2_cpp ("MACRO: found known macro = " ^ s
)
202 let msg_apply_known_macro_hint s
=
203 incr
Stat.nMacroHint
;
204 pr2_cpp ("MACRO: found known macro hint = " ^ s
)
209 let msg_ifdef_bool_passing is_ifdef_positif
=
210 incr
Stat.nIfdefZero
; (* of Version ? *)
212 then pr2_cpp "commenting parts of a #if 1 or #if LINUX_VERSION"
213 else pr2_cpp "commenting a #if 0 or #if LINUX_VERSION or __cplusplus"
216 let msg_ifdef_mid_something () =
217 incr
Stat.nIfdefExprPassing
;
218 pr2_cpp "found ifdef-mid-something"
220 let msg_ifdef_funheaders () =
221 incr
Stat.nIfdefFunheader
;
224 let msg_ifdef_cparen_else () =
225 incr
Stat.nIfdefPassing
;
226 pr2_cpp("found ifdef-cparen-else")
229 let msg_attribute s
=
230 incr
Stat.nMacroAttribute
;
235 (*****************************************************************************)
236 (* The regexp and basic view definitions *)
237 (*****************************************************************************)
239 (* opti: better to built then once and for all, especially regexp_foreach *)
241 let regexp_macro = Str.regexp
245 let regexp_annot = Str.regexp
249 let regexp_declare = Str.regexp
253 let regexp_foreach = Str.regexp_case_fold
254 ".*\\(for_?each\\|for_?all\\|iterate\\|loop\\|walk\\|scan\\|each\\|for\\)"
256 let regexp_typedef = Str.regexp
259 let false_typedef = [
264 let ok_typedef s
= not
(List.mem s
false_typedef)
267 not
(s
==~
regexp_annot)
276 (*****************************************************************************)
278 (*****************************************************************************)
280 (* ------------------------------------------------------------------------- *)
281 (* the pair is the status of '()' and '{}', ex: (-1,0)
282 * if too much ')' and good '{}'
283 * could do for [] too ?
284 * could do for ',' if encounter ',' at "toplevel", not inside () or {}
285 * then if have ifdef, then certainly can lead to a problem.
287 let (count_open_close_stuff_ifdef_clause
: TV.ifdef_grouped list
-> (int * int))=
289 let cnt_paren, cnt_brace
= ref 0, ref 0 in
290 xs
+> TV.iter_token_ifdef
(fun x
->
292 | x
when TH.is_opar x
-> incr
cnt_paren
293 | TOBrace _
-> incr cnt_brace
294 | x
when TH.is_cpar x
-> decr
cnt_paren
295 | TCBrace _
-> decr cnt_brace
299 !cnt_paren, !cnt_brace
302 (* ------------------------------------------------------------------------- *)
303 let forLOOKAHEAD = 30
306 (* look if there is a '{' just after the closing ')', and handling the
307 * possibility to have nested expressions inside nested parenthesis
309 * todo: use indentation instead of premier(statement) ?
311 let rec is_really_foreach xs
=
312 let rec is_foreach_aux = function
314 | TCPar _
::TOBrace _
::xs
-> true, xs
315 (* the following attempts to handle the cases where there is a
316 single statement in the body of the loop. undoubtedly more
318 todo: premier(statement) - suivant(funcall)
320 | TCPar _
::TIdent _
::xs
-> true, xs
321 | TCPar _
::Tif _
::xs
-> true, xs
322 | TCPar _
::Twhile _
::xs
-> true, xs
323 | TCPar _
::Tfor _
::xs
-> true, xs
324 | TCPar _
::Tswitch _
::xs
-> true, xs
325 | TCPar _
::Treturn _
::xs
-> true, xs
328 | TCPar _
::xs
-> false, xs
330 let (_
, xs'
) = is_foreach_aux xs
in
332 | x
::xs
-> is_foreach_aux xs
334 is_foreach_aux xs
+> fst
337 (* ------------------------------------------------------------------------- *)
338 let set_ifdef_token_parenthize_info cnt x
=
341 | TIfdefelse
(tag
, _
)
342 | TIfdefelif
(tag
, _
)
345 | TIfdefBool
(_
, tag
, _
)
346 | TIfdefMisc
(_
, tag
, _
)
347 | TIfdefVersion
(_
, tag
, _
) ->
350 | _
-> raise
(Impossible
89)
354 let ifdef_paren_cnt = ref 0
357 let rec set_ifdef_parenthize_info xs
=
358 xs
+> List.iter
(function
359 | NotIfdefLine xs
-> ()
360 | Ifdefbool
(_
, xxs
, info_ifdef
)
361 | Ifdef
(xxs
, info_ifdef
) ->
363 incr
ifdef_paren_cnt;
364 let total_directives = List.length info_ifdef
in
366 info_ifdef
+> List.iter
(fun x
->
367 set_ifdef_token_parenthize_info (!ifdef_paren_cnt, total_directives)
369 xxs
+> List.iter
set_ifdef_parenthize_info
373 (*****************************************************************************)
374 (* The parsing hack for #define *)
375 (*****************************************************************************)
377 (* To parse macro definitions I need to do some tricks
378 * as some information can be get only at the lexing level. For instance
379 * the space after the name of the macro in '#define foo (x)' is meaningful
380 * but the grammar can not get this information. So define_ident below
381 * look at such space and generate a special TOpardefine. In a similar
382 * way macro definitions can contain some antislash and newlines
383 * and the grammar need to know where the macro ends (which is
384 * a line-level and so low token-level information). Hence the
385 * function 'define_line' below and the TDefEol.
387 * update: TDefEol is handled in a special way at different places,
388 * a little bit like EOF, especially for error recovery, so this
389 * is an important token that should not be retagged!
392 * ugly hack, a better solution perhaps would be to erase TDefEOL
393 * from the Ast and list of tokens in parse_c.
395 * note: I do a +1 somewhere, it's for the unparsing to correctly sync.
397 * note: can't replace mark_end_define by simply a fakeInfo(). The reason
398 * is where is the \n TCommentSpace. Normally there is always a last token
399 * to synchronize on, either EOF or the token of the next toplevel.
400 * In the case of the #define we got in list of token
401 * [TCommentSpace "\n"; TDefEOL] but if TDefEOL is a fakeinfo then we will
402 * not synchronize on it and so we will not print the "\n".
403 * A solution would be to put the TDefEOL before the "\n".
404 * (jll: tried to do this, see the comment "Put end of line..." below)
406 * todo?: could put a ExpandedTok for that ?
408 let mark_end_define ii
=
410 { Ast_c.pinfo
= Ast_c.OriginTok
{ (Ast_c.parse_info_of_info
ii) with
412 Common.charpos
= Ast_c.pos_of_info
ii + 1
414 cocci_tag
= ref Ast_c.emptyAnnot
;
415 annots_tag
= Token_annot.empty
;
416 comments_tag
= ref Ast_c.emptyComments
;
421 (* put the TDefEOL at the good place *)
422 let rec define_line_1 acc xs
=
426 let line = Ast_c.line_of_info
ii in
427 let acc = (TDefine
ii) :: acc in
428 define_line_2
acc line ii xs
430 let line = Ast_c.line_of_info
ii in
431 let acc = (TUndef
ii) :: acc in
432 define_line_2
acc line ii xs
433 | TCppEscapedNewline
ii::xs
->
434 pr2 ("SUSPICIOUS: a \\ character appears outside of a #define at");
435 pr2 (Ast_c.strloc_of_info
ii);
436 let acc = (TCommentSpace
ii) :: acc in
438 | x
::xs
-> define_line_1 (x
::acc) xs
440 and define_line_2
acc line lastinfo xs
=
443 (* should not happened, should meet EOF before *)
445 List.rev
(mark_end_define lastinfo
::acc)
447 let line'
= TH.line_of_tok x
in
448 let info = TH.info_of_tok x
in
452 let acc = (mark_end_define lastinfo
) :: acc in
453 let acc = (EOF
ii) :: acc in
455 | TCppEscapedNewline
ii ->
456 if (line'
<> line) then pr2 "PB: WEIRD: not same line number";
457 let acc = (TCommentSpace
ii) :: acc in
458 define_line_2
acc (line+1) info xs
461 then define_line_2
(x
::acc) line info xs
463 (* Put end of line token before the newline. A newline at least
464 must be there because the line changed and because we saw a
465 #define previously to get to this function at all *)
467 ((List.hd
acc)::(mark_end_define lastinfo
::(List.tl
acc)))
471 let rec define_ident acc xs
=
475 let acc = TUndef
ii :: acc in
477 TCommentSpace i1
::TIdent
(s
,i2
)::xs
->
478 let acc = (TCommentSpace i1
) :: acc in
479 let acc = (TIdentDefine
(s
,i2
)) :: acc in
482 pr2 "WEIRD: weird #define body";
486 let acc = TDefine
ii :: acc in
488 | TCommentSpace i1
::TIdent
(s
,i2
)::TOPar
(i3
)::xs
->
489 (* Change also the kind of TIdent to avoid bad interaction
490 * with other parsing_hack tricks. For instant if keep TIdent then
491 * the stringication algo can believe the TIdent is a string-macro.
492 * So simpler to change the kind of the ident too.
494 (* if TOParDefine sticked to the ident, then
495 * it's a macro-function. Change token to avoid ambiguity
496 * between #define foo(x) and #define foo (x)
498 let acc = (TCommentSpace i1
) :: acc in
499 let acc = (TIdentDefine
(s
,i2
)) :: acc in
500 let acc = (TOParDefine i3
) :: acc in
503 | TCommentSpace i1
::TIdent
(s
,i2
)::xs
->
504 let acc = (TCommentSpace i1
) :: acc in
505 let acc = (TIdentDefine
(s
,i2
)) :: acc in
508 (* bugfix: ident of macro (as well as params, cf below) can be tricky
509 * note, do we need to subst in the body of the define ? no cos
510 * here the issue is the name of the macro, as in #define inline,
511 * so obviously the name of this macro will not be used in its
512 * body (it would be a recursive macro, which is forbidden).
515 | TCommentSpace i1
::t
::xs
516 when TH.str_of_tok t
==~
Common.regexp_alpha
518 let s = TH.str_of_tok t
in
519 let ii = TH.info_of_tok t
in
520 pr2 (spf
"remapping: %s to an ident in macro name" s);
521 let acc = (TCommentSpace i1
) :: acc in
522 let acc = (TIdentDefine
(s,ii)) :: acc in
525 | TCommentSpace _
::_
::xs
527 pr2 "WEIRD: weird #define body";
531 let acc = x
:: acc in
536 let fix_tokens_define2 xs
=
537 define_ident [] (define_line_1 [] xs
)
539 let fix_tokens_define a
=
540 Common.profile_code
"C parsing.fix_define" (fun () -> fix_tokens_define2 a
)
546 (* ------------------------------------------------------------------------- *)
547 (* Other parsing hacks related to cpp, Include/Define hacks *)
548 (* ------------------------------------------------------------------------- *)
550 (* Sometimes I prefer to generate a single token for a list of things in the
551 * lexer so that if I have to passed them, like for passing TInclude then
552 * it's easy. Also if I don't do a single token, then I need to
553 * parse the rest which may not need special stuff, like detecting
554 * end of line which the parser is not really ready for. So for instance
555 * could I parse a #include <a/b/c/xxx.h> as 2 or more tokens ? just
556 * lex #include ? so then need recognize <a/b/c/xxx.h> as one token ?
557 * but this kind of token is valid only after a #include and the
558 * lexing and parsing rules are different for such tokens so not that
559 * easy to parse such things in parser_c.mly. Hence the following hacks.
561 * less?: maybe could get rid of this like I get rid of some of fix_define.
566 (* used to generate new token from existing one *)
567 let new_info posadd str
ii =
569 Ast_c.OriginTok
{ (Ast_c.parse_info_of_info
ii) with
570 charpos
= Ast_c.pos_of_info
ii + posadd
;
572 column
= Ast_c.col_of_info
ii + posadd
;
574 (* must generate a new ref each time, otherwise share *)
575 cocci_tag
= ref Ast_c.emptyAnnot
;
576 annots_tag
= Token_annot.empty
;
577 comments_tag
= ref Ast_c.emptyComments
;
581 let rec comment_until_defeol xs
=
584 (* job not done in Cpp_token_c.define_parse ? *)
585 failwith
"cant find end of define token TDefEOL"
588 | Parser_c.TDefEOL i
->
589 Parser_c.TCommentCpp
(Token_c.CppDirective
, TH.info_of_tok x
)
593 (* bugfix: otherwise may lose a TComment token *)
594 if TH.is_real_comment
x
596 else Parser_c.TCommentCpp
(Token_c.CppPassingNormal
(*good?*), TH.info_of_tok
x)
598 x'
::comment_until_defeol xs
601 let drop_until_defeol xs
=
603 (Common.drop_until
(function Parser_c.TDefEOL _
-> true | _
-> false) xs
)
607 (* ------------------------------------------------------------------------- *)
608 (* returns a pair (replaced token, list of next tokens) *)
609 (* ------------------------------------------------------------------------- *)
611 let tokens_include (info, includes
, filename
, inifdef
) =
612 Parser_c.TIncludeStart
(Ast_c.rewrap_str includes
info, inifdef
),
613 [Parser_c.TIncludeFilename
614 (filename
, (new_info (String.length includes
) filename
info))
620 (*****************************************************************************)
621 (* CPP handling: macros, ifdefs, macros defs *)
622 (*****************************************************************************)
624 (* ------------------------------------------------------------------------- *)
625 (* special skip_start skip_end handling *)
626 (* ------------------------------------------------------------------------- *)
628 (* note: after this normally the token list should not contain any more the
629 * TCommentSkipTagStart and End tokens.
631 let rec commentize_skip_start_to_end xs
=
636 | {tok
= TCommentSkipTagStart
info} ->
638 let (before
, x2
, after
) =
639 xs
+> Common.split_when
(function
640 | {tok
= TCommentSkipTagEnd _
} -> true
644 let topass = x::x2
::before
in
645 topass +> List.iter
(fun tok
->
646 TV.set_as_comment
Token_c.CppPassingExplicit tok
648 commentize_skip_start_to_end after
650 failwith
"could not find end of skip_start special comment"
652 | {tok
= TCommentSkipTagEnd
info} ->
653 failwith
"found skip_end comment but no skip_start"
655 commentize_skip_start_to_end xs
661 (* ------------------------------------------------------------------------- *)
662 (* ifdef keeping/passing *)
663 (* ------------------------------------------------------------------------- *)
665 (* #if 0, #if 1, #if LINUX_VERSION handling *)
666 let rec find_ifdef_bool xs
=
667 xs
+> List.iter
(function
668 | NotIfdefLine _
-> ()
669 | Ifdefbool
(is_ifdef_positif
, xxs
, info_ifdef_stmt
) ->
671 msg_ifdef_bool_passing is_ifdef_positif
;
674 | [] -> raise
(Impossible
90)
675 | firstclause
::xxs
->
677 List.iter
(TV.save_as_comment
(fun x -> Token_c.CppIfDirective
x));
680 then xxs
+> List.iter
681 (iter_token_ifdef
(TV.set_as_comment
Token_c.CppPassingNormal
))
684 iter_token_ifdef
(TV.set_as_comment
Token_c.CppPassingNormal
);
685 (match List.rev xxs
with
689 (iter_token_ifdef
(TV.set_as_comment
Token_c.CppPassingNormal
))
690 | [] -> (* not #else *) ()
695 | Ifdef
(xxs
, info_ifdef_stmt
) -> xxs
+> List.iter
find_ifdef_bool
700 let thresholdIfdefSizeMid = 6
702 (* infer ifdef involving not-closed expressions/statements *)
703 let rec find_ifdef_mid xs
=
704 xs
+> List.iter
(function
705 | NotIfdefLine _
-> ()
706 | Ifdef
(xxs
, info_ifdef_stmt
) ->
708 | [] -> raise
(Impossible
91)
710 | first
::second
::rest
->
711 (* don't analyse big ifdef *)
712 if xxs
+> List.for_all
713 (fun xs
-> List.length xs
<= thresholdIfdefSizeMid) &&
714 (* don't want nested ifdef *)
715 xxs
+> List.for_all
(fun xs
->
717 (function NotIfdefLine _
-> true | _
-> false)
721 let counts = xxs
+> List.map count_open_close_stuff_ifdef_clause
in
722 let cnt1, cnt2
= List.hd
counts in
723 if cnt1 <> 0 || cnt2
<> 0 &&
724 counts +> List.for_all
(fun x -> x =*= (cnt1, cnt2
))
726 if counts +> List.exists (fun (cnt1, cnt2) ->
727 cnt1 <> 0 || cnt2 <> 0
731 msg_ifdef_mid_something();
733 (* keep only first, treat the rest as comment *)
736 (TV.save_as_comment
(function x -> Token_c.CppIfDirective
x));
737 (second
::rest
) +> List.iter
738 (iter_token_ifdef
(TV.set_as_comment
Token_c.CppPassingCosWouldGetError
));
742 List.iter
find_ifdef_mid xxs
744 (* no need complex analysis for ifdefbool *)
745 | Ifdefbool
(_
, xxs
, info_ifdef_stmt
) ->
746 List.iter
find_ifdef_mid xxs
752 let thresholdFunheaderLimit = 4
754 (* ifdef defining alternate function header, type *)
755 let rec find_ifdef_funheaders = function
757 | NotIfdefLine _
::xs
-> find_ifdef_funheaders xs
759 (* ifdef-funheader if ifdef with 2 lines and a '{' in next line *)
761 ([(NotIfdefLine
(({col
= 0} as _xline1
)::line1
))::ifdefblock1
;
762 (NotIfdefLine
(({col
= 0} as xline2
)::line2
))::ifdefblock2
765 ::NotIfdefLine
(({tok
= TOBrace i
; col
= 0})::line3
)
767 when List.length ifdefblock1
<= thresholdFunheaderLimit &&
768 List.length ifdefblock2
<= thresholdFunheaderLimit
770 find_ifdef_funheaders xs
;
772 msg_ifdef_funheaders ();
774 List.iter
(TV.save_as_comment
(fun x -> Token_c.CppIfDirective
x));
775 let all_toks = [xline2
] @ line2
in
776 all_toks +> List.iter
(TV.set_as_comment
Token_c.CppPassingCosWouldGetError
) ;
777 ifdefblock2
+> iter_token_ifdef
(TV.set_as_comment
Token_c.CppPassingCosWouldGetError
);
779 (* ifdef with nested ifdef *)
781 ([[NotIfdefLine
(({col
= 0} as _xline1
)::line1
)];
783 ([[NotIfdefLine
(({col
= 0} as xline2
)::line2
)];
784 [NotIfdefLine
(({col
= 0} as xline3
)::line3
)];
790 ::NotIfdefLine
(({tok
= TOBrace i
; col
= 0})::line4
)
793 find_ifdef_funheaders xs
;
795 msg_ifdef_funheaders ();
797 List.iter
(TV.save_as_comment
(fun x -> Token_c.CppIfDirective
x));
799 List.iter
(TV.save_as_comment
(fun x -> Token_c.CppIfDirective
x));
800 let all_toks = [xline2
;xline3
] @ line2
@ line3
in
801 all_toks +> List.iter
(TV.set_as_comment
Token_c.CppPassingCosWouldGetError
);
803 (* ifdef with elseif *)
805 ([[NotIfdefLine
(({col
= 0} as _xline1
)::line1
)];
806 [NotIfdefLine
(({col
= 0} as xline2
)::line2
)];
807 [NotIfdefLine
(({col
= 0} as xline3
)::line3
)];
810 ::NotIfdefLine
(({tok
= TOBrace i
; col
= 0})::line4
)
813 find_ifdef_funheaders xs
;
815 msg_ifdef_funheaders ();
817 List.iter
(TV.save_as_comment
(fun x -> Token_c.CppIfDirective
x));
818 let all_toks = [xline2
;xline3
] @ line2
@ line3
in
819 all_toks +> List.iter
(TV.set_as_comment
Token_c.CppPassingCosWouldGetError
)
822 | Ifdef
(xxs
,info_ifdef_stmt
)::xs
823 | Ifdefbool
(_
, xxs
,info_ifdef_stmt
)::xs
->
824 List.iter
find_ifdef_funheaders xxs
;
825 find_ifdef_funheaders xs
830 let rec adjust_inifdef_include xs
=
831 xs
+> List.iter
(function
832 | NotIfdefLine _
-> ()
833 | Ifdef
(xxs
, info_ifdef_stmt
) | Ifdefbool
(_
, xxs
, info_ifdef_stmt
) ->
834 xxs
+> List.iter
(iter_token_ifdef
(fun tokext
->
835 match tokext
.tok
with
836 | Parser_c.TInclude
(s1
, s2
, inifdef_ref
, ii) ->
848 let rec find_ifdef_cparen_else xs
=
850 xs
+> List.iter
(function
851 | NotIfdefLine _
-> ()
852 | Ifdef
(xxs
, info_ifdef_stmt
) ->
854 | [] -> raise
(Impossible
92)
856 | first
::second
::rest
->
858 (* found a closing ')' just after the #else *)
860 (* Too bad ocaml does not support better list pattern matching
861 * a la Prolog-III where can match the end of lists.
864 if List.length first
= 0 then false
866 let last_line = Common.last first
in
869 if List.length xs
= 0 then false
871 let last_tok = Common.last xs
in
872 TH.is_cpar
last_tok.tok
873 | Ifdef _
| Ifdefbool _
-> false
875 if condition then begin
876 msg_ifdef_cparen_else();
878 (* keep only first, treat the rest as comment *)
880 List.iter
(TV.save_as_comment
(fun x -> Token_c.CppIfDirective
x));
881 (second
::rest
) +> List.iter
882 (iter_token_ifdef
(TV.set_as_comment
Token_c.CppPassingCosWouldGetError
));
888 (* no need complex analysis for ifdefbool *)
889 | Ifdefbool
(_
, xxs
, info_ifdef_stmt
) ->
895 (* ------------------------------------------------------------------------- *)
896 (* cpp-builtin part2, macro, using standard.h or other defs *)
897 (* ------------------------------------------------------------------------- *)
899 (* now in cpp_token_c.ml *)
901 (* ------------------------------------------------------------------------- *)
902 (* stringification *)
903 (* ------------------------------------------------------------------------- *)
905 let rec find_string_macro_paren xs
=
908 | Parenthised
(xxs
, info_parens
)::xs
->
909 xxs
+> List.iter
(fun xs
->
911 (function PToken
({tok
= (TString _
| TMacroString _
)}) -> true | _
-> false) &&
913 (function PToken
({tok
= (TString _
| TMacroString _
)}) | PToken
({tok
= TIdent _
}) ->
916 xs
+> List.iter
(fun tok
->
918 | PToken
({tok
= TIdent
(s,_
)} as id
) ->
920 msg_stringification s;
921 id
.tok
<- TMacroString
(s, TH.info_of_tok id
.tok
);
925 find_string_macro_paren xs
927 find_string_macro_paren xs
929 find_string_macro_paren xs
932 (* ------------------------------------------------------------------------- *)
934 (* ------------------------------------------------------------------------- *)
936 (* don't forget to recurse in each case *)
937 let rec find_macro_paren xs
=
942 | PToken
({tok
= Tattribute _
} as id
)
943 ::Parenthised
(xxs
,info_parens
)
946 pr2_cpp ("MACRO: __attribute detected ");
947 [Parenthised
(xxs
, info_parens
)] +>
948 iter_token_paren
(TV.set_as_comment
Token_c.CppAttr
);
949 TV.set_as_comment
Token_c.CppAttr id
;
952 | PToken
({tok
= TattributeNoarg _
} as id
)
955 pr2_cpp ("MACRO: __attributenoarg detected ");
956 TV.set_as_comment
Token_c.CppAttr id
;
960 (* attribute cpp, __xxx id *)
961 | PToken
({tok
= TIdent
(s,i1
)} as id
)
962 ::PToken
({tok
= TIdent
(s2
, i2
)} as id2
)
963 ::xs
when s ==~
regexp_annot
966 id
.tok
<- TMacroAttr
(s, i1
);
967 find_macro_paren ((PToken id2
)::xs
); (* recurse also on id2 ? *)
969 (* attribute cpp, id __xxx *)
970 | PToken
({tok
= TIdent
(s,i1
)} as _id
)
971 ::PToken
({tok
= TIdent
(s2
, i2
)} as id2
)
972 ::xs
when s2
==~
regexp_annot && (not
(s ==~
regexp_typedef))
975 id2
.tok
<- TMacroAttr
(s2
, i2
);
978 | PToken
({tok
= (Tstatic _
| Textern _
)} as tok1
)
979 ::PToken
({tok
= TIdent
(s,i1
)} as attr
)
980 ::xs
when s ==~
regexp_annot
982 pr2_cpp ("storage attribute: " ^
s);
983 attr
.tok
<- TMacroAttrStorage
(s,i1
);
984 (* recurse, may have other storage attributes *)
985 find_macro_paren (PToken
(tok1
)::xs
)
989 (* storage attribute *)
990 | PToken
({tok
= (Tstatic _
| Textern _
)} as tok1
)
991 ::PToken
({tok
= TMacroAttr
(s,i1
)} as attr
)::xs
993 pr2_cpp ("storage attribute: " ^
s);
994 attr
.tok
<- TMacroAttrStorage
(s,i1
);
995 (* recurse, may have other storage attributes *)
996 find_macro_paren (PToken
(tok1
)::xs
)
1001 * the order of the matching clause is important
1005 (* string macro with params, before case *)
1006 | PToken
({tok
= (TString _
| TMacroString _
)})::PToken
({tok
= TIdent
(s,_
)} as id
)
1007 ::Parenthised
(xxs
, info_parens
)
1010 msg_stringification_params s;
1011 id
.tok
<- TMacroString
(s, TH.info_of_tok id
.tok
);
1012 [Parenthised
(xxs
, info_parens
)] +>
1013 iter_token_paren
(TV.set_as_comment
Token_c.CppMacro
);
1017 | PToken
({tok
= TIdent
(s,_
)} as id
)
1018 ::Parenthised
(xxs
, info_parens
)
1019 ::PToken
({tok
= (TString _
| TMacroString _
)})
1022 msg_stringification_params s;
1023 id
.tok
<- TMacroString
(s, TH.info_of_tok id
.tok
);
1024 [Parenthised
(xxs
, info_parens
)] +>
1025 iter_token_paren
(TV.set_as_comment
Token_c.CppMacro
);
1029 (* for the case where the string is not inside a funcall, but
1030 * for instance in an initializer.
1033 (* string macro variable, before case *)
1034 | PToken
({tok
= (TString _
| TMacroString _
)})::PToken
({tok
= TIdent
(s,_
)} as id
)
1035 ::xs
when not
!Flag.c_plus_plus
->
1037 msg_stringification s;
1038 id
.tok
<- TMacroString
(s, TH.info_of_tok id
.tok
);
1042 | PToken
({tok
= TIdent
(s,_
)} as id
)
1043 ::PToken
({tok
= (TString _
| TMacroString _
)})
1046 msg_stringification s;
1047 id
.tok
<- TMacroString
(s, TH.info_of_tok id
.tok
);
1055 | (PToken
x)::xs
-> find_macro_paren xs
1056 | (Parenthised
(xxs
, info_parens
))::xs
->
1057 xxs
+> List.iter
find_macro_paren;
1064 (* don't forget to recurse in each case *)
1065 let rec find_macro_lineparen xs
=
1069 (* linuxext: ex: static [const] DEVICE_ATTR(); *)
1072 [PToken
({tok
= Tstatic _
});
1073 PToken
({tok
= TIdent
(s,_
)} as macro
);
1074 Parenthised
(xxs
,info_parens
);
1075 PToken
({tok
= TPtVirg _
});
1079 when (s ==~
regexp_macro) ->
1081 msg_declare_macro s;
1082 let info = TH.info_of_tok macro
.tok
in
1083 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1085 find_macro_lineparen (xs
)
1087 (* the static const case *)
1090 [PToken
({tok
= Tstatic _
});
1091 PToken
({tok
= Tconst _
} as const
);
1092 PToken
({tok
= TIdent
(s,_
)} as macro
);
1093 Parenthised
(xxs
,info_parens
);
1094 PToken
({tok
= TPtVirg _
});
1100 when (s ==~
regexp_macro) ->
1102 msg_declare_macro s;
1103 let info = TH.info_of_tok macro
.tok
in
1104 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1106 (* need retag this const, otherwise ambiguity in grammar
1107 21: shift/reduce conflict (shift 121, reduce 137) on Tconst
1108 decl2 : Tstatic . TMacroDecl TOPar argument_list TCPar ...
1109 decl2 : Tstatic . Tconst TMacroDecl TOPar argument_list TCPar ...
1110 storage_class_spec : Tstatic . (137)
1112 const
.tok
<- TMacroDeclConst
(TH.info_of_tok const
.tok
);
1114 find_macro_lineparen (xs
)
1117 (* same but without trailing ';'
1119 * I do not put the final ';' because it can be on a multiline and
1120 * because of the way mk_line is coded, we will not have access to
1121 * this ';' on the next line, even if next to the ')' *)
1123 ([PToken
({tok
= Tstatic _
});
1124 PToken
({tok
= TIdent
(s,_
)} as macro
);
1125 Parenthised
(xxs
,info_parens
);
1129 when s ==~
regexp_macro ->
1131 msg_declare_macro s;
1132 let info = TH.info_of_tok macro
.tok
in
1133 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1135 find_macro_lineparen (xs
)
1138 (* on multiple lines *)
1141 (PToken
({tok
= Tstatic _
})::[]
1145 [PToken
({tok
= TIdent
(s,_
)} as macro
);
1146 Parenthised
(xxs
,info_parens
);
1147 PToken
({tok
= TPtVirg _
});
1152 when (s ==~
regexp_macro) ->
1154 msg_declare_macro s;
1155 let info = TH.info_of_tok macro
.tok
in
1156 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1158 find_macro_lineparen (xs
)
1161 | (Line
(* initializer case *)
1163 PToken
({tok
= Tstatic _
}) ::
1164 PToken
({tok
= TIdent
(s,_
)} as macro
) ::
1165 Parenthised
(xxs
,info_parens
) ::
1166 PToken
({tok
= TEq _
}) :: rest
1169 when (s ==~
regexp_macro) ->
1171 msg_declare_macro s;
1172 let info = TH.info_of_tok macro
.tok
in
1173 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1175 (* continue with the rest of the line *)
1176 find_macro_lineparen ((Line
(rest
))::xs
)
1179 | (Line
(* multi-line initializer case *)
1181 (PToken
({tok
= Tstatic _
})::[]
1185 PToken
({tok
= Tstatic _
}) ::
1186 PToken
({tok
= TIdent
(s,_
)} as macro
) ::
1187 Parenthised
(xxs
,info_parens
) ::
1188 PToken
({tok
= TEq _
}) :: rest
1191 when (s ==~
regexp_macro) ->
1193 msg_declare_macro s;
1194 let info = TH.info_of_tok macro
.tok
in
1195 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1197 (* continue with the rest of the line *)
1198 find_macro_lineparen ((Line
(rest
))::xs
)
1201 (* linuxext: ex: DECLARE_BITMAP();
1203 * Here I use regexp_declare and not regexp_macro because
1204 * Sometimes it can be a FunCallMacro such as DEBUG(foo());
1205 * Here we don't have the preceding 'static' so only way to
1206 * not have positive is to restrict to .*DECLARE.* macros.
1208 * but there is a grammar rule for that, so don't need this case anymore
1209 * unless the parameter of the DECLARE_xxx are weird and can not be mapped
1210 * on a argument_list
1214 ([PToken
({tok
= TIdent
(s,_
)} as macro
);
1215 Parenthised
(xxs
,info_parens
);
1216 PToken
({tok
= TPtVirg _
});
1220 when (s ==~
regexp_declare) ->
1222 msg_declare_macro s;
1223 let info = TH.info_of_tok macro
.tok
in
1224 macro
.tok
<- TMacroDecl
(Ast_c.str_of_info
info, info);
1226 find_macro_lineparen (xs
)
1232 * Could also transform the TIdent in a TMacroTop but can have false
1233 * positive, so easier to just change the TCPar and so just solve
1234 * the end-of-stream pb of ocamlyacc
1237 ([PToken
({tok
= TIdent
(s,ii); col
= col1
; where
= ctx
} as _macro
);
1238 Parenthised
(xxs
,info_parens
);
1241 ::xs
when col1
=|= 0
1244 (* to reduce number of false positive *)
1246 | (Line
(PToken
({col
= col2
} as other
)::restline2
))::_
->
1247 TH.is_eof other
.tok
|| (col2
=|= 0 &&
1248 (match other
.tok
with
1249 | TOBrace _
-> false (* otherwise would match funcdecl *)
1250 | TCBrace _
when ctx
<> InFunction
-> false
1254 | tok
when TH.is_binary_operator tok
-> false
1265 msg_macro_toplevel_noptvirg s;
1266 (* just to avoid the end-of-stream pb of ocamlyacc *)
1267 let tcpar = Common.last info_parens
in
1268 tcpar.tok
<- TCParEOL
(TH.info_of_tok
tcpar.tok
);
1270 (*macro.tok <- TMacroTop (s, TH.info_of_tok macro.tok);*)
1274 find_macro_lineparen (xs
)
1278 (* macro with parameters
1283 ([PToken
({tok
= TIdent
(s,ii); col
= col1
; where
= ctx
} as macro
);
1284 Parenthised
(xxs
,info_parens
);
1288 (PToken
({col
= col2
} as other
)::restline2
1291 (* when s ==~ regexp_macro *)
1293 (* This can give a false positive for K&R functions if the function
1294 name is in the same column as the first parameter declaration. *)
1297 (match other
.tok
with
1298 | TOBrace _
-> false (* otherwise would match funcdecl *)
1299 | TCBrace _
when ctx
<> InFunction
-> false
1303 | tok
when TH.is_binary_operator tok
-> false
1310 (match other
.tok
, restline2
with
1311 | TCBrace _
, _
when ctx
=*= InFunction
-> true
1312 | Treturn _
, _
-> true
1314 | Telse _
, _
-> true
1316 (* case of label, usually put in first line *)
1317 | TIdent _
, (PToken
({tok
= TDotDot _
}))::_
->
1329 if col1
=|= 0 then ()
1331 msg_macro_noptvirg s;
1332 macro
.tok
<- TMacroStmt
(s, TH.info_of_tok macro
.tok
);
1333 [Parenthised
(xxs
, info_parens
)] +>
1334 iter_token_paren
(TV.set_as_comment
Token_c.CppMacro
);
1337 find_macro_lineparen (line2
::xs
)
1339 (* linuxext:? single macro
1344 * todo: factorize code with previous rule ?
1347 ([PToken
({tok
= TIdent
(s,ii); col
= col1
; where
= ctx
} as macro
);
1351 (PToken
({col
= col2
} as other
)::restline2
1354 (* when s ==~ regexp_macro *)
1358 col1
<> 0 && (* otherwise can match typedef of fundecl*)
1359 (match other
.tok
with
1360 | TPtVirg _
-> false
1362 | TCBrace _
when ctx
<> InFunction
-> false
1363 | tok
when TH.is_binary_operator tok
-> false
1368 (match other
.tok
with
1369 | TCBrace _
when ctx
=*= InFunction
-> true
1379 msg_macro_noptvirg_single s;
1380 macro
.tok
<- TMacroStmt
(s, TH.info_of_tok macro
.tok
);
1382 find_macro_lineparen (line2
::xs
)
1385 find_macro_lineparen xs
1389 (* ------------------------------------------------------------------------- *)
1390 (* define tobrace init *)
1391 (* ------------------------------------------------------------------------- *)
1393 let rec find_define_init_brace_paren xs
=
1398 (* mainly for firefox *)
1399 | (PToken
{tok
= TDefine _
})
1400 ::(PToken
{tok
= TIdentDefine
(s,_
)})
1401 ::(PToken
({tok
= TOBrace i1
} as tokbrace
))
1406 match tok2
.tok
, tok3
.tok
with
1407 | TInt _
, TComma _
-> true
1408 | TString _
, TComma _
-> true
1409 | TIdent _
, TComma _
-> true
1415 pr2_cpp("found define initializer: " ^
s);
1416 tokbrace
.tok
<- TOBraceDefineInit i1
;
1421 (* mainly for linux, especially in sound/ *)
1422 | (PToken
{tok
= TDefine _
})
1423 ::(PToken
{tok
= TIdentDefine
(s,_
)})
1424 ::(Parenthised
(xxx
, info_parens
))
1425 ::(PToken
({tok
= TOBrace i1
} as tokbrace
))
1430 match tok2
.tok
, tok3
.tok
with
1431 | TInt _
, TComma _
-> true
1432 | TDot _
, TIdent _
-> true
1433 | TIdent _
, TComma _
-> true
1439 pr2_cpp("found define initializer with param: " ^
s);
1440 tokbrace
.tok
<- TOBraceDefineInit i1
;
1448 | (PToken
x)::xs
-> aux xs
1449 | (Parenthised
(xxs
, info_parens
))::xs
->
1450 (* not need for tobrace init:
1451 * xxs +> List.iter aux;
1458 (* ------------------------------------------------------------------------- *)
1460 (* ------------------------------------------------------------------------- *)
1462 (* obsolete now with macro expansion ? get some regression if comment.
1463 * todo: if do bad decision here, then it can influence other phases
1464 * and make it hard to parse. So maybe when have a parse error, should
1465 * undo some of the guess those heuristics have done, and restore
1466 * the original token value.
1469 let rec find_actions = function
1472 | PToken
({tok
= TIdent
(s,ii)})
1473 ::Parenthised
(xxs
,info_parens
)
1476 xxs
+> List.iter
find_actions;
1477 let modified = find_actions_params xxs
in
1479 then msg_macro_higher_order s
1484 and find_actions_params xxs
=
1485 xxs
+> List.fold_left
(fun acc xs
->
1486 let toks = tokens_of_paren xs
in
1487 if toks +> List.exists
(fun x -> TH.is_statement
x.tok
)
1488 (* undo: && List.length toks > 1
1489 * good for sparse, not good for linux
1492 xs
+> iter_token_paren
(fun x ->
1495 (* certainly because paren detection had a pb because of
1496 * some ifdef-exp. Do similar additional checking than
1497 * what is done in TV.set_as_comment.
1499 pr2 "PB: weird, I try to tag an EOF token as an action"
1501 (* cf tests-bis/no_cpar_macro.c *)
1504 pr2 "PB: weird, I try to tag an EOM token as an action"
1506 x.tok
<- TAction
(TH.info_of_tok
x.tok
);
1515 (* ------------------------------------------------------------------------- *)
1516 (* main fix cpp function *)
1517 (* ------------------------------------------------------------------------- *)
1519 let filter_cpp_stuff xs
=
1523 | tok
when TH.is_comment tok
-> false
1524 (* don't want drop the define, or if drop, have to drop
1525 * also its body otherwise the line heuristics may be lost
1526 * by not finding the TDefine in column 0 but by finding
1527 * a TDefineIdent in a column > 0
1529 | Parser_c.TDefine _
-> true
1530 | tok
when TH.is_cpp_instruction tok
-> false
1535 let insert_virtual_positions l
=
1536 let strlen x = String.length
(Ast_c.str_of_info
x) in
1537 let rec loop prev offset
acc = function
1540 let ii = TH.info_of_tok
x in
1542 TH.visitor_info_of_tok
(function ii -> Ast_c.rewrap_pinfo pi
ii) x in
1543 match Ast_c.pinfo_of_info
ii with
1544 Ast_c.OriginTok pi
->
1545 let prev = Ast_c.parse_info_of_info
ii in
1546 loop prev (strlen ii) (x::acc) xs
1547 | Ast_c.ExpandedTok
(pi
,_
) ->
1548 let x'
= inject (Ast_c.ExpandedTok
(pi
,(prev,offset
))) in
1549 loop prev (offset
+ (strlen ii)) (x'
::acc) xs
1550 | Ast_c.FakeTok
(s,_
) ->
1551 let x'
= inject (Ast_c.FakeTok
(s,(prev,offset
))) in
1552 loop prev (offset
+ (strlen ii)) (x'
::acc) xs
1553 | Ast_c.AbstractLineTok _
-> failwith
"abstract not expected" in
1554 let rec skip_fake = function
1557 let ii = TH.info_of_tok
x in
1558 match Ast_c.pinfo_of_info
ii with
1559 | Ast_c.OriginTok pi
->
1560 let prev = Ast_c.parse_info_of_info
ii in
1561 let res = loop prev (strlen ii) [] xs
in
1563 | _
-> x::skip_fake xs
in
1566 (* ------------------------------------------------------------------------- *)
1568 let fix_tokens_cpp2 ~macro_defs tokens
=
1569 let tokens2 = ref (tokens
+> Common.acc_map
TV.mk_token_extended
) in
1572 (* the order is important, if you put the action heuristic first,
1573 * then because of ifdef, can have not closed paren
1574 * and so may believe that higher order macro
1575 * and it will eat too much tokens. So important to do
1578 * I recompute multiple times cleaner cos the mutable
1579 * can have be changed and so may have more comments
1580 * in the token original list.
1584 commentize_skip_start_to_end !tokens2;
1587 let cleaner = !tokens2 +> List.filter
(fun x ->
1588 (* is_comment will also filter the TCommentCpp created in
1589 * commentize_skip_start_to_end *)
1590 not
(TH.is_comment
x.tok
) (* could filter also #define/#include *)
1592 let ifdef_grouped = TV.mk_ifdef
cleaner in
1593 set_ifdef_parenthize_info ifdef_grouped;
1595 find_ifdef_funheaders ifdef_grouped;
1596 find_ifdef_bool ifdef_grouped;
1597 find_ifdef_mid ifdef_grouped;
1598 (* change order ? maybe cparen_else heuristic make some of the funheaders
1599 * heuristics irrelevant ?
1601 find_ifdef_cparen_else ifdef_grouped;
1602 adjust_inifdef_include ifdef_grouped;
1606 let cleaner = !tokens2 +> filter_cpp_stuff in
1608 let paren_grouped = TV.mk_parenthised
cleaner in
1609 Cpp_token_c.apply_macro_defs
1610 ~
msg_apply_known_macro
1611 ~
msg_apply_known_macro_hint
1612 macro_defs
paren_grouped;
1613 (* because the before field is used by apply_macro_defs *)
1614 tokens2 := TV.rebuild_tokens_extented
!tokens2;
1616 (* tagging contextual info (InFunc, InStruct, etc). Better to do
1617 * that after the "ifdef-simplification" phase.
1619 let cleaner = !tokens2 +> List.filter
(fun x ->
1620 not
(TH.is_comment
x.tok
) (* could filter also #define/#include *)
1623 let brace_grouped = TV.mk_braceised
cleaner in
1624 set_context_tag
brace_grouped;
1627 let cleaner = !tokens2 +> filter_cpp_stuff in
1629 let paren_grouped = TV.mk_parenthised
cleaner in
1630 let line_paren_grouped = TV.mk_line_parenthised
paren_grouped in
1631 find_define_init_brace_paren paren_grouped;
1632 find_string_macro_paren paren_grouped;
1633 find_macro_lineparen line_paren_grouped;
1634 find_macro_paren paren_grouped;
1637 (* obsolete: actions ? not yet *)
1638 let cleaner = !tokens2 +> filter_cpp_stuff in
1639 let paren_grouped = TV.mk_parenthised
cleaner in
1640 find_actions paren_grouped;
1644 insert_virtual_positions (!tokens2 +> Common.acc_map
(fun x -> x.tok
))
1647 let time_hack1 ~macro_defs a
=
1648 Common.profile_code_exclusif
"HACK" (fun () -> fix_tokens_cpp2 ~macro_defs a
)
1650 let fix_tokens_cpp ~macro_defs a
=
1651 Common.profile_code
"C parsing.fix_cpp" (fun () -> time_hack1 ~macro_defs a
)
1655 let can_be_on_top_level tl
=
1670 (*****************************************************************************)
1671 (* Lexing with lookahead *)
1672 (*****************************************************************************)
1674 (* Why using yet another parsing_hack technique ? The fix_xxx where do
1675 * some pre-processing on the full list of tokens is not enough ?
1676 * No cos sometimes we need more contextual info, and even if
1677 * set_context() tries to give some contextual info, it's not completely
1678 * accurate so the following code give yet another alternative, yet another
1679 * chance to transform some tokens.
1681 * todo?: maybe could try to get rid of this technique. Maybe a better
1682 * set_context() would make possible to move this code using a fix_xx
1685 * LALR(k) trick. We can do stuff by adding cases in lexer_c.mll, but
1686 * it is more general to do it via my LALR(k) tech. Because here we can
1687 * transform some token give some context information. So sometimes it
1688 * makes sense to transform a token in one context, sometimes not, and
1689 * lex can not provide us this context information. Note that the order
1690 * in the pattern matching in lookahead is important. Do not cut/paste.
1692 * Note that in next there is only "clean" tokens, there is no comment
1693 * or space tokens. This is done by the caller.
1697 open Lexer_parser
(* for the fields of lexer_hint type *)
1699 let not_struct_enum = function
1700 | (Parser_c.Tstruct _
| Parser_c.Tunion _
| Parser_c.Tenum _
)::_
-> false
1703 let pointer ?
(followed_by
=fun _
-> true)
1704 ?
(followed_by_more
=fun _
-> true) ts
=
1707 | TMul _
:: rest
-> loop rest
1708 | TAnd _
:: rest
when !Flag.c_plus_plus
-> loop rest
1709 | t
:: ts'
-> followed_by t
&& followed_by_more ts'
1710 | [] -> failwith
"unexpected end of token stream" in
1712 | TMul _
:: rest
-> loop rest
1713 | TAnd _
:: rest
when !Flag.c_plus_plus
-> loop rest
1716 let ident = function
1720 let is_type = function
1736 let is_cparen = function (TCPar _
) -> true | _
-> false
1737 let is_oparen = function (TOPar _
) -> true | _
-> false
1739 let rec not_has_type_before f xs
=
1741 | [] -> raise
(Impossible
666)
1745 else if is_type x then
1748 not_has_type_before f xs
1750 (* This function is inefficient, because it will look over a K&R header,
1751 or function prototype multiple times. At least when we see a , and are in a
1752 parameter list, we know we will eventually see a close paren, and it
1753 should come fairly soon. *)
1755 let l1 = drop_until
is_cparen l
in
1757 (TCPar _
) :: (TOCro _
) :: _
-> false
1758 | (TCPar _
) :: _
-> true
1761 (* (a)(b) is ambiguous, because (a) could be a function name or a cast.
1762 At this point, we just see an ident for a; we don't know if it is eg a local
1763 variable. This function sees at least if b is the only argument, ie there
1764 are no commas at top level *)
1765 let paren_before_comma l
=
1766 let rec loop level
= function
1768 | (TComma _
)::_
when level
= 1 -> false
1769 | (TCPar _
)::_
when level
= 1 -> true
1770 | (TCPar _
)::rest
-> loop (level
-1) rest
1771 | (TOPar _
)::rest
-> loop (level
+1) rest
1772 | x::rest
-> loop level rest
in
1775 let lookahead2 ~pass next before
=
1776 match (next
, before
) with
1779 (* yy xx( and in function *)
1780 | TOPar i1
::_
, TIdent
(s,i2
)::TypedefIdent _
::_
1781 when !Flag.c_plus_plus
&& (LP.current_context
() = (LP.InFunction
)) ->
1782 pr2_cpp("constructed_object: " ^
s);
1783 TOParCplusplusInit i1
1784 | TOPar i1
::_
, TIdent
(s,i2
)::ptr
1785 when !Flag.c_plus_plus
1786 && pointer ~followed_by
:(function TypedefIdent _
-> true | _
-> false) ptr
1787 && (LP.current_context
() = (LP.InFunction
)) ->
1788 pr2_cpp("constructed_object: " ^
s);
1789 TOParCplusplusInit i1
1790 | TypedefIdent
(s,i
)::TOPar i1
::_
,_
1791 when !Flag.c_plus_plus
&& (LP.current_context
() = (LP.InFunction
)) ->
1794 (*-------------------------------------------------------------*)
1795 (* typedef inference, parse_typedef_fix3 *)
1796 (*-------------------------------------------------------------*)
1798 | (TIdent
(s,i1
)::TIdent
(s2
,i2
)::_
, _
) when not_struct_enum before
&& s =$
= s2
1800 (* (take_safe 1 !passed_tok <> [TOPar]) -> *)
1802 (* parse_typedef_fix3:
1803 * acpi_object acpi_object;
1804 * etait mal parsé, car pas le temps d'appeler dt() dans le type_spec.
1805 * Le parser en interne a deja appelé le prochain token pour pouvoir
1806 * decider des choses.
1807 * => special case in lexer_heuristic, again
1809 if !Flag_parsing_c.debug_typedef
1810 then pr2 ("TYPEDEF: disable typedef cos special case: " ^
s);
1813 LP.disable_typedef
();
1814 msg_typedef s i1
1; LP.add_typedef_root
s;
1815 TypedefIdent
(s, i1
)
1820 | (TOCro i1
:: _
, Tdelete _
:: _
)
1821 when !Flag.c_plus_plus
->
1822 TCommentCpp
(Token_c.CppDirective
, i1
)
1824 | (TCCro i1
:: _
, Tdelete _
:: _
)
1825 when !Flag.c_plus_plus
->
1826 TCommentCpp
(Token_c.CppDirective
, i1
)
1829 | ((TString
((s, _
), i1
) | TMacroString
(s, i1
)) :: _
, Textern _
:: _
)
1830 when !Flag.c_plus_plus
->
1831 TCommentCpp
(Token_c.CppDirective
, i1
)
1834 | (Tconst i1
:: TOBrace _
:: _
, TCPar _
:: _
)
1835 when !Flag.c_plus_plus
->
1836 TCommentCpp
(Token_c.CppDirective
, i1
)
1839 | (TIdent
(s, i1
)::(Tconst _
|Tvolatile _
|Trestrict _
)::type_
::_
, _
)
1840 when not_struct_enum before
1842 TCommentCpp
(Token_c.CppDirective
, i1
)
1845 | (TIdent
(s, i1
)::Tstruct _
::_
, _
)
1846 when not_struct_enum before
->
1847 TCommentCpp
(Token_c.CppDirective
, i1
)
1850 | (TIdent
(s, i1
)::type_
::_
, _
)
1851 when not_struct_enum before
1853 TCommentCpp
(Token_c.CppDirective
, i1
)
1856 | (TIdent
(s, i1
)::TIdent
(s2
, i2
)::_
, seen
::_
)
1857 when not_struct_enum before
1862 TCommentCpp
(Token_c.CppDirective
, i1
)
1864 | (TIdent
(s2
, i2
)::_
, TIdent
(s, i1
)::seen
::_
)
1865 when not_struct_enum before
1866 && is_macro s2
&& is_type seen
->
1867 TCommentCpp
(Token_c.CppDirective
, i2
)
1870 | (TIdent
(s, i1
)::ptr
, seen
::_
)
1871 when not_struct_enum before
1872 && pointer ptr
&& is_type seen
->
1873 TCommentCpp
(Token_c.CppDirective
, i1
)
1876 | (TIdent
(s, i1
)::TIdent
(s2
, i2
)::_
, ptr
)
1877 when not_struct_enum before
1882 TCommentCpp
(Token_c.CppDirective
, i1
)
1885 | (TIdent
(s2
, i2
)::_
, TIdent
(s, i1
)::ptr
)
1886 when not_struct_enum before
1887 && is_macro s2
&& pointer ptr
->
1888 TCommentCpp
(Token_c.CppDirective
, i2
)
1890 (* exception to next rule *)
1891 | (TIdent
(s2
, i2
)::TOPar _
:: _
, TIdent
(s, i1
)::seen
::_
)
1892 when not_struct_enum before
1893 && is_macro s2
&& is_type seen
->
1896 | (TIdent
(s2
, i2
)::_
, TIdent
(s, i1
)::seen
::_
)
1897 when not_struct_enum before
1898 && is_macro s2
&& is_type seen
->
1899 TCommentCpp
(Token_c.CppDirective
, i2
)
1901 (* xx * yy AND in paramdecl *)
1902 | (TIdent
(s, i1
)::ptr
, _
)
1903 when not_struct_enum before
&& (LP.current_context
() =*= LP.InParameter
)
1904 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false) ptr
1906 msg_typedef s i1
14; LP.add_typedef_root
s;
1907 TypedefIdent
(s, i1
)
1910 | (TIdent
(s, i1
)::TIdent
(s2
, i2
)::TOPar _
::_
, type_
::_
) when not_struct_enum before
1911 && ok_typedef s && is_macro s2
&& is_type type_
1915 | (TIdent
(s, i1
)::TIdent
(s2
, i2
)::_
, _
) when not_struct_enum before
1918 (* && not_annot s2 BUT lead to false positive*)
1919 msg_typedef s i1
2; LP.add_typedef_root
s;
1920 TypedefIdent
(s, i1
)
1924 | (TIdent
(s, i1
)::Tinline i2
::_
, _
) when not_struct_enum before
1928 msg_typedef s i1
3; LP.add_typedef_root
s;
1929 TypedefIdent
(s, i1
)
1932 (* [,(] xx [,)] AND param decl *)
1933 | (TIdent
(s, i1
)::(((TComma _
|TCPar _
)::_
) as rest
) ,
1934 ((TComma _
|TOPar _
)::_
as bef
))
1935 when not_struct_enum before
&& (LP.current_context
() =*= LP.InParameter
)
1937 && not_has_type_before is_cparen rest
1938 && not_has_type_before is_oparen bef
1943 | (TIdent
(s, i1
)::((TComma _
|TCPar _
)::_
) , (TComma _
|TOPar _
)::_
)
1944 when not_struct_enum before
&& (LP.current_context
() =*= LP.InParameter
)
1948 msg_typedef s i1
4; LP.add_typedef_root
s;
1949 TypedefIdent
(s, i1
)
1952 (* specialcase: [,(] xx* [,)] *)
1953 | (TIdent
(s, i1
)::ptr
, (*(TComma _|TOPar _)::*)_
)
1954 when pointer ~followed_by
:(function TComma _
|TCPar _
-> true | _
-> false) ptr
1955 && not_struct_enum before
1956 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
1960 msg_typedef s i1
5; LP.add_typedef_root
s;
1961 TypedefIdent
(s, i1
)
1965 (* specialcase: [,(] xx** [,)] *)
1966 | (TIdent
(s, i1
)::TMul _
::TMul _
::(TComma _
|TCPar _
)::_
, (*(TComma _|TOPar _)::*)_
)
1967 when not_struct_enum before
1968 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
1972 msg_typedef s i1
6; LP.add_typedef_root
s;
1973 TypedefIdent
(s, i1
)
1977 (* xx const * USELESS because of next rule ? *)
1978 | (TIdent
(s, i1
)::(Tconst _
|Tvolatile _
|Trestrict _
)::TMul _
::_
, _
)
1979 when not_struct_enum before
1980 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
1984 msg_typedef s i1
7; LP.add_typedef_root
s;
1985 TypedefIdent
(s, i1
)
1988 | (TIdent
(s, i1
)::(Tconst _
|Tvolatile _
|Trestrict _
)::_
, _
)
1989 when not_struct_enum before
1991 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
1994 msg_typedef s i1
8; LP.add_typedef_root
s;
1995 TypedefIdent
(s, i1
)
1999 | (TIdent
(s, i1
)::ptr
, _
)
2000 when pointer ~followed_by
:(function Tconst _
| Tvolatile _
| Trestrict _
-> true | _
-> false) ptr
2001 && not_struct_enum before
2005 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
2007 msg_typedef s i1
9; LP.add_typedef_root
s;
2008 TypedefIdent
(s, i1
)
2012 | (TIdent
(s, i1
)::TCPar _
::_
, (Tconst _
| Tvolatile _
|Trestrict _
)::TOPar _
::_
) when
2015 msg_typedef s i1
10; LP.add_typedef_root
s;
2016 TypedefIdent
(s, i1
)
2020 (* ( xx ) [sizeof, ~] *)
2021 | (TIdent
(s, i1
)::TCPar _
::(Tsizeof _
|TTilde _
)::_
, TOPar _
::_
)
2022 when not_struct_enum before
2026 msg_typedef s i1
11; LP.add_typedef_root
s;
2027 TypedefIdent
(s, i1
)
2029 (* [(,] xx [ AND parameterdeclaration *)
2030 | (TIdent
(s, i1
)::TOCro _
::_
, (TComma _
|TOPar _
)::_
)
2031 when (LP.current_context
() =*= LP.InParameter
)
2035 msg_typedef s i1
12; LP.add_typedef_root
s;
2036 TypedefIdent
(s, i1
)
2038 (*------------------------------------------------------------*)
2039 (* if 'x*y' maybe an expr, maybe just a classic multiplication *)
2040 (* but if have a '=', or ',' I think not *)
2041 (*------------------------------------------------------------*)
2043 (* static xx * yy *)
2044 | (TIdent
(s, i1
)::ptr
,
2045 (Tregister _
|Tstatic _
|Tvolatile _
|Tconst _
|Trestrict _
)::_
) when
2046 pointer ~followed_by
:(function TIdent _
-> true | _
-> false) ptr
2050 msg_typedef s i1
13; LP.add_typedef_root
s;
2051 TypedefIdent
(s, i1
)
2053 (* TODO xx * yy ; AND in start of compound element *)
2056 (* xx * yy, AND in paramdecl *)
2057 | (TIdent
(s, i1
)::ptr
, _
)
2058 when not_struct_enum before
&& (LP.current_context
() =*= LP.InParameter
)
2059 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2060 ~followed_by_more
:(function TComma _
:: _
-> true | _
-> false) ptr
2064 msg_typedef s i1
14; LP.add_typedef_root
s;
2065 TypedefIdent
(s, i1
)
2068 (* xx * yy ; AND in Toplevel, except when have = before *)
2069 | (TIdent
(s, i1
)::TMul _
::TIdent
(s2
, i2
)::TPtVirg _
::_
, TEq _
::_
) ->
2071 | (TIdent
(s, i1
)::ptr
, _
)
2072 when not_struct_enum before
2073 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2074 ~followed_by_more
:(function TPtVirg _
:: _
-> true | _
-> false) ptr
2075 && (LP.is_top_or_struct
(LP.current_context
()))
2078 msg_typedef s i1
15; LP.add_typedef_root
s;
2079 TypedefIdent
(s, i1
)
2081 (* xx * yy , AND in Toplevel *)
2082 | (TIdent
(s, i1
)::ptr
, _
)
2083 when not_struct_enum before
&& (LP.current_context
() =*= LP.InTopLevel
)
2085 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2086 ~followed_by_more
:(function TComma _
:: _
-> true | _
-> false) ptr
2089 msg_typedef s i1
16; LP.add_typedef_root
s;
2090 TypedefIdent
(s, i1
)
2092 (* xx * yy ( AND in Toplevel *)
2093 | (TIdent
(s, i1
)::ptr
, _
)
2094 when not_struct_enum before
2095 && (LP.is_top_or_struct
(LP.current_context
()))
2097 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2098 ~followed_by_more
:(function TOPar _
:: _
-> true | _
-> false) ptr
2101 msg_typedef s i1
17; LP.add_typedef_root
s;
2102 TypedefIdent
(s, i1
)
2105 (* todo? enough ? cos in struct def we can have some expression ! *)
2106 | (TIdent
(s, i1
)::ptr
, _
)
2107 when not_struct_enum before
2108 && (LP.is_top_or_struct
(LP.current_context
()))
2110 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2111 ~followed_by_more
:(function TOCro _
:: _
-> true | _
-> false) ptr
2114 msg_typedef s i1
18; LP.add_typedef_root
s;
2115 TypedefIdent
(s, i1
)
2117 (* u16: 10; in struct *)
2118 | (TIdent
(s, i1
)::TDotDot _
::_
, (TOBrace _
| TPtVirg _
)::_
)
2119 when (LP.is_top_or_struct
(LP.current_context
()))
2123 msg_typedef s i1
19; LP.add_typedef_root
s;
2124 TypedefIdent
(s, i1
)
2127 (* why need TOPar condition as stated in preceding rule ? really needed ? *)
2128 (* YES cos at toplevel can have some expression !! for instance when *)
2129 (* enter in the dimension of an array *)
2131 | (TIdent s::TMul::TIdent s2::_ , _)
2132 when (take_safe 1 !passed_tok <> [Tstruct] &&
2133 (take_safe 1 !passed_tok <> [Tenum]))
2135 !LP._lexer_hint = Some LP.Toplevel ->
2136 msg_typedef s 20; LP.add_typedef_root s;
2141 | (TIdent
(s, i1
)::ptr
, _
)
2142 when not_struct_enum before
2144 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2145 ~followed_by_more
:(function TEq _
:: _
-> true | _
-> false) ptr
2148 msg_typedef s i1
21; LP.add_typedef_root
s;
2149 TypedefIdent
(s, i1
)
2152 (* xx * yy) AND in paramdecl *)
2153 | (TIdent
(s, i1
)::ptr
, _
)
2154 when not_struct_enum before
&& (LP.current_context
() =*= LP.InParameter
)
2156 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2157 ~followed_by_more
:(function TCPar _
:: _
-> true | _
-> false) ptr
2160 msg_typedef s i1
22; LP.add_typedef_root
s;
2161 TypedefIdent
(s, i1
)
2164 (* xx * yy; *) (* wrong ? *)
2165 | (TIdent
(s, i1
)::ptr
,
2166 (TOBrace _
| TPtVirg _
)::_
) when not_struct_enum before
2168 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2169 ~followed_by_more
:(function TPtVirg _
:: _
-> true | _
-> false) ptr
2172 msg_typedef s i1
23; LP.add_typedef_root
s;
2173 msg_maybe_dangereous_typedef s;
2174 TypedefIdent
(s, i1
)
2177 (* xx * yy, and ';' before xx *) (* wrong ? *)
2178 | (TIdent
(s, i1
)::ptr
,
2179 (TOBrace _
| TPtVirg _
)::_
) when
2181 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false)
2182 ~followed_by_more
:(function TComma _
:: _
-> true | _
-> false) ptr
2185 msg_typedef s i1
24; LP.add_typedef_root
s;
2186 TypedefIdent
(s, i1
)
2190 | (TIdent
(s, i1
)::ptr
, _
)
2191 when s ==~
regexp_typedef && not_struct_enum before
2192 (* struct user_info_t sometimes *)
2194 && pointer ~followed_by
:(function TIdent _
-> true | _
-> false) ptr
2197 msg_typedef s i1
25; LP.add_typedef_root
s;
2198 TypedefIdent
(s, i1
)
2200 (* xx ** yy *) (* wrong ? *)
2201 | (TIdent
(s, i1
)::TMul _
::TMul _
::TIdent
(s2
, i2
)::_
, _
)
2202 when not_struct_enum before
2203 && (LP.current_context
() =*= LP.InParameter
)
2207 msg_typedef s i1
26; LP.add_typedef_root
s;
2208 TypedefIdent
(s, i1
)
2210 (* xx ** yy *) (* wrong ? *)
2211 | (TIdent
(s, i1
)::TMul _
::TMul _
::TIdent
(s2
, i2
)::_
, (TOBrace _
| TPtVirg _
)::_
)
2212 when not_struct_enum before
2213 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
2215 (* christia : this code catches 'a * *b' which is wrong
2219 msg_typedef s i1
26; LP.add_typedef_root
s;
2220 TypedefIdent
(s, i1
)
2223 | (TIdent
(s, i1
)::TMul _
::TMul _
::TMul _
::TIdent
(s2
, i2
)::_
, _
)
2224 when not_struct_enum before
2226 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
2229 msg_typedef s i1
27; LP.add_typedef_root
s;
2230 TypedefIdent
(s, i1
)
2233 | (TIdent
(s, i1
)::TMul _
::TMul _
::TCPar _
::_
, _
)
2234 when not_struct_enum before
2235 (* && !LP._lexer_hint = Some LP.ParameterDeclaration *)
2239 msg_typedef s i1
28; LP.add_typedef_root
s;
2240 TypedefIdent
(s, i1
)
2244 (* ----------------------------------- *)
2245 (* old: why not do like for other rules and start with TIdent ?
2246 * why do TOPar :: TIdent :: ..., _ and not TIdent :: ..., TOPAr::_ ?
2247 * new: prefer now start with TIdent because otherwise the add_typedef_root
2248 * may have no effect if in second pass or if have disable the add_typedef.
2252 | (TIdent
(s, i1
)::TCPar i2
::(TIdent
(_
,i3
)|TInt
(_
,i3
))::_
,
2254 when not
(TH.is_stuff_taking_parenthized
x) (* &&
2255 Ast_c.line_of_info i2 =|= Ast_c.line_of_info i3 - why useful?
2258 && not
(ident x) (* possible K&R declaration *)
2261 msg_typedef s i1
29; LP.add_typedef_root
s;
2263 TypedefIdent
(s, i1
)
2267 * but false positif: typedef int (xxx_t)(...), so do specialisation below.
2270 | (TIdent (s, i1)::TCPar _::TOPar _::_ , (TOPar info)::x::_)
2271 when not (TH.is_stuff_taking_parenthized x)
2274 msg_typedef s 30; LP.add_typedef_root s;
2276 TypedefIdent
(s, i1
)
2278 (* special case: = (xx) ( yy) *)
2279 | (TIdent
(s, i1
)::TCPar _
::((TOPar _
::_
) as rest
) ,
2280 (TOPar
info)::(TEq _
|TEqEq _
)::_
)
2281 when ok_typedef s && paren_before_comma rest
2284 msg_typedef s i1
31; LP.add_typedef_root
s;
2286 TypedefIdent
(s, i1
)
2290 | (TIdent
(s, i1
)::ptr
, (TOPar
info)::_
)
2292 && pointer ~followed_by
:(function TCPar _
-> true | _
-> false)
2293 ~followed_by_more
:(function TIdent _
:: _
-> true | _
-> false) ptr
2296 msg_typedef s i1
32; LP.add_typedef_root
s;
2301 (* (xx){ ... } constructor *)
2302 | (TIdent
(s, i1
)::TCPar _
::TOBrace _
::_
, TOPar _
::x::_
)
2303 when (*s ==~ regexp_typedef && *) not
(TH.is_stuff_taking_parenthized
x)
2307 msg_typedef s i1
33; LP.add_typedef_root
s;
2308 TypedefIdent
(s, i1
)
2311 (* can have sizeof on expression
2312 | (Tsizeof::TOPar::TIdent s::TCPar::_, _) ->
2313 msg_typedef s; LP.add_typedef_root s;
2318 (* ----------------------------------- *)
2319 (* x ( *y )(params), function pointer *)
2320 | (TIdent
(s, i1
)::TOPar _
::TMul _
::TIdent _
::TCPar _
::TOPar _
::_
, _
)
2321 when not_struct_enum before
2325 msg_typedef s i1
34; LP.add_typedef_root
s;
2326 TypedefIdent
(s, i1
)
2328 (* x* ( *y )(params), function pointer 2 *)
2329 | (TIdent
(s, i1
)::TMul _
::TOPar _
::TMul _
::TIdent _
::TCPar _
::TOPar _
::_
, _
)
2330 when not_struct_enum before
2334 msg_typedef s i1
35; LP.add_typedef_root
s;
2335 TypedefIdent
(s, i1
)
2338 (*-------------------------------------------------------------*)
2340 (*-------------------------------------------------------------*)
2341 | ((TIfdef
(_
,ii) |TIfdefelse
(_
,ii) |TIfdefelif
(_
,ii) |TEndif
(_
,ii) |
2342 TIfdefBool
(_
,_
,ii)|TIfdefMisc
(_
,_
,ii)|TIfdefVersion
(_
,_
,ii))
2348 if not !Flag_parsing_c.ifdef_to_if
2349 then TCommentCpp (Ast_c.CppIfDirective, ii)
2352 (* not !LP._lexer_hint.toplevel *)
2353 if !Flag_parsing_c.ifdef_directive_passing
2357 if (LP.current_context
() =*= LP.InInitializer
)
2359 pr2_cpp "In Initializer passing"; (* cheat: dont count in stat *)
2360 incr
Stat.nIfdefInitializer
;
2362 pr2_cpp("IFDEF: or related inside function. I treat it as comment");
2363 incr
Stat.nIfdefPassing
;
2367 TIfdef _
| TIfdefMisc _
| TIfdefVersion _
-> Token_c.IfDef
2368 | TIfdefBool _
-> Token_c.IfDef0
2369 | TIfdefelse _
| TIfdefelif _
-> Token_c.Else
2370 | TEndif _
-> Token_c.Endif
2371 | _
-> Token_c.Other
in (* not possible here *)
2372 TCommentCpp
(Token_c.CppIfDirective
x, ii)
2376 | (TUndef
(ii) as x)::_
, _
2380 pr2_cpp("UNDEF: I treat it as comment");
2381 TCommentCpp
(Token_c.CppDirective
, ii)
2385 | (TCppDirectiveOther
(ii) as x)::_
, _
2389 pr2_cpp ("OTHER directive: I treat it as comment");
2390 TCommentCpp
(Token_c.CppDirective
, ii)
2394 (* If ident contain a for_each, then certainly a macro. But to be
2395 * sure should look if there is a '{' after the ')', but it requires
2396 * to count the '('. Because this can be expensive, we do that only
2397 * when the token contains "for_each".
2399 | (TIdent
(s, i1
)::TOPar _
::rest
, _
)
2400 when not
(LP.current_context
() =*= LP.InTopLevel
)
2401 (* otherwise a function such as static void loopback_enable(int i) {
2402 * will be considered as a loop
2406 if s ==~
regexp_foreach &&
2407 is_really_foreach (Common.take_safe
forLOOKAHEAD rest
)
2411 TMacroIterator
(s, i1
)
2415 (* (* christia: here insert support for macros on top level *)
2416 | TIdent
(s, ii) :: tl
:: _
, _
when
2417 can_be_on_top_level tl
&& LP.current_context
() = InTopLevel
->
2418 pr2_cpp ("'" ^
s ^
"' looks like a macro, I treat it as comment");
2419 TCommentCpp
(Token_c.CppDirective
, ii)
2422 (*-------------------------------------------------------------*)
2424 | _
-> raise
(Impossible
93)
2426 let lookahead ~pass a b
=
2427 Common.profile_code
"C parsing.lookahead" (fun () -> lookahead2 ~pass a b
)