Changeset 3832f27 in sasmodels


Ignore:
Timestamp:
May 4, 2016 8:04:45 PM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
47e498b
Parents:
5a91c6b
Message:

add line directives to generated source so compiler errors are reported correctly

Location:
sasmodels
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/generate.py

    r81ec7c8 r3832f27  
    215215import warnings 
    216216from collections import namedtuple 
     217import inspect 
    217218 
    218219import numpy as np 
     
    460461 
    461462    # Load additional sources 
    462     source = [open(f).read() for f in model_sources(model_info)] 
     463    source = [p 
     464              for f in model_sources(model_info) 
     465              # Add #line directives at the start of each file 
     466              for p in ('#line 0 "%s"'%f, open(f).read()) 
     467              ] 
     468    source.append('#line 133 "%s"'%C_KERNEL_TEMPLATE_PATH) 
    463469 
    464470    # Prepare defines 
     
    498504double form_volume(VOLUME_PARAMETER_DECLARATIONS); 
    499505double form_volume(VOLUME_PARAMETER_DECLARATIONS) { 
     506#line %(line)d "%(file)s" 
    500507    %(body)s 
    501508} 
    502 """ % {'body':model_info['form_volume']} 
     509""" % {'body':model_info['form_volume'], 
     510       'file':model_info['filename'], 
     511       'line':model_info['form_volume_line'], 
     512       } 
    503513        source.append(fn) 
    504514 
     
    527537double Iq(double q, IQ_PARAMETER_DECLARATIONS); 
    528538double Iq(double q, IQ_PARAMETER_DECLARATIONS) { 
     539#line %(line)d "%(file)s" 
    529540    %(body)s 
    530541} 
    531 """ % {'body':model_info['Iq']} 
     542""" % {'body':model_info['Iq'], 
     543       'file':model_info['filename'], 
     544       'line':model_info['Iq_line'], 
     545       } 
    532546        source.append(fn) 
    533547 
     
    556570double Iqxy(double qx, double qy, IQXY_PARAMETER_DECLARATIONS); 
    557571double Iqxy(double qx, double qy, IQXY_PARAMETER_DECLARATIONS) { 
     572#line %(line)d "%(file)s" 
    558573    %(body)s 
    559574} 
    560 """ % {'body':model_info['Iqxy']} 
     575""" % {'body':model_info['Iqxy'], 
     576       'file':model_info['filename'], 
     577       'line':model_info['Iqxy_line'], 
     578       } 
    561579        source.append(fn) 
    562580 
     
    655673        kernel_module = getattr(models, model_name, None) 
    656674    return kernel_module 
     675 
     676def find_source_lines(model_info, kernel_module): 
     677    """ 
     678    Identify the location of the C source inside the model definition file. 
     679 
     680    This code runs through the source of the kernel module looking for 
     681    lines that start with 'Iq', 'Iqxy' or 'form_volume'.  Clearly there are 
     682    all sorts of reasons why this might not work (e.g., code commented out 
     683    in a triple-quoted line block, code built using string concatenation, 
     684    or code defined in the branch of an 'if' block), but it should work 
     685    properly in the 95% case, and getting the incorrect line number will 
     686    be harmless. 
     687    """ 
     688    # Check if we need line numbers at all 
     689    if callable(model_info['Iq']): 
     690        return None 
     691 
     692    if (model_info['Iq'] is None 
     693        and model_info['Iqxy'] is None 
     694        and model_info['form_volume'] is None): 
     695        return 
     696 
     697    # Make sure we have harmless default values 
     698    model_info['Iqxy_line'] = 0 
     699    model_info['Iq_line'] = 0 
     700    model_info['form_volume_line'] = 0 
     701 
     702    # find the defintion lines for the different code blocks 
     703    source = inspect.getsource(kernel_module) 
     704    for k, v in enumerate(source.split('\n')): 
     705        if v.startswith('Iqxy'): 
     706            model_info['Iqxy_line'] = k+1 
     707        elif v.startswith('Iq'): 
     708            model_info['Iq_line'] = k+1 
     709        elif v.startswith('form_volume'): 
     710            model_info['form_volume_line'] = k+1 
    657711 
    658712 
     
    726780    functions = "ER VR form_volume Iq Iqxy shape sesans".split() 
    727781    model_info.update((k, getattr(kernel_module, k, None)) for k in functions) 
     782    find_source_lines(model_info, kernel_module) 
    728783    return model_info 
    729784 
  • sasmodels/kernel_template.c

    r2a55a6f r3832f27  
     1#line 1 "kernel_template.c" 
    12// GENERATED CODE --- DO NOT EDIT --- 
    23// Code is produced by sasmodels.gen from sasmodels/models/MODEL.c 
Note: See TracChangeset for help on using the changeset viewer.