Skip to content

Commit

Permalink
Translate includes as private import
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Oct 4, 2024
1 parent 4b39336 commit 445a699
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
5 changes: 3 additions & 2 deletions source/ctod/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ int main(string[] args)
else
{
const fname = args[i];
if (!(fname.extension == ".c" || fname.extension == ".h"))
const ext = fname.extension;
if (!(ext == ".c" || ext == ".h"))
{
stderr.writeln("file shoud have .c or .h extension, not ", fname.extension);
return -1;
}
scope source = cast(string) fileReadText(fname);

const moduleName = fname.baseName;
writeFile(fname.withExtension(".d"), translateFile(source, moduleName));
writeFile(fname.withExtension(".d"), translateFile(source, moduleName, ext == ".h"));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/ctod/cpreproc.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool ctodTryPreprocessor(ref scope CtodCtx ctx, ref Node node)
case Sym.aux_preproc_if_token2: // "#endif"
return node.replace("}");
case Sym.aux_preproc_include_token1: // "#include"
return node.replace("public import");
return node.replace(ctx.isHeaderFile ? "public import" : "import");
case Sym.preproc_call: // #error, #pragma, #undef
auto argument = node.childField(Field.argument);
if (auto directive = node.childField(Field.directive))
Expand Down Expand Up @@ -331,7 +331,7 @@ string ctodMacroFunc(ref scope CtodCtx ctx, string macroText)
// Then extract the function body and remove braces
string funcStr = "void __macroFunc(void) {" ~ macroText ~ "}";

CtodCtx ctx2 = CtodCtx(funcStr, ctx.parser);
CtodCtx ctx2 = CtodCtx(funcStr, ctx.parser, ctx.isHeaderFile);
auto root = parseCtree(ctx2);

if (!root || !root.children.length > 0)
Expand Down
26 changes: 13 additions & 13 deletions source/ctod/testcases.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,19 +1018,19 @@ struct S1 { struct { int y; }; }S1[1][1] Sarray = [[ {{2}} ]];
#include <x>
```
```D
public import core.stdc.assert_;
public import core.stdc.string;
public import core.stdc.stdio;
public import small;
public import core.sys.windows.winuser;
public import core.stdc.limits;
public import core.sys.linux.sys.inotify;
public import core.sys.linux.sys.timerfd;
public import core.sys.posix.sys.ioctl;
public import my.lib;
public import core.sys.posix.pthread;
public import cfile;
public import x;
import core.stdc.assert_;
import core.stdc.string;
import core.stdc.stdio;
import small;
import core.sys.windows.winuser;
import core.stdc.limits;
import core.sys.linux.sys.inotify;
import core.sys.linux.sys.timerfd;
import core.sys.posix.sys.ioctl;
import my.lib;
import core.sys.posix.pthread;
import cfile;
import x;
```
---
```C
Expand Down
10 changes: 7 additions & 3 deletions source/ctod/translate.d
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ private TSParser* getCParser() @trusted
/// Params:
/// source = C source code
/// moduleName = name for the `module` declaration on the D side
/// isHeaderFile = true if the source is a .h file (assumes includes are public imports)
/// Returns: `source` translated from C to D
string translateFile(string source, string moduleName)
string translateFile(string source, string moduleName, bool isHeaderFile = false)
{
auto parser = getCParser();
// scope(exit) ts_parser_delete(parser);
// ts_tree_delete(tree);

source = filterCppBlocks(source);

CtodCtx ctx = CtodCtx(source, parser);
CtodCtx ctx = CtodCtx(source, parser, isHeaderFile);
Node root = parseCtree(ctx);

version(none)
Expand Down Expand Up @@ -110,6 +111,8 @@ struct CtodCtx
{
/// input C source code
string sourceC;
/// Translating a .h instead of .c (decides whether includes are assumed public or private imports)
bool isHeaderFile = false;
/// C parser
TSParser* parser;
/// HasVersion(string) template is needed
Expand Down Expand Up @@ -162,10 +165,11 @@ nothrow:
}
}

this(return scope string source, return scope TSParser* parser) scope
this(return scope string source, return scope TSParser* parser, bool isHeaderFile) scope
{
this.sourceC = source;
this.parser = parser;
this.isHeaderFile = isHeaderFile;
this.typeScope = [TypeScope(Sym.null_)];
}

Expand Down

0 comments on commit 445a699

Please sign in to comment.