#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
static char *readFile(const char *zFilename){
FILE *in;
char *z;
long n;
size_t got;
in = fopen(zFilename, "rb");
if( in==0 ){
fprintf(stderr, "unable to open '%s' for reading\n", zFilename);
exit(1);
}
fseek(in, 0, SEEK_END);
n = ftell(in);
rewind(in);
z = malloc( n+1 );
if( z==0 ){
fprintf(stderr, "cannot allocate %d bytes to store '%s'\n",
(int)(n+1), zFilename);
exit(1);
}
got = fread(z, 1, n, in);
fclose(in);
if( got!=(size_t)n ){
fprintf(stderr, "only read %d of %d bytes from '%s'\n",
(int)got, (int)n, zFilename);
exit(1);
}
z[n] = 0;
return z;
}
static int hasSideEffect(const char *z, unsigned int n){
unsigned int i;
for(i=0; i<n; i++){
if( z[i]=='/' && strncmp(&z[i], "/*side-effects-ok*/", 19)==0 ) return 0;
if( z[i]=='=' && i>0 && z[i-1]!='=' && z[i-1]!='>'
&& z[i-1]!='<' && z[i-1]!='!' && z[i+1]!='=' ) return 1;
if( z[i]=='+' && z[i+1]=='+' ) return 1;
if( z[i]=='-' && z[i+1]=='-' ) return 1;
}
return 0;
}
static unsigned int findCloseParen(const char *z){
unsigned int nOpen = 0;
unsigned i;
for(i=0; z[i]; i++){
if( z[i]=='(' ) nOpen++;
if( z[i]==')' ){
if( nOpen==0 ) break;
nOpen--;
}
}
return i;
}
static unsigned int findAllSideEffects(const char *z){
unsigned int lineno = 1;
unsigned int i;
unsigned int nErr = 0;
char c, prevC = 0;
for(i=0; (c = z[i])!=0; prevC=c, i++){
if( c=='\n' ){ lineno++; continue; }
if( isalpha(c) && !isalpha(prevC) ){
if( strncmp(&z[i],"assert(",7)==0
|| strncmp(&z[i],"ALWAYS(",7)==0
|| strncmp(&z[i],"NEVER(",6)==0
|| strncmp(&z[i],"testcase(",9)==0
){
unsigned int n;
const char *z2 = &z[i+5];
while( z2[0]!='(' ){ z2++; }
z2++;
n = findCloseParen(z2);
if( hasSideEffect(z2, n) ){
nErr++;
fprintf(stderr, "side-effect line %u: %.*s\n", lineno,
(int)(&z2[n+1] - &z[i]), &z[i]);
}
}
}
}
return nErr;
}
int main(int argc, char **argv){
char *z;
unsigned int nErr = 0;
if( argc!=2 ){
fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
return 1;
}
z = readFile(argv[1]);
nErr = findAllSideEffects(z);
free(z);
if( nErr ){
fprintf(stderr, "Found %u undesirable side-effects\n", nErr);
return 1;
}
return 0;
}