123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License as published *)(* by the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* This program 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 Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)(** Main entry point of the stub parser *)openMopsa_utilsopenMopsa_c_parseropenClang_ASTopenC_ASTopenLocationletdebugfmt=Debug.debug~channel:"c_stubs_parser.main"fmtletstarts_withtextprefix=(String.lengthtext>=String.lengthprefix)&&(String.subtext0(String.lengthprefix)=prefix)letis_stub_comment(com,_)=letcomment=com.Clang_AST.com_text|>String.triminstarts_withcomment"/*$"letis_predicates_comment(com,_)=letcomment=com.Clang_AST.com_text|>String.triminstarts_withcomment"/*$="letis_directive_comment(com,_)=letcomment=com.Clang_AST.com_text|>String.triminstarts_withcomment"/*$!"exceptionStubNotFound(* Parse function's comment into a stub CST *)letrecparse_cstfunc?(selector=is_stub_comment)prjenumspredicatescache=matchHashtbl.find_optcachefunc.func_org_namewith|Somecst->cst|None->(* Find the stub of the function *)matchList.find_optselectorfunc.func_comwith|None->raiseStubNotFound|Some(com,macros)->(* Create the lexing buffer *)letcomment=com.com_textinletfile=com.com_range.range_begin.eloc_loc.loc_fileinletline=com.com_range.range_begin.eloc_loc.loc_lineinletcol=com.com_range.range_begin.eloc_loc.loc_columninletbuf=Lexing.from_stringcommentinbuf.lex_curr_p<-{pos_fname=file;pos_lnum=line;pos_bol=0;pos_cnum=col;};(* Parse the comment *)tryletcst=Parser.parse_stub(Passes.Preprocessor.readpredicatesmacrosenumsLexer.read)bufin(* Resolve scoping of variables *)letcst'=Passes.Scoping.doitcstin(* Save the stub in the cache, so it can be used later when resolving
aliases *)Hashtbl.addcachefunc.func_org_namecst';cst'with|Passes.Preprocessor.AliasFoundalias->(* Find the alias function *)beginmatchStringMap.find_optaliasprj.proj_funcswith|None->raiseStubNotFound|Somef->parse_cstfprjenumspredicatescacheend|Lexer.SyntaxErrors->letrange=Location.from_lexing_range(Lexing.lexeme_start_pbuf)(Lexing.lexeme_end_pbuf)inExceptions.syntax_errorrange"%s"s|Parser.Error->letrange=Location.from_lexing_range(Lexing.lexeme_start_pbuf)(Lexing.lexeme_end_pbuf)inExceptions.unnamed_syntax_errorrange(** Parse the stub specification from comments of a function *)letrecparse_function_comment(func:C_AST.func)?(selector=is_stub_comment)(prj:C_AST.project)(enums:Z.tC_AST.StringMap.t)(predicates:Passes.Preprocessor.predicateC_AST.StringMap.t)(cache:(string,Cst.stub)Hashtbl.t):Ast.stub=letcst=parse_cstfunc~selectorprjenumspredicatescacheindebug"stub of function %s:@\n @[%a]"func.func_org_nameCst.pp_stubcst;(* Translate CST into AST *)Passes.Cst_to_ast.doitprjfunccst(** Parse comment of a stub directive *)letparse_directive_comment(com:(Clang_AST.comment*C_AST.macroC_AST.StringMap.t)list)(range:Clang_AST.range)(prj:C_AST.project)(enums:Z.tC_AST.StringMap.t)(predicates:Passes.Preprocessor.predicateC_AST.StringMap.t)(stubs:(string,Cst.stub)Hashtbl.t):Ast.stub=(* Create a dummy init function *)letfunc={func_uid=0;func_org_name="$directive:"^(Clang_dump.string_of_rangerange);func_unique_name="$directive:"^(Clang_dump.string_of_rangerange);func_is_static=false;func_return=C_AST.T_void,C_AST.no_qual;func_parameters=[||];func_body=None;func_static_vars=[];func_local_vars=[];func_variadic=false;func_range=range;func_name_range=range;func_com=com;func_attrs=[];}inparse_function_commentfunc~selector:is_directive_commentprjenumspredicatesstubs(** Parse a comment of predicates declarations *)letparse_predicates_comment(coms:(Clang_AST.comment*Clang_AST.macroStringMap.t)list):Passes.Preprocessor.predicatelist=coms|>List.fold_left(funacc(com,macros)->ifis_predicates_comment(com,macros)then(* Create the lexing buffer *)letcomment=com.com_textinletfile=com.com_range.range_begin.eloc_loc.loc_fileinletline=com.com_range.range_begin.eloc_loc.loc_lineinletcol=com.com_range.range_begin.eloc_loc.loc_columninletbuf=Lexing.from_stringcommentinbuf.lex_curr_p<-{pos_fname=file;pos_lnum=line;pos_bol=0;pos_cnum=col;};(* Parse the comment *)tryPasses.Preprocessor.parse_predicatesLexer.readbuf@accwith|Lexer.SyntaxErrors->letrange=Location.from_lexing_range(Lexing.lexeme_start_pbuf)(Lexing.lexeme_end_pbuf)inExceptions.syntax_errorrange"%s"selseacc)[]