Skip to content

Commit

Permalink
Merge pull request #1839 from eskimor/fix767
Browse files Browse the repository at this point in the history
Implemented feature 767 (ready for review & pull)
  • Loading branch information
WalterBright committed Jun 9, 2013
2 parents 44fdd81 + e025f4e commit 2f33a0e
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 43 deletions.
16 changes: 14 additions & 2 deletions src/attrib.c
Expand Up @@ -1038,12 +1038,24 @@ void PragmaDeclaration::semantic(Scope *sc)
StringExp *se = e->toString();
if (!se)
error("string expected for library name, not '%s'", e->toChars());
else if (global.params.verbose)
else
{
char *name = (char *)mem.malloc(se->len + 1);
memcpy(name, se->string, se->len);
name[se->len] = 0;
printf("library %s\n", name);
if (global.params.verbose)
printf("library %s\n", name);
if (global.params.moduleDeps && !global.params.moduleDepsFile)
{
OutBuffer *ob = global.params.moduleDeps;
ob->writestring("depsLib ");
ob->writestring(sc->module->toPrettyChars());
ob->writestring(" (");
escapePath(ob, sc->module->srcfile->toChars());
ob->writestring(") : ");
ob->writestring((char *) name);
ob->writenl();
}
mem.free(name);
}
}
Expand Down
45 changes: 37 additions & 8 deletions src/cond.c
Expand Up @@ -84,16 +84,41 @@ DebugCondition::DebugCondition(Module *mod, unsigned level, Identifier *ident)
{
}

// Helper for printing dependency information
void printDepsConditional(Scope *sc, DVCondition* condition, const char* depType)
{
if (!global.params.moduleDeps || global.params.moduleDepsFile)
return;
OutBuffer *ob = global.params.moduleDeps;
Module* md = sc && sc->module ? sc->module : condition->mod;
if (!md)
return;
ob->writestring(depType);
ob->writestring(md->toPrettyChars());
ob->writestring(" (");
escapePath(ob, md->srcfile->toChars());
ob->writestring(") : ");
if (condition->ident)
ob->printf("%s\n", condition->ident->toChars());
else
ob->printf("%d\n", condition->level);
}


int DebugCondition::include(Scope *sc, ScopeDsymbol *s)
{
//printf("DebugCondition::include() level = %d, debuglevel = %d\n", level, global.params.debuglevel);
if (inc == 0)
{
inc = 2;
bool definedInModule = false;
if (ident)
{
if (findCondition(mod->debugids, ident))
{
inc = 1;
definedInModule = true;
}
else if (findCondition(global.params.debugids, ident))
inc = 1;
else
Expand All @@ -104,6 +129,8 @@ int DebugCondition::include(Scope *sc, ScopeDsymbol *s)
}
else if (level <= global.params.debuglevel || level <= mod->debuglevel)
inc = 1;
if (!definedInModule)
printDepsConditional(sc, this, "depsDebug ");
}
return (inc == 1);
}
Expand All @@ -123,7 +150,7 @@ void VersionCondition::setGlobalLevel(unsigned level)
global.params.versionlevel = level;
}

void VersionCondition::checkPredefined(Loc loc, const char *ident)
bool VersionCondition::isPredefined(const char *ident)
{
static const char* reserved[] =
{
Expand Down Expand Up @@ -211,16 +238,12 @@ void VersionCondition::checkPredefined(Loc loc, const char *ident)
for (unsigned i = 0; i < sizeof(reserved) / sizeof(reserved[0]); i++)
{
if (strcmp(ident, reserved[i]) == 0)
goto Lerror;
return true;
}

if (ident[0] == 'D' && ident[1] == '_')
goto Lerror;

return;

Lerror:
error(loc, "version identifier '%s' is reserved and cannot be set", ident);
return true;
return false;
}

void VersionCondition::addGlobalIdent(const char *ident)
Expand Down Expand Up @@ -249,10 +272,14 @@ int VersionCondition::include(Scope *sc, ScopeDsymbol *s)
if (inc == 0)
{
inc = 2;
bool definedInModule=false;
if (ident)
{
if (findCondition(mod->versionids, ident))
{
inc = 1;
definedInModule = true;
}
else if (findCondition(global.params.versionids, ident))
inc = 1;
else
Expand All @@ -264,6 +291,8 @@ int VersionCondition::include(Scope *sc, ScopeDsymbol *s)
}
else if (level <= global.params.versionlevel || level <= mod->versionlevel)
inc = 1;
if (!definedInModule && (!ident || (!isPredefined(ident->toChars()) && ident != Lexer::idPool(Token::toChars(TOKunittest)) && ident != Lexer::idPool(Token::toChars(TOKassert)))))
printDepsConditional(sc, this, "depsVersion ");
}
return (inc == 1);
}
Expand Down
7 changes: 6 additions & 1 deletion src/cond.h
Expand Up @@ -69,7 +69,12 @@ class VersionCondition : public DVCondition
{
public:
static void setGlobalLevel(unsigned level);
static void checkPredefined(Loc loc, const char *ident);
static bool isPredefined(const char *ident);
static void checkPredefined(Loc loc, const char *ident)
{
if (isPredefined(ident))
error(loc, "version identifier '%s' is reserved and cannot be set", ident);
}
static void addGlobalIdent(const char *ident);
static void addPredefinedGlobalIdent(const char *ident);

Expand Down
14 changes: 14 additions & 0 deletions src/expression.c
Expand Up @@ -7088,6 +7088,20 @@ Expression *FileExp::semantic(Scope *sc)

if (global.params.verbose)
printf("file %s\t(%s)\n", (char *)se->string, name);
if (global.params.moduleDeps != NULL && global.params.moduleDepsFile == NULL)
{
OutBuffer *ob = global.params.moduleDeps;
ob->writestring("depsFile ");
ob->writestring(sc->module->toPrettyChars());
ob->writestring(" (");
escapePath(ob, sc->module->srcfile->toChars());
ob->writestring(") : ");
ob->writestring((char *) se->string);
ob->writestring(" (");
escapePath(ob, name);
ob->writestring(")");
ob->writenl();
}

{ File f(name);
if (f.read())
Expand Down
23 changes: 2 additions & 21 deletions src/import.c
Expand Up @@ -156,26 +156,6 @@ void Import::load(Scope *sc)
//printf("-Import::load('%s'), pkg = %p\n", toChars(), pkg);
}

void escapePath(OutBuffer *buf, const char *fname)
{
while (1)
{
switch (*fname)
{
case 0:
return;
case '(':
case ')':
case '\\':
buf->writebyte('\\');
default:
buf->writebyte(*fname);
break;
}
fname++;
}
}

void Import::importAll(Scope *sc)
{
if (!mod)
Expand Down Expand Up @@ -294,7 +274,8 @@ void Import::semantic(Scope *sc)
*/

OutBuffer *ob = global.params.moduleDeps;

if (!global.params.moduleDepsFile)
ob->writestring("depsImport ");
ob->writestring(sc->module->toPrettyChars());
ob->writestring(" (");
escapePath(ob, sc->module->srcfile->toChars());
Expand Down
59 changes: 48 additions & 11 deletions src/mars.c
Expand Up @@ -335,7 +335,8 @@ Usage:\n\
-debug=ident compile in debug code identified by ident\n\
-debuglib=name set symbolic debug library to name\n\
-defaultlib=name set default library to name\n\
-deps=filename write module dependencies to filename\n%s"
-deps write module import dependencies to stdout. (All dependencies including file/version/debug/lib)\n\
-deps=filename write module dependencies to filename (only imports - deprecated)\n%s"
" -g add symbolic debug info\n\
-gc add symbolic debug info, pretend to be C\n\
-gs always emit stack frame\n\
Expand Down Expand Up @@ -882,11 +883,23 @@ Language changes listed by -transition=id:\n\
setdebuglib = 1;
global.params.debuglibname = p + 1 + 9;
}
else if (memcmp(p + 1, "deps=", 5) == 0)
else if (memcmp(p + 1, "deps", 4) == 0)
{
global.params.moduleDepsFile = p + 1 + 5;
if (!global.params.moduleDepsFile[0])
goto Lnoarg;
if(global.params.moduleDeps)
{
error(Loc(), "-deps[=file] can only be provided once!");
break;
}
if (p[5] == '=')
{
global.params.moduleDepsFile = p + 1 + 5;
if (!global.params.moduleDepsFile[0])
goto Lnoarg;
} // Else output to stdout.
else if (p[5]!='\0')
{
goto Lerror;
}
global.params.moduleDeps = new OutBuffer;
}
else if (strcmp(p + 1, "main") == 0)
Expand Down Expand Up @@ -1489,14 +1502,17 @@ Language changes listed by -transition=id:\n\
}
}

if (global.params.moduleDeps != NULL)
if (global.params.moduleDeps)
{
assert(global.params.moduleDepsFile != NULL);

File deps(global.params.moduleDepsFile);
OutBuffer* ob = global.params.moduleDeps;
deps.setbuffer((void*)ob->data, ob->offset);
deps.writev();
if (global.params.moduleDepsFile)
{
File deps(global.params.moduleDepsFile);
deps.setbuffer((void*)ob->data, ob->offset);
deps.writev();
}
else
printf("%.*s", ob->offset, ob->data);
}

// Scan for functions to inline
Expand Down Expand Up @@ -1788,6 +1804,27 @@ void getenv_setargv(const char *envvar, size_t *pargc, char** *pargv)
*pargv = argv->tdata();
}

void escapePath(OutBuffer *buf, const char *fname)
{
while (1)
{
switch (*fname)
{
case 0:
return;
case '(':
case ')':
case '\\':
buf->writebyte('\\');
default:
buf->writebyte(*fname);
break;
}
fname++;
}
}


/***********************************
* Parse command line arguments for -m32 or -m64
* to detect the desired architecture.
Expand Down
2 changes: 2 additions & 0 deletions src/mars.h
Expand Up @@ -446,5 +446,7 @@ void obj_append(Dsymbol *s);
void obj_write_deferred(Library *library);

const char *importHint(const char *s);
/// Little helper function for writting out deps.
void escapePath(OutBuffer *buf, const char *fname);

#endif /* DMD_MARS_H */

0 comments on commit 2f33a0e

Please sign in to comment.