LCOV - code coverage report
Current view: top level - rsc/source/rscpp - cpp5.c (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 111 313 35.5 %
Date: 2015-06-13 12:38:46 Functions: 4 7 57.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <stdio.h>
      21             : #include <ctype.h>
      22             : #include "cppdef.h"
      23             : #include "cpp.h"
      24             : 
      25             : /*
      26             :  * Evaluate an #if expression.
      27             :  */
      28             : 
      29             : static char* opname[] = {       /* For debug and error messages */
      30             : "end of expression", "val", "id",
      31             :   "+",   "-",  "*",  "/",  "%",
      32             :   "<<", ">>",  "&",  "|",  "^",
      33             :   "==", "!=",  "<", "<=", ">=",  ">",
      34             :   "&&", "||",  "?",  ":",  ",",
      35             :   "unary +", "unary -", "~", "!",  "(",  ")", "(none)",
      36             : };
      37             : 
      38             : /*
      39             :  * opdope[] has the operator precedence:
      40             :  *     Bits
      41             :  *    7 Unused (so the value is always positive)
      42             :  *  6-2 Precedence (000x .. 017x)
      43             :  *  1-0 Binary op. flags:
      44             :  *      01  The binop flag should be set/cleared when this op is seen.
      45             :  *      10  The new value of the binop flag.
      46             :  * Note:  Expected, New binop
      47             :  * constant 0   1   Binop, end, or ) should follow constants
      48             :  * End of line  1   0   End may not be preceded by an operator
      49             :  * binary   1   0   Binary op follows a value, value follows.
      50             :  * unary    0   0   Unary op doesn't follow a value, value follows
      51             :  *   (      0   0   Doesn't follow value, value or unop follows
      52             :  *   )      1   1   Follows value.  Op follows.
      53             :  */
      54             : 
      55             : static char opdope[OP_MAX] = {
      56             :   0001,                 /* End of expression        */
      57             :   0002,                 /* Digit            */
      58             :   0000,                 /* Letter (identifier)      */
      59             :   0141, 0141, 0151, 0151, 0151,     /* ADD, SUB, MUL, DIV, MOD  */
      60             :   0131, 0131, 0101, 0071, 0071,     /* ASL, ASR, AND,  OR, XOR  */
      61             :   0111, 0111, 0121, 0121, 0121, 0121,   /*  EQ,  NE,  LT,  LE,  GE,  GT */
      62             :   0061, 0051, 0041, 0041, 0031,     /* ANA, ORO, QUE, COL, CMA  */
      63             : /*
      64             :  * Unary op's follow
      65             :  */
      66             :   0160, 0160, 0160, 0160,       /* NEG, PLU, COM, NOT       */
      67             :   0170, 0013, 0023,         /* LPA, RPA, END        */
      68             : };
      69             : /*
      70             :  * OP_QUE and OP_RPA have alternate precedences:
      71             :  */
      72             : #define OP_RPA_PREC 0013
      73             : #define OP_QUE_PREC 0034
      74             : 
      75             : /*
      76             :  * S_ANDOR and S_QUEST signal "short-circuit" boolean evaluation, so that
      77             :  *  #if FOO != 0 && 10 / FOO ...
      78             :  * doesn't generate an error message.  They are stored in optab.skip.
      79             :  */
      80             : #define S_ANDOR     2
      81             : #define S_QUEST     1
      82             : 
      83             : typedef struct optab
      84             : {
      85             :     char    op;         /* Operator         */
      86             :     char    prec;           /* Its precedence       */
      87             :     char    skip;           /* Short-circuit: TRUE to skip  */
      88             : } OPTAB;
      89             : static int  evalue;         /* Current value from evallex() */
      90             : 
      91             : #define isbinary(op)    (op >= FIRST_BINOP && op <= LAST_BINOP)
      92             : 
      93             : /*
      94             :  * The following definitions are used to specify basic variable sizes.
      95             :  */
      96             : 
      97             : #ifndef S_CHAR
      98             : #define S_CHAR      (sizeof (char))
      99             : #endif
     100             : #ifndef S_SINT
     101             : #define S_SINT      (sizeof (short int))
     102             : #endif
     103             : #ifndef S_INT
     104             : #define S_INT       (sizeof (int))
     105             : #endif
     106             : #ifndef S_LINT
     107             : #define S_LINT      (sizeof (long int))
     108             : #endif
     109             : #ifndef S_FLOAT
     110             : #define S_FLOAT     (sizeof (float))
     111             : #endif
     112             : #ifndef S_DOUBLE
     113             : #define S_DOUBLE    (sizeof (double))
     114             : #endif
     115             : #ifndef S_PCHAR
     116             : #define S_PCHAR     (sizeof (char *))
     117             : #endif
     118             : #ifndef S_PSINT
     119             : #define S_PSINT     (sizeof (short int *))
     120             : #endif
     121             : #ifndef S_PINT
     122             : #define S_PINT      (sizeof (int *))
     123             : #endif
     124             : #ifndef S_PLINT
     125             : #define S_PLINT     (sizeof (long int *))
     126             : #endif
     127             : #ifndef S_PFLOAT
     128             : #define S_PFLOAT    (sizeof (float *))
     129             : #endif
     130             : #ifndef S_PDOUBLE
     131             : #define S_PDOUBLE   (sizeof (double *))
     132             : #endif
     133             : #ifndef S_PFPTR
     134             : #define S_PFPTR     (sizeof (int (*)(void)))
     135             : #endif
     136             : 
     137             : typedef struct types
     138             : {
     139             :     short   type;           /* This is the bit if       */
     140             :     char    *name;          /* this is the token word   */
     141             : } TYPES;
     142             : 
     143             : static TYPES basic_types[] = {
     144             :     { T_CHAR,   "char",     },
     145             :     { T_INT,    "int",      },
     146             :     { T_FLOAT,  "float",    },
     147             :     { T_DOUBLE, "double",   },
     148             :     { T_SHORT,  "short",    },
     149             :     { T_LONG,   "long",     },
     150             :     { T_SIGNED, "signed",   },
     151             :     { T_UNSIGNED,   "unsigned", },
     152             :     { 0,        NULL,       },  /* Signal end       */
     153             : };
     154             : 
     155             : /*
     156             :  * Test_table[] is used to test for illegal combinations.
     157             :  */
     158             : static short test_table[] = {
     159             :     T_FLOAT | T_DOUBLE | T_LONG | T_SHORT,
     160             :     T_FLOAT | T_DOUBLE | T_CHAR | T_INT,
     161             :     T_FLOAT | T_DOUBLE | T_SIGNED | T_UNSIGNED,
     162             :     T_LONG  | T_SHORT  | T_CHAR,
     163             :     0                       /* end marker   */
     164             : };
     165             : 
     166             : /*
     167             :  * The order of this table is important -- it is also referenced by
     168             :  * the command line processor to allow run-time overriding of the
     169             :  * built-in size values.  The order must not be changed:
     170             :  *  char, short, int, long, float, double (func pointer)
     171             :  */
     172             : SIZES size_table[] = {
     173             :     { T_CHAR,   S_CHAR,     S_PCHAR     },  /* char     */
     174             :     { T_SHORT,  S_SINT,     S_PSINT     },  /* short int    */
     175             :     { T_INT,    S_INT,      S_PINT      },  /* int      */
     176             :     { T_LONG,   S_LINT,     S_PLINT     },  /* long     */
     177             :     { T_FLOAT,  S_FLOAT,    S_PFLOAT    },  /* float    */
     178             :     { T_DOUBLE, S_DOUBLE,   S_PDOUBLE   },  /* double   */
     179             :     { T_FPTR,   0,      S_PFPTR     },  /* int (*())    */
     180             :     { 0,    0,      0       },  /* End of table */
     181             : };
     182             : 
     183             : /*
     184             :  * Evaluate an expression.  Straight-forward operator precedence.
     185             :  * This is called from control() on encountering an #if statement.
     186             :  * It calls the following routines:
     187             :  * evallex  Lexical analyser -- returns the type and value of
     188             :  *      the next input token.
     189             :  * evaleval Evaluate the current operator, given the values on
     190             :  *      the value stack.  Returns a pointer to the (new)
     191             :  *      value stack.
     192             :  * For compatibility with older cpp's, this return returns 1 (TRUE)
     193             :  * if a syntax error is detected.
     194             :  */
     195         466 : int eval()
     196             : {
     197             :     int op;         /* Current operator     */
     198             :     int* valp;      /* -> value vector      */
     199             :     OPTAB* opp;     /* Operator stack       */
     200             :     int prec;       /* Op precedence        */
     201             :     int binop;      /* Set if binary op. needed */
     202             :     int op1;        /* Operand from stack       */
     203             :     int skip;       /* For short-circuit testing    */
     204             :     int value[NEXP]; /* Value stack          */
     205             :     OPTAB opstack[NEXP]; /* Operand stack        */
     206             : 
     207         466 :     valp = value;
     208         466 :     opp = opstack;
     209         466 :     opp->op = OP_END;       /* Mark bottom of stack     */
     210         466 :     opp->prec = opdope[OP_END]; /* And its precedence       */
     211         466 :     opp->skip = 0;          /* Not skipping now     */
     212         466 :     binop = 0;
     213             : 
     214             : again:
     215             : #ifdef  DEBUG_EVAL
     216             :     fprintf( pCppOut, "In #if at again: skip = %d, binop = %d, line is: %s",
     217             :         opp->skip, binop, infile->bptr);
     218             : #endif
     219        9560 :     if ((op = evallex(opp->skip)) == OP_SUB && binop == 0)
     220           0 :         op = OP_NEG;            /* Unary minus      */
     221        9560 :     else if (op == OP_ADD && binop == 0)
     222           0 :         op = OP_PLU;            /* Unary plus       */
     223        9560 :     else if (op == OP_FAIL)
     224           0 :         return 1;             /* Error in evallex */
     225             : #ifdef  DEBUG_EVAL
     226             :     fprintf( pCppOut, "op = %s, opdope = %03o, binop = %d, skip = %d\n",
     227             :         opname[op], opdope[op], binop, opp->skip);
     228             : #endif
     229        9560 :     if (op == DIG)              /* Value?       */
     230             :     {
     231        3075 :         if (binop != 0)
     232             :         {
     233           0 :             cerror("misplaced constant in #if", NULLST);
     234           0 :             return 1;
     235             :         }
     236        3075 :         else if (valp >= &value[NEXP-1])
     237             :         {
     238           0 :             cerror("#if value stack overflow", NULLST);
     239           0 :             return 1;
     240             :         }
     241             :         else
     242             :         {
     243             : #ifdef  DEBUG_EVAL
     244             :             fprintf( pCppOut, "pushing %d onto value stack[%d]\n",
     245             :                      evalue, valp - value);
     246             : #endif
     247        3075 :             *valp++ = evalue;
     248        3075 :             binop = 1;
     249             :         }
     250        3075 :         goto again;
     251             :     }
     252        6485 :     else if (op > OP_END)
     253             :     {
     254           0 :         cerror("Illegal #if line", NULLST);
     255           0 :         return 1;
     256             :     }
     257        6485 :     prec = opdope[op];
     258        6485 :     if (binop != (prec & 1))
     259             :     {
     260           0 :         cerror("Operator %s in incorrect context", opname[op]);
     261           0 :         return 1;
     262             :     }
     263        6485 :     binop = (prec & 2) >> 1;
     264             :     for (;;)
     265             :     {
     266             : #ifdef  DEBUG_EVAL
     267             :         fprintf( pCppOut, "op %s, prec %d., stacked op %s, prec %d, skip %d\n",
     268             :         opname[op], prec, opname[opp->op], opp->prec, opp->skip);
     269             : #endif
     270        9094 :         if (prec > opp->prec)
     271             :         {
     272        4314 :             if (op == OP_LPA)
     273        1705 :                 prec = OP_RPA_PREC;
     274        2609 :             else if (op == OP_QUE)
     275           0 :                 prec = OP_QUE_PREC;
     276        4314 :             op1 = opp->skip;        /* Save skip for test   */
     277             :             /*
     278             :              * Push operator onto op. stack.
     279             :              */
     280        4314 :             opp++;
     281        4314 :             if (opp >= &opstack[NEXP])
     282             :             {
     283           0 :                 cerror("expression stack overflow at op \"%s\"",
     284             :                        opname[op]);
     285           0 :                 return 1;
     286             :             }
     287        4314 :             opp->op = (char)op;
     288        4314 :             opp->prec = (char)prec;
     289             :             /*
     290             :              * Do the short-circuit stuff here.  Short-circuiting
     291             :              * stops automagically when operators are evaluated.
     292             :              */
     293        4314 :             if ((op == OP_ANA && valp[-1] == 0) ||
     294           0 :                 (op == OP_ORO && valp[-1] != 0))
     295             :             {
     296           0 :                 opp->skip = S_ANDOR;    /* And/or skip starts   */
     297             :             }
     298        4314 :             else if (op == OP_QUE)      /* Start of ?: operator */
     299           0 :                 opp->skip = (char)((op1 & S_ANDOR) | ((valp[-1] == 0) ? S_QUEST : 0));
     300        4314 :             else if (op == OP_COL)      /* : inverts S_QUEST    */
     301             :             {
     302           0 :                 opp->skip = (char)((op1 & S_ANDOR)
     303           0 :                                    | (((op1 & S_QUEST) != 0) ? 0 : S_QUEST));
     304             :             }
     305             :             else                 /* Other ops leave  */
     306             :             {
     307        4314 :                 opp->skip = (char)op1;      /*  skipping unchanged. */
     308             :             }
     309             : #ifdef  DEBUG_EVAL
     310             :             fprintf( pCppOut, "stacking %s, valp[-1] == %d at %s",
     311             :                      opname[op], valp[-1], infile->bptr);
     312             :             dumpstack(opstack, opp, value, valp);
     313             : #endif
     314        4314 :             goto again;
     315             :         }
     316             :         /*
     317             :          * Pop operator from op. stack and evaluate it.
     318             :          * End of stack and '(' are specials.
     319             :          */
     320        4780 :         skip = opp->skip;           /* Remember skip value  */
     321        4780 :         switch ((op1 = opp->op))    /* Look at stacked op   */
     322             :         {
     323             :         case OP_END:                /* Stack end marker */
     324         466 :             if (op == OP_EOE)
     325         466 :                 return valp[-1];    /* Finished ok.     */
     326           0 :             goto again;             /* Read another op. */
     327             : 
     328             :         case OP_LPA:                /* ( on stack       */
     329        1705 :             if (op != OP_RPA)       /* Matches ) on input   */
     330             :             {
     331           0 :                 cerror("unbalanced paren's, op is \"%s\"", opname[op]);
     332           0 :                 return 1;
     333             :             }
     334        1705 :             opp--;                  /* Unstack it       */
     335             :             /* goto again;          -- Fall through     */
     336             : 
     337             :         case OP_QUE:
     338        1705 :             goto again;             /* Evaluate true expr.  */
     339             : 
     340             :         case OP_COL:                /* : on stack.      */
     341           0 :             opp--;                  /* Unstack :        */
     342           0 :             if (opp->op != OP_QUE)  /* Matches ? on stack?  */
     343             :             {
     344           0 :                 cerror("Misplaced '?' or ':', previous operator is %s",
     345           0 :                        opname[(int)opp->op]);
     346           0 :                 return 1;
     347             :             }
     348             :         /*
     349             :          * Evaluate op1.
     350             :          */
     351             :         default:                /* Others:      */
     352        2609 :             opp--;              /* Unstack the operator */
     353             : #ifdef  DEBUG_EVAL
     354             :             fprintf( pCppOut, "Stack before evaluation of %s\n", opname[op1]);
     355             :             dumpstack(opstack, opp, value, valp);
     356             : #endif
     357        2609 :             valp = evaleval(valp, op1, skip);
     358             : #ifdef  DEBUG_EVAL
     359             :             fprintf( pCppOut, "Stack after evaluation\n");
     360             :             dumpstack(opstack, opp, value, valp);
     361             : #endif
     362             :         }                   /* op1 switch end   */
     363        2609 :     }                   /* Stack unwind loop    */
     364             : }
     365             : 
     366             : /*
     367             :  * Return next eval operator or value.  Called from eval().  It
     368             :  * calls a special-purpose routines for 'char' strings and
     369             :  * numeric values:
     370             :  * evalchar called to evaluate 'x'
     371             :  * evalnum  called to evaluate numbers.
     372             :  */
     373        9560 : FILE_LOCAL int evallex(int skip)
     374             : {
     375             :     int c;
     376             :     int c1;
     377             :     int t;
     378             : 
     379             : again:
     380             :     do                              /* Collect the token    */
     381             :     {
     382        9560 :         c = skipws();
     383        9560 :         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
     384             :         {
     385         466 :             unget();
     386         466 :             return OP_EOE;          /* End of expression    */
     387             :         }
     388             :     }
     389        9094 :     while ((t = type[c]) == LET && catenate());
     390        9094 :     if (t == INV)                   /* Total nonsense   */
     391             :     {
     392           0 :         if (!skip)
     393             :         {
     394           0 :             if (isascii(c) && isprint(c))
     395           0 :                 cierror("illegal character '%c' in #if", c);
     396             :             else
     397           0 :                 cierror("illegal character (%d decimal) in #if", c);
     398             :         }
     399           0 :         return OP_FAIL;
     400             :     }
     401        9094 :     else if (t == QUO)              /* ' or "       */
     402             :     {
     403           0 :         if (c == '\'')              /* Character constant   */
     404             :         {
     405           0 :             evalue = evalchar(skip);    /* Somewhat messy   */
     406             : #ifdef  DEBUG_EVAL
     407             :             fprintf( pCppOut, "evalchar returns %d.\n", evalue);
     408             : #endif
     409           0 :             return DIG;           /* Return a value   */
     410             :         }
     411           0 :         cerror("Can't use a string in an #if", NULLST);
     412           0 :         return OP_FAIL;
     413             :     }
     414        9094 :     else if (t == LET)              /* ID must be a macro   */
     415             :     {
     416           1 :         if (streq(token, "defined"))  /* Or defined name  */
     417             :         {
     418           1 :             c1 = c = skipws();
     419           1 :             if (c == '(')           /* Allow defined(name)  */
     420           1 :                 c = skipws();
     421           1 :             if (type[c] == LET)
     422             :             {
     423           1 :                 evalue = (lookid(c) != NULL);
     424           2 :                 if (c1 != '(' ||      /* Need to balance  */
     425           1 :                     skipws() == ')')    /* Did we balance?  */
     426             :                 {
     427           1 :                     return DIG;       /* Parsed ok        */
     428             :                 }
     429             :             }
     430           0 :             cerror("Bad #if ... defined() syntax", NULLST);
     431           0 :             return OP_FAIL;
     432             :         }
     433           0 :         else if (streq(token, "sizeof"))    /* New sizeof hackery   */
     434           0 :             return dosizeof();        /* Gets own routine */
     435             :         /*
     436             :          * The Draft ANSI C Standard says that an undefined symbol
     437             :          * in an #if has the value zero.  We are a bit pickier,
     438             :          * warning except where the programmer was careful to write
     439             :          *      #if defined(foo) ? foo : 0
     440             :          */
     441             : #ifdef STRICT_UNDEF
     442             :         if (!skip)
     443             :             cwarn("undefined symbol \"%s\" in #if, 0 used", token);
     444             : #endif
     445           0 :         evalue = 0;
     446           0 :         return DIG;
     447             :     }
     448        9093 :     else if (t == DIG)              /* Numbers are harder   */
     449             :     {
     450        3074 :         evalue = evalnum(c);
     451             : #ifdef  DEBUG_EVAL
     452             :         fprintf( pCppOut, "evalnum returns %d.\n", evalue);
     453             : #endif
     454             :     }
     455        6019 :     else if (strchr("!=<>&|\\", c) != NULL)
     456             :     {
     457             :         /*
     458             :          * Process a possible multi-byte lexeme.
     459             :          */
     460         465 :         c1 = cget();                /* Peek at next char    */
     461         465 :         switch (c)
     462             :         {
     463             :         case '!':
     464           0 :             if (c1 == '=')
     465           0 :                 return OP_NE;
     466           0 :             break;
     467             : 
     468             :         case '=':
     469          96 :             if (c1 != '=')          /* Can't say a=b in #if */
     470             :             {
     471           0 :                 unget();
     472           0 :                 cerror("= not allowed in #if", NULLST);
     473           0 :                 return OP_FAIL;
     474             :             }
     475          96 :             return OP_EQ;
     476             : 
     477             :         case '>':
     478             :         case '<':
     479         369 :             if (c1 == c)
     480           0 :                 return ((c == '<') ? OP_ASL : OP_ASR);
     481         369 :             else if (c1 == '=')
     482           2 :                 return ((c == '<') ? OP_LE  : OP_GE);
     483         367 :             break;
     484             : 
     485             :         case '|':
     486             :         case '&':
     487           0 :             if (c1 == c)
     488           0 :                 return ((c == '|') ? OP_ORO : OP_ANA);
     489           0 :             break;
     490             : 
     491             :         case '\\':
     492           0 :             if (c1 == '\n')         /* Multi-line if    */
     493           0 :                 goto again;
     494           0 :             cerror("Unexpected \\ in #if", NULLST);
     495           0 :             return OP_FAIL;
     496             :         }
     497         367 :         unget();
     498             :     }
     499        8995 :     return t;
     500             : }
     501             : 
     502             : /*
     503             :  * Process the sizeof (basic type) operation in an #if string.
     504             :  * Sets evalue to the size and returns
     505             :  *  DIG     success
     506             :  *  OP_FAIL     bad parse or something.
     507             :  */
     508           0 : FILE_LOCAL int dosizeof()
     509             : {
     510             :     int c;
     511             :     TYPES* tp;
     512             :     SIZES* sizp;
     513             :     short* testp;
     514             :     short typecode;
     515             : 
     516           0 :     if ((c = skipws()) != '(')
     517           0 :         goto nogood;
     518             :     /*
     519             :      * Scan off the tokens.
     520             :      */
     521           0 :     typecode = 0;
     522           0 :     while (0 != (c = skipws()))
     523             :     {
     524           0 :         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
     525             :             goto nogood;            /* End of line is a bug */
     526           0 :         else if (c == '(')          /* thing (*)() func ptr */
     527             :         {
     528           0 :             if (skipws() == '*' && skipws() == ')')
     529             :             {                       /* We found (*)     */
     530           0 :                 if (skipws() != '(')    /* Let () be optional   */
     531           0 :                     unget();
     532           0 :                 else if (skipws() != ')')
     533           0 :                     goto nogood;
     534           0 :                 typecode |= T_FPTR; /* Function pointer */
     535             :             }
     536             :             else                    /* Junk is a bug    */
     537             :                 goto nogood;
     538             :         }
     539           0 :         else if (type[c] != LET)    /* Exit if not a type   */
     540           0 :             break;
     541           0 :         else if (!catenate())       /* Maybe combine tokens */
     542             :         {
     543             :             /*
     544             :              * Look for this unexpandable token in basic_types.
     545             :              * The code accepts "int long" as well as "long int"
     546             :              * which is a minor bug as bugs go (and one shared with
     547             :              * a lot of C compilers).
     548             :              */
     549           0 :             for (tp = basic_types; tp->name != NULLST; tp++)
     550             :             {
     551           0 :                 if (streq(token, tp->name))
     552           0 :                     break;
     553             :             }
     554           0 :             if (tp->name == NULLST)
     555             :             {
     556           0 :                 cerror("#if sizeof, unknown type \"%s\"", token);
     557           0 :                 return OP_FAIL;
     558             :             }
     559           0 :             typecode |= tp->type;   /* Or in the type bit   */
     560             :         }
     561             :     }
     562             :     /*
     563             :      * We are at the end of the type scan.  Chew off '*' if necessary.
     564             :      */
     565           0 :     if (c == '*')
     566             :     {
     567           0 :         typecode |= T_PTR;
     568           0 :         c = skipws();
     569             :     }
     570           0 :     if (c == ')')                   /* Last syntax check    */
     571             :     {
     572           0 :         for (testp = test_table; *testp != 0; testp++)
     573             :         {
     574           0 :             if (!bittest(typecode & *testp))
     575             :             {
     576           0 :                 cerror("#if ... sizeof: illegal type combination", NULLST);
     577           0 :                 return OP_FAIL;
     578             :             }
     579             :         }
     580             :         /*
     581             :          * We assume that all function pointers are the same size:
     582             :          *      sizeof (int (*)()) == sizeof (float (*)())
     583             :          * We assume that signed and unsigned don't change the size:
     584             :          *      sizeof (signed int) == (sizeof unsigned int)
     585             :          */
     586           0 :         if ((typecode & T_FPTR) != 0) /* Function pointer */
     587           0 :             typecode = T_FPTR | T_PTR;
     588             :         else                        /* Var or var * datum   */
     589             :         {
     590           0 :             typecode &= ~(T_SIGNED | T_UNSIGNED);
     591           0 :             if ((typecode & (T_SHORT | T_LONG)) != 0)
     592           0 :                 typecode &= ~T_INT;
     593             :         }
     594           0 :         if ((typecode & ~T_PTR) == 0)
     595             :         {
     596           0 :             cerror("#if sizeof() error, no type specified", NULLST);
     597           0 :             return OP_FAIL;
     598             :         }
     599             :         /*
     600             :          * Exactly one bit (and possibly T_PTR) may be set.
     601             :          */
     602           0 :         for (sizp = size_table; sizp->bits != 0; sizp++)
     603             :         {
     604           0 :             if ((typecode & ~T_PTR) == sizp->bits)
     605             :             {
     606           0 :                 evalue = ((typecode & T_PTR) != 0)
     607           0 :                     ? sizp->psize : sizp->size;
     608           0 :                 return DIG;
     609             :             }
     610             :         }                   /* We shouldn't fail    */
     611           0 :         cierror("#if ... sizeof: bug, unknown type code 0x%x", typecode);
     612           0 :         return OP_FAIL;
     613             :     }
     614             : 
     615             :   nogood:
     616           0 :     unget();
     617           0 :     cerror("#if ... sizeof() syntax error", NULLST);
     618           0 :     return OP_FAIL;
     619             : }
     620             : 
     621             : /*
     622             :  * TRUE if value is zero or exactly one bit is set in value.
     623             :  */
     624           0 : FILE_LOCAL int bittest(int value)
     625             : {
     626             : /* whoaa!! really worried about non 2's complement machines...
     627             :  * but not at all about cross-compiling ?
     628             :  */
     629             : #if (4096 & ~(-4096)) == 0
     630           0 :     return ((value & ~(-value)) == 0);
     631             : #else
     632             :     /*
     633             :      * Do it the hard way (for non 2's complement machines)
     634             :      */
     635             :     return (value == 0 || value ^ (value - 1) == (value * 2 - 1));
     636             : #endif
     637             : }
     638             : 
     639             : /*
     640             :  * Expand number for #if lexical analysis.  Note: evalnum recognizes
     641             :  * the unsigned suffix, but only returns a signed int value.
     642             :  */
     643        3074 : FILE_LOCAL int evalnum(int c)
     644             : {
     645             :     int value;
     646             :     int base;
     647             :     int c1;
     648             : 
     649        3074 :     if (c != '0')
     650        3069 :         base = 10;
     651           5 :     else if ((c = cget()) == 'x' || c == 'X')
     652             :     {
     653           0 :         base = 16;
     654           0 :         c = cget();
     655             :     }
     656           5 :     else base = 8;
     657        3074 :     value = 0;
     658             :     for (;;)
     659             :     {
     660       13236 :         c1 = c;
     661       13236 :         if (isascii(c) && isupper(c1))
     662           0 :             c1 = tolower(c1);
     663       13236 :         if (c1 >= 'a')
     664           0 :             c1 -= ('a' - 10);
     665             :         else
     666       13236 :             c1 -= '0';
     667       13236 :         if (c1 < 0 || c1 >= base)
     668             :             break;
     669       10162 :         value *= base;
     670       10162 :         value += c1;
     671       10162 :         c = cget();
     672       10162 :     }
     673        3074 :     if (c == 'u' || c == 'U')   /* Unsigned nonsense        */
     674           0 :         cget();
     675        3074 :     unget();
     676        3074 :     return value;
     677             : }
     678             : 
     679             : /*
     680             :  * Get a character constant
     681             :  */
     682           0 : FILE_LOCAL int evalchar(int skip)
     683             : {
     684             :     int c;
     685             :     int value;
     686             :     int count;
     687             : 
     688           0 :     instring = TRUE;
     689           0 :     if ((c = cget()) == '\\')
     690             :     {
     691           0 :         switch ((c = cget()))
     692             :         {
     693             :         case 'a':               /* New in Standard  */
     694             : #if ('a' == '\a' || '\a' == ALERT)
     695           0 :             value = ALERT;          /* Use predefined value */
     696             : #else
     697             :             value = '\a';           /* Use compiler's value */
     698             : #endif
     699           0 :             break;
     700             : 
     701             :         case 'b':
     702           0 :             value = '\b';
     703           0 :             break;
     704             : 
     705             :         case 'f':
     706           0 :             value = '\f';
     707           0 :             break;
     708             : 
     709             :         case 'n':
     710           0 :             value = '\n';
     711           0 :             break;
     712             : 
     713             :         case 'r':
     714           0 :             value = '\r';
     715           0 :             break;
     716             : 
     717             :         case 't':
     718           0 :             value = '\t';
     719           0 :             break;
     720             : 
     721             :         case 'v':               /* New in Standard  */
     722             : #if ('v' == '\v' || '\v' == VT)
     723           0 :             value = VT;         /* Use predefined value */
     724             : #else
     725             :             value = '\v';           /* Use compiler's value */
     726             : #endif
     727           0 :             break;
     728             : 
     729             :         case 'x':               /* '\xFF'       */
     730           0 :             count = 3;
     731           0 :             value = 0;
     732           0 :             while ((((c = get()) >= '0' && c <= '9') ||
     733           0 :                     (c >= 'a' && c <= 'f') ||
     734           0 :                     (c >= 'A' && c <= 'F')) &&
     735             :                    (--count >= 0))
     736             :             {
     737           0 :                 value *= 16;
     738           0 :                 value += (c - '0');
     739             :             }
     740           0 :             unget();
     741           0 :             break;
     742             : 
     743             :         default:
     744           0 :             if (c >= '0' && c <= '7')
     745             :             {
     746           0 :                 count = 3;
     747           0 :                 value = 0;
     748           0 :                 while (c >= '0' && c <= '7' && --count >= 0)
     749             :                 {
     750           0 :                     value *= 8;
     751           0 :                     value += (c - '0');
     752           0 :                     c = get();
     753             :                 }
     754           0 :                 unget();
     755             :             }
     756           0 :             else value = c;
     757           0 :             break;
     758             :         }
     759             :     }
     760           0 :     else if (c == '\'')
     761           0 :         value = 0;
     762             :     else
     763           0 :         value = c;
     764             :     /*
     765             :      * We warn on multi-byte constants and try to hack
     766             :      * (big|little)endian machines.
     767             :      */
     768           0 :     while ((c = get()) != '\'' && c != EOF_CHAR && c != '\n')
     769             :     {
     770           0 :         if (!skip)
     771           0 :             ciwarn("multi-byte constant '%c' isn't portable", c);
     772           0 :         value <<= BITS_CHAR;
     773           0 :         value += c;
     774             :     }
     775           0 :     instring = FALSE;
     776           0 :     return value;
     777             : }
     778             : 
     779             : /*
     780             :  * Apply the argument operator to the data on the value stack.
     781             :  * One or two values are popped from the value stack and the result
     782             :  * is pushed onto the value stack.
     783             :  *
     784             :  * OP_COL is a special case.
     785             :  *
     786             :  * evaleval() returns the new pointer to the top of the value stack.
     787             :  */
     788        2609 : FILE_LOCAL int * evaleval(int* valp, int op, int skip)
     789             : {
     790             :     int v1;
     791        2609 :     int v2 = 0;
     792             : 
     793        2609 :     if (isbinary(op))
     794        2609 :         v2 = *--valp;
     795        2609 :     v1 = *--valp;
     796             : #ifdef  DEBUG_EVAL
     797             :     fprintf( pCppOut, "%s op %s", (isbinary(op)) ? "binary" : "unary",
     798             :              opname[op]);
     799             :     if (isbinary(op))
     800             :         fprintf( pCppOut, ", v2 = %d.", v2);
     801             :     fprintf( pCppOut, ", v1 = %d.\n", v1);
     802             : #endif
     803        2609 :     switch (op)
     804             :     {
     805             :     case OP_EOE:
     806           0 :         break;
     807             : 
     808             :     case OP_ADD:
     809        2028 :         v1 += v2;
     810        2028 :         break;
     811             : 
     812             :     case OP_SUB:
     813         116 :         v1 -= v2;
     814         116 :         break;
     815             : 
     816             :     case OP_MUL:
     817           0 :         v1 *= v2;
     818           0 :         break;
     819             : 
     820             :     case OP_DIV:
     821             :     case OP_MOD:
     822           0 :         if (v2 == 0)
     823             :         {
     824           0 :             if (!skip)
     825             :             {
     826           0 :                 cwarn("%s by zero in #if, zero result assumed",
     827             :                       (op == OP_DIV) ? "divide" : "mod");
     828             :             }
     829           0 :             v1 = 0;
     830             :         }
     831           0 :         else if (op == OP_DIV)
     832           0 :             v1 /= v2;
     833             :         else
     834           0 :             v1 %= v2;
     835           0 :         break;
     836             : 
     837             :     case OP_ASL:
     838           0 :         v1 <<= v2;
     839           0 :         break;
     840             : 
     841             :     case OP_ASR:
     842           0 :         v1 >>= v2;
     843           0 :         break;
     844             : 
     845             :     case OP_AND:
     846           0 :         v1 &= v2;
     847           0 :         break;
     848             : 
     849             :     case OP_OR:
     850           0 :         v1 |= v2;
     851           0 :         break;
     852             : 
     853             :     case OP_XOR:
     854           0 :         v1 ^= v2;
     855           0 :         break;
     856             : 
     857             :     case OP_EQ:
     858          96 :         v1 = (v1 == v2);
     859          96 :         break;
     860             : 
     861             :     case OP_NE:
     862           0 :         v1 = (v1 != v2);
     863           0 :         break;
     864             : 
     865             :     case OP_LT:
     866           0 :         v1 = (v1 < v2);
     867           0 :         break;
     868             : 
     869             :     case OP_LE:
     870           0 :         v1 = (v1 <= v2);
     871           0 :         break;
     872             : 
     873             :     case OP_GE:
     874           2 :         v1 = (v1 >= v2);
     875           2 :         break;
     876             : 
     877             :     case OP_GT:
     878         367 :         v1 = (v1 > v2);
     879         367 :         break;
     880             : 
     881             :     case OP_ANA:
     882           0 :         v1 = (v1 && v2);
     883           0 :         break;
     884             : 
     885             :     case OP_ORO:
     886           0 :         v1 = (v1 || v2);
     887           0 :         break;
     888             : 
     889             :     case OP_COL:
     890             :         /*
     891             :          * v1 has the "true" value, v2 the "false" value.
     892             :          * The top of the value stack has the test.
     893             :          */
     894           0 :         v1 = (*--valp) ? v1 : v2;
     895           0 :         break;
     896             : 
     897             :     case OP_NEG:
     898           0 :         v1 = (-v1);
     899           0 :         break;
     900             : 
     901             :     case OP_PLU:
     902           0 :         break;
     903             : 
     904             :     case OP_COM:
     905           0 :         v1 = ~v1;
     906           0 :         break;
     907             : 
     908             :     case OP_NOT:
     909           0 :         v1 = !v1;
     910           0 :         break;
     911             : 
     912             :     default:
     913           0 :         cierror("#if bug, operand = %d.", op);
     914           0 :         v1 = 0;
     915             :     }
     916        2609 :     *valp++ = v1;
     917        2609 :     return valp;
     918             : }
     919             : 
     920             : #ifdef  DEBUG_EVAL
     921             : dumpstack(opstack, opp, value, valp)
     922             : OPTAB   opstack[NEXP];  /* Operand stack        */
     923             : OPTAB  *opp;            /* Operator stack       */
     924             : int     value[NEXP];    /* Value stack          */
     925             : int    *valp;           /* -> value vector      */
     926             : {
     927             :     fprintf( pCppOut, "index op prec skip name -- op stack at %s", infile->bptr);
     928             :     while (opp > opstack)
     929             :     {
     930             :         fprintf( pCppOut, " [%2d] %2d  %03o    %d %s\n", opp - opstack,
     931             :                  opp->op, opp->prec, opp->skip, opname[opp->op]);
     932             :         opp--;
     933             :     }
     934             :     while (--valp >= value)
     935             :     {
     936             :         fprintf( pCppOut, "value[%d] = %d\n", (valp - value), *valp);
     937             :     }
     938             : }
     939             : #endif
     940             : 
     941             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11