diff --git a/c2rust-ast-exporter/src/AstExporter.cpp b/c2rust-ast-exporter/src/AstExporter.cpp index 38e3b5c72..c299c01ac 100644 --- a/c2rust-ast-exporter/src/AstExporter.cpp +++ b/c2rust-ast-exporter/src/AstExporter.cpp @@ -857,6 +857,15 @@ class TranslateASTVisitor final abort(); } + bool VisitStaticAssertDecl(StaticAssertDecl *SAD) { + std::vector 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 childIds = {LS->getSubStmt()}; diff --git a/c2rust-ast-exporter/src/ast_tags.hpp b/c2rust-ast-exporter/src/ast_tags.hpp index 0502ac7be..690acca46 100644 --- a/c2rust-ast-exporter/src/ast_tags.hpp +++ b/c2rust-ast-exporter/src/ast_tags.hpp @@ -23,6 +23,8 @@ enum ASTEntryTag { TagNonCanonicalDecl, + TagStaticAssertDecl, + TagMacroObjectDef, TagMacroFunctionDef, diff --git a/c2rust-transpile/src/c_ast/conversion.rs b/c2rust-transpile/src/c_ast/conversion.rs index 472a96587..8f218af87 100644 --- a/c2rust-transpile/src/c_ast/conversion.rs +++ b/c2rust-transpile/src/c_ast/conversion.rs @@ -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), } } diff --git a/c2rust-transpile/src/c_ast/iterators.rs b/c2rust-transpile/src/c_ast/iterators.rs index 4749c9631..1a00693d9 100644 --- a/c2rust-transpile/src/c_ast/iterators.rs +++ b/c2rust-transpile/src/c_ast/iterators.rs @@ -174,6 +174,13 @@ fn immediate_decl_children(kind: &CDeclKind) -> Vec { 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] + } + }, } } diff --git a/c2rust-transpile/src/c_ast/mod.rs b/c2rust-transpile/src/c_ast/mod.rs index 2ff729f7d..36a0be805 100644 --- a/c2rust-transpile/src/c_ast/mod.rs +++ b/c2rust-transpile/src/c_ast/mod.rs @@ -947,6 +947,11 @@ pub enum CDeclKind { NonCanonicalDecl { canonical_decl: CDeclId, + }, + + StaticAssert { + assert_expr: CExprId, + message: Option } } diff --git a/c2rust-transpile/src/c_ast/print.rs b/c2rust-transpile/src/c_ast/print.rs index 35f4b856f..572d6eb25 100644 --- a/c2rust-transpile/src/c_ast/print.rs +++ b/c2rust-transpile/src/c_ast/print.rs @@ -743,6 +743,10 @@ impl Printer { } } + 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"), } diff --git a/c2rust-transpile/src/translator/mod.rs b/c2rust-transpile/src/translator/mod.rs index a943c009b..38f7b4112 100644 --- a/c2rust-transpile/src/translator/mod.rs +++ b/c2rust-transpile/src/translator/mod.rs @@ -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) + } } }