PERFORCE change 103545 for review
Spencer Whitman
swhitman at FreeBSD.org
Thu Aug 10 01:56:22 UTC 2006
http://perforce.freebsd.org/chv.cgi?CH=103545
Change 103545 by swhitman at swhitman_joethecat on 2006/08/10 01:56:20
Implemented #undef. Fixed some bugs with #define.
Affected files ...
.. //depot/projects/soc2006/swhitman-K_Kernel_Meta-Language/k/cpp.c#16 edit
Differences ...
==== //depot/projects/soc2006/swhitman-K_Kernel_Meta-Language/k/cpp.c#16 (text+ko) ====
@@ -48,7 +48,7 @@
/* XXX This should be optimized, but works for now */
struct macro_arg {
const char *name; /* Argument name */
- int length /* Length of the arg name */
+ int length; /* Length of the arg name */
int *offsets; /* Offset list */
TAILQ_ENTRY(macro_arg) list; /* Entry into the argument list */
};
@@ -65,13 +65,26 @@
TAILQ_ENTRY(define) list; /* Link to list of macros */
};
-#define FREE_LIST(HEAD,TYPE,LIST) do { \
- (TYPE) * tmpvar; \
- TAILQ_FOREACH(tmpvar, (HEAD), LIST) { \
- TAILQ_REMOVE(tmpvar, (HEAD), LIST); \
- free(tmpvar); \
- } \
-}while(0)
+/* XXX Free const char * name value? */
+#define FREE_ARG_LIST(DEF) \
+ do { \
+ struct macro_arg * tmpvar = NULL; \
+ for(; !TAILQ_EMPTY(&(DEF)->args); tmpvar = TAILQ_FIRST(&(DEF)->args)) { \
+ TAILQ_REMOVE(&(DEF)->args, tmpvar, list); \
+ free(tmpvar->offsets); \
+ free(tmpvar); \
+ } \
+ } while(0)
+
+/* XXX Free const char * name value? */
+/* XXX Free const char * value value? */
+#define FREE_DEF(DEF) \
+ do { \
+ TAILQ_REMOVE(&define,(DEF),list); \
+ FREE_ARG_LIST((DEF)); \
+ free((DEF)); \
+ }while(0)
+
static TAILQ_HEAD(,iarg) iarg = TAILQ_HEAD_INITIALIZER(iarg);
static TAILQ_HEAD(,define) define = TAILQ_HEAD_INITIALIZER(define);
@@ -180,6 +193,15 @@
expand_macro(struct define * mac, struct arg_ind * head) {
}
+
+
+static const char *
+expand_string(char * word) {
+ /* Search the macro list to match word. If word matches a macro, return a
+ * pointer to the expanded macro, otherwise return word
+ */
+
+}
#endif
/* -------------------------------------------------------------------*/
@@ -193,8 +215,13 @@
if (s == e)
return;
- /* Expand any macro expansions before passing this to the lexer */
+ /* Expand any macros before passing String(s,e) to the lexer */
+ /* Search through and grab each word to test if it is a macro */
+ for(;;) {
+ break;
+ }
+
D(0x10, "expand <%V>\n", String(s, e));
Lexer(cfs->h->tokens, s, e);
}
@@ -270,11 +297,12 @@
cppfile(cfs->h, r);
}
+
static void
-calculate_offsets(struct macro_arg *arg __unused, struct define * mac __unused)
+calculate_offsets(struct define * mac)
{
-
- /* XXX Use strstr to find substrings of arg in mac */
+ assert(mac->value != NULL);
+ assert(mac->length > 0);
}
@@ -287,6 +315,7 @@
new_arg = calloc(sizeof(*new_arg),1);
assert(new_arg != NULL);
+ /* XXX Make sure the name conforms to ISO C std macro names (start with alpha char?) */
new_arg->name = String(b,e);
printf("adding macro arg: %V\n",new_arg->name);
@@ -304,8 +333,6 @@
}
new_arg->length = strlen(new_arg->name);
-
- calculate_offsets(new_arg,mac);
printf("added argument named: <%V> to macro named <%V>\n",new_arg->name,
mac->name);
@@ -372,6 +399,7 @@
case OBJ_MAC_TYPE:
mac->value = String(name_e,e);
+ mac->length = strlen(mac->value);
break;
case FUNC_MAC_TYPE:
@@ -394,30 +422,35 @@
}
}
- /* Macro was ill-formed. Free everything and exit with error */
+ /* Macro was ill-formed. */
if(*p != ')') {
- FREE_LIST(&mac->args, struct macro_args, list);
- free(mac);
+ FREE_DEF(mac);
errx(1, "Function macro has no ending \')\'");
}
mac->value = String(p,e);
+ mac->length = strlen(mac->value);
+ calculate_offsets(mac);
}
break;
default:
break;
}
- mac->length = strlen(mac->value);
-
/* Add this macro to the defined list */
TAILQ_FOREACH(tmp, &define, list) {
if(tmp == NULL) {
- TAILQ_INSERT_TAIL(&define, mac, list);
break;
}
/* Warn if redefining a previous definition */
- if(tmp->name == mac->name)
+ if(tmp->name == mac->name) {
cpp_warning(cfs,NULL,b,e);
+ break;
+ }
+ }
+
+ if(tmp == NULL) {
+ printf("adding macro named %s to the list\n",mac->name);
+ TAILQ_INSERT_TAIL(&define, mac, list);
}
}
@@ -426,7 +459,17 @@
static void
cpp_remove_define(const char *name)
{
- printf("Removing macro %s\n",name);
+ struct define * tmp;
+
+ TAILQ_FOREACH(tmp, &define, list) {
+ if(tmp->name == name) {
+ printf("Removing macro %s\n",name);
+ FREE_DEF(tmp);
+
+ /* XXX Could there still be more #defines with the same name in the list? */
+ break;
+ }
+ }
}
/*
@@ -443,9 +486,6 @@
static void
cpp_define(CPP_ARGS)
{
- /* struct cppfilestate *cfs __unused, const char *h __unused,
- const char *b __unused, const char *e __unused */
-
struct ref *r;
r = NewRef(cfs->h);
@@ -453,7 +493,7 @@
r->ref.b = h;
r->ref.e = e;
r->ref.r = cfs->r;
-
+ /* XXX Finish the ref cell stuff */
printf("#define of %V\n",String(b,e));
/* The first token is the macro name */
@@ -464,11 +504,9 @@
static void
cpp_undef(CPP_ARGS)
{
- /* struct cppfilestate *cfs __unused, const char *h __unused,
- const char *b __unused, const char *e __unused */
printf("#undef of %V\n",String(b,e));
- cpp_remove_define(String(b,e));
+ cpp_remove_define(String(skipspace(cfs,b,e),e));
}
static void
More information about the p4-projects
mailing list