U
    9%er                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dl	Z	d dl
Z
d dlmZmZmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ G d	d
 d
eZG dd deZedkre	  dS )    )
namedtupleN)dedent)TestCaseoverride_configignore_internal_warnings)jitnjit)types)default_manager)NumbaDebugInfoWarningc                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )TestDebugInfozF
    These tests only checks the compiled assembly for debuginfo.
    c                 C   s   | | ||S N)compileZinspect_asmselffnsig r   Y/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/test_debuginfo.py_getasm   s    
zTestDebugInfo._getasmc                 C   s>   | j ||d}td|tj}|d k	}| j||d| d d S )Nr   z\.section.+debugzdebug info not found in:
%s)msg)r   researchIassertEqual)r   r   r   expectasmmgotr   r   r   _check    s    zTestDebugInfo._checkc                 C   s,   t ddddd }| j|tjfdd d S )NTFnopythondebugc                 S   s   | S r   r   xr   r   r   foo'   s    z3TestDebugInfo.test_no_debuginfo_in_asm.<locals>.foor   r   r   r    r	   int32r   r&   r   r   r   test_no_debuginfo_in_asm&   s    

z&TestDebugInfo.test_no_debuginfo_in_asmc                 C   s,   t ddddd }| j|tjfdd d S )NTr!   c                 S   s   | S r   r   r$   r   r   r   r&   .   s    z0TestDebugInfo.test_debuginfo_in_asm.<locals>.foor'   r(   r*   r   r   r   test_debuginfo_in_asm-   s    

z#TestDebugInfo.test_debuginfo_in_asmc              	   C   sh   t ddT tdddd }| j|tjfdd tddd	d
d }| j|tjfdd W 5 Q R X d S )NDEBUGINFO_DEFAULT   T)r"   c                 S   s   | S r   r   r$   r   r   r   r&   7   s    z4TestDebugInfo.test_environment_override.<locals>.foor'   Fr!   c                 S   s   | S r   r   r$   r   r   r   bar=   s    z4TestDebugInfo.test_environment_override.<locals>.bar)r   r   r    r	   r)   )r   r&   r/   r   r   r   test_environment_override4   s    


z'TestDebugInfo.test_environment_overridec              	      st  t dddd  t dddd t  fdd	}td
d |tj}W 5 Q R X | ||tj ||jd }t	|}|j
|jd  jjfdd|jD }| t|d |d }g }|jD ],}|jD ] }	|	jdkr|t|	  qqd}
d}j
jd  jj}|D ]>}|
s8td|r8d}
|std| d|rd}q| |
 | | d S )NT)forceinlinec                 S   s
   t | S r   )mathsinr$   r   r   r   r/   G   s    z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.barFc                 S   s
   t | S r   )r2   cosr$   r   r   r   bazK   s    z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.bazc                    s    | }| }||fS r   r   )r%   ab)r/   r5   r   r   r&   O   s    z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.foor-   r.   r   c                    s   g | ]}|j  kr|qS r   name.0r%   r8   r   r   
<listcomp>_   s     
 zATestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.<listcomp>callz.*llvm.sin.f64.*z.*)r   r   nppiZassertPreciseEqualpy_funcinspect_llvm
signaturesllvmparse_assembly	overloadsfndescmangled_name	functionsr   lenblocksinstructionsopcodeappendstrstripr   match
assertTrue)r   r&   resultfull_irmodulefuncsfuncZf_namesZblkstmtZ	found_sinZ	found_bazZbaz_namer%   r   )r/   r5   r9   r   test_llvm_inliner_flag_conflictB   s<    






z-TestDebugInfo.test_llvm_inliner_flag_conflictN)
__name__
__module____qualname____doc__r   r    r+   r,   r0   rX   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZddiZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
ejeddd Zejeddd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&TestDebugInfoEmissionz1 Tests that debug info is emitted correctly.
    Z	NUMBA_OPT0c              
   C   s6   t dd" || ||W  5 Q R  S Q R X d S )NOPTr   )r   r   rA   r   r   r   r   _get_llvmir   s    
z!TestDebugInfoEmission._get_llvmirc                 C   s@   |  || }td}g }|D ]}||r"|| q"|S )Nz![0-9]+ =.*)r`   
splitlinesr   r   rP   rM   )r   r   r   llZmeta_remetadataliner   r   r   _get_metadata   s    

z#TestDebugInfoEmission._get_metadatac                 C   s@   t  }td}|D ]&}||}|r| \}}|||< q|S )zdGets the map of DI label to md, e.g.
        '!33' -> '!{!"branch_weights", i32 1, i32 99}'
        z(![0-9]+) = (.*))dictr   r   rP   groups)r   rc   metadata_definition_mapZmeta_definition_splitrd   matcheddbg_valinfor   r   r   _get_metadata_map   s    


z'TestDebugInfoEmission._get_metadata_mapc                 C   sJ   |  |}t }| D ],}td|}|rt|d}|| q|S )Nz!DILocation\(line: (\d+),r.   )rl   setvaluesr   rP   intgroupadd)r   rc   Z
md_def_maplinesmdr   lnr   r   r   _get_lines_from_debuginfo   s    
z/TestDebugInfoEmission._get_lines_from_debuginfoc                 C   sX   t dddd }| j|dd}|d }| d|d d	  | d
| | d| d S )NTr#   c                   S   s   d S r   r   r   r   r   r   r&      s    z/TestDebugInfoEmission.test_DW_LANG.<locals>.foor   r   r   z!0   z,!DICompileUnit(language: DW_LANG_C_plus_pluszproducer: "clang (Numba)")r   re   r   assertIn)r   r&   rc   ZDICompileUnitr   r   r   test_DW_LANG   s    
z"TestDebugInfoEmission.test_DW_LANGc           #         s  t ddddd }tjf}| j||d}| j||d}t|}|j|jd  j	j
fdd	|jD }| t|d
 |d }dd	 |jD }| t|d
 |d }	dd	 |	jD }
dddh}d}|
D ]6}|j|kr||j |sd}q|r|r| d q| |d t }td}d}|
D ]}t| | }|shd}| t fdd	|D  q(| }| t|d
 |d }t|d
d }|dkr| ||k |}|| q(t |\}| !|}dd
ddg}fdd	|D }t"t#|dd d|D ]$\}}|| }| $d| | qd}t|}|% D ]T} || }|rJ| }| t|d
 t|d }!d
 }"| |!|"  qqJ| d  dS )!z: Tests that DILocation information is reasonable.
        Tnumpy)r#   Zerror_modelc                 S   s$   | d }|d }|| }t | |S )NgGz?gQ@)print)r6   r7   cdr   r   r   r&      s
    z2TestDebugInfoEmission.test_DILocation.<locals>.foor   r   c                    s   g | ]}|j  kr|qS r   r8   r:   r8   r   r   r<      s     
 z9TestDebugInfoEmission.test_DILocation.<locals>.<listcomp>r.   c                 S   s   g | ]}|qS r   r   r:   r   r   r   r<      s     c                 S   s   g | ]}|j d kr|qS )r=   )rL   r:   r   r   r   r<      s     
 ZfaddZfmulZfdivFzMath opcodes are not contiguouszMath opcodes were not foundz.*!dbg (![0-9]+).*$)zalloca zstore c                    s   g | ]}| kqS r   r   r:   )inst_as_strr   r   r<      s     Nrw      c                    s   g | ]} d  | qS )rw   r   r:   )pysrc_line_startr   r   r<     s     c                 S   s   t | dd  S Nr.   )ro   r$   r   r   r   <lambda>      z7TestDebugInfoEmission.test_DILocation.<locals>.<lambda>)keyzline: z1.*!DILocalVariable\(name: "a",.*line: ([0-9]+),.*z%Assertion on DILocalVariable not made)&r   r	   float64re   r`   rC   rD   rE   rB   rF   rG   rH   r   rI   rJ   assertGreaterrK   rL   removeZfailassertFalserm   r   r   rN   rP   rQ   anyrg   ro   rq   inspectgetsourcelinesrl   zipsortedrx   rn   )#r   r&   r   rc   rS   rT   rU   rV   rJ   blockZinstrsZ	op_expectstartedr%   Zline2dbgZ
re_dbg_reffoundinstrri   acceptedrg   rj   Zint_dbg_valpysrcrh   offsetsZ
pyln_rangekline_noZdilocation_infoexprZmatch_local_var_aentryZdbg_lineZdefliner   )r   r9   r   r   test_DILocation   s    











z%TestDebugInfoEmission.test_DILocation)envvarsc                    s&  t dddd }|d ||jd }t|}|j|jd  jj  fdd|jD }| 	t
|d	 |d }d
d |jD }| 	t
|d |\}}dd |jD }	|	d }
| 	|
jd dd |
jD }| 	t
|d	 |d }t|dd  }| 	||j | t|
| d S )NTrv   c                 S   s   | d S r   r   r6   r   r   r   r&   @  s    z<TestDebugInfoEmission.test_DILocation_entry_blk.<locals>.foo{   r   c                    s   g | ]}|j  kr|qS r   r8   r:   r8   r   r   r<   T  s     
 zCTestDebugInfoEmission.test_DILocation_entry_blk.<locals>.<listcomp>r.   c                 S   s   g | ]}|qS r   r   r:   r   r   r   r<   W  s     rw   c                 S   s   g | ]}|qS r   r   r:   r   r   r   r<   ]  s     r~   brc                 S   s   g | ]}|qS r   r   r:   r   r   r   r<   `  s     :)r   rA   rB   rC   rD   rE   rF   rG   rH   r   rI   rJ   rK   rL   ZoperandsrN   splitrO   r9   rQ   endswith)r   r&   rS   rT   rU   rV   rJ   Zentry_blockZ
body_blockZentry_instrZujmpZujmp_operandsZtarget_datatargetr   r8   r   test_DILocation_entry_blk0  s*    

z/TestDebugInfoEmission.test_DILocation_entry_blkc                    s   t dddd  t dd fdd}tjddd f}| j||d	}d
}| D ]*}| }|drT| |d |d7 }qT| |d
 dS )z This tests that decref's generated from `ir.Del`s as variables go
        out of scope do not have debuginfo associated with them (the location of
        `ir.Del` is an implementation detail).
        Trv   c                  W   s   d S r   r   r$   r   r   r   sinkp  s    z:TestDebugInfoEmission.test_DILocation_decref.<locals>.sinkc                    s0   | | f}| d dkr  | dS |d d }|S )Nr      r   )r6   r%   zr   r   r   r&   u  s    z9TestDebugInfoEmission.test_DILocation_decref.<locals>.fooNr.   r   r   zcall void @NRT_decrefz.*meminfo\.[0-9]+\)$)	r   r	   r   r`   ra   rO   
startswithassertRegexr   )r   r&   r   rS   countrd   Zline_strippedr   r   r   test_DILocation_decrefi  s    


z,TestDebugInfoEmission.test_DILocation_decrefc                 C   s   t dddd }tjf}| j||d}t|\}}d}t|}t }|D ]<}	|	|	}
|
rN|

 }| t|d |t|d  qN| t|d	 | || d
S )z Tests that DILocation information for undefined vars is associated
        with the line of the function definition (so it ends up in the prologue)
        Trv   c                 S   s$   | r| dkrd}|S |d7 }|S d S )Nr   r.   r   )nr|   r   r   r   r&     s    z<TestDebugInfoEmission.test_DILocation_undefined.<locals>.foor   z:.*!DILocalVariable\(name: "c\$?[0-9]?",.*line: ([0-9]+),.*r.   r   r   N)r   r	   intpre   r   r   r   r   rm   rP   rg   r   rI   rq   ro   rx   )r   r&   r   rc   r   r   r   matcherassociated_linesrs   rP   rg   r   r   r   test_DILocation_undefined  s     



z/TestDebugInfoEmission.test_DILocation_undefinedc                 C   s   t dddd }tjf}| j||d}t|\}}d}t|}t }|D ]<}	|	|	}
|
rN|

 }| t|d |t|d  qN| t|d	 t }t|D ]\}}d
|kr|||  q| t|d	 | || dS )zi Tests that DILocation information for versions of variables matches
        up to their definition site.Trv   c                 S   s"   | r
d}nd}d}d}d}d}|S )N   r.   rw   r      r   )r   r|   Zpy310_defeat1Zpy310_defeat2Zpy310_defeat3Zpy310_defeat4r   r   r   r&     s    zFTestDebugInfoEmission.test_DILocation_versioned_variables.<locals>.foor   z9.*!DILocalVariable\(name: "c\$[0-9]?",.*line: ([0-9]+),.*r.   r   rw   zc = N)r   r	   r   re   r   r   r   r   rm   rP   rg   r   rI   rq   ro   	enumerate)r   r&   r   rc   r   r   r   r   r   rs   rP   rg   Zpy_linesixZpylnr   r   r   #test_DILocation_versioned_variables  s*    


z9TestDebugInfoEmission.test_DILocation_versioned_variablesc                    s  t dd}tj|dddtj|dddtj|dd	d
tj|ddd
tj|ddd
tj|ddd
tj|dd	dtj	|dddtj
|dddtj|dddtj|dddtj|dddi}| D ]\ }tdd fdd}| j|dd}| |}| D ]\}}d|kr|| }	 q,qds,td td!|	 d }
||
 }d"|jkrd#|j d$|j d%|j d&}| || q|jdkrd'nd(}d)|j d*|j d+| d,| d-|j 
}| || qd.S )/z@ Tests that dwarf info is correctly emitted for numeric scalars.DIzname bits encodingfloat32    ZDW_ATE_floatr   @   int8   ZDW_ATE_signedint16   r)   int64uint8ZDW_ATE_unsigneduint16uint32uint64	complex64ZDW_TAG_structure_type
complex128   Trv   c                     s    d} | S )N
   r   r   tyr   r   r&     s    z7TestDebugInfoEmission.test_numeric_scalars.<locals>.foor   r   DILocalVariable(name: "a"r   missing DILocalVariable 'a'.*type: (![0-9]+).*ZDW_ATEz!DIBasicType(name: "	", size: z, encoding: )floatdoublezdistinct !DICompositeType(tag: z	, name: "z ({z, z})", size: N)r   r>   r   r   r   r   r)   r   r   r   r   r   r   r   itemsr   re   rl   AssertionErrorr   rP   rg   encodingr9   bitsr   rx   )r   r   Z
type_infosZ
dwarf_infor&   rc   rh   r   vlvartype_marker	type_declexpectedZraw_fltr   r   r   test_numeric_scalars  s^    
 
 
 
 
 
 
 
 
 
  
(z*TestDebugInfoEmission.test_numeric_scalarsc              	   C   s6  t dddd }| j|dd}| |}| D ]\}}d|kr2|| } q\q2ds\td	td
| d }|| }| d| | dt	t
jd d d d df  | td}	|	| }
| t|
d |
d }|| }d}t|}|| }| |d k	 tt
jd d d d df }| t|t|j t
jj}d| d}d| d| d}d| d| dd|  d| d	}|||||||d}d}t|}t|jD ]x\}}|||  }| d| | d| d| ||}| }| t|d |d }|| }| |||  qd S )NTrv   c                  S   s   t jdt jd} | S )N)rw   r   )Zdtype)r>   Zonesr   r   r   r   r   r&     s    z.TestDebugInfoEmission.test_arrays.<locals>.foor   r   r   r   r   r   z+!DICompositeType(tag: DW_TAG_structure_typezname: "r.   z.*elements: (![0-9]+),.*zN!{(![0-9]+), (![0-9]+), (![0-9]+), (![0-9]+), (![0-9]+), (![0-9]+), (![0-9]+)}zC!DIDerivedType\(tag: DW_TAG_pointer_type, baseType: ![0-9]+, size: z\)z!DIBasicType\(name: "intr   z, encoding: DW_ATE_signed\)z>!DICompositeType\(tag: DW_TAG_array_type, name: "UniTuple\(intz x 2\) \(\[2 x iz \]\)", baseType: ![0-9]+, size: rw   z), elements: ![0-9]+, identifier: "\[2 x iz\]"\))ZmeminfoparentZnitemsitemsizedatashapestridesz*!DIDerivedType\(.*, baseType: (![0-9]+),.*ZDIDerivedType")r   re   rl   r   r   r   rP   rg   rx   rN   r	   r   r   r   rI   assertIsNotNoner
   lookup_fieldsr   Zbitwidthr   r   )r   r&   rc   rh   r   r   r   r   r   Zmatch_elementsZelem_matchesZ
elem_matchZstruct_markersZstruct_patternZmatch_structZstruct_member_matchesZ
data_modelZptr_sizeZptr_reZint_reZ	utuple_rer   Zbase_type_patternZbase_type_matcherr   fieldZderived_typeZbase_type_matchZbase_type_matchesZbase_type_markerZ	data_typer   r   r   test_arrays  s`    

*

"	

z!TestDebugInfoEmission.test_arraysc              
      s\   fdd}dd }dd }t dd|}t ddd	|}t dddd
|}|jj}i }td|d ht tdgd||< ttt|d |d tdgt d||< td|d htdgtdgd||< | }	| D ]|\}
} |
jb |
 } 	||	  	||
|d  ||
}|d } 	||@ | |d } 
||@  W 5 Q R X qd S )Nc                    s      | | jd } |}|S )Nr   )re   rB   ru   )r   rc   rr   r   r   r   get_debug_linesZ  s    
zATestDebugInfoEmission.test_debug_optnone.<locals>.get_debug_linesc                 S   s>   | j | jd  }|j}|j|jj} td| j	
 }|S )Nr       )rE   rB   ZlibraryZ_final_moduleZget_functionrF   rG   rm   join
attributesr   )r   Zcreslibattrsr   r   r   get_func_attrs_  s
    z@TestDebugInfoEmission.test_debug_optnone.<locals>.get_func_attrsc                  S   s"   d} d}t | D ]}||7 }q|S )Nr   r   )range)r   r|   ir   r   r   r&   f  s
    
z5TestDebugInfoEmission.test_debug_optnone.<locals>.fooTrv   )r#   _dbg_optnone)r#   r   r1   r   r   s   optnone)rr   must_have_attrsmust_not_have_attrsr.      s   alwaysinlinerr   r   r   )r   __code__co_firstlinenorf   rm   r   r   ZsubTestZtargetoptionsr   r   )r   r   r   r&   Z	foo_debugZfoo_debug_optnoneZfoo_debug_optnone_inline	firstlineZexpected_infoZexpected_retZudtr   r   r   Z	must_haveZmust_not_haver   r   r   test_debug_optnoneY  sL    




z(TestDebugInfoEmission.test_debug_optnonec              	   C   sT  t ddddd}tdd |  W 5 Q R X | j|td fd}| |}g }|D ]}d|krX|| qX| t|d |	 }t
d	|}| | | }| t|d |d
 }	||	 }
t
d|
}| | | }| t|d dd |d
 dD d }|| }d}| || t
|| d
 }|| }| |d d S )NTrv   c                 S   s   d S r   r   )missingr   r   r   r&     s    z3TestDebugInfoEmission.test_omitted_arg.<locals>.foor-   r.   r   ZDISubroutineTypez(.*!DISubroutineType\(types: ([!0-9]+)\)$r   z!{(.*)}c                 S   s   g | ]}|  qS r   )rO   r:   r   r   r   r<     s     z:TestDebugInfoEmission.test_omitted_arg.<locals>.<listcomp>,r~   zy^.*!DICompositeType\(tag: DW_TAG_structure_type, name: "Anonymous struct \({}\)", elements: (![0-9]+), identifier: "{}"\)z!{})N)r   r   re   r	   ZOmittedrl   rM   r   rI   popr   rP   r   rg   r   r   )r   r&   rc   rh   Z
tmp_disubrrs   ZdisubrZdisubr_matchedZdisubr_groupsZdisubr_metaZdisubr_typesZdisubr_types_matchedZdisubr_types_groupsZ	md_fn_argZarg_tyZexpected_arg_tyZ
md_base_tyZbase_tyr   r   r   test_omitted_arg  s>    


z&TestDebugInfoEmission.test_omitted_argc              	   C   s   d}t  }tt|i | tdd|d }tjdd}tdt t  |  W 5 Q R X | 	t
|d |d }| 	|jt t|j}| d	| | t|j| d S )
Nz1
        def foo():
            return 1
        Trv   r&   recordalwaysr.   r   z"Could not find source for function)rf   execr   r   warningscatch_warningssimplefilterr   r   r   rI   categoryrN   messagerx   r@   )r   Zstrsrclr&   wr   r   r   r   r   test_missing_source  s    
z)TestDebugInfoEmission.test_missing_sourcec              	   C   s   t dddd }tjdd}tdt t  |  W 5 Q R X | t|d | ||j	d }| 
|}| t|d d S )	NTrv   c                   S   s   dS r   r   r   r   r   r   r&     s    zCTestDebugInfoEmission.test_irregularly_indented_source.<locals>.foor   r   r   r.   )r   r   r   r  r   r   r   rI   re   rB   ru   )r   r&   r  rc   rr   r   r   r    test_irregularly_indented_source  s    

z6TestDebugInfoEmission.test_irregularly_indented_sourceN)rY   rZ   r[   r\   Z_NUMBA_OPT_0_ENVr`   re   rl   ru   ry   r   r   Zrun_test_in_subprocessr   r   r   r   r   r   r   r   r  r  r   r   r   r   r]   {   s(   	{

8

  .7JA/r]   __main__)collectionsr   r   r   rz   r>   r2   textwrapr   Zunittestr   Znumba.tests.supportr   r   r   Znumbar   r   Z
numba.corer	   Znumba.core.datamodelr
   Znumba.core.errorsr   Zllvmlite.bindingZbindingrC   r   r]   rY   mainr   r   r   r   <module>   s*   d    ~