Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling of multiline comments #117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ evalexpr = "12"
getrandom = "0.2"
log = "0.4"
object = { version = "0.35", default-features = false, features = ["read_core", "elf", "std"] }
regex = "1.11"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
47 changes: 46 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::{

use object::{elf, Object as _, ObjectSection, SectionFlags};

use regex::Regex;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

const EXIT_CODE_FAILURE: i32 = 1;
Expand Down Expand Up @@ -299,6 +301,20 @@ fn get_includes_from_linker_script(linker_script: &str) -> Vec<&str> {
/// Looks for "RAM : ORIGIN = $origin, LENGTH = $length"
// FIXME this is a dumb line-by-line parser
fn find_ram_in_linker_script(linker_script: &str) -> Option<MemoryEntry> {
let linker_script = {
// Remove any multiline-style comments (/*...*/) in the linker script before processing.
// Comments within one line get replaced by an empty string.
// Comments that span multiple lines get replaced by newlines, in order to keep the final entry
// line index correct, within the original linker script.
let re = Regex::new(r"(?s)/\*.*?\*/").unwrap();
let mut stripped_linker_script = linker_script.to_string();
for m in re.find_iter(linker_script) {
stripped_linker_script = stripped_linker_script
.replace(m.as_str(), &"\n".repeat(m.as_str().lines().count() - 1));
}
stripped_linker_script
};

for (index, mut line) in linker_script.lines().enumerate() {
line = line.trim();
line = eat!(line, "RAM");
Expand Down Expand Up @@ -416,7 +432,7 @@ mod tests {
}

#[test]
fn ingore_comment() {
fn ignore_comment() {
_ = env_logger::try_init();
const LINKER_SCRIPT: &str = "MEMORY
{
Expand All @@ -441,6 +457,35 @@ mod tests {
);
}

#[test]
fn ignore_multiline_comment() {
_ = env_logger::try_init();
const LINKER_SCRIPT: &str = "MEMORY
{
/* This is a single line comment /*
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
/* This is a multiline comment
RAM : ORIGIN = 0x20000000, LENGTH = 64K */
RAM : ORIGIN = 0x20000000, /* This is a bit much... */ LENGTH = 64K
}

INCLUDE device.x";

assert_eq!(
find_ram_in_linker_script(LINKER_SCRIPT),
Some(MemoryEntry {
line: 6,
origin: 0x20000000,
length: 64 * 1024,
})
);

assert_eq!(
get_includes_from_linker_script(LINKER_SCRIPT),
vec!["device.x"]
);
}

#[test]
fn test_perform_addition_hex_and_number() {
_ = env_logger::try_init();
Expand Down
Loading