Skip to content

Commit 462f011

Browse files
committed
ast-exporter: Export macro parameter information to Rust
1 parent 56fdf7f commit 462f011

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,18 @@ class TranslateASTVisitor final
816816
if (encodeMacroInvocations) {
817817
for (auto I = curMacroInvocationStack.rbegin(), E = curMacroInvocationStack.rend();
818818
I != E; ++I) {
819-
cbor_encode_uint(&childEnc, uintptr_t(I->macro));
819+
CborEncoder expansionEnc;
820+
cbor_encoder_create_array(&childEnc, &expansionEnc, 2);
821+
822+
cbor_encode_uint(&expansionEnc, uintptr_t(I->macro));
823+
824+
if (!I->parameter.empty()) {
825+
cbor_encode_string(&expansionEnc, I->parameter.str());
826+
} else {
827+
cbor_encode_null(&expansionEnc);
828+
}
829+
830+
cbor_encoder_close_container(&childEnc, &expansionEnc);
820831
}
821832
}
822833
cbor_encoder_close_container(&local, &childEnc);

c2rust-ast-exporter/src/clang_ast.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use serde::Deserialize;
12
use serde_bytes::ByteBuf;
23
use serde_cbor::error;
34
use std::collections::{HashMap, VecDeque};
@@ -108,11 +109,17 @@ pub struct AstNode {
108109
// Stack of macros this node was expanded from, beginning with the initial
109110
// macro call and ending with the leaf. This needs to be a stack for nested
110111
// macro definitions.
111-
pub macro_invocations: Vec<u64>,
112+
pub macro_invocations: Vec<MacroInvocationInfoRaw>,
112113
pub macro_invocation_text: Option<String>,
113114
pub extras: Vec<Value>,
114115
}
115116

117+
#[derive(Debug, Clone, Deserialize)]
118+
pub struct MacroInvocationInfoRaw {
119+
pub macro_id: u64,
120+
pub parameter: Option<String>,
121+
}
122+
116123
#[derive(Debug, Clone)]
117124
pub struct TypeNode {
118125
pub tag: TypeTag,
@@ -260,7 +267,8 @@ pub fn process(items: Value) -> error::Result<AstContext> {
260267
};
261268

262269
// entry[10]
263-
let macro_invocations = from_value::<Vec<u64>>(entry.pop_front().unwrap()).unwrap();
270+
let macro_invocations =
271+
from_value::<Vec<MacroInvocationInfoRaw>>(entry.pop_front().unwrap()).unwrap();
264272

265273
let macro_invocation_text = expect_opt_str(&entry.pop_front().unwrap())
266274
.unwrap()

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,8 +985,8 @@ impl ConversionContext {
985985
};
986986

987987
if expected_ty & EXPR != 0 {
988-
for mac_id in &node.macro_invocations {
989-
let mac = CDeclId(self.visit_node_type(*mac_id, MACRO_DECL));
988+
for info in &node.macro_invocations {
989+
let mac = CDeclId(self.visit_node_type(info.macro_id, MACRO_DECL));
990990
self.typed_context
991991
.macro_invocations
992992
.entry(CExprId(new_id))

0 commit comments

Comments
 (0)