Skip to content

Commit d6a9961

Browse files
committed
Add attributes for source location.
1 parent 8b2a664 commit d6a9961

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

bindgen/codegen/helpers.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) mod attributes {
7979
}
8080
}
8181

82-
pub trait CppSemanticAttributeCreator {
82+
pub(crate) trait CppSemanticAttributeCreator {
8383
fn do_add(&mut self, ts: TokenStream);
8484
fn is_enabled(&self) -> bool;
8585

@@ -189,6 +189,29 @@ pub trait CppSemanticAttributeCreator {
189189
layout(#sz, #align, #packed)
190190
})
191191
}
192+
193+
fn location(&mut self,
194+
location: Option<&crate::clang::SourceLocation>,
195+
) {
196+
if let Some(location) = location {
197+
let (file, line, col, byte_offset) = location.location();
198+
let file = file.name();
199+
if let Some(filename) = file {
200+
self.add(quote! {
201+
source_file(#filename)
202+
});
203+
self.add(quote! {
204+
source_col(#col)
205+
});
206+
self.add(quote! {
207+
source_line(#line)
208+
});
209+
self.add(quote! {
210+
byte_offset(#byte_offset)
211+
});
212+
}
213+
}
214+
}
192215
}
193216

194217
pub struct CppSemanticAttributeAdder<'a> {

bindgen/codegen/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ impl CodeGenerator for Type {
973973
if has_unused_template_args {
974974
semantic_annotations.discards_template_param();
975975
}
976+
semantic_annotations.location(item.location());
976977
tokens.append_all(semantic_annotations.result());
977978

978979
let alias_style = if ctx.options().type_alias.matches(&name) {
@@ -2229,6 +2230,7 @@ impl CodeGenerator for CompInfo {
22292230
if let Some(layout) = layout {
22302231
semantic_annotations.layout(&layout);
22312232
}
2233+
semantic_annotations.location(item.location());
22322234

22332235
if ctx.options().rust_features().repr_align {
22342236
if let Some(explicit) = explicit_align {
@@ -3293,6 +3295,7 @@ impl CodeGenerator for Enum {
32933295
}
32943296
}
32953297
semantic_annotations.visibility(self.visibility);
3298+
semantic_annotations.location(item.location());
32963299

32973300
// TODO(emilio): Delegate this to the builders?
32983301
match variation {
@@ -4471,6 +4474,7 @@ impl CodeGenerator for Function {
44714474
}
44724475

44734476
semantic_annotations.visibility(self.visibility());
4477+
semantic_annotations.location(item.location());
44744478

44754479
let abi = match signature.abi(ctx, Some(name)) {
44764480
ClangAbi::Known(Abi::ThisCall)

0 commit comments

Comments
 (0)