123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162(****************************************************************************)(* *)(* 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/>. *)(* *)(****************************************************************************)(**
Clang_parser_cache - Cache parsed AST to improve efficiency.
AST are cached in marshalized files.
We store the list of files used during parsing and check that they
have not been modified before using the cache.
*)openClang_ASTopenClang_parserletdebugfmt=Mopsa_utils.Debug.debug~channel:"c.parser_cache"fmt(** Version number.
This is checked when using the cache, and should be changed when
the signature or the AST type change to invalidate the cache.
*)letversion="Mopsa.C.AST/1"(** Source file identification. *)typefile_signature=string(* absolute filename *)*float(* last modification time *)*int(* length *)(** Parse identification. *)typesignature=string(* parser command *)*target_options(* target *)*stringarray(* parser arguments *)*file_signaturelist(* file names and timestamp *)(** Make filename absolute. *)letfile_absf=(* if Filename.is_relative f then Filename.concat (Sys.getcwd ()) f
else*)fletget_file_signature(f:string):file_signature=letf=file_absfinlets=Unix.statfinf,s.Unix.st_mtime,s.Unix.st_sizeletget_signaturecmdtgtoptsfiles:signature=cmd,tgt,opts,List.mapget_file_signaturefiles(** Checks that the signature is valid. *)letcheck_signaturecmdtgtoptssignature:bool=letcmd',tgt',opts',files'=signatureincmd=cmd'&&tgt=tgt'&&opts=opts'&&(List.for_all(funs->letf,_,_=singet_file_signaturef=s)files')(** File name of cache for a given source file name. *)letfile_cache_namefile=file^".mopsa_ast"(** Drop-in replacement to [Clang_parser.cache], but uses a cache on disk. *)letparsecmdtgtfileopts:parse_result=debug"Clang_parser_cache: parsing %s"file;(* try to read cache *)letfile_cache=file_cache_namefileindebug"Clang_parser_cache: looking for cache file %s"file_cache;letfrom_cache:parse_resultoption=try(* try cache file *)letf=Unix.openfilefile_cache[Unix.O_RDWR]0o666inUnix.lockffF_LOCK0;letcache=Unix.in_channel_of_descrfinletv=Marshal.from_channelcacheinletr=ifv<>versionthen(debug"Clang_parser_cache: %s incompatible version"file_cache;None)elseletsignature:signature=Marshal.from_channelcacheinletcheck=trycheck_signaturecmdtgtoptssignaturewith_->falseinifcheckthen((* correct signature -> use cache *)debug"Clang_parser_cache: %s found"file_cache;Some(Marshal.from_channelcache))else((* incorrect signature *)debug"Clang_parser_cache: %s incompatible signature"file_cache;None)inignore(Unix.lseekf0SEEK_SET);Unix.lockffF_ULOCK0;close_incache;rwith_->(* cache file not available *)debug"Clang_parser_cache: %s cache file not found"file_cache;Noneinmatchfrom_cachewith|Somec->c|None->(* parse *)letr=Clang_parser.parse~command:cmd~target:tgt~filename:file~args:optsinletfiles=List.sortcomparer.parse_filesinletfiles=List.filter(funx->x<>"<built-in>")filesinletc=get_signaturecmdtgtoptsfilesin(* store signature & parse result *)debug"Clang_parser_cache: storing cache to %s"file_cache;letf=Unix.openfilefile_cache[Unix.O_WRONLY;Unix.O_CREAT;Unix.O_TRUNC]0o666inletcache=Unix.out_channel_of_descrfinUnix.lockffF_LOCK0;Marshal.to_channelcacheversion[];Marshal.to_channelcachec[];Marshal.to_channelcacher[];flushcache;ignore(Unix.lseekf0SEEK_SET);Unix.lockffF_ULOCK0;close_outcache;rletparsecmdtgtenable_cachefileopts=ifenable_cachethenparsecmdtgtfileoptselseClang_parser.parse~command:cmd~target:tgt~filename:file~args:opts