Skip to content

Commit

Permalink
Fix #190
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackerpilot committed Oct 9, 2015
1 parent 1b6e6a5 commit f9b1997
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ private:
/// True if a space should be placed when parenDepth reaches zero
bool spaceAfterParens;

/// True if we're in an ASM block
bool inAsm;

void formatStep()
{
assert(index < tokens.length);
Expand Down Expand Up @@ -202,14 +205,35 @@ private:
{
formatBlockHeader();
}
else if (currentIs(tok!"do"))
{
formatBlockHeader();
}
else if (currentIs(tok!"do"))
{
formatBlockHeader();
}
else if (currentIs(tok!"else"))
{
formatElse();
}
else if (currentIs(tok!"asm"))
{
formatKeyword();
while (index < tokens.length && !currentIs(tok!"{"))
formatStep();
if (index < tokens.length)
{
int depth = 1;
formatStep();
inAsm = true;
while (index < tokens.length)
{
if (currentIs(tok!"{"))
++depth;
else if (currentIs(tok!"}"))
--depth;
formatStep();
}
inAsm = false;
}
}
else if (isKeyword(current.type))
{
formatKeyword();
Expand All @@ -228,7 +252,9 @@ private:
{
writeToken();
if (index < tokens.length && (currentIs(tok!"identifier")
|| isBasicType(current.type) || currentIs(tok!"@") || currentIs(tok!"if")))
|| isBasicType(current.type) || currentIs(tok!"@")
|| currentIs(tok!"if") || isNumberLiteral(tokens[index].type)
|| (inAsm && peekBack2Is(tok!";") && currentIs(tok!"["))))
{
write(" ");
}
Expand Down Expand Up @@ -699,8 +725,6 @@ private:

void formatBlockHeader()
{
//import std.stdio:stderr;
//stderr.writeln(__FUNCTION__);
immutable bool a = !currentIs(tok!"version") && !currentIs(tok!"debug");
immutable bool b = a
|| astInformation.conditionalWithElseLocations.canFindIndex(current.index);
Expand All @@ -712,11 +736,11 @@ private:
if (shouldPushIndent)
indents.push(current.type);
writeToken();
if (currentIs(tok!"("))
{
write(" ");
writeParens(false);
}
if (currentIs(tok!"("))
{
write(" ");
writeParens(false);
}
if (currentIs(tok!"switch") || (currentIs(tok!"final") && peekIs(tok!"switch")))
write(" ");
else if (currentIs(tok!"comment"))
Expand Down Expand Up @@ -1399,9 +1423,9 @@ const pure @safe @nogc:
return peekImplementation(tokenType, -1, ignoreComments);
}

bool peekBackIsKeyword(bool ignoreComments = true)
{
if (index == 0)
bool peekBackIsKeyword(bool ignoreComments = true)
{
if (index == 0)
return false;
auto i = index - 1;
if (ignoreComments)
Expand All @@ -1411,8 +1435,8 @@ const pure @safe @nogc:
return false;
i--;
}
return isKeyword(tokens[i].type);
}
return isKeyword(tokens[i].type);
}

bool peekBackIsOneOf(bool ignoreComments, IdType[] tokenTypes...)
{
Expand Down
8 changes: 8 additions & 0 deletions tests/allman/issue0190.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
unittest
{
asm
{
dl 12345;
movdqu [R8], XMM0;
}
}
8 changes: 8 additions & 0 deletions tests/issue0190.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
unittest
{
asm
{
dl 12345;
movdqu [R8], XMM0;
}
}
6 changes: 6 additions & 0 deletions tests/otbs/issue0190.d.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unittest {
asm {
dl 12345;
movdqu [R8], XMM0;
}
}

0 comments on commit f9b1997

Please sign in to comment.