Skip to content

Commit c8e8772

Browse files
committed
ast-exporter: Export macro parameter names
1 parent 832d4bd commit c8e8772

3 files changed

Lines changed: 22 additions & 18 deletions

File tree

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,9 +1014,18 @@ class TranslateASTVisitor final
10141014
// Extend the range to include the entire final token.
10151015
expandSpanToFinalChar(range, Context);
10161016
encode_entry_raw(Mac, tag, range, QualType(), false,
1017-
false, false, childIds, [Name](CborEncoder *local) {
1018-
cbor_encode_string(local, Name.str());
1019-
});
1017+
false, false, childIds, [Name, Mac](CborEncoder *local) {
1018+
cbor_encode_string(local, Name.str());
1019+
1020+
CborEncoder array;
1021+
cbor_encoder_create_array(local, &array, Mac->getNumParams());
1022+
1023+
for (auto IdentifierInfo : Mac->params()) {
1024+
cbor_encode_string(&array, IdentifierInfo->getName().str());
1025+
}
1026+
1027+
cbor_encoder_close_container(local, &array);
1028+
});
10201029

10211030
}
10221031
}

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,10 +2375,18 @@ impl ConversionContext {
23752375
{
23762376
let name = from_value::<String>(node.extras[0].clone())
23772377
.expect("Macros must have a name");
2378+
let raw_params = from_value::<Vec<Value>>(node.extras[1].clone())
2379+
.expect("Macros must have a parameter list");
2380+
let params: Vec<String> = raw_params
2381+
.into_iter()
2382+
.map(|v| from_value(v).expect("param name"))
2383+
.collect();
23782384

23792385
let mac_object = match node.tag {
23802386
ASTEntryTag::TagMacroObjectDef => CDeclKind::MacroObject { name },
2381-
ASTEntryTag::TagMacroFunctionDef => CDeclKind::MacroFunction { name },
2387+
ASTEntryTag::TagMacroFunctionDef => {
2388+
CDeclKind::MacroFunction { name, params }
2389+
}
23822390
_ => unreachable!("Unexpected tag for macro"),
23832391
};
23842392

@@ -2391,20 +2399,6 @@ impl ConversionContext {
23912399
self.typed_context.c_decls_top.push(CDeclId(new_id));
23922400
}
23932401

2394-
ASTEntryTag::TagMacroFunctionDef if expected_ty & MACRO_DECL != 0 => {
2395-
let name = from_value::<String>(node.extras[0].clone())
2396-
.expect("Macros must have a name");
2397-
2398-
let mac_object = CDeclKind::MacroFunction { name };
2399-
self.add_decl(new_id, located(node, mac_object));
2400-
self.processed_nodes.insert(new_id, MACRO_DECL);
2401-
2402-
// Macros aren't technically top-level decls, so clang
2403-
// doesn't put them in top_nodes, but we do need to process
2404-
// them early.
2405-
self.typed_context.c_decls_top.push(CDeclId(new_id));
2406-
}
2407-
24082402
ASTEntryTag::TagNonCanonicalDecl if expected_ty & DECL != 0 => {
24092403
let canonical_decl =
24102404
node.children[0].expect("NonCanonicalDecl must point to a canonical decl");

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,6 +1620,7 @@ pub enum CDeclKind {
16201620

16211621
MacroFunction {
16221622
name: String,
1623+
params: Vec<String>,
16231624
// replacements: Vec<CExprId>,
16241625
},
16251626

0 commit comments

Comments
 (0)