Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a TranslationUnit by loading ast file (aka precompiled header). #422

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Clang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ include("index.jl")
export Index

include("trans_unit.jl")
export TranslationUnit, spelling, parse_header, parse_headers
export TranslationUnit, getTranslationUnitCursor, spelling, parse_header, parse_headers, load_ast

include("cursor.jl")
export kind, name, spelling, value
Expand Down
55 changes: 47 additions & 8 deletions src/trans_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ Parse the given source file and the translation unit corresponding to that file.
mutable struct TranslationUnit
ptr::CXTranslationUnit
idx::Index

function TranslationUnit(
ptr::CXTranslationUnit,
idx::Index,
)
@assert ptr != C_NULL
obj = new(ptr, idx)
finalizer(obj) do x
if x.ptr != C_NULL
clang_disposeTranslationUnit(x)
x.ptr = C_NULL
end
end
return obj
end

function TranslationUnit(
idx::Index,
ast_filename::AbstractString,
)
ptr = clang_createTranslationUnit(
idx,
ast_filename)
@assert ptr != C_NULL "failed to parse file: $ast_filename"
return TranslationUnit(ptr, idx)
end


function TranslationUnit(
idx::Index,
source_filename,
Expand All @@ -25,14 +53,7 @@ mutable struct TranslationUnit
options,
)
@assert ptr != C_NULL "failed to parse file: $source_filename"
obj = new(ptr, idx)
finalizer(obj) do x
if x.ptr != C_NULL
clang_disposeTranslationUnit(x)
x.ptr = C_NULL
end
end
return obj
return TranslationUnit(ptr, idx)
end
end
function TranslationUnit(idx, source, args, unsavedFiles, options)
Expand Down Expand Up @@ -143,3 +164,21 @@ function parse_headers(
parse_header(index, header, args, flags)
end
end

"""
load_ast(
index::Index,
ast_file::AbstractString,
) -> TranslationUnit
Return the [`TranslationUnit`](@ref) for a given precompiled header (aka an "ast file" in clang).

# Arguments
- `index::Index`: Index.
- `ast_file::AbstractString`: the ast file to load`.
Ast files can be generated using clang e.g. `clang t.h -emit-ast -o t.pch`
"""
function load_ast(
index::Index,
ast_file::AbstractString)
TranslationUnit(index, ast_file)
end
17 changes: 17 additions & 0 deletions test/ast.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Clang
using Clang.LibClang.Clang_jll
using Test

@testset "Load AST" begin
mktempdir() do dir
header = joinpath(dir,"test.h")
pch = joinpath(dir,"test.pch")
open(header, "w") do io
write(io, "void foo();")
end
run(`$(clang()) -x c $header -emit-ast -o $pch`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To use Clang on Windows, we need some setups to let Clang know where to find those system headers and that's a bit tricky to get it right. If the precompiled header is not very large, we can use pre-generatad the pch files.

index = Index()
tu = load_ast(index, pch)
@test tu |> getTranslationUnitCursor |> children |> only |> spelling == "foo"
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Clang
using Test

include("ast.jl")
include("generators.jl")

include("test_mpi.jl")
Expand Down