From b9917acac14eb5d84e45fdefcc3cea7d96b263b8 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sun, 7 Apr 2019 15:32:08 -0400 Subject: [PATCH] Add DIBuilder and constructors for DIInfo --- src/LLVM.jl | 1 - src/debuginfo.jl | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/dibuilder.jl | 7 ------- test/dibuilder.jl | 27 +++++++++++++++++++++++++ test/instructions.jl | 2 +- test/runtests.jl | 1 + 6 files changed, 77 insertions(+), 9 deletions(-) delete mode 100644 src/dibuilder.jl create mode 100644 test/dibuilder.jl diff --git a/src/LLVM.jl b/src/LLVM.jl index 685dca17..6e0f9977 100644 --- a/src/LLVM.jl +++ b/src/LLVM.jl @@ -61,7 +61,6 @@ include("ir.jl") include("bitcode.jl") include("transform.jl") include("debuginfo.jl") -include("dibuilder.jl") include("jitevents.jl") include("utils.jl") diff --git a/src/debuginfo.jl b/src/debuginfo.jl index 577ef1f8..b6647659 100644 --- a/src/debuginfo.jl +++ b/src/debuginfo.jl @@ -1,3 +1,18 @@ +## DIBuilder + +export DIBuilder + +@checked struct DIBuilder + ref::API.LLVMDIBuilderRef +end +Base.unsafe_convert(::Type{API.LLVMDIBuilderRef}, builder::DIBuilder) = builder.ref + +# LLVMCreateDIBuilderDisallowUnresolved +DIBuilder(mod::Module) = DIBuilder(API.LLVMCreateDIBuilder(mod)) + +dispose(builder::DIBuilder) = API.LLVMDisposeDIBuilder(builder) +finalize(builder::DIBuilder) = API.LLVMDIBuilderFinalize(builder) + ## location information export DILocation @@ -20,6 +35,11 @@ function inlined_at(location::DILocation) ref == C_NULL ? nothing : Metadata(ref)::DILocation end +function DILocation(line, col, scope=nothing, inlined_at=nothing; ctx::Context) + DILocation(API.LLVMDIBuilderCreateDebugLocation(ctx, line, col, + something(scope, C_NULL), + something(inlined_at, C_NULL))) +end ## nodes @@ -106,6 +126,13 @@ function source(file::DIFile) unsafe_string(convert(Ptr{Int8}, data), len[]) end +function file!(builder::DIBuilder, file::String, dir::String) + DIFile(API.LLVMDIBuilderCreateFile( + builder, + file, convert(Csize_t, length(file)), + dir, convert(Csize_t, length(dir)) + )) +end ## type @@ -158,6 +185,27 @@ export DICompileUnit end register(DICompileUnit, API.LLVMDICompileUnitMetadataKind) +function compilationunit!(builder::DIBuilder, lang, file, producer; + optimized::Base.Bool=true, flags="", runtime_version=0, split_name=nothing, emission_kind=API.LLVMDWARFEmissionFull, + dwo_id=0, split_debug_inlining=true, debug_info_for_profiling=false, sysroot="", sdk="") + + DICompileUnit(API.LLVMDIBuilderCreateCompileUnit( + builder, + lang, + file, + producer, length(producer), + optimized ? LLVM.True : LLVM.False, + flags, length(flags), + runtime_version, + something(split_name, C_NULL), split_name === nothing ? 0 : length(split_name), + emission_kind, + dwo_id, + split_debug_inlining ? LLVM.True : LLVM.False, + debug_info_for_profiling ? LLVM.True : LLVM.False, + sysroot, length(sysroot), + sdk, length(sdk) + )) +end ## other diff --git a/src/dibuilder.jl b/src/dibuilder.jl deleted file mode 100644 index 6a80dfe7..00000000 --- a/src/dibuilder.jl +++ /dev/null @@ -1,7 +0,0 @@ -function DILocation(ctx, line, col, scope=nothing, inlined_at=nothing) - # XXX: are null scopes valid? they crash LLVM: - # DILocation(Context(), 1, 2).scope - DILocation(API.LLVMDIBuilderCreateDebugLocation(ctx, line, col, - something(scope, C_NULL), - something(inlined_at, C_NULL))) -end diff --git a/test/dibuilder.jl b/test/dibuilder.jl new file mode 100644 index 00000000..7896347e --- /dev/null +++ b/test/dibuilder.jl @@ -0,0 +1,27 @@ +@testset "dibuilder" begin + +Context() do ctx +LLVM.Module("SomeModule"; ctx) do mod + builder = DIBuilder(mod) + dispose(builder) +end +end + +Context() do ctx +LLVM.Module("SomeModule"; ctx) do mod + di_builder = DIBuilder(mod) + file = LLVM.file!(di_builder, "test.jl", "src") + + @test LLVM.filename(file) == "test.jl" + @test LLVM.directory(file) == "src" + @test LLVM.source(file) == "" # No C-API to attach source + + cu = LLVM.compilationunit!(di_builder, + LLVM.API.LLVMDWARFSourceLanguageJulia, file, "LLVM.jl Tests") + + finalize(di_builder) + dispose(di_builder) +end +end + +end # testset diff --git a/test/instructions.jl b/test/instructions.jl index 94f025a9..107c3ab6 100644 --- a/test/instructions.jl +++ b/test/instructions.jl @@ -13,7 +13,7 @@ @assert position(builder) == entrybb @test debuglocation(builder) === nothing - loc = DILocation(ctx, 1, 1) + loc = DILocation(1, 1; ctx) debuglocation!(builder, loc) @test debuglocation(builder) == loc debuglocation!(builder) diff --git a/test/runtests.jl b/test/runtests.jl index 4fd665a0..ea13d336 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -64,6 +64,7 @@ include("target.jl") include("targetmachine.jl") include("datalayout.jl") include("debuginfo.jl") +include("dibuilder.jl") include("utils.jl") if LLVM.has_orc_v1() include("orc.jl")