Skip to content

Commit

Permalink
Fix jacob-carlborg#46: Generating code that will not compile.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciechowoj committed Jul 27, 2016
1 parent 4d172f2 commit 66fe26a
Show file tree
Hide file tree
Showing 25 changed files with 488 additions and 58 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Issue #2: Self alias should be removed bug.
Issue #29: Don't name anonymous enums.
Issue #39: Recognize and translate __attribute__((__packed__)).
Issue #46: Generating code that will not compile.
Issue #47: Treatment of #define enhancement.
Issue #50: struct typedef generates recursive alias bug.

Expand Down
11 changes: 11 additions & 0 deletions clang/Cursor.d
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ struct Cursor
return Type(r);
}

@property Type underlyingType() const
{
return Type(clang_getTypedefDeclUnderlyingType(cx));
}

@property bool isDeclaration ()
{
return clang_isDeclaration(cx.kind) != 0;
Expand Down Expand Up @@ -403,6 +408,12 @@ struct Cursor
dumpAST(result, 0);
return result.data;
}

@property string toString()
{
import std.format : format;
return format("Cursor(kind = %s, spelling = %s)", kind, spelling);
}
}

struct ObjcCursor
Expand Down
2 changes: 1 addition & 1 deletion clang/SourceLocation.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct SourceLocation

@property string toString() const
{
import std.format: format;
import std.format : format;
auto s = spelling;
return format("SourceLocation(file = %s, line = %d, column = %d, offset = %d)", s.file, s.line, s.column, s.offset);
}
Expand Down
33 changes: 30 additions & 3 deletions clang/Type.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct Type
mixin CX;

private Type* pointee_;
private Type* canonical_;

mixin(bitfields!(
bool, "isConst", 1,
Expand Down Expand Up @@ -50,11 +51,24 @@ struct Type
return result;
}

static Type makeTypedef(string spelling, Type canonical)
{
Type result = Type(CXTypeKind.CXType_Typedef, spelling);
result.canonical_ = new Type();
*result.canonical_ = canonical;
return result;
}

@property bool isAnonymous ()
{
return spelling == "";
}

@property Type underlying ()
{
return declaration.underlyingType;
}

@property bool isArray ()
{
return
Expand Down Expand Up @@ -139,10 +153,17 @@ struct Type

@property Type canonical()
{
if (isClang)
return Type(clang_getCanonicalType(cx));
if (canonical_)
{
return *canonical_;
}
else
return Type.init;
{
if (isClang)
return Type(clang_getCanonicalType(cx));
else
return Type.init;
}
}

@property Type pointee()
Expand Down Expand Up @@ -183,6 +204,12 @@ struct Type
import std.format: format;
return format("Type(kind = %s, spelling = %s, isConst = %s)", kind, spelling, isConst);
}

@property string toString()
{
import std.format : format;
return format("Type(kind = %s, spelling = %s)", kind, spelling);
}
}

struct FuncType
Expand Down
3 changes: 3 additions & 0 deletions dstep/Configuration.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ struct Configuration

/// use public imports for submodules
bool publicSubmodules;

/// disable reduction of primitive type aliases
bool dontReduceAliases;
}
7 changes: 4 additions & 3 deletions dstep/driver/Application.d
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Application
foreach (fileName; config.inputFiles)
{
string outputFilename;

if (singleFileInput)
{
if (config.output.length)
Expand All @@ -79,7 +79,7 @@ class Application
if (!exists(outputDir))
mkdirRecurse(outputDir);

auto conversionTask = task!startParsingFile(config,
auto conversionTask = task!startParsingFile(config,
fileName, outputFilename);

conversionTask.executeInNewThread();
Expand Down Expand Up @@ -117,7 +117,7 @@ private struct ParseFile
}

this (
const Configuration config,
const Configuration config,
string inputFile,
string outputFile)
{
Expand Down Expand Up @@ -153,6 +153,7 @@ private struct ParseFile
options.enableComments = !config.noComments;
options.packageName = config.packageName;
options.publicSubmodules = config.publicSubmodules;
options.reduceAliases = !config.dontReduceAliases;

auto translator = new Translator(translationUnit, options);
translator.translate;
Expand Down
10 changes: 5 additions & 5 deletions dstep/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ auto parseCLI (string[] args)
"objective-c", "Treat source input file as Objective-C input.", &forceObjectiveC,
"no-comments", "Disable translation of comments.", &config.noComments,
"public-submodules", "Use public imports for submodules.", &config.publicSubmodules,
"package", "Specify package name.", &config.packageName
);
"package", "Specify package name.", &config.packageName,
"dont-reduce-aliases", "Disable reduction of primitive type aliases.", &config.dontReduceAliases);

// remove dstep binary name (args[0])
args = args[1 .. $];
Expand Down Expand Up @@ -91,19 +91,19 @@ auto parseCLI (string[] args)
unittest
{
import std.meta : AliasSeq;

Configuration config;
GetoptResult getopResult;

AliasSeq!(config, getopResult) = parseCLI(
[ "dstep", "-Xpreprocessor", "-lsomething", "-x", "c-header", "file.h" ]);
[ "dstep", "-Xpreprocessor", "-lsomething", "-x", "c-header", "file.h" ]);
assert(config.language == Language.c);
assert(config.inputFiles == [ "file.h" ]);
assert(config.clangParams == [ "-x", "c-header", "-Xpreprocessor", "-lsomething" ]);
assert(config.output == "");

AliasSeq!(config, getopResult) = parseCLI(
[ "dstep", "-ObjC", "file2.h", "--output=folder", "file.h" ]);
[ "dstep", "-ObjC", "file2.h", "--output=folder", "file.h" ]);
assert(config.language == Language.objC);
assert(config.inputFiles == [ "file2.h", "file.h" ]);
assert(config.clangParams == [ "-ObjC" ]);
Expand Down
4 changes: 2 additions & 2 deletions dstep/translator/Context.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ class Context
private Translator translator_ = null;
private Output globalScope_ = null;
private Cursor[string] typeNames_;

public MacroDefinition[string] macroDefinitions;

const Options options;
Options options;

public this(TranslationUnit translUnit, Options options, Translator translator = null)
{
Expand All @@ -59,6 +58,7 @@ class Context

globalScope_ = new Output();
typeNames_ = collectGlobalTypes(translUnit);
this.options = options;
}

public string getAnonymousName (Cursor cursor)
Expand Down
9 changes: 9 additions & 0 deletions dstep/translator/IncludeHandler.d
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ class IncludeHandler
"stdarg" : "core.stdc.stdarg",
"stddef" : "core.stdc.stddef",
"stdint" : "core.stdc.stdint",
"_int8_t" : "core.stdc.stdint",
"_int16_t" : "core.stdc.stdint",
"_int32_t" : "core.stdc.stdint",
"_int64_t" : "core.stdc.stdint",
"_uint8_t" : "core.stdc.stdint",
"_uint16_t" : "core.stdc.stdint",
"_uint32_t" : "core.stdc.stdint",
"_uint64_t" : "core.stdc.stdint",
"stdio" : "core.stdc.stdio",
"stdlib" : "core.stdc.stdlib",
"string" : "core.stdc.string",
Expand Down Expand Up @@ -80,6 +88,7 @@ class IncludeHandler
"sys/socket" : "core.sys.posix.sys.socket",
"sys/stat" : "core.sys.posix.sys.stat",
"sys/time" : "core.sys.posix.sys.time",
"_time_t" : "core.stdc.time",
"sys/types" : "core.sys.posix.sys.types",
"sys/_types" : "core.sys.posix.sys.types",
"sys/uio" : "core.sys.posix.sys.uio",
Expand Down
7 changes: 4 additions & 3 deletions dstep/translator/MacroDefinition.d
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,9 @@ bool parseTypedefName(ref TokenRange tokens, ref Type type, Cursor[string] table
{
if (auto ptr = (spelling in table))
{
type.kind = CXTypeKind.CXType_Typedef;
type.spelling = spelling;

type = Type.makeTypedef(spelling, ptr.type.canonical);
tokens = local;

return true;
}
}
Expand Down Expand Up @@ -1513,13 +1512,15 @@ bool basicSpecifierListToType(ref Type type, Set!string specifiers)
return false;

type = Type(CXTypeKind.CXType_LongDouble, "long double");

return true;
}

if (specifiers.length != 1)
return false;

type = Type(CXTypeKind.CXType_Double, "double");

return true;
}

Expand Down
16 changes: 16 additions & 0 deletions dstep/translator/Options.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ struct Options
bool enableComments = true;
bool publicSubmodules = false;
bool keepUntranslatable = false;
bool reduceAliases = true;
bool portableWCharT = true;

string toString() const
{
import std.format : format;

return format(
"Options(outputFile = %s, language = %s, enableComments = %s, "
"reduceAliases = %s, portableWCharT = %s)",
outputFile,
language,
enableComments,
reduceAliases,
portableWCharT);
}
}

string fullModuleName(string packageName, string path)
Expand Down
11 changes: 3 additions & 8 deletions dstep/translator/Translator.d
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ class Translator
break;
}

if (cursor.type.canonical.isPrimitive && context.options.reduceAliases)
ignoreTypedef = true;

if (!ignoreTypedef)
{
output.singleLine(
Expand All @@ -261,14 +264,6 @@ class Translator
translateVariable(output, context, cursor, prefix);
}

void typedef_ (Output output, Cursor cursor)
{
output.singleLine(
"alias %s %s;",
translateType(context, cursor, cursor.type.canonical),
cursor.spelling);
}

private:

bool skipDeclaration (Cursor cursor)
Expand Down
Loading

0 comments on commit 66fe26a

Please sign in to comment.