Skip to content

Commit

Permalink
#154 Detection of stale files
Browse files Browse the repository at this point in the history
Also ensure that there are no HTML files lingering of classes that are
no longer in the package.
  • Loading branch information
SanderMertens committed Oct 25, 2015
1 parent c6f1195 commit 23f76a3
Show file tree
Hide file tree
Showing 178 changed files with 662 additions and 1,543 deletions.
114 changes: 98 additions & 16 deletions generators/c/interface/src/c_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,24 @@ typedef struct c_typeWalk_t {
corto_bool generateHeader;
corto_bool generateSource;
corto_id sizeExpr;
corto_ll generated;
} c_typeWalk_t;

static g_file c_interfaceOpenFile(corto_string name, c_typeWalk_t *data) {
g_file result = g_fileOpen(data->g, name);
if (result) {
corto_llAppend(data->generated, corto_strdup(name));
g_fileWrite(result, "/* $CORTO_GENERATED\n");
g_fileWrite(result, " *\n");
g_fileWrite(result, " * %s\n", name);
g_fileWrite(result, " *\n");
g_fileWrite(result, " * Code written between the begin and end tags will be preserved when the\n");
g_fileWrite(result, " * file is regenerated.\n");
g_fileWrite(result, " */\n\n");
}
return result;
}

/* Generate parameters for method */
static int c_interfaceParam(corto_parameter *o, void *userData) {
c_typeWalk_t* data;
Expand Down Expand Up @@ -494,15 +510,13 @@ static int c_interfaceClassProcedure(corto_object o, void *userData) {

/* Write identifying comment to headerfile */
g_fileWrite(data->header, "\n");
g_fileWrite(data->header, "/* %s */\n", fullname);

/* Start of function */
g_fileWrite(data->header, "%s_EXPORT %s%s _%s", upperName, returnSpec, returnPostfix,
functionName);

/* Write to sourcefile */
g_fileWrite(data->source, "\n");
g_fileWrite(data->source, "/* %s */\n", fullname);

/* Lookup header for function */
header = g_fileLookupHeader(data->source, signatureName);
Expand Down Expand Up @@ -645,8 +659,8 @@ static g_file c_interfaceHeaderFileOpen(corto_generator g, corto_object o, c_typ
} else {
sprintf(headerFileName, "_%s.h", g_fullOid(g, o, name));
}
result = g_fileOpen(g, headerFileName);

result = g_fileOpen(g, headerFileName);
if (!result) {
goto error;
}
Expand All @@ -661,8 +675,9 @@ static g_file c_interfaceHeaderFileOpen(corto_generator g, corto_object o, c_typ
} else {
sprintf(mainHeader, "_%s.h", g_fullOid(g, topLevelObject, topLevelName));
}

data->mainHeader = g_fileOpen(g, mainHeader);
if (!result) {
if (!data->mainHeader) {
goto error;
}
}
Expand Down Expand Up @@ -794,25 +809,18 @@ static corto_string c_interfaceSourceFileName(corto_string name, corto_char* buf
}

/* Open generator sourcefile */
static g_file c_interfaceSourceFileOpen(corto_generator g, corto_string name) {
static g_file c_interfaceSourceFileOpen(corto_string name, c_typeWalk_t *data) {
g_file result;
corto_char fileName[512];
corto_id topLevelName;

result = g_fileOpen(g, c_interfaceSourceFileName(name, fileName));
result = c_interfaceOpenFile(c_interfaceSourceFileName(name, fileName), data);
if (!result) {
goto error;
}

/* Print standard comments and includes */
g_fileWrite(result, "/* %s\n", fileName);
g_fileWrite(result, " *\n");
g_fileWrite(result, " * This file contains the implementation for the generated interface.\n");
g_fileWrite(result, " *\n");
g_fileWrite(result, " * Don't mess with the begin and end tags, since these will ensure that modified\n");
g_fileWrite(result, " * code in interface functions isn't replaced when code is re-generated.\n");
g_fileWrite(result, " */\n\n");
g_fileWrite(result, "#include \"%s.h\"\n", g_fullOid(g, g_getCurrent(g), topLevelName));
/* Print includes */
g_fileWrite(result, "#include \"%s.h\"\n", g_fullOid(data->g, g_getCurrent(data->g), topLevelName));

return result;
error:
Expand Down Expand Up @@ -866,7 +874,7 @@ static corto_int16 c_interfaceObject(corto_object o, c_typeWalk_t* data) {
g_fullOid(data->g, o, id);

/* Open sourcefile */
data->source = c_interfaceSourceFileOpen(data->g, id);
data->source = c_interfaceSourceFileOpen(id, data);
if (!data->source) {
goto error;
}
Expand Down Expand Up @@ -905,6 +913,8 @@ static corto_int16 c_interfaceObject(corto_object o, c_typeWalk_t* data) {
}
g_fileWrite(data->source, "}\n");
}

g_fileClose(data->source);
}

/* Close */
Expand Down Expand Up @@ -938,6 +948,75 @@ static int c_interfaceWalk(corto_object o, void *userData) {
return 0;
}

static corto_bool c_interfaceIsGenerated(corto_string file) {
corto_string content = corto_fileLoad(file);
corto_bool result = FALSE;

if (content) {
if (strlen(content) > 1024) {
/* Token must appear before the first 1024 bytes */
content[1024] = '\0';
}

if (strstr(content, "$CORTO_GENERATED")) {
result = TRUE;
}

corto_dealloc(content);
}
return result;
}

static corto_bool c_interfaceWasGeneratedNow(
corto_string name,
c_typeWalk_t *data)
{
corto_iter iter = corto_llIter(data->generated);

while(corto_iterHasNext(&iter)) {
corto_string file = corto_iterNext(&iter);
if (!strcmp(file, name)) {
return TRUE;
}
}

return FALSE;
}

/* Mark files that haven't been regenerated */
static int c_interfaceMarkUnusedFiles(c_typeWalk_t *data) {
corto_ll files = corto_opendir("./src");
corto_iter iter = corto_llIter(files);

while(corto_iterHasNext(&iter)) {
corto_string file = corto_iterNext(&iter);
corto_id id;
sprintf(id, "./src/%s", file);
if (c_interfaceIsGenerated(id)) {
if (!c_interfaceWasGeneratedNow(file, data)) {
if (!strstr(id, ".old")) {
corto_id newname;
sprintf(newname, "src/%s.old", file);
rename (id, newname);
printf("c_interface: %s: stale file, please remove (renamed to %s.old)\n", file, file);
} else {
printf("c_interface: %s: stale file, please remove\n", file);
}
}
}
}

iter = corto_llIter(data->generated);
while (corto_iterHasNext(&iter)) {
corto_dealloc(corto_iterNext(&iter));
}

corto_closedir(files);
corto_llFree(data->generated);

return 0;
}

/* Entry point for generator */
int corto_genMain(corto_generator g) {
c_typeWalk_t walkData;
Expand All @@ -961,6 +1040,7 @@ int corto_genMain(corto_generator g) {
walkData.source = NULL;
walkData.wrapper = NULL;
walkData.mainHeader = NULL;
walkData.generated = corto_llNew();

/* Walk objects, generate procedures and class members */
if (!g_walkNoScope(g, c_interfaceWalk, &walkData)) {
Expand All @@ -984,6 +1064,8 @@ int corto_genMain(corto_generator g) {
corto_loadFreePackages(packages);
}

c_interfaceMarkUnusedFiles(&walkData);

return 0;
error:
return -1;
Expand Down
1 change: 1 addition & 0 deletions generators/html/src/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ corto_int16 corto_genMain(corto_generator g) {
const char docsFilename[] = "doc";
corto_object md;

system("rm -rf doc");
corto_mkdir(docsFilename);
htmlData_t data = {docsFilename, 1, "", g};

Expand Down
13 changes: 4 additions & 9 deletions packages/corto/ast/src/ast.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* ast.c
/* $CORTO_GENERATED
*
* This file contains the implementation for the generated interface.
* ast.c
*
* Don't mess with the begin and end tags, since these will ensure that modified
* code in interface functions isn't replaced when code is re-generated.
* Code written between the begin and end tags will be preserved when the
* file is regenerated.
*/

#include "ast.h"
Expand Down Expand Up @@ -147,7 +147,6 @@ ast_Call ast_createCallFromExpr(ast_Expression f, ast_Expression arguments) {
}
/* $end */

/* ::corto::ast::isOperatorAssignment(operatorKind operator) */
corto_bool _ast_isOperatorAssignment(corto_operatorKind _operator) {
/* $begin(::corto::ast::isOperatorAssignment) */
corto_bool result;
Expand All @@ -170,7 +169,6 @@ corto_bool _ast_isOperatorAssignment(corto_operatorKind _operator) {
/* $end */
}

/* ::corto::ast::report(string kind,string filename,uint32 line,uint32 column,string error,string token) */
corto_void _ast_report(corto_string kind, corto_string filename, corto_uint32 line, corto_uint32 column, corto_string error, corto_string token) {
/* $begin(::corto::ast::report) */
CORTO_UNUSED(token);
Expand All @@ -184,7 +182,6 @@ corto_void _ast_report(corto_string kind, corto_string filename, corto_uint32 li
/* $end */
}

/* ::corto::ast::reportError(string filename,uint32 line,uint32 column,string error,string token) */
corto_void _ast_reportError(corto_string filename, corto_uint32 line, corto_uint32 column, corto_string error, corto_string token) {
/* $begin(::corto::ast::reportError) */

Expand All @@ -193,7 +190,6 @@ corto_void _ast_reportError(corto_string filename, corto_uint32 line, corto_uint
/* $end */
}

/* ::corto::ast::reportWarning(string filename,uint32 line,uint32 column,string error,string token) */
corto_void _ast_reportWarning(corto_string filename, corto_uint32 line, corto_uint32 column, corto_string error, corto_string token) {
/* $begin(::corto::ast::reportWarning) */

Expand All @@ -202,7 +198,6 @@ corto_void _ast_reportWarning(corto_string filename, corto_uint32 line, corto_ui
/* $end */
}

/* ::corto::ast::valueKindFromType(type type) */
ast_valueKind _ast_valueKindFromType(corto_type type) {
/* $begin(::corto::ast::valueKindFromType) */
ast_valueKind result = Ast_Nothing;
Expand Down
14 changes: 4 additions & 10 deletions packages/corto/ast/src/ast_Binary.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* ast_Binary.c
/* $CORTO_GENERATED
*
* This file contains the implementation for the generated interface.
* ast_Binary.c
*
* Don't mess with the begin and end tags, since these will ensure that modified
* code in interface functions isn't replaced when code is re-generated.
* Code written between the begin and end tags will be preserved when the
* file is regenerated.
*/

#include "ast.h"
Expand Down Expand Up @@ -404,7 +404,6 @@ corto_int16 ast_Binary_toIc_strOp(

/* $end */

/* ::corto::ast::Binary::construct() */
corto_int16 _ast_Binary_construct(ast_Binary this) {
/* $begin(::corto::ast::Binary::construct) */
corto_type lvalueType, rvalueType;
Expand Down Expand Up @@ -454,7 +453,6 @@ corto_int16 _ast_Binary_construct(ast_Binary this) {
/* $end */
}

/* ::corto::ast::Binary::fold() */
ast_Expression _ast_Binary_fold(ast_Binary this) {
/* $begin(::corto::ast::Binary::fold) */
ast_Expression result = ast_Expression(this);
Expand Down Expand Up @@ -540,7 +538,6 @@ ast_Expression _ast_Binary_fold(ast_Binary this) {
/* $end */
}

/* ::corto::ast::Binary::hasReturnedResource() */
corto_bool _ast_Binary_hasReturnedResource_v(ast_Binary this) {
/* $begin(::corto::ast::Binary::hasReturnedResource) */

Expand All @@ -550,7 +547,6 @@ corto_bool _ast_Binary_hasReturnedResource_v(ast_Binary this) {
/* $end */
}

/* ::corto::ast::Binary::hasSideEffects() */
corto_bool _ast_Binary_hasSideEffects_v(ast_Binary this) {
/* $begin(::corto::ast::Binary::hasSideEffects) */
corto_bool result = FALSE;
Expand All @@ -574,7 +570,6 @@ corto_bool _ast_Binary_hasSideEffects_v(ast_Binary this) {
/* $end */
}

/* ::corto::ast::Binary::setOperator(operatorKind kind) */
corto_void _ast_Binary_setOperator(ast_Binary this, corto_operatorKind kind) {
/* $begin(::corto::ast::Binary::setOperator) */
ast_Binary compoundExpr = NULL;
Expand Down Expand Up @@ -645,7 +640,6 @@ corto_void _ast_Binary_setOperator(ast_Binary this, corto_operatorKind kind) {
/* $end */
}

/* ::corto::ast::Binary::toIc(ic::program program,ic::storage storage,bool stored) */
ic_node _ast_Binary_toIc_v(ast_Binary this, ic_program program, ic_storage storage, corto_bool stored) {
/* $begin(::corto::ast::Binary::toIc) */
ic_node returnsResult = NULL;
Expand Down
Loading

0 comments on commit 23f76a3

Please sign in to comment.