Skip to content

Commit

Permalink
Style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jan 29, 2024
1 parent 4691c3b commit eb0546b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 97 deletions.
2 changes: 1 addition & 1 deletion source/ctod/cdeclaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Decl[] ctodTryDeclaration(ref CtodCtx ctx, ref Node node) {
// Translation of function signautre is pretty destructive regarding layout,
// but at least it's rare to have comments inside the parameter list
// For now: add whitespace before bodyNode to preserve brace style and comments after the signature
const layout = node.fullSource[declNode.end..bodyNode.start];
const layout = node.fullSource[declNode.end .. bodyNode.start];
ctx.enterFunction("???");
translateNode(ctx, *bodyNode);
ctx.leaveFunction();
Expand Down
2 changes: 1 addition & 1 deletion source/ctod/cexpr.d
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ bool translateSpecialFunction(ref Node node, ref Node funcNode) {
}

private string toSizeof(scope string str) {
foreach (i; 0..str.length) {
foreach (i; 0 .. str.length) {
// A pointer / array needs brackets, can't have `int*.sizeof` or `int[].sizeof`
if (str[i] == '*' || str[i] == ']') {
return "(" ~ str ~ ").sizeof";
Expand Down
24 changes: 13 additions & 11 deletions source/ctod/cpreproc.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool ctodTryPreprocessor(ref CtodCtx ctx, ref Node node) {
}
} else if (directive.source == "#pragma") {
// preceeding whitespace is part of the argument node
if (argument && argument.source.length >= 4 && argument.source[$-4..$] == "once") {
if (argument && argument.source.length >= 4 && argument.source[$-4 .. $] == "once") {
node.prepend("//");
return true;
}
Expand All @@ -58,8 +58,8 @@ bool ctodTryPreprocessor(ref CtodCtx ctx, ref Node node) {
while (p < argText.length && argText[p].isWhite) {
p++;
}
string whitespace = argText[0..p];
string value = argText[p..$];
string whitespace = argText[0 .. p];
string value = argText[p .. $];
string comment = "";

// tree sitter doesn't parse line comments inside preproc arg,
Expand All @@ -75,15 +75,15 @@ bool ctodTryPreprocessor(ref CtodCtx ctx, ref Node node) {
while (p > 0 && value[p-1].isWhite) {
p--;
}
comment = value[p..$];
value = value[0..p];
comment = value[p .. $];
value = value[0 .. p];
break;
}
p++;
}

if (auto c = node.firstChildType(Sym.aux_preproc_def_token1)) {
import ctod.ctype : ctodPrimitiveType;
import ctod.ctype: ctodPrimitiveType;
const newValue = ctodPrimitiveType(value);
if (newValue != value) {
c.replace("alias");
Expand Down Expand Up @@ -139,7 +139,7 @@ bool ctodTryPreprocessor(ref CtodCtx ctx, ref Node node) {

valueNode.prepend(" = `");
valueNode.replace(ctodMacroFunc(ctx, valueNode.source));
ctx.macroFuncParams = ctx.macroFuncParams.init; //.clear();
(() @trusted => ctx.macroFuncParams.clear())();
valueNode.append("`;");
break;
case Sym.preproc_ifdef:
Expand Down Expand Up @@ -307,7 +307,7 @@ private bool ctodHeaderGuard(ref CtodCtx ctx, ref Node ifdefNode) {
// second node is always field `name` with a `Sym.identifier`
string id = ifdefNode.children[1].source;

foreach (i; 0..ifdefNode.children.length) {
foreach (i; 0 .. ifdefNode.children.length) {
switch (ifdefNode.children[i].typeEnum) {
case Sym.comment:
commentCount++;
Expand All @@ -328,7 +328,7 @@ private bool ctodHeaderGuard(ref CtodCtx ctx, ref Node ifdefNode) {
return false;
}
// put remaining children under translation unit instead of the ifdef
foreach (j; 0..ifdefNode.children.length) {
foreach (j; 0 .. ifdefNode.children.length) {
if (j <= i || j + 1 == ifdefNode.children.length) {
ifdefNode.children[j].replace(""); // header guard nodes
} else {
Expand Down Expand Up @@ -402,7 +402,8 @@ void replaceChar(char[] s, char from, char to) pure {
}
}

@("ctodIncludePath") unittest {
@("ctodIncludePath") unittest
{
assert(ctodIncludePath("<folder/file.h>") == "folder.file;");
}

Expand Down Expand Up @@ -515,7 +516,8 @@ string filterCppBlocks(string source) {
return source;
}

unittest {
unittest
{
string source = "
#ifdef __cplusplus
extern \"C\" {
Expand Down
44 changes: 27 additions & 17 deletions source/ctod/ctype.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import ctod.cdeclaration;
import ctod.cexpr;

/// Declaration
struct Decl {
struct Decl
{
CQuals quals; /// qualifiers / storage classes
CType type = CType.none;
string identifier = ""; /// name of variable / function
Expand Down Expand Up @@ -52,10 +53,11 @@ pure nothrow:
///
enum none = Decl.init;
///
bool opCast() const scope {return cast(bool) type;}
bool opCast() const scope { return cast(bool) type; }
}

unittest {
unittest
{
assert(Decl(CQuals.init, CType.named("int"), "x", "3").toString() == "int x = 3");
}

Expand All @@ -77,15 +79,16 @@ private string fmtFunction(const CType retType, string name, const Decl[] params

/// A type in the middle of an expression.
/// C allows to e.g. define a struct inside a parameter list.
struct InlineType {
struct InlineType
{
string keyword;
string name = null;
string body_;
Node* node; // body node

pure nothrow:

bool hasBody() const {return body_.length > 0;}
bool hasBody() const { return body_.length > 0; }
string toString() const {
return keyword ~ " " ~ name ~ (hasBody() ? " " ~ body_ : ";");
}
Expand Down Expand Up @@ -240,7 +243,8 @@ string ctodSizedTypeSpecifier(ref CtodCtx ctx, ref Node node) {
}

/// Qualifiers for a C declaration
struct CQuals {
struct CQuals
{
bool const_;
bool volatile_;
bool restrict;
Expand Down Expand Up @@ -392,7 +396,8 @@ private string intToString(uint i) {
}
}

unittest {
unittest
{
assert(intToString(0) == "0");
assert(intToString(9) == "9");
assert(intToString(4790) == "4790");
Expand Down Expand Up @@ -583,15 +588,17 @@ int stringLiteralSize(string s) {
return cast(uint) result;
}

unittest {
unittest
{
assert(stringLiteralSize(`"abc"`) == 3);
assert(stringLiteralSize(`"\x22\n"`) == 2);
assert(stringLiteralSize(`"\U"`) == -1); // malformed
assert(stringLiteralSize(``) == -1); // malformed
}

package
struct CType {
struct CType
{
enum Tag {
none,
unknown,
Expand Down Expand Up @@ -628,11 +635,11 @@ pure nothrow:
return result;
}

bool opCast() const scope {return tag != Tag.none;}
bool isFunction() const {return tag == Tag.funcDecl;}
bool isStaticArray() const {return tag == Tag.staticArray;}
bool isCArray() const {return tag == Tag.cArray;}
bool isPointer() const {return tag == Tag.pointer;}
bool opCast() const scope { return tag != Tag.none; }
bool isFunction() const { return tag == Tag.funcDecl; }
bool isStaticArray() const { return tag == Tag.staticArray; }
bool isCArray() const { return tag == Tag.cArray; }
bool isPointer() const { return tag == Tag.pointer; }

bool opEquals(const CType other) const scope {
if (other.tag != this.tag) {
Expand Down Expand Up @@ -747,7 +754,8 @@ pure nothrow:
}
}

unittest {
unittest
{
assert(CType.array(CType.array(CType.named("float"), "2"), "10").toString() == "float[2][10]");
assert(CType.pointer(CType.pointer(CType.named("ab"))).toString() == "ab**");
}
Expand All @@ -763,7 +771,8 @@ bool noZeroInitInD(const CType t) {
return false;
}

unittest {
unittest
{
assert(noZeroInitInD(CType.named("float")));
assert(noZeroInitInD(CType.array(CType.array(CType.named("char"), "2"), "10")));
assert(!noZeroInitInD(CType.pointer(CType.named("float"))));
Expand Down Expand Up @@ -813,7 +822,8 @@ string ctodPrimitiveType(string s) {
return mapLookup(basicTypeMap, s, s);
}

unittest {
unittest
{
assert(ctodPrimitiveType("int16_t") == "short");
assert(ctodPrimitiveType("float") == "float");
}
38 changes: 12 additions & 26 deletions source/ctod/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ private void test(string c, string d) {
test("char * const(*(* const bar)[5])(int);", "char* function(int)[5]* bar;");

test("
struct S {
struct S
{
char x[10];
float y;
int z;
};
", "
struct S {
struct S
{
char[10] x = 0;
float y = 0;
int z;
Expand Down Expand Up @@ -235,12 +237,14 @@ void main() {
test("enum OpaqueE;", "enum OpaqueE;");

test("
struct S {
struct S
{
int in;
unsigned char out[2];
};
", "
struct S {
struct S
{
int in_;
ubyte[2] out_;
}
Expand Down Expand Up @@ -342,25 +346,7 @@ alias two = AnEnum.two;
");

test("typedef enum { a, b } Foo;", "enum Foo { a, b }\nalias a = Foo.a;\nalias b = Foo.b;\n");

test("typedef enum { a } *Foo;", "enum _Foo { a }alias Foo = _Foo*;\nalias a = _Foo.a;\n");

test("
typedef enum AnEnum
{
one = 1000123000,
two = 0x7FFFFFFF
} AnEnum;
", "
enum AnEnum {
one = 1000123000,
two = 0x7FFFFFFF
}
alias one = AnEnum.one;
alias two = AnEnum.two;
");

test("enum {A};", "enum {A}"); // don't create aliases for anonymous enums

test("
Expand Down Expand Up @@ -767,15 +753,15 @@ static assert(0, \"error message\");

test("
#ifndef _WIN32
int x;
int x;
#else
int y;
int y;
#endif
", "
version (Windows) {} else {
int x;
int x;
} version (Windows) {
int y;
int y;
}
");

Expand Down
Loading

0 comments on commit eb0546b

Please sign in to comment.