~lsof/antcc

f02fb99340edd86848794dd9b85088998458a70d — lemon 11 days ago d0a11e2 trunk
frontend: #ifdef
3 files changed, 67 insertions(+), 6 deletions(-)

M lex.c
M test/pp.c
M test/pp.h
M lex.c => lex.c +56 -5
@@ 889,6 889,23 @@ ppif(struct lexer *lx, const struct span *span)
}

static void
ppifxdef(struct lexer *lx, bool defp, const struct span *span)
{
   struct token tk;

   lex0(lx, &tk);
   if (!isppident(tk)) {
      error(&tk.span, "macro name missing");
      ppskipline(lx);
      return;
   }
   assert(nppcnd < arraylength(ppcndstk) && "too many nested #if");
   ppcndstk[nppcnd].ifspan = span->sl;
   ppcndstk[nppcnd].cnd = (findmac(tk.s) == NULL) ^ defp ? PPCNDTRUE : PPCNDFALSE;
   ppcndstk[nppcnd++].elsep = 0;
}

static void
ppelif(struct lexer *lx, const struct span *span)
{
   vlong v;


@@ 911,6 928,36 @@ ppelif(struct lexer *lx, const struct span *span)
   case PPCNDTAKEN: assert(0);
   }
}
static void
ppelifxdef(struct lexer *lx, bool defp, const struct span *span)
{
   struct token tk;
   struct ppcnd *cnd;

   if (!nppcnd) {
      error(span, "#elif%sdef without matching #if", &"n"[defp]);
      ppif(lx, span);
      return;
   }
   cnd = &ppcndstk[nppcnd-1];
   if (cnd->elsep) {
      error(span, "#elif%sdef after #else", &"n"[defp]);
      return;
   }
   lex0(lx, &tk);
   if (!isppident(tk)) {
      error(&tk.span, "macro name missing");
      ppskipline(lx);
      return;
   }
   switch (cnd->cnd) {
   case PPCNDTRUE: cnd->cnd = PPCNDTAKEN; break;
   case PPCNDFALSE: cnd->cnd = (findmac(tk.s) == NULL) ^ defp ? PPCNDTRUE : PPCNDFALSE; break;
   case PPCNDTAKEN: assert(0);
   }
}



static void
ppendif(struct lexer *lx, const struct span *span)


@@ 1137,11 1184,15 @@ lex(struct lexer *lx, struct token *tk_)
            if (!skip) {
               switch (findppcmd(tk)) {
               case PPXXX: goto BadPP;
               case PPDEFINE: ppdefine(lx); break;
               case PPIF:     ppif(lx, &tk->span); break;
               case PPELIF:   ppelif(lx, &tk->span); break;
               case PPENDIF:  ppendif(lx, &tk->span); break;
               case PPELSE:   ppelse(lx, &tk->span); break;
               case PPDEFINE:   ppdefine(lx); break;
               case PPIF:       ppif(lx, &tk->span); break;
               case PPIFDEF:    ppifxdef(lx, 1, &tk->span); break;
               case PPIFNDEF:   ppifxdef(lx, 0, &tk->span); break;
               case PPELIF:     ppelif(lx, &tk->span); break;
               case PPELIFDEF:  ppelifxdef(lx, 1, &tk->span); break;
               case PPELIFNDEF: ppelifxdef(lx, 0, &tk->span); break;
               case PPENDIF:    ppendif(lx, &tk->span); break;
               case PPELSE:     ppelse(lx, &tk->span); break;
               case PPINCLUDE: ppinclude(lx, &tk->span); break;
               default: assert(0&&"nyi");
               }

M test/pp.c => test/pp.c +3 -1
@@ 1,8 1,10 @@

#include "pp.h"
#include "pp.h"

int
main(void)
{
   hi();
   return Foo;
   return Foo + Bar;
}

M test/pp.h => test/pp.h +8 -0
@@ 1,6 1,14 @@
#ifndef GUARD
#define GUARD

extern warnhere();
#define Foo 9
void hi() {
   extern int printf();
   printf("hi from header\n");
}

#elifndef Ww
#define Bar 7

#endif