improve cpp performance

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7862 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-11-23 03:10:00 +00:00
commit 1c7786574f
3 changed files with 201 additions and 162 deletions

View file

@ -65,6 +65,14 @@
#define DohGetc DOH_NAMESPACE(Getc) #define DohGetc DOH_NAMESPACE(Getc)
#define DohPutc DOH_NAMESPACE(Putc) #define DohPutc DOH_NAMESPACE(Putc)
#define DohUngetc DOH_NAMESPACE(Ungetc) #define DohUngetc DOH_NAMESPACE(Ungetc)
#define DohStringGetc DOH_NAMESPACE(StringGetc)
#define DohStringPutc DOH_NAMESPACE(StringPutc)
#define DohStringUngetc DOH_NAMESPACE(StringUngetc)
#define DohStringAppend DOH_NAMESPACE(StringAppend)
#define DohGetline DOH_NAMESPACE(Getline) #define DohGetline DOH_NAMESPACE(Getline)
#define DohSetline DOH_NAMESPACE(Setline) #define DohSetline DOH_NAMESPACE(Setline)
#define DohGetfile DOH_NAMESPACE(Getfile) #define DohGetfile DOH_NAMESPACE(Getfile)
@ -218,6 +226,8 @@ extern int DohGetc(DOHFile *obj);
extern int DohPutc(int ch, DOHFile *obj); extern int DohPutc(int ch, DOHFile *obj);
extern int DohUngetc(int ch, DOHFile *obj); extern int DohUngetc(int ch, DOHFile *obj);
/* Iterators */ /* Iterators */
extern DohIterator DohFirst(DOH *obj); extern DohIterator DohFirst(DOH *obj);
extern DohIterator DohNext(DohIterator x); extern DohIterator DohNext(DohIterator x);
@ -234,6 +244,16 @@ extern void DohSetfile(DOH *obj, DOH *file);
extern int DohReplace(DOHString *src, const DOHString_or_char *token, const DOHString_or_char *rep, int flags); extern int DohReplace(DOHString *src, const DOHString_or_char *token, const DOHString_or_char *rep, int flags);
extern void DohChop(DOHString *src); extern void DohChop(DOHString *src);
extern int DohString_putc(DOH *so, int ch);
extern int DohString_getc(DOH *so);
extern int DohString_ungetc(DOH *so, int ch);
extern void DohString_append(DOH *so, DOH *str);
#define DohStringPutc(ch,so) DohString_putc(so, ch)
#define DohStringGetc(so) DohString_getc(so)
#define DohStringUngetc(ch,so) DohString_ungetc(so, ch)
#define DohStringAppend(so,str) DohString_append(so, str)
/* Meta-variables */ /* Meta-variables */
extern DOH *DohGetmeta(DOH *, const DOH *); extern DOH *DohGetmeta(DOH *, const DOH *);
extern int DohSetmeta(DOH *, const DOH *, const DOH *value); extern int DohSetmeta(DOH *, const DOH *, const DOH *value);
@ -355,6 +375,12 @@ extern void DohMemoryDebug(void);
#define Getc DohGetc #define Getc DohGetc
#define Putc DohPutc #define Putc DohPutc
#define Ungetc DohUngetc #define Ungetc DohUngetc
#define StringGetc DohStringGetc
#define StringPutc DohStringPutc
#define StringUngetc DohStringUngetc
#define StringAppend DohStringAppend
#define Close DohClose #define Close DohClose
#define vPrintf DohvPrintf #define vPrintf DohvPrintf
#define GetInt DohGetInt #define GetInt DohGetInt

View file

@ -154,13 +154,16 @@ String_hash(DOH *so) {
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* static add(String *s, const char *newstr) - Append to s * DohString_append(String *s, const char *newstr) - Append to s
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
static void void
add(String *s, const char *newstr) { DohString_append(DOH *so, DOH *str) {
int oldlen, newlen, newmaxsize, l, i, sp; int oldlen, newlen, newmaxsize, l, i, sp;
char *tc; char *tc;
String *s = (String *) ObjData(so);
char *newstr = (char *) DohData(str);
if (!newstr) return; if (!newstr) return;
s->hashkey = -1; s->hashkey = -1;
l = (int) strlen(newstr); l = (int) strlen(newstr);
@ -208,19 +211,21 @@ String_clear(DOH *so)
static int static int
String_insert(DOH *so, int pos, DOH *str) String_insert(DOH *so, int pos, DOH *str)
{ {
String *s = (String *) ObjData(so); String *s;
char *nstr; char *nstr;
int len; int len;
char *data; char *data;
if (pos == DOH_END) {
DohString_append(so, str);
return 0;
}
s = (String *) ObjData(so);
s->hashkey = -1;
data = (char *) DohData(str); data = (char *) DohData(str);
nstr = s->str; nstr = s->str;
s->hashkey = -1;
if (pos == DOH_END) {
add(s, data);
return 0;
}
if (pos < 0) pos = 0; if (pos < 0) pos = 0;
else if (pos > s->len) pos = s->len; else if (pos > s->len) pos = s->len;
@ -417,8 +422,8 @@ String_tell(DOH *so)
* int String_putc() * int String_putc()
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
static int int
String_putc(DOH *so, int ch) DohString_putc(DOH *so, int ch)
{ {
register int len, maxsize, sp; register int len, maxsize, sp;
String *s = (String *) ObjData(so); String *s = (String *) ObjData(so);
@ -429,9 +434,9 @@ String_putc(DOH *so, int ch)
register char *tc; register char *tc;
maxsize = s->maxsize; maxsize = s->maxsize;
if (len > (maxsize-2)) { if (len > (maxsize-2)) {
s->str = (char *) DohRealloc(s->str,2*maxsize); s->str = (char *) DohRealloc(s->str,3*maxsize);
assert(s->str); assert(s->str);
s->maxsize = 2*maxsize; s->maxsize = 3*maxsize;
} }
tc = s->str + len; tc = s->str + len;
*(tc++) = ch; *(tc++) = ch;
@ -452,8 +457,8 @@ String_putc(DOH *so, int ch)
* int String_getc() * int String_getc()
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
static int int
String_getc(DOH *so) DohString_getc(DOH *so)
{ {
int c; int c;
String *s = (String *) ObjData(so); String *s = (String *) ObjData(so);
@ -469,8 +474,8 @@ String_getc(DOH *so)
* int String_ungetc() * int String_ungetc()
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
static int int
String_ungetc(DOH *so, int ch) DohString_ungetc(DOH *so, int ch)
{ {
String *s = (String *) ObjData(so); String *s = (String *) ObjData(so);
if (ch == EOF) return ch; if (ch == EOF) return ch;
@ -480,7 +485,6 @@ String_ungetc(DOH *so, int ch)
return ch; return ch;
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* replace_simple(String *str, char *token, char *rep, int flags, int count) * replace_simple(String *str, char *token, char *rep, int flags, int count)
* *
@ -882,9 +886,9 @@ static DohListMethods StringListMethods = {
static DohFileMethods StringFileMethods = { static DohFileMethods StringFileMethods = {
String_read, String_read,
String_write, String_write,
String_putc, DohString_putc,
String_getc, DohString_getc,
String_ungetc, DohString_ungetc,
String_seek, String_seek,
String_tell, String_tell,
0, /* close */ 0, /* close */

View file

@ -46,26 +46,26 @@ isidchar(int c) {
/* Skip whitespace */ /* Skip whitespace */
static void static void
skip_whitespace(File *s, File *out) { skip_whitespace(String *s, String *out) {
int c; int c;
while ((c = Getc(s)) != EOF) { while ((c = StringGetc(s)) != EOF) {
if (!isspace(c)) { if (!isspace(c)) {
Ungetc(c,s); StringUngetc(c,s);
break; break;
} else if (out) Putc(c,out); } else if (out) StringPutc(c,out);
} }
} }
/* Skip to a specified character taking line breaks into account */ /* Skip to a specified character taking line breaks into account */
static int static int
skip_tochar(File *s, int ch, File *out) { skip_tochar(String *s, int ch, String *out) {
int c; int c;
while ((c = Getc(s)) != EOF) { while ((c = StringGetc(s)) != EOF) {
if (out) Putc(c,out); if (out) StringPutc(c,out);
if (c == ch) break; if (c == ch) break;
if (c == '\\') { if (c == '\\') {
c = Getc(s); c = StringGetc(s);
if ((c != EOF) && (out)) Putc(c,out); if ((c != EOF) && (out)) StringPutc(c,out);
} }
} }
if (c == EOF) return -1; if (c == EOF) return -1;
@ -113,6 +113,10 @@ List *Preprocessor_depend(void) {
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* void Preprocessor_cpp_init() - Initialize the preprocessor * void Preprocessor_cpp_init() - Initialize the preprocessor
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
static String *k_args = 0;
static String *k_name = 0;
static String *k_value = 0;
void Preprocessor_init(void) { void Preprocessor_init(void) {
Hash *s; Hash *s;
cpp = NewHash(); cpp = NewHash();
@ -120,6 +124,10 @@ void Preprocessor_init(void) {
Setattr(cpp,"symbols",s); Setattr(cpp,"symbols",s);
Preprocessor_expr_init(); /* Initialize the expression evaluator */ Preprocessor_expr_init(); /* Initialize the expression evaluator */
included_files = NewHash(); included_files = NewHash();
k_args = NewString("args");
k_name = NewString("name");
k_value = NewString("value");
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* void Preprocessor_include_all() - Instruct preprocessor to include all files * void Preprocessor_include_all() - Instruct preprocessor to include all files
@ -199,14 +207,14 @@ Hash *Preprocessor_define(const String_or_char *_str, int swigmacro)
/* Now look for a macro name */ /* Now look for a macro name */
macroname = NewString(""); macroname = NewString("");
while ((c = Getc(str)) != EOF) { while ((c = StringGetc(str)) != EOF) {
if (c == '(') { if (c == '(') {
argstr = NewString(""); argstr = NewString("");
copy_location(str,argstr); copy_location(str,argstr);
/* It is a macro. Go extract it's argument string */ /* It is a macro. Go extract it's argument string */
while ((c = Getc(str)) != EOF) { while ((c = StringGetc(str)) != EOF) {
if (c == ')') break; if (c == ')') break;
else Putc(c,argstr); else StringPutc(c,argstr);
} }
if (c != ')') { if (c != ')') {
Swig_error(Getfile(str),Getline(str), "Missing \')\' in macro parameters\n"); Swig_error(Getfile(str),Getline(str), "Missing \')\' in macro parameters\n");
@ -214,28 +222,28 @@ Hash *Preprocessor_define(const String_or_char *_str, int swigmacro)
} }
break; break;
} else if (isidchar(c) || (c == '%')) { } else if (isidchar(c) || (c == '%')) {
Putc(c,macroname); StringPutc(c,macroname);
} else if (isspace(c)) { } else if (isspace(c)) {
break; break;
} else if (c == '\\') { } else if (c == '\\') {
c = Getc(str); c = StringGetc(str);
if (c != '\n') { if (c != '\n') {
Ungetc(c,str); StringUngetc(c,str);
Ungetc('\\',str); StringUngetc('\\',str);
break; break;
} }
} else { } else {
/*Swig_error(Getfile(str),Getline(str),"Illegal character in macro name\n"); /*Swig_error(Getfile(str),Getline(str),"Illegal character in macro name\n");
goto macro_error; */ goto macro_error; */
Ungetc(c,str); StringUngetc(c,str);
break; break;
} }
} }
if (!swigmacro) if (!swigmacro)
skip_whitespace(str,0); skip_whitespace(str,0);
macrovalue = NewString(""); macrovalue = NewString("");
while ((c = Getc(str)) != EOF) { while ((c = StringGetc(str)) != EOF) {
Putc(c,macrovalue); StringPutc(c,macrovalue);
} }
/* If there are any macro arguments, convert into a list */ /* If there are any macro arguments, convert into a list */
@ -244,7 +252,7 @@ Hash *Preprocessor_define(const String_or_char *_str, int swigmacro)
arglist = NewList(); arglist = NewList();
Seek(argstr,0,SEEK_SET); Seek(argstr,0,SEEK_SET);
argname = NewString(""); argname = NewString("");
while ((c = Getc(argstr)) != EOF) { while ((c = StringGetc(argstr)) != EOF) {
if (c == ',') { if (c == ',') {
varargname = Macro_vararg_name(argname, str); varargname = Macro_vararg_name(argname, str);
if (varargname) { if (varargname) {
@ -255,7 +263,7 @@ Hash *Preprocessor_define(const String_or_char *_str, int swigmacro)
Delete(argname); Delete(argname);
argname = NewString(""); argname = NewString("");
} else if (isidchar(c) || (c == '.')) { } else if (isidchar(c) || (c == '.')) {
Putc(c,argname); StringPutc(c,argname);
} else if (!(isspace(c) || (c == '\\'))) { } else if (!(isspace(c) || (c == '\\'))) {
Swig_error(Getfile(str),Getline(str),"Illegal character in macro argument name\n"); Swig_error(Getfile(str),Getline(str),"Illegal character in macro argument name\n");
goto macro_error; goto macro_error;
@ -349,15 +357,15 @@ Hash *Preprocessor_define(const String_or_char *_str, int swigmacro)
/* Go create the macro */ /* Go create the macro */
macro = NewHash(); macro = NewHash();
Setattr(macro,"name", macroname); Setattr(macro,k_name, macroname);
if (arglist) { if (arglist) {
Setattr(macro,"args",arglist); Setattr(macro,k_args,arglist);
Delete(arglist); Delete(arglist);
if (varargs) { if (varargs) {
Setattr(macro,"varargs","1"); Setattr(macro,"varargs","1");
} }
} }
Setattr(macro,"value",macrovalue); Setattr(macro,k_value,macrovalue);
Delete(macrovalue); Delete(macrovalue);
Setline(macro,line); Setline(macro,line);
Setfile(macro,file); Setfile(macro,file);
@ -366,7 +374,7 @@ Hash *Preprocessor_define(const String_or_char *_str, int swigmacro)
} }
symbols = Getattr(cpp,"symbols"); symbols = Getattr(cpp,"symbols");
if ((m1 = Getattr(symbols,macroname))) { if ((m1 = Getattr(symbols,macroname))) {
if (Cmp(Getattr(m1,"value"),macrovalue)) { if (Cmp(Getattr(m1,k_value),macrovalue)) {
Swig_error(Getfile(str),Getline(str),"Macro '%s' redefined,\n",macroname); Swig_error(Getfile(str),Getline(str),"Macro '%s' redefined,\n",macroname);
Swig_error(Getfile(m1),Getline(m1),"previous definition of '%s'.\n",macroname); Swig_error(Getfile(m1),Getline(m1),"previous definition of '%s'.\n",macroname);
goto macro_error; goto macro_error;
@ -422,41 +430,41 @@ find_args(String *s)
skip_whitespace(s,0); skip_whitespace(s,0);
/* Now see if the next character is a '(' */ /* Now see if the next character is a '(' */
c = Getc(s); c = StringGetc(s);
if (c != '(') { if (c != '(') {
/* Not a macro, bail out now! */ /* Not a macro, bail out now! */
Seek(s,pos, SEEK_SET); Seek(s,pos, SEEK_SET);
Delete(args); Delete(args);
return 0; return 0;
} }
c = Getc(s); c = StringGetc(s);
/* Okay. This appears to be a macro so we will start isolating arguments */ /* Okay. This appears to be a macro so we will start isolating arguments */
while (c != EOF) { while (c != EOF) {
if (isspace(c)) { if (isspace(c)) {
skip_whitespace(s,0); /* Skip leading whitespace */ skip_whitespace(s,0); /* Skip leading whitespace */
c = Getc(s); c = StringGetc(s);
} }
str = NewString(""); str = NewString("");
copy_location(s,str); copy_location(s,str);
level = 0; level = 0;
while (c != EOF) { while (c != EOF) {
if (c == '\"') { if (c == '\"') {
Putc(c,str); StringPutc(c,str);
skip_tochar(s,'\"',str); skip_tochar(s,'\"',str);
c = Getc(s); c = StringGetc(s);
continue; continue;
} else if (c == '\'') { } else if (c == '\'') {
Putc(c,str); StringPutc(c,str);
skip_tochar(s,'\'',str); skip_tochar(s,'\'',str);
c = Getc(s); c = StringGetc(s);
continue; continue;
} }
if ((c == ',') && (level == 0)) break; if ((c == ',') && (level == 0)) break;
if ((c == ')') && (level == 0)) break; if ((c == ')') && (level == 0)) break;
Putc(c,str); StringPutc(c,str);
if (c == '(') level++; if (c == '(') level++;
if (c == ')') level--; if (c == ')') level--;
c = Getc(s); c = StringGetc(s);
} }
if (level > 0) { if (level > 0) {
goto unterm; goto unterm;
@ -470,7 +478,7 @@ find_args(String *s)
Append(args,str); */ Append(args,str); */
if (c == ')') return args; if (c == ')') return args;
c = Getc(s); c = StringGetc(s);
} }
unterm: unterm:
Swig_error(Getfile(args),Getline(args),"Unterminated macro call.\n"); Swig_error(Getfile(args),Getline(args),"Unterminated macro call.\n");
@ -492,17 +500,17 @@ get_filename(String *str, int* sysfile) {
skip_whitespace(str,0); skip_whitespace(str,0);
fn = NewString(""); fn = NewString("");
copy_location(str,fn); copy_location(str,fn);
c = Getc(str); c = StringGetc(str);
*sysfile = 0; *sysfile = 0;
if (c == '\"') { if (c == '\"') {
while (((c = Getc(str)) != EOF) && (c != '\"')) Putc(c,fn); while (((c = StringGetc(str)) != EOF) && (c != '\"')) StringPutc(c,fn);
} else if (c == '<') { } else if (c == '<') {
*sysfile = 1; *sysfile = 1;
while (((c = Getc(str)) != EOF) && (c != '>')) Putc(c,fn); while (((c = StringGetc(str)) != EOF) && (c != '>')) StringPutc(c,fn);
} else { } else {
Putc(c,fn); StringPutc(c,fn);
while (((c = Getc(str)) != EOF) && (!isspace(c))) Putc(c,fn); while (((c = StringGetc(str)) != EOF) && (!isspace(c))) StringPutc(c,fn);
if (isspace(c)) Ungetc(c,str); if (isspace(c)) StringUngetc(c,str);
} }
#if defined(_WIN32) || defined(MACSWIG) #if defined(_WIN32) || defined(MACSWIG)
/* accept Unix path separator on non-Unix systems */ /* accept Unix path separator on non-Unix systems */
@ -521,13 +529,13 @@ get_options(String *str) {
int c; int c;
skip_whitespace(str,0); skip_whitespace(str,0);
c = Getc(str); c = StringGetc(str);
if (c == '(') { if (c == '(') {
String *opt; String *opt;
int level = 1; int level = 1;
opt = NewString("("); opt = NewString("(");
while (((c = Getc(str)) != EOF)) { while (((c = StringGetc(str)) != EOF)) {
Putc(c,opt); StringPutc(c,opt);
if (c == ')') { if (c == ')') {
level--; level--;
if (!level) return opt; if (!level) return opt;
@ -537,7 +545,7 @@ get_options(String *str) {
Delete(opt); Delete(opt);
return 0; return 0;
} else { } else {
Ungetc(c,str); StringUngetc(c,str);
return 0; return 0;
} }
} }
@ -564,24 +572,24 @@ expand_macro(String_or_char *name, List *args)
if (!macro) return 0; if (!macro) return 0;
if (Getattr(macro,"*expanded*")) { if (Getattr(macro,"*expanded*")) {
ns = NewString(""); ns = NewString("");
Printf(ns,"%s",name); StringAppend(ns,name);
if (args) { if (args) {
if (Len(args)) if (Len(args))
Putc('(',ns); StringPutc('(',ns);
for (i = 0; i < Len(args); i++) { for (i = 0; i < Len(args); i++) {
Printf(ns,"%s",Getitem(args,i)); StringAppend(ns,Getitem(args,i));
if (i < (Len(args) -1)) Putc(',',ns); if (i < (Len(args) -1)) StringPutc(',',ns);
} }
if (i) if (i)
Putc(')',ns); StringPutc(')',ns);
} }
return ns; return ns;
} }
/* Get macro arguments and value */ /* Get macro arguments and value */
mvalue = Getattr(macro,"value"); mvalue = Getattr(macro,k_value);
assert(mvalue); assert(mvalue);
margs = Getattr(macro,"args"); margs = Getattr(macro,k_args);
if (args && Getattr(macro,"varargs")) { if (args && Getattr(macro,"varargs")) {
isvarargs = 1; isvarargs = 1;
@ -593,9 +601,9 @@ expand_macro(String_or_char *name, List *args)
vi = Len(margs)-1; vi = Len(margs)-1;
na = Len(args); na = Len(args);
for (i = vi; i < na; i++) { for (i = vi; i < na; i++) {
Append(vararg,Getitem(args,i)); StringAppend(vararg,Getitem(args,i));
if ((i+1) < na) { if ((i+1) < na) {
Append(vararg,","); StringAppend(vararg,",");
} }
} }
/* Remove arguments */ /* Remove arguments */
@ -652,12 +660,12 @@ expand_macro(String_or_char *name, List *args)
Clear(temp); Clear(temp);
Clear(tempa); Clear(tempa);
Printf(temp,"\002%s",aname); Printf(temp,"\002%s",aname);
Append(tempa,"\002\003"); StringAppend(tempa,"\002\003");
Replace(ns, temp, tempa, DOH_REPLACE_ID_END); Replace(ns, temp, tempa, DOH_REPLACE_ID_END);
Clear(temp); Clear(temp);
Clear(tempa); Clear(tempa);
Printf(temp,"%s\002",aname); Printf(temp,"%s\002",aname);
Append(tempa,"\003\002"); StringAppend(tempa,"\003\002");
Replace(ns,temp,tempa, DOH_REPLACE_ID_BEGIN); Replace(ns,temp,tempa, DOH_REPLACE_ID_BEGIN);
} }
@ -754,7 +762,7 @@ expand_macro(String_or_char *name, List *args)
#if 0 #if 0
Printf(f,"/*@%s,%d,%s@*/%s/*@@*/", Getfile(macro), Getline(macro), name, g); Printf(f,"/*@%s,%d,%s@*/%s/*@@*/", Getfile(macro), Getline(macro), name, g);
#else #else
Printf(f,"%s", g); Append(f,g);
#endif #endif
/* Printf(f," }\n"); */ /* Printf(f," }\n"); */
Delete(g); Delete(g);
@ -807,53 +815,53 @@ Preprocessor_replace(DOH *s)
id = NewString(""); id = NewString("");
/* Try to locate identifiers in s and replace them with macro replacements */ /* Try to locate identifiers in s and replace them with macro replacements */
while ((c = Getc(s)) != EOF) { while ((c = StringGetc(s)) != EOF) {
switch (state) { switch (state) {
case 0: case 0:
if (isidentifier(c) || (c == '%')) { if (isidentifier(c) || (c == '%')) {
Clear(id); Clear(id);
copy_location(s,id); copy_location(s,id);
Putc(c,id); StringPutc(c,id);
state = 1; state = 1;
} else if (c == '\"') { } else if (c == '\"') {
Putc(c,ns); StringPutc(c,ns);
skip_tochar(s,'\"',ns); skip_tochar(s,'\"',ns);
} else if (c == '\'') { } else if (c == '\'') {
Putc(c,ns); StringPutc(c,ns);
skip_tochar(s,'\'',ns); skip_tochar(s,'\'',ns);
} else if (c == '/') { } else if (c == '/') {
Putc(c,ns); StringPutc(c,ns);
state = 10; state = 10;
} else { } else {
Putc(c,ns); StringPutc(c,ns);
} }
break; break;
case 1: /* An identifier */ case 1: /* An identifier */
if (isidchar(c)) { if (isidchar(c)) {
Putc(c,id); StringPutc(c,id);
state = 1; state = 1;
} else { } else {
/* We found the end of a valid identifier */ /* We found the end of a valid identifier */
Ungetc(c,s); StringUngetc(c,s);
/* See if this is the special "defined" macro */ /* See if this is the special "defined" macro */
if (Cmp(id,"defined") == 0) { if (Cmp(id,"defined") == 0) {
DOH *args = 0; DOH *args = 0;
/* See whether or not a paranthesis has been used */ /* See whether or not a paranthesis has been used */
skip_whitespace(s,0); skip_whitespace(s,0);
c = Getc(s); c = StringGetc(s);
if (c == '(') { if (c == '(') {
Seek(s,-1,SEEK_CUR); Seek(s,-1,SEEK_CUR);
args = find_args(s); args = find_args(s);
} else if (isidchar(c)) { } else if (isidchar(c)) {
DOH *arg = NewString(""); DOH *arg = NewString("");
args = NewList(); args = NewList();
Putc(c,arg); StringPutc(c,arg);
while (((c = Getc(s)) != EOF)) { while (((c = StringGetc(s)) != EOF)) {
if (!isidchar(c)) { if (!isidchar(c)) {
Seek(s,-1,SEEK_CUR); Seek(s,-1,SEEK_CUR);
break; break;
} }
Putc(c,arg); StringPutc(c,arg);
} }
if (Len(arg)) Append(args,arg); if (Len(arg)) Append(args,arg);
Delete(arg); Delete(arg);
@ -862,7 +870,7 @@ Preprocessor_replace(DOH *s)
} }
if ((!args) || (!Len(args))) { if ((!args) || (!Len(args))) {
/* This is not a defined() macro. */ /* This is not a defined() macro. */
Printf(ns,"%s",id); StringAppend(ns,id);
state = 0; state = 0;
break; break;
} }
@ -872,8 +880,8 @@ Preprocessor_replace(DOH *s)
break; break;
} }
} }
if (i < Len(args)) Putc('0',ns); if (i < Len(args)) StringPutc('0',ns);
else Putc('1',ns); else StringPutc('1',ns);
Delete(args); Delete(args);
state = 0; state = 0;
break; break;
@ -896,7 +904,7 @@ Preprocessor_replace(DOH *s)
DOH *args = 0; DOH *args = 0;
DOH *e; DOH *e;
/* See if the macro expects arguments */ /* See if the macro expects arguments */
if (Getattr(m,"args")) { if (Getattr(m,k_args)) {
/* Yep. We need to go find the arguments and do a substitution */ /* Yep. We need to go find the arguments and do a substitution */
args = find_args(s); args = find_args(s);
if (!Len(args)) { if (!Len(args)) {
@ -908,12 +916,12 @@ Preprocessor_replace(DOH *s)
} }
e = expand_macro(id,args); e = expand_macro(id,args);
if (e) { if (e) {
Printf(ns,"%s",e); StringAppend(ns,e);
} }
Delete(e); Delete(e);
Delete(args); Delete(args);
} else { } else {
Printf(ns,"%s",id); StringAppend(ns,id);
} }
state = 0; state = 0;
} }
@ -922,22 +930,22 @@ Preprocessor_replace(DOH *s)
if (c == '/') state = 11; if (c == '/') state = 11;
else if (c == '*') state = 12; else if (c == '*') state = 12;
else { else {
Ungetc(c,s); StringUngetc(c,s);
state = 0; state = 0;
break; break;
} }
Putc(c,ns); StringPutc(c,ns);
break; break;
case 11: case 11:
Putc(c,ns); StringPutc(c,ns);
if (c == '\n') state = 0; if (c == '\n') state = 0;
break; break;
case 12: case 12:
Putc(c,ns); StringPutc(c,ns);
if (c == '*') state = 13; if (c == '*') state = 13;
break; break;
case 13: case 13:
Putc(c,ns); StringPutc(c,ns);
if (c == '/') state = 0; if (c == '/') state = 0;
else if (c != '*') state = 12; else if (c != '*') state = 12;
break; break;
@ -960,10 +968,10 @@ Preprocessor_replace(DOH *s)
Swig_error(Getfile(id),Getline(id),"Macro arguments expected.\n"); Swig_error(Getfile(id),Getline(id),"Macro arguments expected.\n");
} */ } */
e = expand_macro(id,0); e = expand_macro(id,0);
Printf(ns,"%s",e); StringAppend(ns,e);
Delete(e); Delete(e);
} else { } else {
Printf(ns,"%s",id); StringAppend(ns,id);
} }
} }
Delete(id); Delete(id);
@ -1008,11 +1016,11 @@ static void
addline(DOH *s1, DOH *s2, int allow) addline(DOH *s1, DOH *s2, int allow)
{ {
if (allow) { if (allow) {
Append(s1,s2); StringAppend(s1,s2);
} else { } else {
char *c = Char(s2); char *c = Char(s2);
while (*c) { while (*c) {
if (*c == '\n') Putc('\n',s1); if (*c == '\n') StringPutc('\n',s1);
c++; c++;
} }
} }
@ -1051,6 +1059,7 @@ pop_imported() {
} }
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Preprocessor_parse() * Preprocessor_parse()
* *
@ -1096,7 +1105,7 @@ Preprocessor_parse(String *s)
symbols = Getattr(cpp,"symbols"); symbols = Getattr(cpp,"symbols");
state = 0; state = 0;
while ((c = Getc(s)) != EOF) { while ((c = StringGetc(s)) != EOF) {
switch(state) { switch(state) {
case 0: /* Initial state - in first column */ case 0: /* Initial state - in first column */
/* Look for C preprocessor directives. Otherwise, go directly to state 1 */ /* Look for C preprocessor directives. Otherwise, go directly to state 1 */
@ -1106,11 +1115,11 @@ Preprocessor_parse(String *s)
cpp_lines = 1; cpp_lines = 1;
state = 40; state = 40;
} else if (isspace(c)) { } else if (isspace(c)) {
Putc(c,chunk); StringPutc(c,chunk);
skip_whitespace(s,chunk); skip_whitespace(s,chunk);
} else { } else {
state = 1; state = 1;
Ungetc(c,s); StringUngetc(c,s);
} }
break; break;
case 1: /* Non-preprocessor directive */ case 1: /* Non-preprocessor directive */
@ -1119,7 +1128,7 @@ Preprocessor_parse(String *s)
state = 100; state = 100;
break; break;
} }
Putc(c,chunk); StringPutc(c,chunk);
if (c == '\n') state = 0; if (c == '\n') state = 0;
else if (c == '\"') { else if (c == '\"') {
start_line = Getline(s); start_line = Getline(s);
@ -1137,34 +1146,34 @@ Preprocessor_parse(String *s)
case 30: /* Possibly a comment string of some sort */ case 30: /* Possibly a comment string of some sort */
start_line = Getline(s); start_line = Getline(s);
Putc(c,chunk); StringPutc(c,chunk);
if (c == '/') state = 31; if (c == '/') state = 31;
else if (c == '*') state = 32; else if (c == '*') state = 32;
else state = 1; else state = 1;
break; break;
case 31: case 31:
Putc(c,chunk); StringPutc(c,chunk);
if (c == '\n') state = 0; if (c == '\n') state = 0;
break; break;
case 32: case 32:
Putc(c,chunk); StringPutc(c,chunk);
if (c == '*') state = 33; if (c == '*') state = 33;
break; break;
case 33: case 33:
Putc(c,chunk); StringPutc(c,chunk);
if (c == '/') state = 1; if (c == '/') state = 1;
else if (c != '*') state = 32; else if (c != '*') state = 32;
break; break;
case 40: /* Start of a C preprocessor directive */ case 40: /* Start of a C preprocessor directive */
if (c == '\n') { if (c == '\n') {
Putc('\n',chunk); StringPutc('\n',chunk);
state = 0; state = 0;
} else if (isspace(c)) { } else if (isspace(c)) {
state = 40; state = 40;
} else { } else {
/* Got the start of a preprocessor directive */ /* Got the start of a preprocessor directive */
Ungetc(c,s); StringUngetc(c,s);
Clear(id); Clear(id);
copy_location(s,id); copy_location(s,id);
state = 41; state = 41;
@ -1176,25 +1185,25 @@ Preprocessor_parse(String *s)
Clear(value); Clear(value);
Clear(comment); Clear(comment);
if (c == '\n') { if (c == '\n') {
Ungetc(c,s); StringUngetc(c,s);
state = 50; state = 50;
} else { } else {
state = 42; state = 42;
if (!isspace(c)) { if (!isspace(c)) {
Ungetc(c,s); StringUngetc(c,s);
} }
} }
copy_location(s,value); copy_location(s,value);
break; break;
} }
Putc(c,id); StringPutc(c,id);
break; break;
case 42: /* Strip any leading space before preprocessor value */ case 42: /* Strip any leading space before preprocessor value */
if (isspace(c)) { if (isspace(c)) {
if (c == '\n') { if (c == '\n') {
Ungetc(c,s); StringUngetc(c,s);
state = 50; state = 50;
} }
break; break;
@ -1205,28 +1214,28 @@ Preprocessor_parse(String *s)
case 43: case 43:
/* Get preprocessor value */ /* Get preprocessor value */
if (c == '\n') { if (c == '\n') {
Ungetc(c,s); StringUngetc(c,s);
state = 50; state = 50;
} else if (c == '/') { } else if (c == '/') {
state = 45; state = 45;
} else if (c == '\"') { } else if (c == '\"') {
Putc(c,value); StringPutc(c,value);
skip_tochar(s,'\"',value); skip_tochar(s,'\"',value);
} else if (c == '\'') { } else if (c == '\'') {
Putc(c,value); StringPutc(c,value);
skip_tochar(s,'\'',value); skip_tochar(s,'\'',value);
} else { } else {
Putc(c,value); StringPutc(c,value);
if (c == '\\') state = 44; if (c == '\\') state = 44;
} }
break; break;
case 44: case 44:
if (c == '\n') { if (c == '\n') {
Putc(c,value); StringPutc(c,value);
cpp_lines++; cpp_lines++;
} else { } else {
Ungetc(c,s); StringUngetc(c,s);
} }
state = 43; state = 43;
break; break;
@ -1238,33 +1247,33 @@ Preprocessor_parse(String *s)
if (c == '/') state = 46; if (c == '/') state = 46;
else if (c == '*') state = 47; else if (c == '*') state = 47;
else if (c == '\n') { else if (c == '\n') {
Putc('/',value); StringPutc('/',value);
Ungetc(c,s); StringUngetc(c,s);
cpp_lines++; cpp_lines++;
state = 50; state = 50;
} else { } else {
Putc('/',value); StringPutc('/',value);
Putc(c,value); StringPutc(c,value);
state = 43; state = 43;
} }
break; break;
case 46: case 46:
if (c == '\n') { if (c == '\n') {
Ungetc(c,s); StringUngetc(c,s);
cpp_lines++; cpp_lines++;
state = 50; state = 50;
} else Putc(c,comment); } else StringPutc(c,comment);
break; break;
case 47: case 47:
if (c == '*') state = 48; if (c == '*') state = 48;
else Putc(c,comment); else StringPutc(c,comment);
break; break;
case 48: case 48:
if (c == '/') state = 43; if (c == '/') state = 43;
else if (c == '*') Putc(c,comment); else if (c == '*') StringPutc(c,comment);
else { else {
Putc('*',comment); StringPutc('*',comment);
Putc(c,comment); StringPutc(c,comment);
state = 47; state = 47;
} }
break; break;
@ -1276,8 +1285,8 @@ Preprocessor_parse(String *s)
DOH *m, *v, *v1; DOH *m, *v, *v1;
Seek(value,0,SEEK_SET); Seek(value,0,SEEK_SET);
m = Preprocessor_define(value,0); m = Preprocessor_define(value,0);
if ((m) && !(Getattr(m,"args"))) { if ((m) && !(Getattr(m,k_args))) {
v = Copy(Getattr(m,"value")); v = Copy(Getattr(m,k_value));
if (Len(v)) { if (Len(v)) {
Swig_error_silent(1); Swig_error_silent(1);
v1 = Preprocessor_replace(v); v1 = Preprocessor_replace(v);
@ -1285,9 +1294,9 @@ Preprocessor_parse(String *s)
/* Printf(stdout,"checking '%s'\n", v1); */ /* Printf(stdout,"checking '%s'\n", v1); */
if (!check_id(v1)) { if (!check_id(v1)) {
if (Len(comment) == 0) if (Len(comment) == 0)
Printf(ns,"%%constant %s = %s;\n", Getattr(m,"name"), v1); Printf(ns,"%%constant %s = %s;\n", Getattr(m,k_name), v1);
else else
Printf(ns,"%%constant %s = %s; /*%s*/\n", Getattr(m,"name"),v1,comment); Printf(ns,"%%constant %s = %s; /*%s*/\n", Getattr(m,k_name),v1,comment);
cpp_lines--; cpp_lines--;
} }
Delete(v1); Delete(v1);
@ -1451,7 +1460,7 @@ Preprocessor_parse(String *s)
Swig_error(Getfile(s),Getline(id),"cpp debug: level = %d, startlevel = %d\n", level, start_level); Swig_error(Getfile(s),Getline(id),"cpp debug: level = %d, startlevel = %d\n", level, start_level);
} }
for (i = 0; i < cpp_lines; i++) for (i = 0; i < cpp_lines; i++)
Putc('\n',ns); StringPutc('\n',ns);
state = 0; state = 0;
break; break;
@ -1462,35 +1471,35 @@ Preprocessor_parse(String *s)
start_line = Getline(s); start_line = Getline(s);
add_chunk(ns,chunk,allow); add_chunk(ns,chunk,allow);
copy_location(s,chunk); copy_location(s,chunk);
Putc('%',chunk); StringPutc('%',chunk);
Putc(c,chunk); StringPutc(c,chunk);
state = 105; state = 105;
} }
/* %#cpp - an embedded C preprocessor directive (we strip off the %) */ /* %#cpp - an embedded C preprocessor directive (we strip off the %) */
else if (c == '#') { else if (c == '#') {
add_chunk(ns,chunk,allow); add_chunk(ns,chunk,allow);
Putc(c,chunk); StringPutc(c,chunk);
state = 107; state = 107;
} else if (isidentifier(c)) { } else if (isidentifier(c)) {
Clear(decl); Clear(decl);
Putc('%',decl); StringPutc('%',decl);
Putc(c,decl); StringPutc(c,decl);
state = 110; state = 110;
} else { } else {
Putc('%',chunk); StringPutc('%',chunk);
Putc(c,chunk); StringPutc(c,chunk);
state = 1; state = 1;
} }
break; break;
case 105: case 105:
Putc(c,chunk); StringPutc(c,chunk);
if (c == '%') if (c == '%')
state = 106; state = 106;
break; break;
case 106: case 106:
Putc(c,chunk); StringPutc(c,chunk);
if (c == '}') { if (c == '}') {
state = 1; state = 1;
addline(ns,chunk,allow); addline(ns,chunk,allow);
@ -1502,7 +1511,7 @@ Preprocessor_parse(String *s)
break; break;
case 107: case 107:
Putc(c,chunk); StringPutc(c,chunk);
if (c == '\n') { if (c == '\n') {
addline(ns,chunk,allow); addline(ns,chunk,allow);
Clear(chunk); Clear(chunk);
@ -1513,13 +1522,13 @@ Preprocessor_parse(String *s)
break; break;
case 108: case 108:
Putc(c,chunk); StringPutc(c,chunk);
state = 107; state = 107;
break; break;
case 110: case 110:
if (!isidchar(c)) { if (!isidchar(c)) {
Ungetc(c,s); StringUngetc(c,s);
/* Look for common Swig directives */ /* Look for common Swig directives */
if ((Cmp(decl,"%include") == 0) || (Cmp(decl,"%import") == 0) || (Cmp(decl,"%extern") == 0)) { if ((Cmp(decl,"%include") == 0) || (Cmp(decl,"%import") == 0) || (Cmp(decl,"%extern") == 0)) {
/* Got some kind of file inclusion directive */ /* Got some kind of file inclusion directive */
@ -1529,7 +1538,7 @@ Preprocessor_parse(String *s)
if (Cmp(decl,"%extern") == 0) { if (Cmp(decl,"%extern") == 0) {
Swig_warning(WARN_DEPRECATED_EXTERN, Getfile(s),Getline(s),"%%extern is deprecated. Use %%import instead.\n"); Swig_warning(WARN_DEPRECATED_EXTERN, Getfile(s),Getline(s),"%%extern is deprecated. Use %%import instead.\n");
Clear(decl); Clear(decl);
Printf(decl,"%%import"); StringAppend(decl,"%%import");
} }
opt = get_options(s); opt = get_options(s);
fn = get_filename(s, &sysfile); fn = get_filename(s, &sysfile);
@ -1556,7 +1565,7 @@ Preprocessor_parse(String *s)
pop_imported(); pop_imported();
} }
addline(ns,s2,allow); addline(ns,s2,allow);
Printf(ns,"\n]"); StringAppend(ns,"\n]");
Delete(s2); Delete(s2);
Delete(s1); Delete(s1);
} }
@ -1575,30 +1584,30 @@ Preprocessor_parse(String *s)
copy_location(s,value); copy_location(s,value);
state = 150; state = 150;
} else { } else {
Printf(chunk,"%s", decl); StringAppend(chunk,decl);
state = 1; state = 1;
} }
} else { } else {
Putc(c,decl); StringPutc(c,decl);
} }
break; break;
/* Searching for the end of a %define statement */ /* Searching for the end of a %define statement */
case 150: case 150:
Putc(c,value); StringPutc(c,value);
if (c == '%') { if (c == '%') {
const char *ed = "enddef"; const char *ed = "enddef";
const char *df = "define"; const char *df = "define";
char statement[7]; char statement[7];
int i = 0; int i = 0;
for (i = 0; i < 6; ) { for (i = 0; i < 6; ) {
c = Getc(s); c = StringGetc(s);
Putc(c,value); StringPutc(c,value);
statement[i++] = c; statement[i++] = c;
if (strncmp(statement, ed, i) && strncmp(statement, df, i)) break; if (strncmp(statement, ed, i) && strncmp(statement, df, i)) break;
} }
c = Getc(s); c = StringGetc(s);
Ungetc(c,s); StringUngetc(c,s);
if ((i == 6) && (isspace(c))) { if ((i == 6) && (isspace(c))) {
if (strncmp(statement, df, i) == 0) { if (strncmp(statement, df, i) == 0) {
++dlevel; ++dlevel;
@ -1614,7 +1623,7 @@ Preprocessor_parse(String *s)
Seek(value,0,SEEK_SET); Seek(value,0,SEEK_SET);
Preprocessor_define(value,1); Preprocessor_define(value,1);
} }
Putc('\n',ns); StringPutc('\n',ns);
addline(ns,value,0); addline(ns,value,0);
state = 0; state = 0;
} }