Skip to content

Commit

Permalink
Move Module::gendocfile into a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed Feb 21, 2014
1 parent 0624e86 commit 5e025ac
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
82 changes: 43 additions & 39 deletions src/doc.c
Expand Up @@ -225,7 +225,7 @@ static const char ddoc_decl_dd_e[] = ")\n";
/****************************************************
*/

void Module::gendocfile()
void gendocfile(Module *m)
{
static OutBuffer mbuf;
static int mbuf_done;
Expand All @@ -235,7 +235,8 @@ void Module::gendocfile()
//printf("Module::gendocfile()\n");

if (!mbuf_done) // if not already read the ddoc files
{ mbuf_done = 1;
{
mbuf_done = 1;

// Use our internal default
mbuf.write(ddoc_default, strlen(ddoc_default));
Expand All @@ -250,75 +251,77 @@ void Module::gendocfile()
{
FileName f((*global.params.ddocfiles)[i]);
File file(&f);
readFile(loc, &file);
readFile(m->loc, &file);
// BUG: convert file contents to UTF-8 before use

//printf("file: '%.*s'\n", file.len, file.buffer);
mbuf.write(file.buffer, file.len);
}
}
DocComment::parseMacros(&escapetable, &macrotable, (utf8_t *)mbuf.data, mbuf.offset);
DocComment::parseMacros(&m->escapetable, &m->macrotable, (utf8_t *)mbuf.data, mbuf.offset);

Scope *sc = Scope::createGlobal(this); // create root scope
Scope *sc = Scope::createGlobal(m); // create root scope
sc->docbuf = &buf;

DocComment *dc = DocComment::parse(sc, this, comment);
dc->pmacrotable = &macrotable;
dc->pescapetable = &escapetable;
DocComment *dc = DocComment::parse(sc, m, m->comment);
dc->pmacrotable = &m->macrotable;
dc->pescapetable = &m->escapetable;

// Generate predefined macros

// Set the title to be the name of the module
{ const char *p = toPrettyChars();
Macro::define(&macrotable, (utf8_t *)"TITLE", 5, (utf8_t *)p, strlen(p));
{
const char *p = m->toPrettyChars();
Macro::define(&m->macrotable, (utf8_t *)"TITLE", 5, (utf8_t *)p, strlen(p));
}

// Set time macros
{ time_t t;
{
time_t t;
time(&t);
char *p = ctime(&t);
p = mem.strdup(p);
Macro::define(&macrotable, (utf8_t *)"DATETIME", 8, (utf8_t *)p, strlen(p));
Macro::define(&macrotable, (utf8_t *)"YEAR", 4, (utf8_t *)p + 20, 4);
Macro::define(&m->macrotable, (utf8_t *)"DATETIME", 8, (utf8_t *)p, strlen(p));
Macro::define(&m->macrotable, (utf8_t *)"YEAR", 4, (utf8_t *)p + 20, 4);
}

char *srcfilename = srcfile->toChars();
Macro::define(&macrotable, (utf8_t *)"SRCFILENAME", 11, (utf8_t *)srcfilename, strlen(srcfilename));
char *srcfilename = m->srcfile->toChars();
Macro::define(&m->macrotable, (utf8_t *)"SRCFILENAME", 11, (utf8_t *)srcfilename, strlen(srcfilename));

char *docfilename = docfile->toChars();
Macro::define(&macrotable, (utf8_t *)"DOCFILENAME", 11, (utf8_t *)docfilename, strlen(docfilename));
char *docfilename = m->docfile->toChars();
Macro::define(&m->macrotable, (utf8_t *)"DOCFILENAME", 11, (utf8_t *)docfilename, strlen(docfilename));

if (dc->copyright)
{
dc->copyright->nooutput = 1;
Macro::define(&macrotable, (utf8_t *)"COPYRIGHT", 9, dc->copyright->body, dc->copyright->bodylen);
Macro::define(&m->macrotable, (utf8_t *)"COPYRIGHT", 9, dc->copyright->body, dc->copyright->bodylen);
}

buf.printf("$(DDOC_COMMENT Generated by Ddoc from %s)\n", srcfile->toChars());
if (isDocFile)
buf.printf("$(DDOC_COMMENT Generated by Ddoc from %s)\n", m->srcfile->toChars());
if (m->isDocFile)
{
size_t commentlen = strlen((char *)comment);
size_t commentlen = strlen((char *)m->comment);
if (dc->macros)
{
commentlen = dc->macros->name - comment;
dc->macros->write(dc, sc, this, sc->docbuf);
commentlen = dc->macros->name - m->comment;
dc->macros->write(dc, sc, m, sc->docbuf);
}
sc->docbuf->write(comment, commentlen);
highlightText(sc, this, sc->docbuf, 0);
sc->docbuf->write(m->comment, commentlen);
highlightText(sc, m, sc->docbuf, 0);
}
else
{
dc->writeSections(sc, this, sc->docbuf);
emitMemberComments(this, sc);
dc->writeSections(sc, m, sc->docbuf);
emitMemberComments(m, sc);
}

//printf("BODY= '%.*s'\n", buf.offset, buf.data);
Macro::define(&macrotable, (utf8_t *)"BODY", 4, (utf8_t *)buf.data, buf.offset);
Macro::define(&m->macrotable, (utf8_t *)"BODY", 4, (utf8_t *)buf.data, buf.offset);

OutBuffer buf2;
buf2.writestring("$(DDOC)\n");
size_t end = buf2.offset;
macrotable->expand(&buf2, 0, &end, NULL, 0);
m->macrotable->expand(&buf2, 0, &end, NULL, 0);

#if 1
/* Remove all the escape sequences from buf2,
Expand Down Expand Up @@ -352,15 +355,16 @@ void Module::gendocfile()
}

// Transfer image to file
assert(docfile);
docfile->setbuffer(buf.data, buf.offset);
docfile->ref = 1;
ensurePathToNameExists(Loc(), docfile->toChars());
writeFile(loc, docfile);
assert(m->docfile);
m->docfile->setbuffer(buf.data, buf.offset);
m->docfile->ref = 1;
ensurePathToNameExists(Loc(), m->docfile->toChars());
writeFile(m->loc, m->docfile);
#else
/* Remove all the escape sequences from buf2
*/
{ size_t i = 0;
{
size_t i = 0;
utf8_t *p = buf2.data;
for (size_t j = 0; j < buf2.offset; j++)
{
Expand All @@ -376,10 +380,10 @@ void Module::gendocfile()
}

// Transfer image to file
docfile->setbuffer(buf2.data, buf2.offset);
docfile->ref = 1;
ensurePathToNameExists(Loc(), docfile->toChars());
writeFile(loc, docfile);
m->docfile->setbuffer(buf2.data, buf2.offset);
m->docfile->ref = 1;
ensurePathToNameExists(Loc(), m->docfile->toChars());
writeFile(m->loc, m->docfile);
#endif
}

Expand Down
1 change: 1 addition & 0 deletions src/doc.h
Expand Up @@ -16,5 +16,6 @@
#endif /* __DMC__ */

void escapeDdocString(OutBuffer *buf, size_t start);
void gendocfile(Module *m);

#endif
7 changes: 4 additions & 3 deletions src/mars.c
Expand Up @@ -36,6 +36,7 @@
#include "json.h"
#include "declaration.h"
#include "hdrgen.h"
#include "doc.h"

int response_expand(size_t *pargc, const char ***pargv);
void browse(const char *url);
Expand Down Expand Up @@ -1464,7 +1465,7 @@ Language changes listed by -transition=id:\n\
if (m->isDocFile)
{
anydocfiles = true;
m->gendocfile();
gendocfile(m);

// Remove m from list of modules
modules.remove(modi);
Expand Down Expand Up @@ -1695,7 +1696,7 @@ Language changes listed by -transition=id:\n\
if (entrypoint && m == rootHasMain)
entrypoint->genobjfile(0);
if (!global.errors && global.params.doDocComments)
m->gendocfile();
gendocfile(m);
}
if (!global.errors && modules.dim)
{
Expand Down Expand Up @@ -1726,7 +1727,7 @@ Language changes listed by -transition=id:\n\
else
{
if (global.params.doDocComments)
m->gendocfile();
gendocfile(m);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/module.h
Expand Up @@ -127,7 +127,6 @@ class Module : public Package
void semantic3(); // pass 3 semantic analysis
void genobjfile(int multiobj);
void gensymfile();
void gendocfile();
int needModuleInfo();
Dsymbol *search(Loc loc, Identifier *ident, int flags = IgnoreNone);
void deleteObjFile();
Expand Down

0 comments on commit 5e025ac

Please sign in to comment.