o
    ۾iRr                     @   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)compileinspect_asmselffnsig r   N/home/ubuntu/.local/lib/python3.10/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(   r3   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   r3   G      
z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.barFc                 S   r6   r   )r7   cosr&   r   r   r   bazK   r9   z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.bazc                    s    | }| }||fS r   r   )r'   ab)r3   r;   r   r   r(   O   s   z:TestDebugInfo.test_llvm_inliner_flag_conflict.<locals>.foor1   r2   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pi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funcf_namesblkstmt	found_sin	found_bazbaz_namer'   r   )r3   r;   r@   r   test_llvm_inliner_flag_conflictB   sD   






z-TestDebugInfo.test_llvm_inliner_flag_conflictN)
__name__
__module____qualname____doc__r   r!   r.   r0   r4   rf   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.
    	NUMBA_OPT0c                 C   sB   t dd || ||W  d    S 1 sw   Y  d S )NOPTr   )r   r   rJ   r   r   r   r   _get_llvmir   s   
$z!TestDebugInfoEmission._get_llvmirc                 C   s@   |  || }td}g }|D ]}||r|| q|S )Nz![0-9]+ =.*)ro   
splitlinesr   r   rY   rV   )r   r   r   ll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   rY   groups)r   rs   metadata_definition_mapmeta_definition_splitrt   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+),r2   )r}   setvaluesr   rY   intgroupadd)r   rs   
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   ru   r   assertIn)r   r(   rs   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$   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?   rA   r?   r   r   rC      rD   z9TestDebugInfoEmission.test_DILocation.<locals>.<listcomp>r2   c                 S      g | ]}|qS r   r   rA   r   r   r   rC          c                 S   s   g | ]	}|j d vr|qS ))rE   loadstore)rU   rA   r   r   r   rC      s    >   faddfdivfmulFz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   rA   )inst_as_strr   r   rC          N)r   r2   r      c                    s   g | ]} d  | qS )r   r   rA   )pysrc_line_startr   r   rC     s    c                 S   s   t | dd  S Nr2   )r   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	   float64ru   ro   rL   rM   rN   rK   rO   rP   rQ   r   rR   rS   assertGreaterrT   rU   removefailassertFalser~   r   r   rW   rY   rZ   anyrw   r   r   inspectgetsourcelinesr}   zipsortedr   r   )#r   r(   r   rs   r\   r]   r^   r_   rS   blockinstrs	op_expectstartedr'   line2dbg
re_dbg_reffoundinstrrz   acceptedrw   r{   int_dbg_valpysrcrx   offsets
pyln_rangekline_nodilocation_infoexprmatch_local_var_aentrydbg_line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?   rA   r?   r   r   rC   U  rD   zCTestDebugInfoEmission.test_DILocation_entry_blk.<locals>.<listcomp>r2   c                 S   r   r   r   rA   r   r   r   rC   X  r   r   c                 S   r   r   r   rA   r   r   r   rC   ^  r   r   brc                 S   r   r   r   rA   r   r   r   rC   a  r   :)r   rJ   rK   rL   rM   rN   rO   rP   rQ   r   rR   rS   rT   rU   operandsrW   splitrX   r@   rZ   endswith)r   r(   r\   r]   r^   r_   rS   entry_block
body_blockentry_instrujmpujmp_operands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>.fooNr2   r   r   zcall void @NRT_decrefz.*meminfo\.[0-9]+\)$)	r   r	   r   ro   rp   rX   
startswithassertRegexr   )r   r(   r   r\   countrt   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   r2   r   )nr   r   r   r   r(     s   z<TestDebugInfoEmission.test_DILocation_undefined.<locals>.foor   z:.*!DILocalVariable\(name: "c\$?[0-9]?",.*line: ([0-9]+),.*r2   r   r   N)r   r	   intpru   r   r   r   r   r~   rY   rw   r   rR   r   r   r   )r   r(   r   rs   r   r   r   matcherassociated_linesr   rY   rw   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   r2   r   r      r   )r   r   py310_defeat1py310_defeat2py310_defeat3py310_defeat4r   r   r   r(     s   zFTestDebugInfoEmission.test_DILocation_versioned_variables.<locals>.foor   z9.*!DILocalVariable\(name: "c\$[0-9]?",.*line: ([0-9]+),.*r2   r   r   zc = N)r   r	   r   ru   r   r   r   r   r~   rY   rw   r   rR   r   r   	enumerate)r   r(   r   rs   r   r   r   r   r   r   rY   rw   py_linesix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    DW_ATE_floatr   @   int8   DW_ATE_signedint16   r,   int64uint8DW_ATE_unsigneduint16uint32uint64	complex64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]+).*DW_ATEz!DIBasicType(name: "	", size: z, encoding: )floatdoublezdistinct !DICompositeType(tag: z	, name: "z ({z, z})", size: N)r   rF   r   r   r   r   r,   r   r   r   r   r   r   r   itemsr   ru   r}   r   rY   rw   encodingr@   bitsr   r   )r   r   
type_infos
dwarf_infor(   rs   rx   r   vlvartype_marker	type_declexpected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   )dtype)rF   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: "r2   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\]"\))meminfoparentnitemsitemsizedatashapestridesz*!DIDerivedType\(.*, baseType: (![0-9]+),.*DIDerivedType")r   ru   r}   r
  r   rY   rw   r   rW   r	   r   r   r   rR   assertIsNotNoner
   lookup_fieldsr   bitwidthr   r   )r   r(   rs   rx   r   r  r  r  r  match_elementselem_matches
elem_matchstruct_markersstruct_patternmatch_structstruct_member_matches
data_modelptr_sizeptr_reint_re	utuple_rer  base_type_patternbase_type_matcherr   fieldderived_typebase_type_matchbase_type_matchesbase_type_marker	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   )ru   rK   r   )r   rs   r   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       )rN   rK   library_final_moduleget_functionrO   rP   r~   join
attributesr   )r   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$   rH  r5   r   r   s   optnone)r   must_have_attrsmust_not_have_attrsr2      s   alwaysinliner   rI  rJ  )r   __code__co_firstlinenorv   r~   rF  r
  subTesttargetoptionsr   r   )r   r;  rE  r(   	foo_debugfoo_debug_optnonefoo_debug_optnone_inline	firstlineexpected_infoexpected_retudtr  r    rD  	must_have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>.foor1   r2   r   DISubroutineTypez(.*!DISubroutineType\(types: ([!0-9]+)\)$r   z!{(.*)}c                 S   s   g | ]}|  qS r   )rX   rA   r   r   r   rC     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   ru   r	   Omittedr}   rV   r   rR   popr   rY   r!  rw   r   r   )r   r(   rs   rx   
tmp_disubrr   disubrdisubr_matcheddisubr_groupsdisubr_metadisubr_typesdisubr_types_matcheddisubr_types_groups	md_fn_argarg_tyexpected_arg_ty
md_base_ty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alwaysr2   r   z"Could not find source for function)rv   execr   r   warningscatch_warningssimplefilterr   r   r   rR   categoryrW   messager   rI   )r   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>.foorm  ro  r   r2   )r   rq  rr  rs  r   r   r   rR   ru   rK   r   )r   r(   rx  rs   r   r   r   r    test_irregularly_indented_source  s   

z6TestDebugInfoEmission.test_irregularly_indented_sourceN)rg   rh   ri   rj   _NUMBA_OPT_0_ENVro   ru   r}   r   r   r   r   run_test_in_subprocessr   r   r   r   r  r9  rY  rl  ry  rz  r   r   r   r   rk   {   s*    	
|

8
  .7JA/rk   __main__)collectionsr   r   r   r   rF   r7   textwrapr   unittestrq  numba.tests.supportr   r   r   numbar   r   
numba.corer	   numba.core.datamodelr
   numba.core.errorsr   llvmlite.bindingbindingrL   r   rk   rg   mainr   r   r   r   <module>   s.    d    