123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100(****************************************************************************)(* *)(* 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 C parser
*)openMopsa_utilsmoduleStringSet=SetExt.StringSetletdebugfmt=Debug.debug~channel:"c.parser"fmt(* if only_parse is true, only parses the file without translating it to
C AST nor adding the result to the context
*)letparse_file(command:string)(file:string)?(target_options=Clang_parser.get_default_target_options())(opts:stringlist)(warn_all:bool)(enable_cache:bool)(keep_static:bool)(only_parse:bool)(ctx:Clang_to_C.context)(forced_stub_list:stringlist)=(* remove some options that are in the way *)letfiltered_opts=List.filter(funo->not(List.memo["-MF"]))optsinletopts="-fparse-all-comments"::(* needed to get all comments *)filtered_optsindebug"Parsing %s, command '%s', target '%s', argument list %a"filecommandtarget_options.target_triple(ListExt.fprintListExt.printer_list(funchs->Format.fprintfch"'%s'"s))opts;letr=Clang_parser_cache.parsecommandtarget_optionsenable_cachefile(Array.of_listopts)inletis_error=List.exists(functionClang_AST.({diag_level=Level_Fatal})->true|_->false)r.parse_diaginifnotis_errorthen(List.iter(fund->ifd.Clang_AST.diag_level=Level_Error||d.Clang_AST.diag_level=Level_Warning&&warn_allthenletpos=Location.mk_posd.diag_loc.loc_filed.diag_loc.loc_lined.diag_loc.loc_columninletrange=Location.mk_orig_rangeposposinExceptions.warn_atrange"%s"d.diag_message)r.parse_diag;ifonly_parsethen()elseClang_to_C.add_translation_unitctx(Filename.basenamefile)r.parse_declr.parse_filesr.parse_commentsr.parse_macroskeep_staticforced_stub_list)elseleterrors=List.map(fundiag->letopenClang_ASTinletpos=Location.mk_posdiag.diag_loc.loc_filediag.diag_loc.loc_linediag.diag_loc.loc_columninletrange=Location.mk_orig_rangeposposin(range,diag.diag_message))r.parse_diaginExceptions.syntax_errorserrors