Skip to content

Commit

Permalink
Handle _Static_assert gracefully. Closes #276 (#277)
Browse files Browse the repository at this point in the history
* Warn that static asserts are not transpiled

* Include message when iterating children of StaticAssert

* Fix handling of message in VisitStaticAssertDecl

* Formatting fix in c2rust-transpile/src/c_ast/iterators.rs

Co-authored-by: ahomescu <[email protected]>

* Update c2rust-transpile/src/c_ast/iterators.rs

Co-authored-by: ahomescu <[email protected]>

* Update c2rust-transpile/src/c_ast/iterators.rs

Co-authored-by: ahomescu <[email protected]>

* Clean up VisitStaticAssertDecl

Co-authored-by: ahomescu <[email protected]>
  • Loading branch information
thedataking and ahomescu authored Jul 9, 2020
1 parent 9108a10 commit c04009f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions c2rust-ast-exporter/src/AstExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,15 @@ class TranslateASTVisitor final
abort();
}

bool VisitStaticAssertDecl(StaticAssertDecl *SAD) {
std::vector<void *> childIds = {SAD->getAssertExpr()};
auto msg = SAD->getMessage();
if (msg != nullptr)
childIds.push_back(msg);
encode_entry(SAD, TagStaticAssertDecl, childIds, QualType()); // 4th argument unused
return true;
}

bool VisitLabelStmt(LabelStmt *LS) {

std::vector<void *> childIds = {LS->getSubStmt()};
Expand Down
2 changes: 2 additions & 0 deletions c2rust-ast-exporter/src/ast_tags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum ASTEntryTag {

TagNonCanonicalDecl,

TagStaticAssertDecl,

TagMacroObjectDef,
TagMacroFunctionDef,

Expand Down
13 changes: 13 additions & 0 deletions c2rust-transpile/src/c_ast/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,19 @@ impl ConversionContext {
self.processed_nodes.insert(new_id, OTHER_DECL);
}

ASTEntryTag::TagStaticAssertDecl if expected_ty & DECL != 0 => {
let assert_expr = CExprId(node.children[0]
.expect("StaticAssert must point to an expression"));
let message = if node.children.len() > 1 {
Some(CExprId(node.children[1]
.expect("Expected static assert message")))
} else {
None
};
let static_assert = CDeclKind::StaticAssert{ assert_expr, message };
self.add_decl(new_id, located(node, static_assert));
}

t => panic!("Could not translate node {:?} as type {}", t, expected_ty),
}
}
Expand Down
7 changes: 7 additions & 0 deletions c2rust-transpile/src/c_ast/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ fn immediate_decl_children(kind: &CDeclKind) -> Vec<SomeId> {
Field { typ, .. } => intos![typ.ctype],
MacroObject { .. } | MacroFunction { .. } => vec![],
NonCanonicalDecl { canonical_decl } => intos![canonical_decl],
StaticAssert { assert_expr, message } => {
if let Some(message) = message {
intos![assert_expr, message]
} else {
intos![assert_expr]
}
},
}
}

Expand Down
5 changes: 5 additions & 0 deletions c2rust-transpile/src/c_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,11 @@ pub enum CDeclKind {

NonCanonicalDecl {
canonical_decl: CDeclId,
},

StaticAssert {
assert_expr: CExprId,
message: Option<CExprId>
}
}

Expand Down
4 changes: 4 additions & 0 deletions c2rust-transpile/src/c_ast/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ impl<W: Write> Printer<W> {
}
}

Some(&CDeclKind::StaticAssert { .. }) => {
self.writer.write_fmt(format_args!("static_assert(...)"))
}

None => panic!("Could not find declaration with ID {:?}", decl_id),
// _ => unimplemented!("Printer::print_decl"),
}
Expand Down
5 changes: 5 additions & 0 deletions c2rust-transpile/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,11 @@ impl<'c> Translation<'c> {
// Do not translate non-canonical decls. They will be translated at
// their canonical declaration.
CDeclKind::NonCanonicalDecl { .. } => Ok(ConvertedDecl::NoItem),

CDeclKind::StaticAssert { .. } => {
warn!("ignoring static assert during translation");
Ok(ConvertedDecl::NoItem)
}
}
}

Expand Down

0 comments on commit c04009f

Please sign in to comment.