(*
-* Copyright 2005-2008, Ecole des Mines de Nantes, University of Copenhagen
-* Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller
-* This file is part of Coccinelle.
-*
-* Coccinelle is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, according to version 2 of the License.
-*
-* Coccinelle is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
-*
-* The authors reserve the right to distribute this or future versions of
-* Coccinelle under other licenses.
-*)
+ * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
+ * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
+ * This file is part of Coccinelle.
+ *
+ * Coccinelle is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, according to version 2 of the License.
+ *
+ * Coccinelle is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The authors reserve the right to distribute this or future versions of
+ * Coccinelle under other licenses.
+ *)
(* splits the entire file into minus and plus fragments, and parses each
module D = Data
module PC = Parser_cocci_menhir
module V0 = Visitor_ast0
+module VT0 = Visitor_ast0_types
module Ast = Ast_cocci
module Ast0 = Ast0_cocci
let pr = Printf.sprintf
let pr2 s = Printf.printf "%s\n" s
(* for isomorphisms. all should be at the front!!! *)
-let reserved_names =
+let reserved_names =
["all";"optional_storage";"optional_qualifier";"value_format";"comm_assoc"]
(* ----------------------------------------------------------------------- *)
match line_type tok with
D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> ":-"
| D.PLUS -> ":+"
+ | D.PLUSPLUS -> ":++"
| D.CONTEXT | D.UNIQUE | D.OPT -> ""
let token2c (tok,_) =
| PC.TConstant -> "constant"
| PC.TExpression -> "expression"
| PC.TIdExpression -> "idexpression"
+ | PC.TInitialiser -> "initialiser"
| PC.TStatement -> "statement"
| PC.TPosition -> "position"
| PC.TPosAny -> "any"
| PC.TLocal -> "local"
| PC.Tlist -> "list"
| PC.TFresh -> "fresh"
+ | PC.TCppConcatOp -> "##"
| PC.TPure -> "pure"
| PC.TContext -> "context"
| PC.TTypedef -> "typedef"
| PC.TName -> "name"
| PC.TRuleName str -> "rule_name-"^str
| PC.TUsing -> "using"
+ | PC.TVirtual -> "virtual"
| PC.TPathIsoFile str -> "path_iso_file-"^str
| PC.TDisable -> "disable"
| PC.TExtends -> "extends"
| PC.TNever -> "never"
| PC.TExists -> "exists"
| PC.TForall -> "forall"
- | PC.TReverse -> "reverse"
| PC.TError -> "error"
| PC.TWords -> "words"
+ | PC.TGenerated -> "generated"
| PC.TNothing -> "nothing"
| PC.Tvoid(clt) -> "void"^(line_type2c clt)
| PC.Tstruct(clt) -> "struct"^(line_type2c clt)
| PC.Tunion(clt) -> "union"^(line_type2c clt)
+ | PC.Tenum(clt) -> "enum"^(line_type2c clt)
| PC.Tunsigned(clt) -> "unsigned"^(line_type2c clt)
| PC.Tsigned(clt) -> "signed"^(line_type2c clt)
| PC.Tstatic(clt) -> "static"^(line_type2c clt)
| PC.Tconst(clt) -> "const"^(line_type2c clt)
| PC.Tvolatile(clt) -> "volatile"^(line_type2c clt)
- | PC.TPragma(s) -> s
+ | PC.TPragma(s,_) -> s
| PC.TIncludeL(s,clt) -> (pr "#include \"%s\"" s)^(line_type2c clt)
| PC.TIncludeNL(s,clt) -> (pr "#include <%s>" s)^(line_type2c clt)
| PC.TDefine(clt,_) -> "#define"^(line_type2c clt)
- | PC.TDefineParam(clt,_,_) -> "#define_param"^(line_type2c clt)
+ | PC.TDefineParam(clt,_,_,_) -> "#define_param"^(line_type2c clt)
| PC.TMinusFile(s,clt) -> (pr "--- %s" s)^(line_type2c clt)
| PC.TPlusFile(s,clt) -> (pr "+++ %s" s)^(line_type2c clt)
| PC.TInc(clt) -> "++"^(line_type2c clt)
| PC.TDec(clt) -> "--"^(line_type2c clt)
-
+
| PC.TIf(clt) -> "if"^(line_type2c clt)
| PC.TElse(clt) -> "else"^(line_type2c clt)
| PC.TWhile(clt) -> "while"^(line_type2c clt)
| PC.TAnd (clt) -> "&"^(line_type2c clt)
| PC.TEqEq(clt) -> "=="^(line_type2c clt)
| PC.TNotEq(clt) -> "!="^(line_type2c clt)
+ | PC.TTildeEq(clt) -> "~="^(line_type2c clt)
+ | PC.TTildeExclEq(clt) -> "~!="^(line_type2c clt)
| PC.TLogOp(op,clt) ->
(match op with
Ast.Inf -> "<"
| PC.TMetaExpList(_,_,_,clt) -> "explistmeta"^(line_type2c clt)
| PC.TMetaId(_,_,_,clt) -> "idmeta"^(line_type2c clt)
| PC.TMetaType(_,_,clt) -> "typemeta"^(line_type2c clt)
+ | PC.TMetaInit(_,_,clt) -> "initmeta"^(line_type2c clt)
| PC.TMetaStm(_,_,clt) -> "stmmeta"^(line_type2c clt)
| PC.TMetaStmList(_,_,clt) -> "stmlistmeta"^(line_type2c clt)
| PC.TMetaFunc(_,_,_,clt) -> "funcmeta"^(line_type2c clt)
| PC.TArobArob -> "@@"
| PC.TArob -> "@"
| PC.TPArob -> "P@"
+ | PC.TScript -> "script"
+ | PC.TInitialize -> "initialize"
+ | PC.TFinalize -> "finalize"
| PC.TWhen(clt) -> "WHEN"^(line_type2c clt)
+ | PC.TWhenTrue(clt) -> "WHEN TRUE"^(line_type2c clt)
+ | PC.TWhenFalse(clt) -> "WHEN FALSE"^(line_type2c clt)
| PC.TAny(clt) -> "ANY"^(line_type2c clt)
| PC.TStrict(clt) -> "STRICT"^(line_type2c clt)
| PC.TEllipsis(clt) -> "..."^(line_type2c clt)
| PC.TCBrace(clt) -> "}"^(line_type2c clt)
| PC.TOCro(clt) -> "["^(line_type2c clt)
| PC.TCCro(clt) -> "]"^(line_type2c clt)
+ | PC.TOInit(clt) -> "{"^(line_type2c clt)
| PC.TPtrOp(clt) -> "->"^(line_type2c clt)
type plus = PLUS | NOTPLUS | SKIP
-let plus_attachable (tok,_) =
+let plus_attachable only_plus (tok,_) =
match tok with
PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
| PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
- | PC.Tunion(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt) | PC.Tstatic(clt)
+ | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
+ | PC.Tstatic(clt)
| PC.Tinline(clt) | PC.Ttypedef(clt) | PC.Tattr(_,clt)
| PC.Tauto(clt) | PC.Tregister(clt)
| PC.Textern(clt) | PC.Tconst(clt) | PC.Tvolatile(clt)
| PC.TIncludeL(_,clt) | PC.TIncludeNL(_,clt) | PC.TDefine(clt,_)
- | PC.TDefineParam(clt,_,_) | PC.TMinusFile(_,clt) | PC.TPlusFile(_,clt)
+ | PC.TDefineParam(clt,_,_,_) | PC.TMinusFile(_,clt) | PC.TPlusFile(_,clt)
| PC.TInc(clt) | PC.TDec(clt)
-
+
| PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
| PC.TSwitch(clt) | PC.TCase(clt) | PC.TDefault(clt) | PC.TReturn(clt)
| PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt) | PC.TIdent(_,clt)
| PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
| PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
- | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+ | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TLogOp(_,clt)
| PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
| PC.TDmOp(_,clt) | PC.TTilde (clt)
| PC.TMetaLocalIdExp(_,_,_,_,clt)
| PC.TMetaExpList(_,_,_,clt)
| PC.TMetaId(_,_,_,clt)
- | PC.TMetaType(_,_,clt) | PC.TMetaStm(_,_,clt)
- | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
+ | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt) | PC.TMetaStm(_,_,clt)
+ | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
| PC.TMetaLocalFunc(_,_,_,clt)
- | PC.TWhen(clt) | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
+ | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
+ | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
(* | PC.TCircles(clt) | PC.TStars(clt) *)
- | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
+ | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
| PC.TCPar(clt)
| PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
+ | PC.TOInit(clt)
| PC.TPtrOp(clt)
| PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
| PC.TPtVirg(clt) ->
- if line_type clt = D.PLUS then PLUS else NOTPLUS
+ if List.mem (line_type clt) [D.PLUS;D.PLUSPLUS]
+ then PLUS
+ else if only_plus then NOTPLUS
+ else if line_type clt = D.CONTEXT then PLUS else NOTPLUS
| PC.TOPar0(clt) | PC.TMid0(clt) | PC.TCPar0(clt)
- | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
+ | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
| PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) (* | PC.TOCircles(clt)
| PC.TCCircles(clt) | PC.TOStars(clt) | PC.TCStars(clt) *) -> NOTPLUS
| PC.TMetaPos(nm,_,_,_) -> NOTPLUS
match tok with
PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
| PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
- | PC.Tunion(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt) | PC.Tstatic(clt)
+ | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
+ | PC.Tstatic(clt)
| PC.Tinline(clt) | PC.Tattr(_,clt) | PC.Tauto(clt) | PC.Tregister(clt)
| PC.Textern(clt) | PC.Tconst(clt) | PC.Tvolatile(clt)
| PC.TIncludeL(_,clt) | PC.TIncludeNL(_,clt) | PC.TDefine(clt,_)
- | PC.TDefineParam(clt,_,_) | PC.TMinusFile(_,clt) | PC.TPlusFile(_,clt)
+ | PC.TDefineParam(clt,_,_,_) | PC.TMinusFile(_,clt) | PC.TPlusFile(_,clt)
| PC.TInc(clt) | PC.TDec(clt)
-
+
| PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
| PC.TSwitch(clt) | PC.TCase(clt) | PC.TDefault(clt) | PC.TReturn(clt)
| PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt) | PC.TIdent(_,clt)
| PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
| PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
- | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+ | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TLogOp(_,clt)
| PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
| PC.TDmOp(_,clt) | PC.TTilde (clt)
| PC.TMetaLocalIdExp(_,_,_,_,clt)
| PC.TMetaExpList(_,_,_,clt)
| PC.TMetaId(_,_,_,clt)
- | PC.TMetaType(_,_,clt) | PC.TMetaStm(_,_,clt)
- | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
+ | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt) | PC.TMetaStm(_,_,clt)
+ | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
| PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt)
- | PC.TWhen(clt) | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
+ | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt) |
+ PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
(* | PC.TCircles(clt) | PC.TStars(clt) *)
- | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
+ | PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
| PC.TCPar(clt)
| PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
+ | PC.TOInit(clt)
| PC.TPtrOp(clt)
| PC.Tvoid(_) -> (PC.Tvoid(clt),x)
| PC.Tstruct(_) -> (PC.Tstruct(clt),x)
| PC.Tunion(_) -> (PC.Tunion(clt),x)
+ | PC.Tenum(_) -> (PC.Tenum(clt),x)
| PC.Tunsigned(_) -> (PC.Tunsigned(clt),x)
| PC.Tsigned(_) -> (PC.Tsigned(clt),x)
| PC.Tstatic(_) -> (PC.Tstatic(clt),x)
| PC.TIncludeL(s,_) -> (PC.TIncludeL(s,clt),x)
| PC.TIncludeNL(s,_) -> (PC.TIncludeNL(s,clt),x)
| PC.TDefine(_,a) -> (PC.TDefine(clt,a),x)
- | PC.TDefineParam(_,a,b) -> (PC.TDefineParam(clt,a,b),x)
+ | PC.TDefineParam(_,a,b,c) -> (PC.TDefineParam(clt,a,b,c),x)
| PC.TMinusFile(s,_) -> (PC.TMinusFile(s,clt),x)
| PC.TPlusFile(s,_) -> (PC.TPlusFile(s,clt),x)
| PC.TInc(_) -> (PC.TInc(clt),x)
| PC.TDec(_) -> (PC.TDec(clt),x)
-
+
| PC.TIf(_) -> (PC.TIf(clt),x)
| PC.TElse(_) -> (PC.TElse(clt),x)
| PC.TWhile(_) -> (PC.TWhile(clt),x)
| PC.TAnd (_) -> (PC.TAnd (clt),x)
| PC.TEqEq(_) -> (PC.TEqEq(clt),x)
| PC.TNotEq(_) -> (PC.TNotEq(clt),x)
+ | PC.TTildeEq(_) -> (PC.TTildeEq(clt),x)
| PC.TLogOp(op,_) -> (PC.TLogOp(op,clt),x)
| PC.TShOp(op,_) -> (PC.TShOp(op,clt),x)
| PC.TPlus(_) -> (PC.TPlus(clt),x)
| PC.TMetaExpList(a,b,c,_) -> (PC.TMetaExpList(a,b,c,clt),x)
| PC.TMetaId(a,b,c,_) -> (PC.TMetaId(a,b,c,clt),x)
| PC.TMetaType(a,b,_) -> (PC.TMetaType(a,b,clt),x)
+ | PC.TMetaInit(a,b,_) -> (PC.TMetaInit(a,b,clt),x)
| PC.TMetaStm(a,b,_) -> (PC.TMetaStm(a,b,clt),x)
| PC.TMetaStmList(a,b,_) -> (PC.TMetaStmList(a,b,clt),x)
| PC.TMetaFunc(a,b,c,_) -> (PC.TMetaFunc(a,b,c,clt),x)
| PC.TMetaLocalFunc(a,b,c,_) -> (PC.TMetaLocalFunc(a,b,c,clt),x)
| PC.TWhen(_) -> (PC.TWhen(clt),x)
+ | PC.TWhenTrue(_) -> (PC.TWhenTrue(clt),x)
+ | PC.TWhenFalse(_) -> (PC.TWhenFalse(clt),x)
| PC.TAny(_) -> (PC.TAny(clt),x)
| PC.TStrict(_) -> (PC.TStrict(clt),x)
| PC.TEllipsis(_) -> (PC.TEllipsis(clt),x)
| PC.TCBrace(_) -> (PC.TCBrace(clt),x)
| PC.TOCro(_) -> (PC.TOCro(clt),x)
| PC.TCCro(_) -> (PC.TCCro(clt),x)
+ | PC.TOInit(_) -> (PC.TOInit(clt),x)
| PC.TPtrOp(_) -> (PC.TPtrOp(clt),x)
(* Read tokens *)
let wrap_lexbuf_info lexbuf =
- (Lexing.lexeme lexbuf, Lexing.lexeme_start lexbuf)
+ (Lexing.lexeme lexbuf, Lexing.lexeme_start lexbuf)
let tokens_all_full token table file get_ats lexbuf end_markers :
(bool * ((PC.token * (string * (int * int) * (int * int))) list)) =
- try
- let rec aux () =
+ try
+ let rec aux () =
let result = token lexbuf in
- let info = (Lexing.lexeme lexbuf,
+ let info = (Lexing.lexeme lexbuf,
(table.(Lexing.lexeme_start lexbuf)),
(Lexing.lexeme_start lexbuf, Lexing.lexeme_end lexbuf)) in
- if result = PC.EOF
+ if result = PC.EOF
then
if get_ats
then failwith "unexpected end of file in a metavariable declaration"
else
let (more,rest) = aux() in
(more,(result, info)::rest)
- in aux ()
+ in aux ()
with
e -> pr2 (Common.error_message file (wrap_lexbuf_info lexbuf) ); raise e
let (d,_,_,_,_,_,_,_) = clt in
match d with
D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> ([t],[])
- | D.PLUS -> ([],[t])
+ | D.PLUS | D.PLUSPLUS -> ([],[t])
| D.CONTEXT | D.UNIQUE | D.OPT -> ([t],[t])
let split_token ((tok,_) as t) =
match tok with
PC.TIdentifier | PC.TConstant | PC.TExpression | PC.TIdExpression
- | PC.TStatement | PC.TPosition | PC.TPosAny
+ | PC.TStatement | PC.TPosition | PC.TPosAny | PC.TInitialiser
| PC.TFunction | PC.TTypedef | PC.TDeclarer | PC.TIterator | PC.TName
- | PC.TType | PC.TParameter | PC.TLocal | PC.Tlist | PC.TFresh | PC.TPure
- | PC.TContext | PC.TRuleName(_) | PC.TUsing | PC.TDisable | PC.TExtends
- | PC.TPathIsoFile(_)
+ | PC.TType | PC.TParameter | PC.TLocal | PC.Tlist | PC.TFresh
+ | PC.TCppConcatOp | PC.TPure
+ | PC.TContext | PC.TRuleName(_) | PC.TUsing | PC.TVirtual | PC.TDisable
+ | PC.TExtends | PC.TPathIsoFile(_)
| PC.TDepends | PC.TOn | PC.TEver | PC.TNever | PC.TExists | PC.TForall
- | PC.TReverse
- | PC.TError | PC.TWords | PC.TNothing -> ([t],[t])
+ | PC.TError | PC.TWords | PC.TGenerated | PC.TNothing -> ([t],[t])
| PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
| PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
- | PC.Tunion(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
+ | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
| PC.Tstatic(clt) | PC.Tauto(clt) | PC.Tregister(clt) | PC.Textern(clt)
| PC.Tinline(clt) | PC.Ttypedef(clt) | PC.Tattr(_,clt)
| PC.Tconst(clt) | PC.Tvolatile(clt) -> split t clt
- | PC.TPragma(s) -> ([],[t]) (* only allowed in + *)
+ | PC.TPragma(s,_) -> ([],[t]) (* only allowed in + *)
| PC.TPlusFile(s,clt) | PC.TMinusFile(s,clt)
| PC.TIncludeL(s,clt) | PC.TIncludeNL(s,clt) ->
split t clt
- | PC.TDefine(clt,_) | PC.TDefineParam(clt,_,_) -> split t clt
+ | PC.TDefine(clt,_) | PC.TDefineParam(clt,_,_,_) -> split t clt
| PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
| PC.TSwitch(clt) | PC.TCase(clt) | PC.TDefault(clt)
| PC.TMetaIdExp(_,_,_,_,clt) | PC.TMetaLocalIdExp(_,_,_,_,clt)
| PC.TMetaExpList(_,_,_,clt)
| PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
- | PC.TMetaId(_,_,_,clt) | PC.TMetaType(_,_,clt)
+ | PC.TMetaId(_,_,_,clt) | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt)
| PC.TMetaStm(_,_,clt) | PC.TMetaStmList(_,_,clt) | PC.TMetaErr(_,_,_,clt)
| PC.TMetaFunc(_,_,_,clt) | PC.TMetaLocalFunc(_,_,_,clt)
| PC.TMetaDeclarer(_,_,_,clt) | PC.TMetaIterator(_,_,_,clt) -> split t clt
- | PC.TMPtVirg | PC.TArob | PC.TArobArob -> ([t],[t])
+ | PC.TMPtVirg | PC.TArob | PC.TArobArob | PC.TScript
+ | PC.TInitialize | PC.TFinalize -> ([t],[t])
| PC.TPArob | PC.TMetaPos(_,_,_,_) -> ([t],[])
| PC.TFunDecl(clt)
- | PC.TWhen(clt) | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
+ | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
+ | PC.TAny(clt) | PC.TStrict(clt) | PC.TLineEnd(clt)
| PC.TEllipsis(clt) (* | PC.TCircles(clt) | PC.TStars(clt) *) -> split t clt
| PC.TOEllipsis(_) | PC.TCEllipsis(_) (* clt must be context *)
split t clt
| PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
- | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+ | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TTildeEq(clt) | PC.TTildeExclEq(clt) | PC.TLogOp(_,clt)
| PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
| PC.TDmOp(_,clt) | PC.TTilde (clt) -> split t clt
- | PC.TOBrace(clt) | PC.TCBrace(clt) -> split t clt
+ | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOInit(clt) -> split t clt
| PC.TOCro(clt) | PC.TCCro(clt) -> split t clt
| PC.TPtrOp(clt) -> split t clt
let is_id = function
(PC.TIdent(_,_),_) | (PC.TMetaId(_,_,_,_),_) | (PC.TMetaFunc(_,_,_,_),_)
| (PC.TMetaLocalFunc(_,_,_,_),_) -> true
- | _ -> false in
+ | _ -> false in
let rec loop = function
[] -> []
| [x] -> [x]
(PC.TOEllipsis(_),_) (* | (PC.TOCircles(_),_) | (PC.TOStars(_),_) *)
| (PC.TPOEllipsis(_),_) (* | (PC.TOCircles(_),_) | (PC.TOStars(_),_) *)
| (PC.TEllipsis(_),_) (* | (PC.TCircles(_),_) | (PC.TStars(_),_) *)
- | (PC.TPtVirg(_),_) | (PC.TOBrace(_),_) | (PC.TCBrace(_),_)
+ | (PC.TPtVirg(_),_) | (PC.TOBrace(_),_) | (PC.TOInit(_),_)
+ | (PC.TCBrace(_),_)
| (PC.TPure,_) | (PC.TContext,_)
| (PC.Tstatic(_),_) | (PC.Textern(_),_)
| (PC.Tinline(_),_) | (PC.Ttypedef(_),_) | (PC.Tattr(_),_) -> true
| (PC.TMetaLocalIdExp(_,_,_,_,_),_)
| (PC.TMetaExpList(_,_,_,_),_)
| (PC.TMetaType(_,_,_),_)
+ | (PC.TMetaInit(_,_,_),_)
| (PC.TMetaStm(_,_,_),_)
| (PC.TMetaStmList(_,_,_),_)
- | (PC.TMetaPos(_,_,_,_),_) -> in_meta_decls
+ | (PC.TMetaPos(_,_,_,_),_) -> in_meta_decls
| _ -> false in
let redo_id ident clt v =
!Data.add_type_name ident;
let token2line (tok,_) =
match tok with
- PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
- | PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
- | PC.Tunion(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
- | PC.Tstatic(clt) | PC.Tauto(clt) | PC.Tregister(clt) | PC.Textern(clt)
+ PC.Tchar(clt) | PC.Tshort(clt) | PC.Tint(clt) | PC.Tdouble(clt)
+ | PC.Tfloat(clt) | PC.Tlong(clt) | PC.Tvoid(clt) | PC.Tstruct(clt)
+ | PC.Tunion(clt) | PC.Tenum(clt) | PC.Tunsigned(clt) | PC.Tsigned(clt)
+ | PC.Tstatic(clt) | PC.Tauto(clt) | PC.Tregister(clt) | PC.Textern(clt)
| PC.Tinline(clt) | PC.Ttypedef(clt) | PC.Tattr(_,clt) | PC.Tconst(clt)
- | PC.Tvolatile(clt)
+ | PC.Tvolatile(clt)
- | PC.TInc(clt) | PC.TDec(clt)
-
- | PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
+ | PC.TInc(clt) | PC.TDec(clt)
+
+ | PC.TIf(clt) | PC.TElse(clt) | PC.TWhile(clt) | PC.TFor(clt) | PC.TDo(clt)
| PC.TSwitch (clt) | PC.TCase (clt) | PC.TDefault (clt) | PC.TSizeof (clt)
| PC.TReturn(clt) | PC.TBreak(clt) | PC.TContinue(clt) | PC.TGoto(clt)
| PC.TIdent(_,clt)
| PC.TTypeId(_,clt) | PC.TDeclarerId(_,clt) | PC.TIteratorId(_,clt)
| PC.TMetaDeclarer(_,_,_,clt) | PC.TMetaIterator(_,_,_,clt)
- | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
+ | PC.TString(_,clt) | PC.TChar(_,clt) | PC.TFloat(_,clt) | PC.TInt(_,clt)
| PC.TOrLog(clt) | PC.TAndLog(clt) | PC.TOr(clt) | PC.TXor(clt)
- | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
- | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
- | PC.TDmOp(_,clt) | PC.TTilde (clt)
+ | PC.TAnd (clt) | PC.TEqEq(clt) | PC.TNotEq(clt) | PC.TLogOp(_,clt)
+ | PC.TShOp(_,clt) | PC.TPlus(clt) | PC.TMinus(clt) | PC.TMul(clt)
+ | PC.TDmOp(_,clt) | PC.TTilde (clt)
- | PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
+ | PC.TMetaParam(_,_,clt) | PC.TMetaParamList(_,_,_,clt)
| PC.TMetaConst(_,_,_,_,clt) | PC.TMetaExp(_,_,_,_,clt)
| PC.TMetaIdExp(_,_,_,_,clt) | PC.TMetaLocalIdExp(_,_,_,_,clt)
- | PC.TMetaExpList(_,_,_,clt)
- | PC.TMetaId(_,_,_,clt) | PC.TMetaType(_,_,clt)
+ | PC.TMetaExpList(_,_,_,clt)
+ | PC.TMetaId(_,_,_,clt) | PC.TMetaType(_,_,clt) | PC.TMetaInit(_,_,clt)
| PC.TMetaStm(_,_,clt) | PC.TMetaStmList(_,_,clt) | PC.TMetaFunc(_,_,_,clt)
| PC.TMetaLocalFunc(_,_,_,clt) | PC.TMetaPos(_,_,_,clt)
| PC.TFunDecl(clt)
- | PC.TWhen(clt) | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
+ | PC.TWhen(clt) | PC.TWhenTrue(clt) | PC.TWhenFalse(clt)
+ | PC.TAny(clt) | PC.TStrict(clt) | PC.TEllipsis(clt)
(* | PC.TCircles(clt) | PC.TStars(clt) *)
- | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
+ | PC.TOEllipsis(clt) | PC.TCEllipsis(clt)
| PC.TPOEllipsis(clt) | PC.TPCEllipsis(clt) (*| PC.TOCircles(clt)
| PC.TCCircles(clt) | PC.TOStars(clt) | PC.TCStars(clt) *)
| PC.TWhy(clt) | PC.TDotDot(clt) | PC.TBang(clt) | PC.TOPar(clt)
- | PC.TOPar0(clt) | PC.TMid0(clt) | PC.TCPar(clt)
- | PC.TCPar0(clt)
+ | PC.TOPar0(clt) | PC.TMid0(clt) | PC.TCPar(clt)
+ | PC.TCPar0(clt)
- | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
+ | PC.TOBrace(clt) | PC.TCBrace(clt) | PC.TOCro(clt) | PC.TCCro(clt)
+ | PC.TOInit(clt)
- | PC.TPtrOp(clt)
+ | PC.TPtrOp(clt)
- | PC.TDefine(clt,_) | PC.TDefineParam(clt,_,_)
+ | PC.TDefine(clt,_) | PC.TDefineParam(clt,_,_,_)
| PC.TIncludeL(_,clt) | PC.TIncludeNL(_,clt)
- | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
+ | PC.TEq(clt) | PC.TAssign(_,clt) | PC.TDot(clt) | PC.TComma(clt)
| PC.TPtVirg(clt) ->
let (_,line,_,_,_,_,_,_) = clt in Some line
| (((PC.TWhen(clt),q) as x)::xs) ->
x::(find_line_end true (token2line x) clt q xs)
| (((PC.TDefine(clt,_),q) as x)::xs)
- | (((PC.TDefineParam(clt,_,_),q) as x)::xs) ->
+ | (((PC.TDefineParam(clt,_,_,_),q) as x)::xs) ->
x::(find_line_end false (token2line x) clt q xs)
| x::xs -> x::(insert_line_end xs)
| x::xs when token2line x = line -> x :: (find_line_end inwhen line clt q xs)
| xs -> (PC.TLineEnd(clt),q)::(insert_line_end xs)
+let rec translate_when_true_false = function
+ [] -> []
+ | (PC.TWhen(clt),q)::((PC.TNotEq(_),_) as x)::(PC.TIdent("true",_),_)::xs ->
+ (PC.TWhenTrue(clt),q)::x::(translate_when_true_false xs)
+ | (PC.TWhen(clt),q)::((PC.TNotEq(_),_) as x)::(PC.TIdent("false",_),_)::xs ->
+ (PC.TWhenFalse(clt),q)::x::(translate_when_true_false xs)
+ | x::xs -> x :: (translate_when_true_false xs)
+
+(* ----------------------------------------------------------------------- *)
+
+let check_parentheses tokens =
+ let clt2line (_,line,_,_,_,_,_,_) = line in
+ let rec loop seen_open = function
+ [] -> tokens
+ | (PC.TOPar(clt),q) :: rest
+ | (PC.TDefineParam(clt,_,_,_),q) :: rest ->
+ loop (Common.Left (clt2line clt) :: seen_open) rest
+ | (PC.TOPar0(clt),q) :: rest ->
+ loop (Common.Right (clt2line clt) :: seen_open) rest
+ | (PC.TCPar(clt),q) :: rest ->
+ (match seen_open with
+ [] ->
+ failwith
+ (Printf.sprintf
+ "unexpected close parenthesis in line %d\n" (clt2line clt))
+ | Common.Left _ :: seen_open -> loop seen_open rest
+ | Common.Right open_line :: _ ->
+ failwith
+ (Printf.sprintf
+ "disjunction parenthesis in line %d column 0 matched to normal parenthesis on line %d\n" open_line (clt2line clt)))
+ | (PC.TCPar0(clt),q) :: rest ->
+ (match seen_open with
+ [] ->
+ failwith
+ (Printf.sprintf
+ "unexpected close parenthesis in line %d\n" (clt2line clt))
+ | Common.Right _ :: seen_open -> loop seen_open rest
+ | Common.Left open_line :: _ ->
+ failwith
+ (Printf.sprintf
+ "normal parenthesis in line %d matched to disjunction parenthesis on line %d column 0\n" open_line (clt2line clt)))
+ | x::rest -> loop seen_open rest in
+ loop [] tokens
+
(* ----------------------------------------------------------------------- *)
-(* process pragmas: they can only be used in + code, and adjacent to
-another + token. They are concatenated to the string representation of
-that other token. *)
+(* top level initializers: a sequence of braces followed by a dot *)
+
+let find_top_init tokens =
+ match tokens with
+ (PC.TOBrace(clt),q) :: rest ->
+ let rec dot_start acc = function
+ ((PC.TOBrace(_),_) as x) :: rest ->
+ dot_start (x::acc) rest
+ | ((PC.TDot(_),_) :: rest) as x ->
+ Some ((PC.TOInit(clt),q) :: (List.rev acc) @ x)
+ | l -> None in
+ let rec comma_end acc = function
+ ((PC.TCBrace(_),_) as x) :: rest ->
+ comma_end (x::acc) rest
+ | ((PC.TComma(_),_) :: rest) as x ->
+ Some ((PC.TOInit(clt),q) :: (List.rev x) @ acc)
+ | l -> None in
+ (match dot_start [] rest with
+ Some x -> x
+ | None ->
+ (match List.rev rest with
+ (* not super sure what this does, but EOF, @, and @@ should be
+ the same, markind the end of a rule *)
+ ((PC.EOF,_) as x)::rest | ((PC.TArob,_) as x)::rest
+ | ((PC.TArobArob,_) as x)::rest ->
+ (match comma_end [x] rest with
+ Some x -> x
+ | None -> tokens)
+ | _ ->
+ failwith "unexpected empty token list"))
+ | _ -> tokens
+
+(* ----------------------------------------------------------------------- *)
+(* Integrate pragmas into some adjacent token. + tokens are preferred. Dots
+are not allowed. *)
let rec collect_all_pragmas collected = function
- (PC.TPragma(s),_)::rest -> collect_all_pragmas (s::collected) rest
+ (PC.TPragma(s,(_,line,logical_line,offset,col,_,_,pos)),_)::rest ->
+ let i =
+ { Ast0.line_start = line; Ast0.line_end = line;
+ Ast0.logical_start = logical_line; Ast0.logical_end = logical_line;
+ Ast0.column = col; Ast0.offset = offset; } in
+ collect_all_pragmas ((s,i)::collected) rest
| l -> (List.rev collected,l)
-let rec collect_up_to_pragmas skipped = function
- [] -> None (* didn't reach a pragma, so nothing to do *)
- | ((PC.TPragma(s),_) as t)::rest ->
- let (pragmas,rest) = collect_all_pragmas [] (t::rest) in
- Some (List.rev skipped,pragmas,rest)
- | x::xs ->
- match plus_attachable x with
- PLUS -> None
- | NOTPLUS -> None
- | SKIP -> collect_up_to_pragmas (x::skipped) xs
-
-let rec collect_up_to_plus skipped = function
- [] -> failwith "nothing to attach a pragma to"
+let rec collect_pass = function
+ [] -> ([],[])
| x::xs ->
- match plus_attachable x with
- PLUS -> (List.rev skipped,x,xs)
- | NOTPLUS -> failwith "nothing to attach a pragma to"
- | SKIP -> collect_up_to_plus (x::skipped) xs
-
-let rec process_pragmas = function
- [] -> []
- | ((PC.TPragma(s),_)::_) as l ->
+ match plus_attachable false x with
+ SKIP ->
+ let (pass,rest) = collect_pass xs in
+ (x::pass,rest)
+ | _ -> ([],x::xs)
+
+let plus_attach strict = function
+ None -> NOTPLUS
+ | Some x -> plus_attachable strict x
+
+let add_bef = function Some x -> [x] | None -> []
+
+(*skips should be things like line end
+skips is things before pragmas that can't be attached to, pass is things
+after. pass is used immediately. skips accumulates. *)
+let rec process_pragmas bef skips = function
+ [] -> add_bef bef @ List.rev skips
+ | ((PC.TPragma(s,i),_)::_) as l ->
let (pragmas,rest) = collect_all_pragmas [] l in
- let (skipped,aft,rest) = collect_up_to_plus [] rest in
- let (a,b,c,d,e,strbef,straft,pos) = get_clt aft in
- skipped@
- (process_pragmas ((update_clt aft (a,b,c,d,e,pragmas,straft,pos))::rest))
- | bef::xs ->
- (match plus_attachable bef with
- PLUS ->
- (match collect_up_to_pragmas [] xs with
- Some(skipped,pragmas,rest) ->
+ let (pass,rest0) = collect_pass rest in
+ let (next,rest) =
+ match rest0 with [] -> (None,[]) | next::rest -> (Some next,rest) in
+ (match (bef,plus_attach true bef,next,plus_attach true next) with
+ (Some bef,PLUS,_,_) ->
+ let (a,b,c,d,e,strbef,straft,pos) = get_clt bef in
+ (update_clt bef (a,b,c,d,e,strbef,pragmas,pos))::List.rev skips@
+ pass@process_pragmas None [] rest0
+ | (_,_,Some next,PLUS) ->
+ let (a,b,c,d,e,strbef,straft,pos) = get_clt next in
+ (add_bef bef) @ List.rev skips @ pass @
+ (process_pragmas
+ (Some (update_clt next (a,b,c,d,e,pragmas,straft,pos)))
+ [] rest)
+ | _ ->
+ (match (bef,plus_attach false bef,next,plus_attach false next) with
+ (Some bef,PLUS,_,_) ->
let (a,b,c,d,e,strbef,straft,pos) = get_clt bef in
- (update_clt bef (a,b,c,d,e,strbef,pragmas,pos))::
- skipped@(process_pragmas rest)
- | None -> bef::(process_pragmas xs))
- | _ -> bef::(process_pragmas xs))
+ (update_clt bef (a,b,c,d,e,strbef,pragmas,pos))::List.rev skips@
+ pass@process_pragmas None [] rest0
+ | (_,_,Some next,PLUS) ->
+ let (a,b,c,d,e,strbef,straft,pos) = get_clt next in
+ (add_bef bef) @ List.rev skips @ pass @
+ (process_pragmas
+ (Some (update_clt next (a,b,c,d,e,pragmas,straft,pos)))
+ [] rest)
+ | _ -> failwith "nothing to attach pragma to"))
+ | x::xs ->
+ (match plus_attachable false x with
+ SKIP -> process_pragmas bef (x::skips) xs
+ | _ -> (add_bef bef) @ List.rev skips @ (process_pragmas (Some x) [] xs))
(* ----------------------------------------------------------------------- *)
(* Drop ... ... . This is only allowed in + code, and arises when there is
it easier to align the + and - code in context_neg and in preparation for the
isomorphisms. This shouldn't matter because the context code of the +
slice is mostly ignored anyway *)
+let minus_to_nothing l =
+ (* for cases like | <..., which may or may not arise from removing minus
+ code, depending on whether <... is a statement or expression *)
+ let is_minus tok =
+ try
+ let (d,_,_,_,_,_,_,_) = get_clt tok in
+ (match d with
+ D.MINUS | D.OPTMINUS | D.UNIQUEMINUS -> true
+ | D.PLUS | D.PLUSPLUS -> false
+ | D.CONTEXT | D.UNIQUE | D.OPT -> false)
+ with _ -> false in
+ let rec minus_loop = function
+ [] -> []
+ | (d::ds) as l -> if is_minus d then minus_loop ds else l in
+ let rec loop = function
+ [] -> []
+ | ((PC.TMid0(clt),i) as x)::t1::ts when is_minus t1 ->
+ (match minus_loop ts with
+ ((PC.TOEllipsis(_),_)::_) | ((PC.TPOEllipsis(_),_)::_)
+ | ((PC.TEllipsis(_),_)::_) as l -> x::(PC.TNothing,i)::(loop l)
+ | l -> x::(loop l))
+ | t::ts -> t::(loop ts) in
+ loop l
+
let rec drop_double_dots l =
let start = function
(PC.TOEllipsis(_),_) | (PC.TPOEllipsis(_),_)
let middle = function
(PC.TEllipsis(_),_) (* | (PC.TCircles(_),_) | (PC.TStars(_),_) *) -> true
| _ -> false in
+ let whenline = function
+ (PC.TLineEnd(_),_) -> true
+ (*| (PC.TMid0(_),_) -> true*)
+ | _ -> false in
let final = function
(PC.TCEllipsis(_),_) | (PC.TPCEllipsis(_),_)
(* | (PC.TCCircles(_),_) | (PC.TCStars(_),_) *) ->
true
| _ -> false in
+ let any_before x = start x or middle x or final x or whenline x in
+ let any_after x = start x or middle x or final x in
let rec loop ((_,i) as prev) = function
[] -> []
- | x::rest when middle prev && middle x -> (PC.TNothing,i)::x::(loop x rest)
- | x::rest when start prev && middle x -> (PC.TNothing,i)::x::(loop x rest)
- | x::rest when start prev && final x -> (PC.TNothing,i)::x::(loop x rest)
- | x::rest when middle prev && final x -> (PC.TNothing,i)::x::(loop x rest)
+ | x::rest when any_before prev && any_after x ->
+ (PC.TNothing,i)::x::(loop x rest)
| x::rest -> x :: (loop x rest) in
match l with
[] -> []
let get_s_starts (_, (s,_,(starts, ends))) =
Printf.printf "%d %d\n" starts ends; (s, starts)
-let pop2 l =
+let pop2 l =
let v = List.hd !l in
l := List.tl !l;
v
reinit();
- try parsefn lexer_function lexbuf_fake
- with
+ try parsefn lexer_function lexbuf_fake
+ with
Lexer_cocci.Lexical s ->
failwith
(Printf.sprintf "%s: lexical error: %s\n =%s\n" str s
| e -> raise e
let prepare_tokens tokens =
- insert_line_end
- (detect_types false (find_function_names (detect_attr tokens)))
+ find_top_init
+ (translate_when_true_false (* after insert_line_end *)
+ (insert_line_end
+ (detect_types false
+ (find_function_names (detect_attr (check_parentheses tokens))))))
+
+let prepare_mv_tokens tokens =
+ detect_types false (detect_attr tokens)
let rec consume_minus_positions = function
[] -> []
+ | ((PC.TOPar0(_),_) as x)::xs | ((PC.TCPar0(_),_) as x)::xs
+ | ((PC.TMid0(_),_) as x)::xs -> x::consume_minus_positions xs
| x::(PC.TPArob,_)::(PC.TMetaPos(name,constraints,per,clt),_)::xs ->
let (arity,ln,lln,offset,col,strbef,straft,_) = get_clt x in
let name = Parse_aux.clt2mcode name clt in
let any_modif rule =
let mcode x =
match Ast0.get_mcode_mcodekind x with
- Ast0.MINUS _ | Ast0.PLUS -> true
+ Ast0.MINUS _ | Ast0.PLUS _ -> true
| _ -> false in
let donothing r k e = k e in
let bind x y = x or y in
let option_default = false in
let fn =
- V0.combiner bind option_default
+ V0.flat_combiner bind option_default
mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode mcode
- mcode
donothing donothing donothing donothing donothing donothing
donothing donothing donothing donothing donothing donothing donothing
donothing donothing in
- List.exists fn.V0.combiner_top_level rule
+ List.exists fn.VT0.combiner_rec_top_level rule
+
+let eval_virt virt =
+ List.iter
+ (function x ->
+ if not (List.mem x virt)
+ then
+ failwith
+ (Printf.sprintf "unknown virtual rule %s\n" x))
+ !Flag_parsing_cocci.defined_virtual_rules
let drop_last extra l = List.rev(extra@(List.tl(List.rev l)))
let partition_either l =
let rec part_either left right = function
| [] -> (List.rev left, List.rev right)
- | x :: l ->
+ | x :: l ->
(match x with
| Common.Left e -> part_either (e :: left) right l
| Common.Right e -> part_either left (e :: right) l) in
let get_metavars parse_fn table file lexbuf =
let rec meta_loop acc (* read one decl at a time *) =
let (_,tokens) =
- tokens_all table file true lexbuf [PC.TArobArob;PC.TMPtVirg] in
- let tokens = prepare_tokens tokens in
+ Data.call_in_meta
+ (function _ ->
+ tokens_all table file true lexbuf [PC.TArobArob;PC.TMPtVirg]) in
+ let tokens = prepare_mv_tokens tokens in
match tokens with
[(PC.TArobArob,_)] -> List.rev acc
| _ ->
let tokens = prepare_tokens tokens in
match tokens with
[(PC.TArobArob, _)] -> List.rev acc
- | _ ->
+ | _ ->
let metavar = parse_one "scriptmeta" parse_fn file tokens in
meta_loop (metavar :: acc)
in
if starts_with_name
then
let (_,tokens) = get_tokens [PC.TArob] in
+ let check_name = function
+ None -> Some (mknm())
+ | Some nm ->
+ (if List.mem nm reserved_names
+ then failwith (Printf.sprintf "invalid name %s\n" nm));
+ Some nm in
match parse_one "rule name" parse_fn file tokens with
- Ast.CocciRulename (None,a,b,c,d,e) ->
- Ast.CocciRulename (Some (mknm()),a,b,c,d,e)
- | Ast.CocciRulename (Some nm,a,b,c,d,e) ->
- (if List.mem nm reserved_names
- then failwith (Printf.sprintf "invalid name %s\n" nm));
- Ast.CocciRulename (Some nm,a,b,c,d,e)
+ Ast.CocciRulename (nm,a,b,c,d,e) ->
+ Ast.CocciRulename (check_name nm,a,b,c,d,e)
+ | Ast.GeneratedRulename (nm,a,b,c,d,e) ->
+ Ast.GeneratedRulename (check_name nm,a,b,c,d,e)
| Ast.ScriptRulename(s,deps) -> Ast.ScriptRulename(s,deps)
+ | Ast.InitialScriptRulename(s) -> Ast.InitialScriptRulename(s)
+ | Ast.FinalScriptRulename(s) -> Ast.FinalScriptRulename(s)
else
Ast.CocciRulename(Some(mknm()),Ast.NoDep,[],[],Ast.Undetermined,false) in
Data.in_rule_name := false;
| _ -> failwith "Script rules cannot appear in isomorphism rules"
in
Ast0.rule_name := rule_name;
- Data.in_meta := true;
let iso_metavars =
match get_metavars PC.iso_meta_main table file lexbuf with
(iso_metavars,[]) -> iso_metavars
- | _ -> failwith "unexpected inheritance in iso" in
- Data.in_meta := false;
+ | _ -> failwith "unexpected inheritance in iso" in
(* get the rule *)
let (more,tokens) =
get_tokens
Data.in_iso := false;
existing_isos@(List.concat (List.rev res))
-let parse file =
+(* None = dependency not satisfied
+ Some dep = dependency satisfied or unknown and dep has virts optimized
+ away *)
+let eval_depend dep virt =
+ let rec loop dep =
+ match dep with
+ Ast.Dep req | Ast.EverDep req ->
+ if List.mem req virt
+ then
+ if List.mem req !Flag_parsing_cocci.defined_virtual_rules
+ then Some Ast.NoDep
+ else None
+ else Some dep
+ | Ast.AntiDep antireq | Ast.NeverDep antireq ->
+ if List.mem antireq virt
+ then
+ if not(List.mem antireq !Flag_parsing_cocci.defined_virtual_rules)
+ then Some Ast.NoDep
+ else None
+ else Some dep
+ | Ast.AndDep(d1,d2) ->
+ (match (loop d1, loop d2) with
+ (None,_) | (_,None) -> None
+ | (Some Ast.NoDep,x) | (x,Some Ast.NoDep) -> x
+ | (Some x,Some y) -> Some (Ast.AndDep(x,y)))
+ | Ast.OrDep(d1,d2) ->
+ (match (loop d1, loop d2) with
+ (None,None) -> None
+ | (Some Ast.NoDep,x) | (x,Some Ast.NoDep) | (None,x) | (x,None) -> x
+ | (Some x,Some y) -> Some (Ast.OrDep(x,y)))
+ | Ast.NoDep | Ast.FailDep -> Some dep
+ in
+ loop dep
+
+let rec parse file =
+ Lexer_cocci.init();
let table = Common.full_charpos_to_pos file in
Common.with_open_infile file (fun channel ->
let lexbuf = Lexing.from_channel channel in
(true,data) ->
(match List.rev data with
((PC.TArobArob as x),_)::_ | ((PC.TArob as x),_)::_ ->
- let iso_files =
- parse_one "iso file names" PC.include_main file data in
-
- let parse_cocci_rule old_metas
+ let include_and_iso_files =
+ parse_one "include and iso file names" PC.include_main file data in
+
+ let (include_files,iso_files,virt) =
+ List.fold_left
+ (function (include_files,iso_files,virt) ->
+ function
+ Data.Include s -> (s::include_files,iso_files,virt)
+ | Data.Iso s -> (include_files,s::iso_files,virt)
+ | Data.Virt l -> (include_files,iso_files,l@virt))
+ ([],[],[]) include_and_iso_files in
+
+ List.iter (function x -> Hashtbl.add Lexer_cocci.rule_names x ())
+ virt;
+
+ let (extra_iso_files, extra_rules, extra_virt) =
+ let rec loop = function
+ [] -> ([],[],[])
+ | (a,b,c)::rest ->
+ let (x,y,z) = loop rest in
+ (a::x,b::y,c::z) in
+ loop (List.map parse include_files) in
+
+ let parse_cocci_rule ruletype old_metas
(rule_name, dependencies, iso, dropiso, exists, is_expression) =
Ast0.rule_name := rule_name;
Data.inheritable_positions :=
rule_name :: !Data.inheritable_positions;
(* get metavariable declarations *)
- Data.in_meta := true;
let (metavars, inherited_metavars) =
- get_metavars PC.meta_main table file lexbuf in
- Data.in_meta := false;
- Hashtbl.add Data.all_metadecls rule_name metavars;
- Hashtbl.add Lexer_cocci.rule_names rule_name ();
- Hashtbl.add Lexer_cocci.all_metavariables rule_name
- (Hashtbl.fold
+ get_metavars PC.meta_main table file lexbuf in
+ Hashtbl.add Data.all_metadecls rule_name metavars;
+ Hashtbl.add Lexer_cocci.rule_names rule_name ();
+ Hashtbl.add Lexer_cocci.all_metavariables rule_name
+ (Hashtbl.fold
(fun key v rest -> (key,v)::rest)
Lexer_cocci.metavariables []);
(* get transformation rules *)
let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
- let (minus_tokens, plus_tokens) = split_token_stream tokens in
+ let (minus_tokens, _) = split_token_stream tokens in
+ let (_, plus_tokens) =
+ split_token_stream (minus_to_nothing tokens) in
+
+ (*
+ print_tokens "minus tokens" minus_tokens;
+ print_tokens "plus tokens" plus_tokens;
+ *)
let minus_tokens = consume_minus_positions minus_tokens in
let minus_tokens = prepare_tokens minus_tokens in
*)
let plus_tokens =
- process_pragmas
+ process_pragmas None []
(fix (function x -> drop_double_dots (drop_empty_or x))
(drop_when plus_tokens)) in
(*
Printf.printf "before plus parse\n";
*)
let plus_res =
- if !Flag.sgrep_mode2
+ (* put ignore_patch_or_match with * case, which is less
+ constraining *)
+ if !Flag.sgrep_mode2 or !D.ignore_patch_or_match
then (* not actually used for anything, except context_neg *)
List.map
- (Iso_pattern.rebuild_mcode None).V0.rebuilder_top_level
+ (Iso_pattern.rebuild_mcode None).VT0.rebuilder_rec_top_level
minus_res
else
if is_expression
(more, Ast0.CocciRule ((minus_res, metavars,
(iso, dropiso, dependencies, rule_name, exists)),
- (plus_res, metavars)), metavars, tokens) in
+ (plus_res, metavars), ruletype), metavars, tokens) in
+
+ let rec collect_script_tokens = function
+ [(PC.EOF,_)] | [(PC.TArobArob,_)] | [(PC.TArob,_)] -> ""
+ | (PC.TScriptData(s),_)::xs -> s^(collect_script_tokens xs)
+ | toks ->
+ List.iter
+ (function x ->
+ Printf.printf "%s\n" (token2c x))
+ toks;
+ failwith "Malformed script rule" in
let parse_script_rule language old_metas deps =
let get_tokens = tokens_script_all table file false lexbuf in
(* meta-variables *)
- Data.in_meta := true;
let metavars =
- get_script_metavars PC.script_meta_main table file lexbuf in
- Data.in_meta := false;
+ Data.call_in_meta
+ (function _ ->
+ get_script_metavars PC.script_meta_main table file lexbuf) in
let exists_in old_metas (py,(r,m)) =
let test (rr,mr) x =
(* script code *)
let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
- let data =
- match List.hd tokens with
- (PC.TScriptData(s),_) -> s
- | (PC.TArobArob,_) | (PC.TArob,_) -> ""
- | _ -> failwith "Malformed script rule" in
+ let data = collect_script_tokens tokens in
(more,Ast0.ScriptRule(language, deps, metavars, data),[],tokens) in
+ let parse_if_script_rule k language =
+ let get_tokens = tokens_script_all table file false lexbuf in
+
+ (* script code *)
+ let (more, tokens) = get_tokens [PC.TArobArob; PC.TArob] in
+ let data = collect_script_tokens tokens in
+ (more,k (language, data),[],tokens) in
+
+ let parse_iscript_rule =
+ parse_if_script_rule
+ (function (language,data) ->
+ Ast0.InitialScriptRule(language,data)) in
+
+ let parse_fscript_rule =
+ parse_if_script_rule
+ (function (language,data) ->
+ Ast0.FinalScriptRule(language,data)) in
+
let parse_rule old_metas starts_with_name =
let rulename =
get_rule_name PC.rule_name starts_with_name get_tokens file
"rule" in
match rulename with
- Ast.CocciRulename (Some s, a, b, c, d, e) ->
- parse_cocci_rule old_metas (s, a, b, c, d, e)
- | Ast.ScriptRulename (l,deps) -> parse_script_rule l old_metas deps
- | _ -> failwith "Malformed rule name"
- in
+ Ast.CocciRulename (Some s, dep, b, c, d, e) ->
+ (match eval_depend dep virt with
+ Some (dep) ->
+ parse_cocci_rule Ast.Normal old_metas (s,dep,b,c,d,e)
+ | None ->
+ D.ignore_patch_or_match := true;
+ let res =
+ parse_cocci_rule Ast.Normal old_metas
+ (s, Ast.FailDep, b, c, d, e) in
+ D.ignore_patch_or_match := false;
+ res)
+ | Ast.GeneratedRulename (Some s, dep, b, c, d, e) ->
+ (match eval_depend dep virt with
+ Some (dep) ->
+ Data.in_generating := true;
+ let res =
+ parse_cocci_rule Ast.Normal old_metas (s,dep,b,c,d,e) in
+ Data.in_generating := false;
+ res
+ | None ->
+ D.ignore_patch_or_match := true;
+ Data.in_generating := true;
+ let res =
+ parse_cocci_rule Ast.Normal old_metas
+ (s, Ast.FailDep, b, c, d, e) in
+ D.ignore_patch_or_match := false;
+ Data.in_generating := false;
+ res)
+ | Ast.ScriptRulename(l,deps) ->
+ (match eval_depend deps virt with
+ Some deps -> parse_script_rule l old_metas deps
+ | None -> parse_script_rule l old_metas Ast.FailDep)
+ | Ast.InitialScriptRulename(l) -> parse_iscript_rule l
+ | Ast.FinalScriptRulename(l) -> parse_fscript_rule l
+ | _ -> failwith "Malformed rule name" in
let rec loop old_metas starts_with_name =
(!Data.init_rule)();
(match List.hd (List.rev tokens) with
(PC.TArobArob,_) -> false
| (PC.TArob,_) -> true
- | _ -> failwith "unexpected token")
+ | _ -> failwith "unexpected token")
in
let (more, rule, metavars, tokens) =
if more then
rule::
(loop (metavars @ old_metas) (gen_starts_with_name more tokens))
- else [rule];
-
- in
-
- (iso_files, loop [] (x = PC.TArob))
+ else [rule] in
+
+ (List.fold_left
+ (function prev -> function cur -> Common.union_set cur prev)
+ iso_files extra_iso_files,
+ (* included rules first *)
+ List.fold_left (function prev -> function cur -> cur@prev)
+ (loop [] (x = PC.TArob)) (List.rev extra_rules),
+ List.fold_left (@) virt extra_virt (*no dups allowed*))
| _ -> failwith "unexpected code before the first rule\n")
| (false,[(PC.TArobArob,_)]) | (false,[(PC.TArob,_)]) ->
- ([],([] : Ast0.parsed_rule list))
+ ([],([] : Ast0.parsed_rule list),[] (*virtual rules*))
| _ -> failwith "unexpected code before the first rule\n" in
res)
(* parse to ast0 and then convert to ast *)
let process file isofile verbose =
let extra_path = Filename.dirname file in
- Lexer_cocci.init();
- let (iso_files, rules) = parse file in
+ let (iso_files, rules, virt) = parse file in
+ eval_virt virt;
let std_isos =
match isofile with
None -> []
List.map
(function
Ast0.ScriptRule (a,b,c,d) -> [([],Ast.ScriptRule (a,b,c,d))]
+ | Ast0.InitialScriptRule (a,b) -> [([],Ast.InitialScriptRule (a,b))]
+ | Ast0.FinalScriptRule (a,b) -> [([],Ast.FinalScriptRule (a,b))]
| Ast0.CocciRule
((minus, metavarsm,
(iso, dropiso, dependencies, rule_name, exists)),
- (plus, metavars)) ->
+ (plus, metavars),ruletype) ->
let chosen_isos =
parse_iso_files global_isos
(List.map (function x -> Common.Left x) iso)
failwith
("invalid iso name " ^ bad_dropped ^ " in " ^ rule_name)
with Not_found -> ());
- if List.mem "all" dropiso
- then
+ if List.mem "all" dropiso
+ then
if List.length dropiso = 1
then []
else failwith "disable all should only be by itself"
failwith
"bad list of reserved names - all must be at start" in
let minus = Test_exps.process minus in
- let minus = Compute_lines.compute_lines minus in
- let plus = Compute_lines.compute_lines plus in
+ let minus = Compute_lines.compute_lines false minus in
+ let plus = Compute_lines.compute_lines false plus in
let is_exp =
(* only relevant to Flag.make_hrule *)
(* doesn't handle multiple minirules properly, but since
let minus = Arity.minus_arity minus in
let ((metavars,minus),function_prototypes) =
Function_prototypes.process
- rule_name metavars dropped_isos minus plus in
+ rule_name metavars dropped_isos minus plus ruletype in
+ let plus = Adjust_pragmas.process plus in
(* warning! context_neg side-effects its arguments *)
- let (m,p) = List.split (Context_neg.context_neg minus plus) in
+ let (m,p) = List.split (Context_neg.context_neg minus plus) in
Type_infer.type_infer p;
- (if not !Flag.sgrep_mode2 then Insert_plus.insert_plus m p);
+ (if not !Flag.sgrep_mode2
+ then Insert_plus.insert_plus m p (chosen_isos = []));
Type_infer.type_infer minus;
let (extra_meta, minus) =
- Iso_pattern.apply_isos chosen_isos minus rule_name in
+ match (chosen_isos,ruletype) with
+ (* separate case for [] because applying isos puts
+ some restrictions on the -+ code *)
+ ([],_) | (_,Ast.Generated) -> ([],minus)
+ | _ -> Iso_pattern.apply_isos chosen_isos minus rule_name in
+ (* after iso, because iso can intro ... *)
+ let minus = Adjacency.compute_adjacency minus in
let minus = Comm_assoc.comm_assoc minus rule_name dropiso in
let minus =
if !Flag.sgrep_mode2 then minus
let minus = Simple_assignments.simple_assignments minus in
let minus_ast =
Ast0toast.ast0toast rule_name dependencies dropped_isos
- exists minus is_exp in
+ exists minus is_exp ruletype in
+
match function_prototypes with
None -> [(extra_meta @ metavars, minus_ast)]
- | Some mv_fp ->
- [(extra_meta @ metavars, minus_ast); mv_fp])
+ | Some mv_fp -> [(extra_meta @ metavars, minus_ast); mv_fp])
(* Ast0.CocciRule ((minus, metavarsm, (iso, dropiso, dependencies, rule_name, exists)), (plus, metavars))*)
rules in
let parsed = List.concat parsed in
let disjd = Disjdistr.disj parsed in
-
- let (code,fvs,neg_pos,ua,pos) = Free_vars.free_vars disjd in
+
+ let (metavars,code,fvs,neg_pos,ua,pos) = Free_vars.free_vars disjd in
if !Flag_parsing_cocci.show_SP
then List.iter Pretty_print_cocci.unparse code;
-
+
let grep_tokens =
Common.profile_code "get_constants"
(fun () -> Get_constants.get_constants code) in (* for grep *)
let glimpse_tokens2 =
- Common.profile_code "get_glimpse_constants"
- (fun () -> Get_constants2.get_constants code neg_pos) in(* for glimpse *)
- (code,fvs,neg_pos,ua,pos,grep_tokens,glimpse_tokens2)
+ Common.profile_code "get_glimpse_constants" (* for glimpse *)
+ (fun () -> Get_constants2.get_constants code neg_pos) in
+
+ (metavars,code,fvs,neg_pos,ua,pos,grep_tokens,glimpse_tokens2)