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

Keep original label names from C code #415

Merged
merged 2 commits into from May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions c2rust-transpile/src/c_ast/conversion.rs
Expand Up @@ -3,6 +3,7 @@ use c2rust_ast_exporter::clang_ast::*;
use failure::err_msg;
use serde_bytes::ByteBuf;
use std::collections::HashMap;
use std::rc::Rc;
use std::vec::Vec;

use super::Located;
Expand Down Expand Up @@ -1018,6 +1019,18 @@ impl ConversionContext {
let substmt_old = node.children[0].expect("Label sub-statement not found");
let substmt = self.visit_stmt(substmt_old);

let label_name = from_value::<Rc<str>>(node.extras[0].clone())
.expect("unnamed label in C source code");
match self.typed_context.label_names.insert(CStmtId(new_id), label_name.clone()) {
Some(old_label_name) => {
panic!(
"Duplicate label name with id {}. Old name: {}. New name: {}",
new_id, old_label_name, label_name,
);
}
None => {}
}

let label_stmt = CStmtKind::Label(substmt);

self.add_stmt(new_id, located(node, label_stmt));
Expand Down Expand Up @@ -2127,10 +2140,7 @@ impl ConversionContext {
} else {
None
};
let static_assert = CDeclKind::StaticAssert {
assert_expr,
message,
};
let static_assert = CDeclKind::StaticAssert { assert_expr, message };
self.add_decl(new_id, located(node, static_assert));
}

Expand Down
5 changes: 5 additions & 0 deletions c2rust-transpile/src/c_ast/mod.rs
Expand Up @@ -7,6 +7,7 @@ use std::fmt::{self, Debug, Display};
use std::mem;
use std::ops::Index;
use std::path::{Path, PathBuf};
use std::rc::Rc;

pub use c2rust_ast_exporter::clang_ast::{BuiltinVaListKind, SrcFile, SrcLoc, SrcSpan};

Expand Down Expand Up @@ -66,6 +67,9 @@ pub struct TypedAstContext {
// that location.
include_map: Vec<Vec<SrcLoc>>,

// Names of the labels defined in the C source code.
pub label_names: IndexMap<CLabelId, Rc<str>>,

// map expressions to the stack of macros they were expanded from
pub macro_invocations: HashMap<CExprId, Vec<CDeclId>>,

Expand Down Expand Up @@ -178,6 +182,7 @@ impl TypedAstContext {
macro_invocations: HashMap::new(),
macro_expansions: HashMap::new(),
macro_expansion_text: HashMap::new(),
label_names: Default::default(),

comments: vec![],
prenamed_decls: IndexMap::new(),
Expand Down
10 changes: 5 additions & 5 deletions c2rust-transpile/src/cfg/loops.rs
Expand Up @@ -54,7 +54,7 @@ pub fn match_loop_body(
desired_body.swap_remove(&following);

follow_entries.swap_remove(&following);
follow_entries.extend(&bb.successors());
follow_entries.extend(bb.successors());
follow_entries.retain(|e| !body_blocks.contains_key(e));
body_blocks.insert(following, bb);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn heuristic_loop_body(
) -> () {
if follow_entries.len() > 1 {
for follow_entry in follow_entries.clone().iter() {
let mut following: Label = *follow_entry;
let mut following: Label = follow_entry.clone();

loop {
// If this block might have come from 2 places, give up
Expand All @@ -104,16 +104,16 @@ pub fn heuristic_loop_body(
};
let succs = bb.successors();

body_blocks.insert(following, bb);
body_blocks.insert(following.clone(), bb);
follow_entries.swap_remove(&following);
follow_entries.extend(&succs);
follow_entries.extend(succs.clone());

// If it has more than one successor, don't try following the successor
if succs.len() != 1 {
break;
}

following = *succs.iter().next().unwrap();
following = succs.iter().next().unwrap().clone();
}
}
}
Expand Down