Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 83cd42b

Browse files
committedJun 21, 2023
Don't count include locations of main header file.
1 parent 6553527 commit 83cd42b

File tree

9 files changed

+189
-69
lines changed

9 files changed

+189
-69
lines changed
 

‎bindgen-tests/tests/expectations/tests/source-order-recursive.rs

+67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// bindgen-flags: -- -Itests/headers -Itests/headers/source-order-recursive
2+
3+
#ifndef A_H
4+
#define A_H
5+
6+
struct foo {};
7+
8+
#include "source-order-recursive-2.h"
9+
10+
struct baz {
11+
struct bar field;
12+
};
13+
14+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "source-order-recursive.h"
2+
3+
struct bar {
4+
struct foo field;
5+
};

‎bindgen/clang.rs

+35-15
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use crate::ir::context::{BindgenContext, IncludeLocation};
88
use clang_sys::*;
99
use std::cmp;
10-
1110
use std::convert::TryInto;
1211
use std::ffi::{CStr, CString};
1312
use std::fmt;
1413
use std::hash::Hash;
1514
use std::hash::Hasher;
1615
use std::os::raw::{c_char, c_int, c_longlong, c_uint, c_ulong, c_ulonglong};
16+
use std::path::Path;
17+
use std::path::PathBuf;
1718
use std::{mem, ptr, slice};
1819

1920
/// Type representing a clang attribute.
@@ -395,8 +396,13 @@ impl Cursor {
395396
offset: offset.try_into().unwrap(),
396397
}
397398
} else {
399+
let file_name = cxstring_into_string(clang_getFileName(file));
400+
let file_path = Path::new(&file_name)
401+
.canonicalize()
402+
.expect("Source location doesn't exist?");
403+
398404
SourceLocation::File {
399-
file_name: cxstring_into_string(clang_getFileName(file)),
405+
file_path,
400406
line: line.try_into().unwrap(),
401407
column: column.try_into().unwrap(),
402408
offset: offset.try_into().unwrap(),
@@ -535,7 +541,10 @@ impl Cursor {
535541
for child in &children {
536542
if child.kind() == CXCursor_InclusionDirective {
537543
if let Some(included_file) = child.get_included_file_name() {
538-
ctx.add_include(included_file, child.location());
544+
let included_file_path = Path::new(&included_file)
545+
.canonicalize()
546+
.expect("Included file doesn't exist?");
547+
ctx.add_include(included_file_path, child.location());
539548
}
540549
}
541550
}
@@ -1574,7 +1583,7 @@ pub(crate) enum SourceLocation {
15741583
/// Location in a source file.
15751584
File {
15761585
/// Name of the source file.
1577-
file_name: String,
1586+
file_path: PathBuf,
15781587
/// Line in the source file.
15791588
line: usize,
15801589
/// Column in the source file.
@@ -1608,26 +1617,26 @@ impl SourceLocation {
16081617
}
16091618
(
16101619
SourceLocation::File {
1611-
file_name, offset, ..
1620+
file_path, offset, ..
16121621
},
16131622
SourceLocation::File {
1614-
file_name: other_file_name,
1623+
file_path: other_file_path,
16151624
offset: other_offset,
16161625
..
16171626
},
16181627
) => {
1619-
if file_name == other_file_name {
1628+
if file_path == other_file_path {
16201629
return offset.cmp(other_offset);
16211630
}
16221631

16231632
// If `file` is transitively included via `ancestor_file`,
16241633
// find the offset of the include directive in `ancestor_file`.
1625-
let offset_in_ancestor = |file: &str, ancestor_file: &str| {
1634+
let offset_in_ancestor = |file: &Path, ancestor_file: &Path| {
16261635
let mut file = file;
16271636
while file != ancestor_file {
16281637
let include_location = ctx.include_location(file);
16291638
file = if let IncludeLocation::File {
1630-
file_name: file,
1639+
file_path: file,
16311640
offset,
16321641
..
16331642
} = include_location
@@ -1646,20 +1655,20 @@ impl SourceLocation {
16461655
};
16471656

16481657
if let Some(offset) =
1649-
offset_in_ancestor(file_name, other_file_name)
1658+
offset_in_ancestor(file_path, other_file_path)
16501659
{
16511660
return offset.cmp(other_offset);
16521661
}
16531662

16541663
if let Some(other_offset) =
1655-
offset_in_ancestor(other_file_name, file_name)
1664+
offset_in_ancestor(other_file_path, file_path)
16561665
{
16571666
return offset.cmp(other_offset);
16581667
}
16591668

16601669
// If the source files are siblings, compare their include locations.
1661-
let parent = ctx.include_location(file_name);
1662-
let other_parent = ctx.include_location(other_file_name);
1670+
let parent = ctx.include_location(file_path);
1671+
let other_parent = ctx.include_location(other_file_path);
16631672
parent.cmp_by_source_order(other_parent, ctx)
16641673
}
16651674
}
@@ -1671,11 +1680,11 @@ impl fmt::Display for SourceLocation {
16711680
match self {
16721681
Self::Builtin { .. } => "built-in".fmt(f),
16731682
Self::File {
1674-
file_name,
1683+
file_path,
16751684
line,
16761685
column,
16771686
..
1678-
} => write!(f, "{}:{}:{}", file_name, line, column),
1687+
} => write!(f, "{}:{}:{}", file_path.display(), line, column),
16791688
}
16801689
}
16811690
}
@@ -1906,6 +1915,17 @@ impl TranslationUnit {
19061915
}
19071916
}
19081917

1918+
/// Get the source file path of this translation unit.
1919+
pub(crate) fn path(&self) -> PathBuf {
1920+
let file_name = unsafe {
1921+
cxstring_into_string(clang_getTranslationUnitSpelling(self.x))
1922+
};
1923+
1924+
Path::new(&file_name)
1925+
.canonicalize()
1926+
.expect("This translation unit doesn't exist?")
1927+
}
1928+
19091929
/// Is this the null translation unit?
19101930
pub(crate) fn is_null(&self) -> bool {
19111931
self.x.is_null()

‎bindgen/codegen/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4354,17 +4354,17 @@ fn unsupported_abi_diagnostic(
43544354
);
43554355

43564356
if let Some(crate::clang::SourceLocation::File {
4357-
file_name,
4357+
file_path,
43584358
line,
43594359
column,
43604360
..
43614361
}) = location.cloned()
43624362
{
4363-
if let Ok(Some(source)) = get_line(&file_name, line) {
4363+
if let Ok(Some(source)) = get_line(&file_path, line) {
43644364
let mut slice = Slice::default();
43654365
slice
43664366
.with_source(source)
4367-
.with_location(file_name, line, column);
4367+
.with_location(file_path, line, column);
43684368
diag.add_slice(slice);
43694369
}
43704370
}
@@ -4395,17 +4395,17 @@ fn variadic_fn_diagnostic(
43954395
.add_annotation("No code will be generated for this function.", Level::Note);
43964396

43974397
if let Some(crate::clang::SourceLocation::File {
4398-
file_name,
4398+
file_path,
43994399
line,
44004400
column,
44014401
..
44024402
}) = location.cloned()
44034403
{
4404-
if let Ok(Some(source)) = get_line(&file_name, line) {
4404+
if let Ok(Some(source)) = get_line(&file_path, line) {
44054405
let mut slice = Slice::default();
44064406
slice
44074407
.with_source(source)
4408-
.with_location(file_name, line, column);
4408+
.with_location(file_path, line, column);
44094409
diag.add_slice(slice);
44104410
}
44114411
}

‎bindgen/diagnostics.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//!
33
//! The entry point of this module is the [`Diagnostic`] type.
44
5-
use std::fmt::Write;
65
use std::io::{self, BufRead, BufReader};
6+
use std::path::Path;
77
use std::{borrow::Cow, fs::File};
88

99
use annotate_snippets::{
@@ -162,25 +162,24 @@ impl<'a> Slice<'a> {
162162
}
163163

164164
/// Set the file, line and column.
165-
pub(crate) fn with_location(
165+
pub(crate) fn with_location<P: AsRef<Path>>(
166166
&mut self,
167-
mut name: String,
167+
path: P,
168168
line: usize,
169169
col: usize,
170170
) -> &mut Self {
171-
write!(name, ":{}:{}", line, col)
172-
.expect("Writing to a string cannot fail");
173-
self.filename = Some(name);
171+
self.filename =
172+
Some(format!("{}:{}:{}", path.as_ref().display(), line, col));
174173
self.line = Some(line);
175174
self
176175
}
177176
}
178177

179-
pub(crate) fn get_line(
180-
filename: &str,
178+
pub(crate) fn get_line<P: AsRef<Path>>(
179+
file_path: P,
181180
line: usize,
182181
) -> io::Result<Option<String>> {
183-
let file = BufReader::new(File::open(filename)?);
182+
let file = BufReader::new(File::open(file_path.as_ref())?);
184183
if let Some(line) = file.lines().nth(line.wrapping_sub(1)) {
185184
return line.map(Some);
186185
}

0 commit comments

Comments
 (0)
Please sign in to comment.