Skip to content

Commit

Permalink
Merge pull request #3907 from 9rnsr/fix12567
Browse files Browse the repository at this point in the history
[enh] Issue 12567 - Modules can't be marked as deprecated
Conflicts:
	src/hdrgen.c
	src/module.c
  • Loading branch information
MartinNowak committed Oct 8, 2014
1 parent 26e68fd commit 194b4c2
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/hdrgen.c
Expand Up @@ -80,6 +80,17 @@ void toCBuffer(Module *m, OutBuffer *buf, HdrGenState *hgs)
{
if (m->md)
{
if (m->md->isdeprecated)
{
if (m->md->msg)
{
buf->writestring("deprecated(");
toCBuffer(m->md->msg, buf, hgs);
buf->writestring(") ");
}
else
buf->writestring("deprecated ");
}
buf->writestring("module ");
buf->writestring(m->md->toChars());
buf->writeByte(';');
Expand Down
9 changes: 9 additions & 0 deletions src/import.c
Expand Up @@ -190,6 +190,15 @@ void Import::importAll(Scope *sc)
load(sc);
if (mod) // if successfully loaded module
{
if (mod->md && mod->md->isdeprecated)
{
Expression *msg = mod->md->msg;
if (StringExp *se = msg ? msg->toStringExp() : NULL)
mod->deprecation(loc, "is deprecated - %s", se->string);
else
mod->deprecation(loc, "is deprecated");
}

mod->importAll(NULL);

if (!isstatic && !aliasId && !names.dim)
Expand Down
13 changes: 11 additions & 2 deletions src/module.c
Expand Up @@ -22,6 +22,7 @@
#include "import.h"
#include "dsymbol.h"
#include "hdrgen.h"
#include "expression.h"
#include "lexer.h"

#ifdef IN_GCC
Expand Down Expand Up @@ -642,6 +643,14 @@ void Module::importAll(Scope *prevsc)
return;
}

if (md && md->msg)
{
if (StringExp *se = md->msg->toStringExp())
md->msg = se;
else
md->msg->error("string expected, not '%s'", md->msg->toChars());
}

/* Note that modules get their own scope, from scratch.
* This is so regardless of where in the syntax a module
* gets imported, it is unaffected by context.
Expand Down Expand Up @@ -1039,6 +1048,8 @@ ModuleDeclaration::ModuleDeclaration(Loc loc, Identifiers *packages, Identifier
this->packages = packages;
this->id = id;
this->safe = safe;
this->isdeprecated = false;
this->msg = NULL;
}

char *ModuleDeclaration::toChars()
Expand Down Expand Up @@ -1234,5 +1245,3 @@ const char *lookForSourceFile(const char *filename)
}
return NULL;
}


2 changes: 2 additions & 0 deletions src/module.h
Expand Up @@ -187,6 +187,8 @@ struct ModuleDeclaration
Identifier *id;
Identifiers *packages; // array of Identifier's representing packages
bool safe;
bool isdeprecated; // if it is a deprecated module
Expression *msg;

ModuleDeclaration(Loc loc, Identifiers *packages, Identifier *id, bool safe);

Expand Down
30 changes: 27 additions & 3 deletions src/parse.c
Expand Up @@ -98,6 +98,25 @@ Parser::Parser(Loc loc, Module *module, const utf8_t *base, size_t length, int d
Dsymbols *Parser::parseModule()
{
Dsymbols *decldefs;
bool isdeprecated = false;
Expression *msg = NULL;

if (token.value == TOKdeprecated)
{
Token *tk;
if (skipParensIf(peek(&token), &tk) && tk->value == TOKmodule)
{
// deprecated (...) module ...
isdeprecated = true;
nextToken();
if (token.value == TOKlparen)
{
check(TOKlparen);
msg = parseAssignExp();
check(TOKrparen);
}
}
}

// ModuleDeclation leads off
if (token.value == TOKmodule)
Expand All @@ -112,7 +131,8 @@ Dsymbols *Parser::parseModule()
{
nextToken();
if (token.value != TOKidentifier)
{ error("module (system) identifier expected");
{
error("module (system) identifier expected");
goto Lerr;
}
Identifier *id = token.ident;
Expand All @@ -127,7 +147,8 @@ Dsymbols *Parser::parseModule()
#endif

if (token.value != TOKidentifier)
{ error("Identifier expected following module");
{
error("Identifier expected following module");
goto Lerr;
}
else
Expand All @@ -143,13 +164,16 @@ Dsymbols *Parser::parseModule()
a->push(id);
nextToken();
if (token.value != TOKidentifier)
{ error("Identifier expected following package");
{
error("Identifier expected following package");
goto Lerr;
}
id = token.ident;
}

md = new ModuleDeclaration(loc, a, id, safe);
md->isdeprecated = isdeprecated;
md->msg = msg;

if (token.value != TOKsemicolon)
error("';' expected following module declaration instead of %s", token.toChars());
Expand Down
2 changes: 2 additions & 0 deletions test/compilable/extra-files/header12567a.di
@@ -0,0 +1,2 @@
deprecated module header12567a;
void main();
2 changes: 2 additions & 0 deletions test/compilable/extra-files/header12567b.di
@@ -0,0 +1,2 @@
deprecated("message") module header12567b;
void main();
4 changes: 4 additions & 0 deletions test/compilable/imports/a12567.d
@@ -0,0 +1,4 @@
deprecated("This module will be removed in future release.")
module imports.a12567;

void foo() {}
11 changes: 11 additions & 0 deletions test/compilable/test12567a.d
@@ -0,0 +1,11 @@
// REQUIRED_ARGS:
// PERMUTE_ARGS:
/*
TEST_OUTPUT:
---
---
*/
deprecated
module test12567a;

void main() {}
11 changes: 11 additions & 0 deletions test/compilable/test12567b.d
@@ -0,0 +1,11 @@
// REQUIRED_ARGS:
// PERMUTE_ARGS:
/*
TEST_OUTPUT:
---
---
*/
deprecated("message")
module test12567b;

void main() {}
11 changes: 11 additions & 0 deletions test/compilable/test12567c.d
@@ -0,0 +1,11 @@
// REQUIRED_ARGS:
// PERMUTE_ARGS:
/*
TEST_OUTPUT:
---
compilable/test12567c.d(9): Deprecation: module imports.a12567 is deprecated - This module will be removed in future release.
---
*/
import imports.a12567;

void main() { foo(); }
10 changes: 10 additions & 0 deletions test/compilable/test12567d.d
@@ -0,0 +1,10 @@
// REQUIRED_ARGS: -d
// PERMUTE_ARGS:
/*
TEST_OUTPUT:
---
---
*/
import imports.a12567;

void main() { foo(); }
7 changes: 7 additions & 0 deletions test/compilable/testheader12567a.d
@@ -0,0 +1,7 @@
// REQUIRED_ARGS: -o- -H -Hf${RESULTS_DIR}/compilable/header12567a.di
// PERMUTE_ARGS:
// POST_SCRIPT: compilable/extra-files/header-postscript.sh header12567a

deprecated module header12567a;

void main() {}
7 changes: 7 additions & 0 deletions test/compilable/testheader12567b.d
@@ -0,0 +1,7 @@
// REQUIRED_ARGS: -o- -H -Hf${RESULTS_DIR}/compilable/header12567b.di
// PERMUTE_ARGS:
// POST_SCRIPT: compilable/extra-files/header-postscript.sh header12567b

deprecated("message") module header12567b;

void main() {}
8 changes: 8 additions & 0 deletions test/fail_compilation/fail12567.d
@@ -0,0 +1,8 @@
// REQUIRED_ARGS: -o-
/*
TEST_OUTPUT:
---
fail_compilation/fail12567.d(8): Error: string expected, not '"a" ~ "b"'
---
*/
deprecated("a" ~ "b") module fail12567;

0 comments on commit 194b4c2

Please sign in to comment.