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

Handle _Static_assert gracefully. Closes #276 #277

Merged
merged 7 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions c2rust-ast-exporter/src/AstExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,13 @@ class TranslateASTVisitor final
abort();
}

bool VisitStaticAssertDecl(StaticAssertDecl *SAD) {
std::vector<void *> childIds = {SAD->getAssertExpr(), SAD->getMessage()};
auto T = QualType(); // don't care about the type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a separate variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, but if I just pass QualType() as the 4th argument how do I communicate that we don't care about this parameter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the comment to that line?

encode_entry(SAD, TagStaticAssertDecl, childIds, T);
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} => {
thedataking marked this conversation as resolved.
Show resolved Hide resolved
if message.is_some() {
thedataking marked this conversation as resolved.
Show resolved Hide resolved
intos![assert_expr, message.unwrap()]
thedataking marked this conversation as resolved.
Show resolved Hide resolved
} 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