Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix Issue 9474 - Ddoc'd unittests should work correctly with interspe…
…rsed version(none)
  • Loading branch information
9rnsr committed Mar 20, 2013
1 parent ab1890a commit c74a858
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/func.c
Expand Up @@ -4447,6 +4447,8 @@ Dsymbol *UnitTestDeclaration::syntaxCopy(Dsymbol *s)

void UnitTestDeclaration::semantic(Scope *sc)
{
protection = sc->protection;

if (scope)
{ sc = scope;
scope = NULL;
Expand Down
61 changes: 28 additions & 33 deletions src/parse.c
Expand Up @@ -143,7 +143,7 @@ Dsymbols *Parser::parseModule()
return new Dsymbols();
}

Dsymbols *Parser::parseDeclDefs(int once)
Dsymbols *Parser::parseDeclDefs(int once, Dsymbol **pLastDecl)
{ Dsymbol *s;
Dsymbols *decldefs;
Dsymbols *a;
Expand All @@ -153,6 +153,9 @@ Dsymbols *Parser::parseDeclDefs(int once)
StorageClass storageClass;
Condition *condition;
unsigned char *comment;
Dsymbol *lastDecl = NULL; // used to link unittest to its previous declaration
if (!pLastDecl)
pLastDecl = &lastDecl;

//printf("Parser::parseDeclDefs()\n");
decldefs = new Dsymbols();
Expand Down Expand Up @@ -232,6 +235,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
case TOKinterface:
Ldeclaration:
a = parseDeclarations(STCundefined, NULL);
if (a->dim) *pLastDecl = (*a)[a->dim-1];
decldefs->append(a);
continue;

Expand Down Expand Up @@ -269,20 +273,7 @@ Dsymbols *Parser::parseDeclDefs(int once)

case TOKunittest:
s = parseUnitTest();
if (decldefs && decldefs->dim)
{
Dsymbol *ds = (*decldefs)[decldefs->dim-1];
AttribDeclaration *ad;
while ((ad = ds->isAttribDeclaration()) != NULL)
{
if (ad->decl && ad->decl->dim)
ds = (*ad->decl)[ad->decl->dim-1];
else
break;
}

ds->unittest = (UnitTestDeclaration *)s;
}
if (*pLastDecl) (*pLastDecl)->unittest = (UnitTestDeclaration *)s;
break;

case TOKnew:
Expand Down Expand Up @@ -310,20 +301,20 @@ Dsymbols *Parser::parseDeclDefs(int once)
else if (token.value == TOKif)
{ condition = parseStaticIfCondition();
if (token.value == TOKcolon)
a = parseBlock();
a = parseBlock(pLastDecl);
else
{
Loc lookingForElseSave = lookingForElse;
lookingForElse = loc;
a = parseBlock();
a = parseBlock(pLastDecl);
lookingForElse = lookingForElseSave;
}
aelse = NULL;
if (token.value == TOKelse)
{
Loc elseloc = this->loc;
nextToken();
aelse = parseBlock();
aelse = parseBlock(pLastDecl);
checkDanglingElse(elseloc);
}
s = new StaticIfDeclaration(condition, a, aelse);
Expand Down Expand Up @@ -394,7 +385,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
stc = parseAttribute(&exps);
if (stc)
goto Lstc; // it's a predefined attribute
a = parseBlock();
a = parseBlock(pLastDecl);
s = new UserAttributeDeclaration(exps, a);
break;
}
Expand Down Expand Up @@ -471,6 +462,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
peek(&token)->value == TOKassign)
{
a = parseAutoDeclarations(storageClass, comment);
if (a->dim) *pLastDecl = (*a)[a->dim-1];
decldefs->append(a);
continue;
}
Expand All @@ -491,10 +483,11 @@ Dsymbols *Parser::parseDeclDefs(int once)
)
{
a = parseDeclarations(storageClass, comment);
if (a->dim) *pLastDecl = (*a)[a->dim-1];
decldefs->append(a);
continue;
}
a = parseBlock();
a = parseBlock(pLastDecl);
s = new StorageClassDeclaration(storageClass, a);
break;

Expand All @@ -509,7 +502,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
check(TOKlparen);
Expression *e = parseAssignExp();
check(TOKrparen);
a = parseBlock();
a = parseBlock(pLastDecl);
s = new DeprecatedDeclaration(e, a);
break;
}
Expand All @@ -518,7 +511,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
{
warning(loc, "use @(attributes) instead of [attributes]");
Expressions *exps = parseArguments();
a = parseBlock();
a = parseBlock(pLastDecl);
s = new UserAttributeDeclaration(exps, a);
break;
}
Expand All @@ -531,7 +524,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
{
enum LINK linksave = linkage;
linkage = parseLinkage();
a = parseBlock();
a = parseBlock(pLastDecl);
s = new LinkDeclaration(linkage, a);
linkage = linksave;
break;
Expand All @@ -555,7 +548,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
break;
default: break;
}
a = parseBlock();
a = parseBlock(pLastDecl);
s = new ProtDeclaration(prot, a);
break;

Expand Down Expand Up @@ -583,7 +576,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
else
n = global.structalign; // default

a = parseBlock();
a = parseBlock(pLastDecl);
s = new AlignDeclaration(n, a);
break;
}
Expand All @@ -608,7 +601,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
if (token.value == TOKsemicolon)
a = NULL;
else
a = parseBlock();
a = parseBlock(pLastDecl);
s = new PragmaDeclaration(loc, ident, args, a);
break;
}
Expand Down Expand Up @@ -661,12 +654,12 @@ Dsymbols *Parser::parseDeclDefs(int once)
Lcondition:
{
if (token.value == TOKcolon)
a = parseBlock();
a = parseBlock(pLastDecl);
else
{
Loc lookingForElseSave = lookingForElse;
lookingForElse = loc;
a = parseBlock();
a = parseBlock(pLastDecl);
lookingForElse = lookingForElseSave;
}
}
Expand All @@ -675,7 +668,7 @@ Dsymbols *Parser::parseDeclDefs(int once)
{
Loc elseloc = this->loc;
nextToken();
aelse = parseBlock();
aelse = parseBlock(pLastDecl);
checkDanglingElse(elseloc);
}
s = new ConditionalDeclaration(condition, a, aelse);
Expand All @@ -698,6 +691,8 @@ Dsymbols *Parser::parseDeclDefs(int once)
if (s)
{ decldefs->push(s);
addComment(s, comment);
if (!s->isAttribDeclaration())
*pLastDecl = s;
}
} while (!once);
return decldefs;
Expand Down Expand Up @@ -851,7 +846,7 @@ StorageClass Parser::parseTypeCtor()
* Parse declarations after an align, protection, or extern decl.
*/

Dsymbols *Parser::parseBlock()
Dsymbols *Parser::parseBlock(Dsymbol **pLastDecl)
{
Dsymbols *a = NULL;

Expand All @@ -873,7 +868,7 @@ Dsymbols *Parser::parseBlock()
lookingForElse = 0;

nextToken();
a = parseDeclDefs(0);
a = parseDeclDefs(0, pLastDecl);
if (token.value != TOKrcurly)
{ /* { */
error("matching '}' expected, not %s", token.toChars());
Expand All @@ -889,12 +884,12 @@ Dsymbols *Parser::parseBlock()
#if 0
a = NULL;
#else
a = parseDeclDefs(0); // grab declarations up to closing curly bracket
a = parseDeclDefs(0, pLastDecl); // grab declarations up to closing curly bracket
#endif
break;

default:
a = parseDeclDefs(1);
a = parseDeclDefs(1, pLastDecl);
break;
}
return a;
Expand Down
4 changes: 2 additions & 2 deletions src/parse.h
Expand Up @@ -71,9 +71,9 @@ struct Parser : Lexer
Parser(Module *module, unsigned char *base, size_t length, int doDocComment);

Dsymbols *parseModule();
Dsymbols *parseDeclDefs(int once);
Dsymbols *parseDeclDefs(int once, Dsymbol **pLastDecl = NULL);
Dsymbols *parseAutoDeclarations(StorageClass storageClass, unsigned char *comment);
Dsymbols *parseBlock();
Dsymbols *parseBlock(Dsymbol **pLastDecl);
void composeStorageClass(StorageClass stc);
StorageClass parseAttribute(Expressions **pexps);
StorageClass parsePostfix();
Expand Down
49 changes: 48 additions & 1 deletion test/compilable/ddocunittest.d
Expand Up @@ -173,6 +173,53 @@ unittest
foo(4);
}

// ------------------------------------
// 9474

///
void foo9474() { }

version(none)
unittest { }

/// Example
unittest { foo9474(); }

/// doc
void bar9474() { }

version(none)
unittest { }

/// Example
unittest { bar9474(); }

///
struct S9474
{
}
///
unittest { S9474 s; }

///
auto autovar9474 = 1;
///
unittest { int v = autovar9474; }

///
auto autofun9474() { return 1; }
///
unittest { int n = autofun9474(); }

///
template Template9474()
{
/// Shouldn't link following unittest to here
void foo() {}
}
///
unittest { alias Template9474!() T; }

// ------------------------------------
// 9713

Expand Down Expand Up @@ -221,7 +268,6 @@ unittest
}

// ------------------------------------

// Issue 9758

/// test
Expand All @@ -230,5 +276,6 @@ void foo(){}
///
unittest { }

// ------------------------------------

void main() { }
50 changes: 50 additions & 0 deletions test/compilable/extra-files/ddocunittest.html
Expand Up @@ -117,6 +117,56 @@ <h1>ddocunittest</h1>
</pre>
<br><br>
</dd>
<dt><big><a name="foo9474"></a>void <u>foo9474</u>();
</big></dt>
<dd><b>Examples:</b><br>
Example
<pre class="d_code"><u>foo9474</u>();
</pre>
<br><br>
</dd>
<dt><big><a name="bar9474"></a>void <u>bar9474</u>();
</big></dt>
<dd>doc<br><br>
<b>Examples:</b><br>
Example
<pre class="d_code"><u>bar9474</u>();
</pre>
<br><br>
</dd>
<dt><big><a name="S9474"></a>struct <u>S9474</u>;
</big></dt>
<dd><b>Examples:</b><br>
<pre class="d_code"><u>S9474</u> s;
</pre>
<br><br>
</dd>
<dt><big><a name="autovar9474"></a>int <u>autovar9474</u>;
</big></dt>
<dd><b>Examples:</b><br>
<pre class="d_code"><font color=blue>int</font> v = <u>autovar9474</u>;
</pre>
<br><br>
</dd>
<dt><big><a name="autofun9474"></a>auto <u>autofun9474</u>();
</big></dt>
<dd><b>Examples:</b><br>
<pre class="d_code"><font color=blue>int</font> n = <u>autofun9474</u>();
</pre>
<br><br>
</dd>
<dt><big><a name="Template9474"></a>template <u>Template9474</u>()</big></dt>
<dd><b>Examples:</b><br>
<pre class="d_code"><font color=blue>alias</font> <u>Template9474</u>!() T;
</pre>
<br><br>
<dl><dt><big><a name="foo"></a>void <u>foo</u>();
</big></dt>
<dd>Shouldn't link following unittest to here<br><br>

</dd>
</dl>
</dd>
<dt><big><a name="fooNoDescription"></a>void <u>fooNoDescription</u>();
</big></dt>
<dd><b>Examples:</b><br>
Expand Down

0 comments on commit c74a858

Please sign in to comment.