Skip to content

Commit

Permalink
Change row and column numbers to be reported with NonZeroU64.
Browse files Browse the repository at this point in the history
This allows for more compact representation of their values (the return values of line() and column() should both be representable in a single u64 now) and makes it clear to users that Some(0) and ColumnType::Column(0) are never returned.
  • Loading branch information
khuey committed Feb 14, 2021
1 parent 705842a commit 5a4ff25
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

--------------------------------------------------------------------------------

## 0.24.0

Not yet released

### Breaking changes

* `read::LineRow::line` now returns Option<NonZeroU64>.
The `read::ColumnType::Column` variant now contains a NonZeroU64.
[#551](https://github.com/gimli-rs/gimli/pull/551)

--------------------------------------------------------------------------------

## 0.23.0

Released 2020/10/27.
Expand Down
7 changes: 5 additions & 2 deletions examples/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1991,9 +1991,12 @@ fn dump_line_program<R: Reader, W: Write>(
let mut rows = program.rows();
let mut file_index = 0;
while let Some((header, row)) = rows.next_row()? {
let line = row.line().unwrap_or(0);
let line = match row.line() {
Some(line) => line.get(),
None => 0,
};
let column = match row.column() {
gimli::ColumnType::Column(column) => column,
gimli::ColumnType::Column(column) => column.get(),
gimli::ColumnType::LeftEdge => 0,
};
write!(w, "0x{:08x} [{:4},{:2}]", row.address(), line, column)?;
Expand Down
7 changes: 5 additions & 2 deletions examples/simple_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ fn dump_file(object: &object::File, endian: gimli::RunTimeEndian) -> Result<(),

// Determine line/column. DWARF line/column is never 0, so we use that
// but other applications may want to display this differently.
let line = row.line().unwrap_or(0);
let line = match row.line() {
Some(line) => line.get(),
None => 0,
};
let column = match row.column() {
gimli::ColumnType::LeftEdge => 0,
gimli::ColumnType::Column(x) => x,
gimli::ColumnType::Column(column) => column.get(),
};

println!("{:x} {}:{}:{}", row.address(), path.display(), line, column);
Expand Down
21 changes: 8 additions & 13 deletions src/read/line.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::vec::Vec;
use core::fmt;
use core::num::Wrapping;
use core::num::{NonZeroU64, Wrapping};
use core::result;

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

/// "An unsigned integer indicating a column number within a source
/// line. Columns are numbered beginning at 1. The value 0 is reserved to
/// indicate that a statement begins at the “left edge” of the line."
#[inline]
pub fn column(&self) -> ColumnType {
if self.column == 0 {
ColumnType::LeftEdge
} else {
ColumnType::Column(self.column)
}
NonZeroU64::new(self.column)
.map(ColumnType::Column)
.unwrap_or(ColumnType::LeftEdge)
}

/// "A boolean indicating that the current instruction is a recommended
Expand Down Expand Up @@ -996,7 +991,7 @@ pub enum ColumnType {
/// line.
LeftEdge,
/// A column number, whose range begins at 1.
Column(u64),
Column(NonZeroU64),
}

/// Deprecated. `LineNumberSequence` has been renamed to `LineSequence`.
Expand Down
13 changes: 8 additions & 5 deletions src/write/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,13 @@ mod convert {
}
files[file as usize]
};
program.row().line = from_row.line().unwrap_or(0);
program.row().line = match from_row.line() {
Some(line) => line.get(),
None => 0,
};
program.row().column = match from_row.column() {
read::ColumnType::LeftEdge => 0,
read::ColumnType::Column(val) => val,
read::ColumnType::Column(val) => val.get(),
};
program.row().discriminator = from_row.discriminator();
program.row().is_statement = from_row.is_stmt();
Expand Down Expand Up @@ -1801,7 +1804,7 @@ mod tests {
{
let row = rows.next_row().unwrap().unwrap().1;
address = row.address();
line = row.line().unwrap();
line = row.line().unwrap().get();
}
assert_eq!(address, 0x1000);
assert_eq!(line, 0x10000);
Expand All @@ -1812,11 +1815,11 @@ mod tests {
address_advance * u64::from(minimum_instruction_length)
);
assert_eq!(
(row.line().unwrap() as i64) - (line as i64),
(row.line().unwrap().get() as i64) - (line as i64),
line_advance
);
address = row.address();
line = row.line().unwrap();
line = row.line().unwrap().get();
}
let row = rows.next_row().unwrap().unwrap().1;
assert!(row.end_sequence());
Expand Down

0 comments on commit 5a4ff25

Please sign in to comment.