Expose dwarf constants for debug info generation

This commit is contained in:
Mark Florisson 2013-01-21 10:52:29 -06:00
commit 93f8c70d19
4 changed files with 223 additions and 1 deletions

86
llvm/_dwarf.cpp Normal file
View file

@ -0,0 +1,86 @@
#include <Python.h>
#include <llvm/Support/Dwarf.h>
namespace llvmpy {
namespace dwarf {
using namespace llvm::dwarf;
const char *constant_names[] = {
"LLVMDebugVersion",
#define define(x) #x,
#include "_dwarf.h"
#undef define
};
int constant_values[] = {
llvm::LLVMDebugVersion,
#define define(x) x,
#include "_dwarf.h"
#undef define
};
enum enum_values {
__llvmpy_LLVMDebugVersion,
#define define(x) __llvmpy_##x,
#include "_dwarf.h"
#undef define
NVALUES /* Get the number of constants */
};
} // End namespace dwarf
int
set_dwarf_constants(PyObject *module)
{
int i;
for (i = 0; i < dwarf::NVALUES; i++) {
if (PyModule_AddIntConstant(module, dwarf::constant_names[i],
dwarf::constant_values[i]) > 0)
return -1;
}
return 0;
}
} // End namespace llvmpy
#if (PY_MAJOR_VERSION >= 3)
struct PyModuleDef module_def = {
PyModuleDef_HEAD_INIT,
"_dwarf",
NULL,
-1,
NULL, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#define INITERROR return NULL
PyObject *PyInit__dwarf(void)
#else
#define INITERROR return
PyMODINIT_FUNC init_dwarf(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create( &module_def );
#else
PyObject *module = Py_InitModule("_dwarf", NULL);
#endif
if (module == NULL)
INITERROR;
if (llvmpy::set_dwarf_constants(module) < 0)
INITERROR;
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}

101
llvm/_dwarf.h Normal file
View file

@ -0,0 +1,101 @@
/* Dwarf constant wrapping. See include/llvm/Support/Dwarf.h */
define(DWARF_VERSION)
/* Dwarf tags */
define(DW_TAG_array_type)
define(DW_TAG_class_type)
define(DW_TAG_entry_point)
define(DW_TAG_enumeration_type)
define(DW_TAG_formal_parameter)
define(DW_TAG_imported_declaration)
define(DW_TAG_label)
define(DW_TAG_lexical_block)
define(DW_TAG_member)
define(DW_TAG_pointer_type)
define(DW_TAG_reference_type)
define(DW_TAG_compile_unit)
define(DW_TAG_string_type)
define(DW_TAG_structure_type)
define(DW_TAG_subroutine_type)
define(DW_TAG_typedef)
define(DW_TAG_union_type)
define(DW_TAG_unspecified_parameters)
define(DW_TAG_variant)
define(DW_TAG_common_block)
define(DW_TAG_common_inclusion)
define(DW_TAG_inheritance)
define(DW_TAG_inlined_subroutine)
define(DW_TAG_module)
define(DW_TAG_ptr_to_member_type)
define(DW_TAG_set_type)
define(DW_TAG_subrange_type)
define(DW_TAG_with_stmt)
define(DW_TAG_access_declaration)
define(DW_TAG_base_type)
define(DW_TAG_catch_block)
define(DW_TAG_const_type)
define(DW_TAG_constant)
define(DW_TAG_enumerator)
define(DW_TAG_file_type)
define(DW_TAG_friend)
define(DW_TAG_namelist)
define(DW_TAG_namelist_item)
define(DW_TAG_packed_type)
define(DW_TAG_subprogram)
define(DW_TAG_template_type_parameter)
define(DW_TAG_template_value_parameter)
define(DW_TAG_thrown_type)
define(DW_TAG_try_block)
define(DW_TAG_variant_part)
define(DW_TAG_variable)
define(DW_TAG_volatile_type)
define(DW_TAG_dwarf_procedure)
define(DW_TAG_restrict_type)
define(DW_TAG_interface_type)
define(DW_TAG_namespace)
define(DW_TAG_imported_module)
define(DW_TAG_unspecified_type)
define(DW_TAG_partial_unit)
define(DW_TAG_imported_unit)
define(DW_TAG_condition)
define(DW_TAG_shared_type)
define(DW_TAG_type_unit)
define(DW_TAG_rvalue_reference_type)
define(DW_TAG_template_alias)
define(DW_TAG_MIPS_loop)
define(DW_TAG_format_label)
define(DW_TAG_function_template)
define(DW_TAG_class_template)
define(DW_TAG_GNU_template_template_param)
define(DW_TAG_GNU_template_parameter_pack)
define(DW_TAG_GNU_formal_parameter_pack)
define(DW_TAG_lo_user)
define(DW_TAG_APPLE_property)
define(DW_TAG_hi_user)
/* Dwarf language constants */
define(DW_LANG_C89)
define(DW_LANG_C)
define(DW_LANG_Ada83)
define(DW_LANG_C_plus_plus)
define(DW_LANG_Cobol74)
define(DW_LANG_Cobol85)
define(DW_LANG_Fortran77)
define(DW_LANG_Fortran90)
define(DW_LANG_Pascal83)
define(DW_LANG_Modula2)
define(DW_LANG_Java)
define(DW_LANG_C99)
define(DW_LANG_Ada95)
define(DW_LANG_Fortran95)
define(DW_LANG_PLI)
define(DW_LANG_ObjC)
define(DW_LANG_ObjC_plus_plus)
define(DW_LANG_UPC)
define(DW_LANG_D)
define(DW_LANG_Python)
define(DW_LANG_lo_user)
define(DW_LANG_Mips_Assembler)
define(DW_LANG_hi_user)

View file

@ -116,7 +116,12 @@ kwds = dict(ext_modules = [Extension(
library_dirs = [libdir],
libraries = libs_core,
extra_objects = objs_core,
extra_link_args = extra_link_args)])
extra_link_args = extra_link_args),
Extension(name='llvm._dwarf',
sources=['llvm/_dwarf.cpp'],
include_dirs=[incdir])
])
def run_2to3():
import lib2to3.refactor

30
test/test_debuginfo.py Normal file
View file

@ -0,0 +1,30 @@
import os
import unittest
from llvm.core import *
from llvm import _dwarf
class TestDebugInfo(unittest.TestCase):
def test_dwarf_constants(self):
dwarf_constants = vars(_dwarf)
# Version numbers
self.assertIn("LLVMDebugVersion", dwarf_constants)
self.assertIn("DWARF_VERSION", dwarf_constants)
# Tags
self.assertIn("DW_TAG_compile_unit", dwarf_constants)
# Language identifiers
self.assertIn("DW_LANG_Python", dwarf_constants)
self.assertIn("DW_LANG_C89", dwarf_constants)
self.assertIn("DW_LANG_C99", dwarf_constants)
# print sorted([constname for constname in dwarf_constants
# if constname.startswith("DW_LANG_")])
if __name__ == '__main__':
# TestDebugInfo("test_dwarf_constants").debug()
unittest.main()