Skip to content

Commit 5bc1f4b

Browse files
authored
Merge pull request gimli-rs#551 from khuey/nonzerou64
Change row and column numbers to be reported with NonZeroU64.
2 parents 705842a + 5a4ff25 commit 5bc1f4b

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
--------------------------------------------------------------------------------
44

5+
## 0.24.0
6+
7+
Not yet released
8+
9+
### Breaking changes
10+
11+
* `read::LineRow::line` now returns Option<NonZeroU64>.
12+
The `read::ColumnType::Column` variant now contains a NonZeroU64.
13+
[#551](https://github.com/gimli-rs/gimli/pull/551)
14+
15+
--------------------------------------------------------------------------------
16+
517
## 0.23.0
618

719
Released 2020/10/27.

examples/dwarfdump.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,9 +1991,12 @@ fn dump_line_program<R: Reader, W: Write>(
19911991
let mut rows = program.rows();
19921992
let mut file_index = 0;
19931993
while let Some((header, row)) = rows.next_row()? {
1994-
let line = row.line().unwrap_or(0);
1994+
let line = match row.line() {
1995+
Some(line) => line.get(),
1996+
None => 0,
1997+
};
19951998
let column = match row.column() {
1996-
gimli::ColumnType::Column(column) => column,
1999+
gimli::ColumnType::Column(column) => column.get(),
19972000
gimli::ColumnType::LeftEdge => 0,
19982001
};
19992002
write!(w, "0x{:08x} [{:4},{:2}]", row.address(), line, column)?;

examples/simple_line.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,13 @@ fn dump_file(object: &object::File, endian: gimli::RunTimeEndian) -> Result<(),
8484

8585
// Determine line/column. DWARF line/column is never 0, so we use that
8686
// but other applications may want to display this differently.
87-
let line = row.line().unwrap_or(0);
87+
let line = match row.line() {
88+
Some(line) => line.get(),
89+
None => 0,
90+
};
8891
let column = match row.column() {
8992
gimli::ColumnType::LeftEdge => 0,
90-
gimli::ColumnType::Column(x) => x,
93+
gimli::ColumnType::Column(column) => column.get(),
9194
};
9295

9396
println!("{:x} {}:{}:{}", row.address(), path.display(), line, column);

src/read/line.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::vec::Vec;
22
use core::fmt;
3-
use core::num::Wrapping;
3+
use core::num::{NonZeroU64, Wrapping};
44
use core::result;
55

66
use crate::common::{
@@ -713,25 +713,20 @@ impl LineRow {
713713
/// "An unsigned integer indicating a source line number. Lines are numbered
714714
/// beginning at 1. The compiler may emit the value 0 in cases where an
715715
/// instruction cannot be attributed to any source line."
716+
/// Line number values of 0 are represented as `None`.
716717
#[inline]
717-
pub fn line(&self) -> Option<u64> {
718-
if self.line.0 == 0 {
719-
None
720-
} else {
721-
Some(self.line.0)
722-
}
718+
pub fn line(&self) -> Option<NonZeroU64> {
719+
NonZeroU64::new(self.line.0)
723720
}
724721

725722
/// "An unsigned integer indicating a column number within a source
726723
/// line. Columns are numbered beginning at 1. The value 0 is reserved to
727724
/// indicate that a statement begins at the “left edge” of the line."
728725
#[inline]
729726
pub fn column(&self) -> ColumnType {
730-
if self.column == 0 {
731-
ColumnType::LeftEdge
732-
} else {
733-
ColumnType::Column(self.column)
734-
}
727+
NonZeroU64::new(self.column)
728+
.map(ColumnType::Column)
729+
.unwrap_or(ColumnType::LeftEdge)
735730
}
736731

737732
/// "A boolean indicating that the current instruction is a recommended
@@ -996,7 +991,7 @@ pub enum ColumnType {
996991
/// line.
997992
LeftEdge,
998993
/// A column number, whose range begins at 1.
999-
Column(u64),
994+
Column(NonZeroU64),
1000995
}
1001996

1002997
/// Deprecated. `LineNumberSequence` has been renamed to `LineSequence`.

src/write/line.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,10 +1102,13 @@ mod convert {
11021102
}
11031103
files[file as usize]
11041104
};
1105-
program.row().line = from_row.line().unwrap_or(0);
1105+
program.row().line = match from_row.line() {
1106+
Some(line) => line.get(),
1107+
None => 0,
1108+
};
11061109
program.row().column = match from_row.column() {
11071110
read::ColumnType::LeftEdge => 0,
1108-
read::ColumnType::Column(val) => val,
1111+
read::ColumnType::Column(val) => val.get(),
11091112
};
11101113
program.row().discriminator = from_row.discriminator();
11111114
program.row().is_statement = from_row.is_stmt();
@@ -1801,7 +1804,7 @@ mod tests {
18011804
{
18021805
let row = rows.next_row().unwrap().unwrap().1;
18031806
address = row.address();
1804-
line = row.line().unwrap();
1807+
line = row.line().unwrap().get();
18051808
}
18061809
assert_eq!(address, 0x1000);
18071810
assert_eq!(line, 0x10000);
@@ -1812,11 +1815,11 @@ mod tests {
18121815
address_advance * u64::from(minimum_instruction_length)
18131816
);
18141817
assert_eq!(
1815-
(row.line().unwrap() as i64) - (line as i64),
1818+
(row.line().unwrap().get() as i64) - (line as i64),
18161819
line_advance
18171820
);
18181821
address = row.address();
1819-
line = row.line().unwrap();
1822+
line = row.line().unwrap().get();
18201823
}
18211824
let row = rows.next_row().unwrap().unwrap().1;
18221825
assert!(row.end_sequence());

0 commit comments

Comments
 (0)