Skip to content

Commit 828e044

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 b100556 commit 828e044

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
@@ -1838,6 +1838,15 @@ class TranslateASTVisitor final
18381838
auto body =
18391839
FD->getBody(paramsFD); // replaces its argument if body exists
18401840

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

18481857
childIds.push_back(body);
18491858

1850-
auto functionType = FD->getType();
1859+
// We prefer non-implicit decls for their type information.
1860+
auto functionType = paramsFD->getType();
18511861
auto span = paramsFD->getSourceRange();
18521862
encode_entry(
18531863
FD, TagFunctionDecl, span, childIds, functionType,

0 commit comments

Comments
 (0)