3 !a_%@sddlZddlZddlZddlZddlZddlZddlmZddlmZej dZ ej dZ Gddde Z Gdd d e Zd d Zd d ZddZddZddZddZGdddejZedkreedS)N) PATCHES_DIR)HEADERSz(?:['\"])?([\w]+)z\s*#\s*define\s*([\w]+)c@s eZdZdS) DirtyGitErrorN)__name__ __module__ __qualname__rrH/home/aniruddha/Desktop/UnitTestSetup/test/cbmc/patches/compute_patch.pyr(src@s eZdZdS)PatchCreationErrorN)rrrrrrr r +sr cCs tjdS)Nz This script generates patch files for the header files used in the cbmc proof. These patches permit setting values of preprocessor macros as part of the proof configuration. )textwrapdedentrrrr prolog.sr c Cst}tjjtjjtdd}xtj|D]\}}}d|krPtjj|d}d}nd|kr*tjj|d}d}nq*t|d`}djd d |D}tj |} || j krx.| |D]"} t j | } | r|j | jd qWWd QRXq*W|S) zCollects all define values in Makefile.json. Some of the Makefiles use # in the json to make comments. As this is non standard json, we need to remove the comment lines before parsing. Then we extract all defines from the file. z..Zproofsz Makefile.jsonDEFzMakefileCommon.jsonzDEF rcSs$g|]}|r|jjd r|qS)#)strip startswith).0linerrr Ksz$find_all_defines..N)setospathabspathjoinrwalkopenjsonloadskeysDEFINE_REGEX_MAKEFILEmatchaddgroup) definesZ proof_dirfldr_fylesfilekeysourcecontentmakefiledefineZmatchedrrr find_all_defines6s&    "r0c Csd}t|d}d}x|D]}tj|}|r|jd|kr|jjd r|}x"|jjdrpt|}||7}qPW|t j dj |jd|d7}n||7}|}qWWdQRXt|d }|j |WdQRXdS) z$Wraps all defines used in an ifndef.rrrz#ifndef\zv #ifndef {target} {original} #endif )targetZoriginalNw) rDEFINE_REGEX_HEADERr#r%lstriprrstripendswithnextr r formatwrite) r& header_fileZmodified_contentr,lastrr#Zfull_defoutputrrr manipulate_headerfileYs$     r>cCstjddgtjtjdd}tjddgtjtjdd}|jrTttjdj|j|j |jrtttjdj|j|j x&|D]}t j j |d|j krzdSqzWd S) z6Check that the header_file is not previously modified.gitstatusT)stdoutstderruniversal_newlinesz diff-filesz` Could not run git status. Exited: {} stderr: {} zd Could not run git diff-files. Exited: {} stderr: {}  F) subprocessrunDEVNULLPIPE returncoderr r r9rBrrbasenamerA)Z header_filesr@Z diff_stater;rrr header_dirtyws   rKc Cst||tjdd|gtjtjdd}tjddd|gtjtjdd}|jrdttjdj |j|j |jrt tjdj |j||j |j t jd }d |d }t jjt|}|jrt|d }|j|jWd QRXd S)zDComputes a patch enclosing defines used in CBMC proofs with #ifndef.r?ZdiffT)rArBrCZcheckoutz--z_ git diff exited with error code: {} stderr: {} z git checkout for cleaning files failed with error code: {} on file {} stderr: {} r(Z auto_patch_z.patchr3N)r>rErFrHrGrIr r r r9rBrreplacerseprrrrArr:)r&r;patchcleanedZheader_path_partZ path_nameZ patch_filerrr create_patchs&      rPcCs:t}t|s(x&|D]}t||qWnttjddS)Nz It seems like one of the header files is in dirty state. This script cannot patch files in dirty state. )r0rKrPrr r )headersr&headerrrr create_patchess  rSc@seZdZddZddZdS)TestDefineRegexesc Csd}d}d}d}d}tj|}tj|}tj|}tj|} tj|} |j||j||j||j| |j| |j|jdd|j|jdd|j|jdd|j| jdd|j| jdddS) NzrKrPrSZTestCaserTrrrrr s(    # /