U
    ,dq                     @   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   >/tmp/pip-unpacked-wheel-eu7e0c37/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   expectZ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   )r1   cosr#   r   r   r   bazK   s    z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.bazc                    s    | }| }||fS r   r   )r$   ab)r.   r4   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$   r7   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blkZstmtZ	found_sinZ	found_bazZbaz_namer$   r   )r.   r4   r8   r   test_llvm_inliner_flag_conflictB   s<    






z-TestDebugInfo.test_llvm_inliner_flag_conflictN)
__name__
__module____qualname____doc__r   r   r*   r+   r/   rV   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   r@   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   rO   rL   )r   r   r   Z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   rO   groups)r   r`   metadata_definition_mapZmeta_definition_splitra   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-   )ri   setvaluesr   rO   intgroupadd)r   r`   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   rb   r   assertIn)r   r%   r`   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 }d}| d|d| t }td}d}|
D ]}t| | }|s:d}| t fdd	|D  q| }| t|d
 |d }t|d
d }|dkr| ||k |}|| qt|\}| |}dd
ddg}fdd	|D }t t!|dd d|D ]$\}}|| }| d| | qd}t|}|" D ]T}||}|r| }| t|d
 t|d } d
 }!| | |!  qz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)r5   r6   cdr   r   r   r%      s
    z2TestDebugInfoEmission.test_DILocation.<locals>.foor   r   c                    s   g | ]}|j  kr|qS r   r7   r9   r7   r   r   r;      s     
 z9TestDebugInfoEmission.test_DILocation.<locals>.<listcomp>r-   c                 S   s   g | ]}|qS r   r   r9   r   r   r   r;      s     c                 S   s   g | ]}|j d kr|qS )r<   rK   r9   r   r   r   r;      s     
 c                 S   s   g | ]
}|j qS r   r{   r9   r   r   r   r;      s     )ZfaddZfmulZfdiv z.*!dbg (![0-9]+).*$)zalloca zstore c                    s   g | ]}| kqS r   r   r9   )inst_as_strr   r   r;      s     Nrt      c                    s   g | ]} d  | qS )rt   r   r9   )pysrc_line_startr   r   r;     s     c                 S   s   t | dd  S Nr-   )rl   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	   float64rb   r^   rB   rC   rD   rA   rE   rF   rG   r   rH   rI   assertGreaterrJ   ru   joinrj   r   r   rM   rO   rP   anyrd   rl   rn   inspectgetsourcelinesri   zipsortedrk   Zfail)"r   r%   r   r`   rR   rS   rT   rU   rI   blockZinstrsZop_seqZ	op_expectZline2dbgZ
re_dbg_reffoundinstrrf   acceptedrd   rg   Zint_dbg_valpysrcre   offsetsZ
pyln_rangekZline_noZdilocation_infoexprZmatch_local_var_aentryZdbg_lineZdefliner   )r~   r8   r   r   test_DILocation   sx    









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 )NTrs   c                 S   s   | d S r   r   r5   r   r   r   r%   9  s    z<TestDebugInfoEmission.test_DILocation_entry_blk.<locals>.foo{   r   c                    s   g | ]}|j  kr|qS r   r7   r9   r7   r   r   r;   M  s     
 zCTestDebugInfoEmission.test_DILocation_entry_blk.<locals>.<listcomp>r-   c                 S   s   g | ]}|qS r   r   r9   r   r   r   r;   P  s     rt   c                 S   s   g | ]}|qS r   r   r9   r   r   r   r;   V  s     r}   brc                 S   s   g | ]}|qS r   r   r9   r   r   r   r;   Y  s     :)r   r@   rA   rB   rC   rD   rE   rF   rG   r   rH   rI   rJ   rK   ZoperandsrM   splitrN   r8   rP   endswith)r   r%   rR   rS   rT   rU   rI   Zentry_blockZ
body_blockZentry_instrZujmpZujmp_operandsZtarget_datatargetr   r7   r   test_DILocation_entry_blk)  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).
        Trs   c                  W   s   d S r   r   r#   r   r   r   sinki  s    z:TestDebugInfoEmission.test_DILocation_decref.<locals>.sinkc                    s0   | | f}| d dkr  | dS |d d }|S )Nr      r   )r5   r$   zr   r   r   r%   n  s    z9TestDebugInfoEmission.test_DILocation_decref.<locals>.fooNr-   r   r   zcall void @NRT_decrefz.*meminfo\.[0-9]+\)$)	r   r	   r   r^   r_   rN   
startswithassertRegexr   )r   r%   r   rR   countra   Zline_strippedr   r   r   test_DILocation_decrefb  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)
        Trs   c                 S   s$   | r| dkrd}|S |d7 }|S d S )Nr   r-   r   )nry   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	   intprb   r   r   r   r   rj   rO   rd   r   rH   rn   rl   ru   )r   r%   r   r`   r   r   r   matcherassociated_linesrp   rO   rd   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.Trs   c                 S   s"   | r
d}nd}d}d}d}d}|S )N   r-   rt   r      r   )r   ry   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   rt   zc = N)r   r	   r   rb   r   r   r   r   rj   rO   rd   r   rH   rn   rl   	enumerate)r   r%   r   r`   r   r   r   r   r   rp   rO   rd   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   Trs   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   rb   ri   AssertionErrorr   rO   rd   encodingr8   bitsr   ru   )r   r   Z
type_infosZ
dwarf_infor%   r`   re   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 )NTrs   c                  S   s   t jdt jd} | S )N)rt   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: rt   z), elements: ![0-9]+, identifier: "\[2 x iz\]"\))ZmeminfoparentZnitemsitemsizedatashapestridesz*!DIDerivedType\(.*, baseType: (![0-9]+),.*ZDIDerivedType")r   rb   ri   r   r   r   rO   rd   ru   rM   r	   r   r   r   rH   assertIsNotNoner
   lookup_fieldsr   Zbitwidthr   r   )r   r%   r`   re   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   )rb   rA   rr   )r   r`   ro   r   r   r   get_debug_linesS  s    
zATestDebugInfoEmission.test_debug_optnone.<locals>.get_debug_linesc                 S   s>   | j | jd  }|j}|j|jj} td| j	
 }|S )Nr       )rD   rA   ZlibraryZ_final_moduleZget_functionrE   rF   rj   r   
attributesr   )r   Zcreslibattrsr   r   r   get_func_attrsX  s
    z@TestDebugInfoEmission.test_debug_optnone.<locals>.get_func_attrsc                  S   s"   d} d}t | D ]}||7 }q|S )Nr   r   )range)r   ry   ir   r   r   r%   _  s
    
z5TestDebugInfoEmission.test_debug_optnone.<locals>.fooTrs   )r"   _dbg_optnone)r"   r   r0   r   r   s   optnone)ro   must_have_attrsmust_not_have_attrsr-      s   alwaysinlinero   r   r   )r   __code__co_firstlinenorc   rj   r   r   ZsubTestZtargetoptionsr   ZassertFalse)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_optnoneR  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 )NTrs   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   )rN   r9   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   rb   r	   ZOmittedri   rL   r   rH   popr   rO   r   rd   r   r   )r   r%   r`   re   Z
tmp_disubrrp   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
        Trs   r%   recordalwaysr-   r   z"Could not find source for function)rc   execr   r   warningscatch_warningssimplefilterr   r   r   rH   categoryrM   messageru   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 )	NTrs   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   rH   rb   rA   rr   )r   r%   r   r`   ro   r   r   r    test_irregularly_indented_source  s    

z6TestDebugInfoEmission.test_irregularly_indented_sourceN)rW   rX   rY   rZ   Z_NUMBA_OPT_0_ENVr^   rb   ri   rr   rv   r   r   Zrun_test_in_subprocessr   r   r   r   r   r   r   r   r  r  r   r   r   r   r[   {   s(   	t

8

  .7JA/r[   __main__)collectionsr   r   r   rw   r=   r1   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bindingrB   r   r[   rW   mainr   r   r   r   <module>   s*   d    w