Skip to content

Commit af11d61

Browse files
committed
ast-exporter: prefer non-implicit declarations when exporting function declarations
implicit declarations for functions like malloc do not know about the typedefs that may be used in the declaration present in system headers. because we want to reify these typedefs into portable Rust types, we want to prefer the actual written decl when exporting function parameter declarations and function types
1 parent 0f0503c commit af11d61

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,15 @@ class TranslateASTVisitor final
18361836
auto body =
18371837
FD->getBody(paramsFD); // replaces its argument if body exists
18381838

1839+
// Avoid getting params from an implicit decl if a subsequent non-implicit decl exists.
1840+
// Implicit decls will not have names for params, but more importantly, they will never
1841+
// reference header-declared typedefs, so we would miss the fact that e.g. malloc is
1842+
// declared to accept `size_t` in its stdlib.h declaration, while its implicit declaration
1843+
// accepts the built-in `unsigned long`.
1844+
if (FD->isImplicit()) {
1845+
paramsFD = FD->getMostRecentDecl();
1846+
}
1847+
18391848
std::vector<void *> childIds;
18401849
for (auto x : paramsFD->parameters()) {
18411850
auto cd = x->getCanonicalDecl();
@@ -1845,7 +1854,8 @@ class TranslateASTVisitor final
18451854

18461855
childIds.push_back(body);
18471856

1848-
auto functionType = FD->getType();
1857+
// We prefer non-implicit decls for their type information.
1858+
auto functionType = paramsFD->getType();
18491859
auto span = paramsFD->getSourceRange();
18501860
encode_entry(
18511861
FD, TagFunctionDecl, span, childIds, functionType,

0 commit comments

Comments
 (0)