o
    1&iw                     @   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rhe	  dS 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   EC:\wamp64\www\opt\env\Lib\site-packages\numba/tests/test_debuginfo.py_getasm   s   

zTestDebugInfo._getasmc                 C   s>   | j ||d}td|tj}|d u}| 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 r   r   xr   r   r   foo'      z3TestDebugInfo.test_no_debuginfo_in_asm.<locals>.foor   r   r   r    r	   int32r   r'   r   r   r   test_no_debuginfo_in_asm&      

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   r$   r   r   r%   r   r   r   r'   .   r(   z0TestDebugInfo.test_debuginfo_in_asm.<locals>.foor)   r*   r,   r   r   r   test_debuginfo_in_asm-   r.   z#TestDebugInfo.test_debuginfo_in_asmc                 C   s~   t dd0 tdddd }| j|tjfdd tddd	d
d }| j|tjfdd W d    d S 1 s8w   Y  d S )NDEBUGINFO_DEFAULT   T)r"   c                 S   r$   r   r   r%   r   r   r   r'   7   r(   z4TestDebugInfo.test_environment_override.<locals>.foor)   Fr!   c                 S   r$   r   r   r%   r   r   r   bar=   r(   z4TestDebugInfo.test_environment_override.<locals>.bar)r   r   r    r	   r+   )r   r'   r2   r   r   r   test_environment_override4   s   


"z'TestDebugInfo.test_environment_overridec                    s~  t dddd  t dddd t  fdd	}td
d |tj}W d    n1 s0w   Y  | ||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|	  qvqqd}
d}j
jd  jj}|D ]}|
std|rd}
|std| d|rd}q| |
 | | d S )NT)forceinlinec                 S   
   t | S r   )mathsinr%   r   r   r   r2   G      
z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.barFc                 S   r5   r   )r6   cosr%   r   r   r   bazK   r8   z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.bazc                    s    | }| }||fS r   r   )r&   ab)r2   r:   r   r   r'   O   s   z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.foor0   r1   r   c                       g | ]	}|j  kr|qS r   name.0r&   r>   r   r   
<listcomp>_       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   )r2   r:   r?   r   test_llvm_inliner_flag_conflictB   sD   






z-TestDebugInfo.test_llvm_inliner_flag_conflictN)
__name__
__module____qualname____doc__r   r    r-   r/   r3   r_   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%d& Zd'S )(TestDebugInfoEmissionz1 Tests that debug info is emitted correctly.
    Z	NUMBA_OPT0c                 C   sB   t dd || ||W  d    S 1 sw   Y  d S )NZOPTr   )r   r   rH   r   r   r   r   _get_llvmir   s   
$z!TestDebugInfoEmission._get_llvmirc                 C   s@   |  || }td}g }|D ]}||r|| q|S )Nz![0-9]+ =.*)rf   
splitlinesr   r   rW   rT   )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   rW   groups)r   ri   metadata_definition_mapZmeta_definition_splitrj   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+),r1   )rr   setvaluesr   rW   intgroupadd)r   ri   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      d S r   r   r   r   r   r   r'      r(   z/TestDebugInfoEmission.test_DW_LANG.<locals>.foor   r   r   z!0   z,!DICompileUnit(language: DW_LANG_C_plus_pluszproducer: "clang (Numba)")r   rk   r   assertIn)r   r'   ri   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 }
h d}d}|
D ]}|j|v rw||j |svd}qe|r|r| d qe| |d t }td}d}|
D ]H}t| | }|sd}| t fdd	|D  q| }| t|d
 |d }t|d
d }|dkr| ||k |}|| qt |\}| !|}g d}fdd	|D }t"t#|dd d|D ]\}}|| }| $d| | q d}t|}|% D ]*} || }|rG| }| t|d
 t|d }!d
 }"| |!|"  dS q| 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)r;   r<   cdr   r   r   r'      s
   z2TestDebugInfoEmission.test_DILocation.<locals>.foor   r   c                    r=   r   r>   r@   r>   r   r   rB      rC   z9TestDebugInfoEmission.test_DILocation.<locals>.<listcomp>r1   c                 S      g | ]}|qS r   r   r@   r   r   r   rB          c                 S   s   g | ]	}|j d vr|qS ))rD   loadstore)rS   r@   r   r   r   rB      s    >   ZfmulZfdivZfaddFzMath opcodes are not contiguouszMath opcodes were not foundz.*!dbg (![0-9]+).*$)zalloca zstore c                    s   g | ]}| v qS r   r   r@   )inst_as_strr   r   rB          N)r   r1   r~      c                    s   g | ]} d  | qS )r~   r   r@   )pysrc_line_startr   r   rB     s    c                 S   s   t | dd  S Nr1   )ru   r%   r   r   r   <lambda>  r   z7TestDebugInfoEmission.test_DILocation.<locals>.<lambda>)keyzline: z1.*!DILocalVariable\(name: "a",.*line: ([0-9]+),.*z%Assertion on DILocalVariable not made)&r   r	   float64rk   rf   rJ   rK   rL   rI   rM   rN   rO   r   rP   rQ   assertGreaterrR   rS   removeZfailassertFalsers   r   r   rU   rW   rX   anyrm   ru   rw   inspectgetsourcelinesrr   zipsortedr   rt   )#r   r'   r   ri   rZ   r[   r\   r]   rQ   blockZinstrsZ	op_expectstartedr&   Zline2dbgZ
re_dbg_reffoundinstrro   acceptedrm   rp   Zint_dbg_valpysrcrn   offsetsZ
pyln_rangekline_noZdilocation_infoexprZmatch_local_var_aentryZdbg_lineZdefliner   )r   r?   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 )NTr|   c                 S   s   | d S r   r   r;   r   r   r   r'   A  s   z<TestDebugInfoEmission.test_DILocation_entry_blk.<locals>.foo{   r   c                    r=   r   r>   r@   r>   r   r   rB   U  rC   zCTestDebugInfoEmission.test_DILocation_entry_blk.<locals>.<listcomp>r1   c                 S   r   r   r   r@   r   r   r   rB   X  r   r~   c                 S   r   r   r   r@   r   r   r   rB   ^  r   r   brc                 S   r   r   r   r@   r   r   r   rB   a  r   :)r   rH   rI   rJ   rK   rL   rM   rN   rO   r   rP   rQ   rR   rS   ZoperandsrU   splitrV   r?   rX   endswith)r   r'   rZ   r[   r\   r]   rQ   Zentry_blockZ
body_blockZentry_instrZujmpZujmp_operandsZtarget_datatargetr   r>   r   test_DILocation_entry_blk1  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r?| |d |d7 }q*| |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).
        Tr|   c                  W   r}   r   r   r%   r   r   r   sinkq  r(   z:TestDebugInfoEmission.test_DILocation_decref.<locals>.sinkc                    s0   | | f}| d dkr | dS |d d }|S )Nr      r   )r;   r&   zr   r   r   r'   v  s   z9TestDebugInfoEmission.test_DILocation_decref.<locals>.fooNr1   r   r   zcall void @NRT_decrefz.*meminfo\.[0-9]+\)$)	r   r	   r   rf   rg   rV   
startswithassertRegexr   )r   r'   r   rZ   countrj   Zline_strippedr   r   r   test_DILocation_decrefj  s   

z,TestDebugInfoEmission.test_DILocation_decrefc                 C   s   t dddd }tjf}| j||d}t|\}}d}t|}t }|D ]}	|	|	}
|
rE|

 }| t|d |t|d  q'| 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)
        Tr|   c                 S   s    | r
| dkrd}|S |d7 }|S )Nr   r1   r   )nr   r   r   r   r'     s   z<TestDebugInfoEmission.test_DILocation_undefined.<locals>.foor   z:.*!DILocalVariable\(name: "c\$?[0-9]?",.*line: ([0-9]+),.*r1   r   r   N)r   r	   intprk   r   r   r   r   rs   rW   rm   r   rP   rw   ru   r   )r   r'   r   ri   r   r   r   matcherassociated_linesry   rW   rm   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 ]}	|	|	}
|
rE|

 }| t|d |t|d  q'| t|d	 t }t|D ]\}}d
|v rd|||  qU| t|d	 | || dS )zi Tests that DILocation information for versions of variables matches
        up to their definition site.Tr|   c                 S   s"   | rd}nd}d}d}d}d}|S )N   r1   r~   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]+),.*r1   r   r~   zc = N)r   r	   r   rk   r   r   r   r   rs   rW   rm   r   rP   rw   ru   	enumerate)r   r'   r   ri   r   r   r   r   r   ry   rW   rm   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|v r|| }	 nq~J d td!|	 d }
||
 }d"|jv rd#|j d$|j d%|j d&}| || q_|jdkrd'nd(}d)|j d*|j d+| d,| d-|j 
}| || q_d.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   Tr|   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   rE   r   r   r   r   r+   r   r   r   r   r   r   r   itemsr   rk   rr   r   rW   rm   encodingr?   bitsr   r   )r   r   Z
type_infosZ
dwarf_infor'   ri   rn   r   vlvartype_marker	type_declexpectedZraw_fltr   r   r   test_numeric_scalars  sh   





z*TestDebugInfoEmission.test_numeric_scalarsc              	   C   s0  t dddd }| j|dd}| |}| D ]\}}d|v r'|| } nqJ d	td
| d }|| }| d| | dtt	j
d d d d df  | td}	|	| }
| t|
d |
d }|| }d}t|}|| }| |d u 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 ];\}}|||  }| d| | d| d| ||}| }| t|d |d }|| }| |||  qd S )NTr|   c                  S   s   t jdt jd} | S )N)r~   r   )Zdtype)rE   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: "r1   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: r~   z), elements: ![0-9]+, identifier: "\[2 x iz\]"\))ZmeminfoparentZnitemsitemsizedatashapestridesz*!DIDerivedType\(.*, baseType: (![0-9]+),.*ZDIDerivedType")r   rk   rr   r   r   rW   rm   r   rU   r	   r   r   r   rP   assertIsNotNoner
   lookup_fieldsr   Zbitwidthr   r   )r   r'   ri   rn   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  sx   

*

	

z!TestDebugInfoEmission.test_arraysc              	      sp   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 ]H\}
} |
j6 |
 } 	||	  	||
|d  ||
}|d } 	||@ | |d } 
||@  W d    n1 sw   Y  qmd S )Nc                    s      | | jd } |}|S )Nr   )rk   rI   r{   )r   ri   rx   r   r   r   get_debug_lines[  s   
zATestDebugInfoEmission.test_debug_optnone.<locals>.get_debug_linesc                 S   s>   | j | jd  }|j}|j|jj} td| j	
 }|S )Nr       )rL   rI   ZlibraryZ_final_moduleZget_functionrM   rN   rs   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'   g  s
   
z5TestDebugInfoEmission.test_debug_optnone.<locals>.fooTr|   r#   _dbg_optnone)r#   r   r4   r   r   s   optnone)rx   must_have_attrsmust_not_have_attrsr1      s   alwaysinlinerx   r   r   )r   __code__co_firstlinenorl   rs   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_optnoneZ  sR   




z(TestDebugInfoEmission.test_debug_optnonec                 C   sh  t ddddd}tdd |  W d    n1 sw   Y  | j|td fd}| |}g }|D ]}d|v rA|| q6| t|d |	 }t
d	|}| | | }| t|d |d
 }	||	 }
t
d|
}| | | }| t|d dd |d
 dD d }|| }d}| || t
|| d
 }|| }| |d d S )NTr|   c                 S   r}   r   r   )missingr   r   r   r'     r(   z3TestDebugInfoEmission.test_omitted_arg.<locals>.foor0   r1   r   ZDISubroutineTypez(.*!DISubroutineType\(types: ([!0-9]+)\)$r   z!{(.*)}c                 S   s   g | ]}|  qS r   )rV   r@   r   r   r   rB     r   z:TestDebugInfoEmission.test_omitted_arg.<locals>.<listcomp>,r   zy^.*!DICompositeType\(tag: DW_TAG_structure_type, name: "Anonymous struct \({}\)", elements: (![0-9]+), identifier: "{}"\)z!{}r   )r   r   rk   r	   ZOmittedrr   rT   r   rP   popr   rW   r   rm   r   r   )r   r'   ri   rn   Z
tmp_disubrry   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  sB   



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 d    n1 s3w   Y  | 	t
|d |d }| 	|jt t|j}| d	| | t|j| d S )
Nz1
        def foo():
            return 1
        Tr|   r'   recordalwaysr1   r   z"Could not find source for function)rl   execr   r   warningscatch_warningssimplefilterr   r   r   rP   categoryrU   messager   rG   )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 d    n1 s&w   Y  | t|d | ||j	d }| 
|}| t|d d S )	NTr|   c                   S   s   dS r   r   r   r   r   r   r'     s   zCTestDebugInfoEmission.test_irregularly_indented_source.<locals>.foor  r  r   r1   )r   r
  r  r  r   r   r   rP   rk   rI   r{   )r   r'   r  ri   rx   r   r   r    test_irregularly_indented_source  s   

z6TestDebugInfoEmission.test_irregularly_indented_sourcec                 C   sT   t dddddd }tt|  d }| D ]}d|v r'| d| qd S )	Nzint64(boolean, boolean)Tr   c                 S   s   | r|rdS dS )Nr1   r~   r   )Zcond1Zcond2r   r   r   choice  s   zBTestDebugInfoEmission.test_no_if_op_bools_declared.<locals>.choicer1   zllvm.dbg.declarebool)r   nextiterrH   r   rg   ZassertNotIn)r   r  Zllvm_irrj   r   r   r   test_no_if_op_bools_declared  s   
z2TestDebugInfoEmission.test_no_if_op_bools_declaredN)r`   ra   rb   rc   Z_NUMBA_OPT_0_ENVrf   rk   rr   r{   r   r   r   Zrun_test_in_subprocessr   r   r   r   r   r   r  r  r  r  r  r   r   r   r   rd   {   s,    	
|

8
  .7JA/rd   __main__)collectionsr   r   r   r   rE   r6   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bindingrJ   r   rd   r`   mainr   r   r   r   <module>   s0    d     