I am trying to write a rust script that parses a C++ file, finds all extern C
declared functions and prints them out.
To that effect I have this first attempt:
use clang::*;
use clap::{Arg, App};
fn main()
{
let matches = App::new("Shared C++ API parser")
.version("0.0.1")
.author("Makogan")
.about("Parse a cpp file that exposes functions to be loaded at runtime from \
A shared object (.so/.dll).")
.arg(Arg::with_name("file")
.short('f')
.long("file")
.takes_value(true)
.help("The cpp file that will be parsed")
.required(true))
.get_matches();
let cpp_file = matches.value_of("file").unwrap();
println!("The file passed is: {}", cpp_file);
let clang = Clang::new().unwrap();
let index = Index::new(&clang, false, false);
let tu = index.parser(cpp_file).parse().unwrap();
let funcs = tu.get_entity().get_children().into_iter().filter(
|e| {
e.get_kind() == EntityKind::FunctionDecl
}).collect::<Vec<_>>();
for func_ in funcs
{
let type_ = func_.get_type().unwrap();
let size = type_.get_sizeof().unwrap();
println!("func: {:?} (size: {} bytes)", func_.get_display_name().unwrap(), size);
}
}
I am giving it a true cpp file with lots of functions, this is one such declared funciton:
void DrawOffScreen(
void* vk_data_ptr,
const NECore::RenderRequest& render_request,
const NECore::UniformBufferDataContainer& uniform_container);
When I give it a very very simple file I stil don't get an output
extern "C"
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type-c-linkage"
void InitializeRendering(void* window, bool use_vsync)
{
}
}
If I comment out the extern C part I do get the correct output. It seems that when the defientiion is inside an extern C block clang thinks the funciton is undeclared:
Some(Entity { kind: UnexposedDecl, display_name: None, location: Some(SourceLocation { file: Some(File { path: "/path/dummy.cpp" }), line: 1, column: 8, offset: 7 }) })
What do I do?