LCOV - code coverage report
Current view: top level - rsc/source/rscpp - cpp1.c (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 122 139 87.8 %
Date: 2015-06-13 12:38:46 Functions: 5 5 100.0 %
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             : #define NOMAIN
      21             : 
      22             : #include <stdio.h>
      23             : #include <ctype.h>
      24             : #include "cppdef.h"
      25             : #include "cpp.h"
      26             : 
      27             : FILE* pCppOut = NULL;
      28             : FILE* pCppIn  = NULL;
      29             : 
      30             : #if OSL_DEBUG_LEVEL > 1
      31             : FILE* pDefOut = NULL;       /* ER  evtl. #define's dump */
      32             : #endif
      33             : 
      34             : #ifdef B200
      35             : /* einzige Moeglichkeit unter BC Stack und Heap festzusetzen */
      36             : extern unsigned _stklen  = 24000;
      37             : extern unsigned _heaplen = 30000;
      38             : #endif
      39             : 
      40             : 
      41             : 
      42             : /*
      43             :  * Commonly used global variables:
      44             :  * line         is the current input line number.
      45             :  * wrongline    is set in many places when the actual output
      46             :  *              line is out of sync with the numbering, e.g,
      47             :  *              when expanding a macro with an embedded newline.
      48             :  *
      49             :  * token        holds the last identifier scanned (which might
      50             :  *              be a candidate for macro expansion).
      51             :  * errors       is the running cpp error counter.
      52             :  * infile       is the head of a linked list of input files (extended by
      53             :  *              #include and macros being expanded).  infile always points
      54             :  *              to the current file/macro.  infile->parent to the includer,
      55             :  *              etc.  infile->fd is NULL if this input stream is a macro.
      56             :  */
      57             : int line;                   /* Current line number          */
      58             : int wrongline;              /* Force #line to compiler      */
      59             : char token[IDMAX + 1];      /* Current input token          */
      60             : int errors;                 /* cpp error counter            */
      61             : FILEINFO* infile = NULL;    /* Current input file           */
      62             : #if OSL_DEBUG_LEVEL > 1
      63             : int debug;                  /* TRUE if debugging now        */
      64             : int bDumpDefs;              /* TRUE if #define's dump req.  */
      65             : #ifdef EVALDEFS
      66             : int bIsInEval;              /* TRUE if #define eval now     */
      67             : char EvalBuf[NEVALBUF + 1]; /* evaluation buffer            */
      68             : int nEvalOff = 0;           /* offset to free buffer pos    */
      69             : #endif
      70             : #endif
      71             : /*
      72             :  * This counter is incremented when a macro expansion is initiated.
      73             :  * If it exceeds a built-in value, the expansion stops -- this tests
      74             :  * for a runaway condition:
      75             :  *      #define X Y
      76             :  *      #define Y X
      77             :  *      X
      78             :  * This can be disabled by falsifying rec_recover.  (Nothing does this
      79             :  * currently: it is a hook for an eventual invocation flag.)
      80             :  */
      81             : int recursion;              /* Infinite recursion counter   */
      82             : int rec_recover = TRUE;     /* Unwind recursive macros      */
      83             : 
      84             : /*
      85             :  * instring is set TRUE when a string is scanned.  It modifies the
      86             :  * behavior of the "get next character" routine, causing all characters
      87             :  * to be passed to the caller (except <DEF_MAGIC>).  Note especially that
      88             :  * comments and \<newline> are not removed from the source.  (This
      89             :  * prevents cpp output lines from being arbitrarily long).
      90             :  *
      91             :  * inmacro is set by #define -- it absorbs comments and converts
      92             :  * form-feed and vertical-tab to space, but returns \<newline>
      93             :  * to the caller.  Strictly speaking, this is a bug as \<newline>
      94             :  * shouldn't delimit tokens, but we'll worry about that some other
      95             :  * time -- it is more important to prevent infinitly long output lines.
      96             :  *
      97             :  * instring and inmarcor are parameters to the get() routine which
      98             :  * were made global for speed.
      99             :  */
     100             : int instring = FALSE;       /* TRUE if scanning string      */
     101             : int inmacro = FALSE;        /* TRUE if #defining a macro    */
     102             : 
     103             : /*
     104             :  * work[] and workp are used to store one piece of text in a temporay
     105             :  * buffer.  To initialize storage, set workp = work.  To store one
     106             :  * character, call save(c);  (This will fatally exit if there isn't
     107             :  * room.)  To terminate the string, call save(EOS).  Note that
     108             :  * the work buffer is used by several subroutines -- be sure your
     109             :  * data won't be overwritten.  The extra byte in the allocation is
     110             :  * needed for string formal replacement.
     111             :  */
     112             : char work[NWORK + 1];        /* Work buffer                  */
     113             : char* workp;                 /* Work buffer pointer          */
     114             : 
     115             : /*
     116             :  * keepcomments is set TRUE by the -C option.  If TRUE, comments
     117             :  * are written directly to the output stream.  This is needed if
     118             :  * the output from cpp is to be passed to lint (which uses commands
     119             :  * embedded in comments).  cflag contains the permanent state of the
     120             :  * -C flag.  keepcomments is always falsified when processing #control
     121             :  * commands and when compilation is suppressed by a false #if
     122             :  *
     123             :  * If eflag is set, CPP returns "success" even if non-fatal errors
     124             :  * were detected.
     125             :  *
     126             :  * If nflag is non-zero, no symbols are predefined except __LINE__.
     127             :  * __FILE__, and __DATE__.  If nflag > 1, absolutely no symbols
     128             :  * are predefined.
     129             :  */
     130             : int keepcomments = FALSE;   /* Write out comments flag      */
     131             : int cflag = FALSE;          /* -C option (keep comments)    */
     132             : int eflag = FALSE;          /* -E option (never fail)       */
     133             : int nflag = 0;              /* -N option (no predefines)    */
     134             : 
     135             : /*
     136             :  * ifstack[] holds information about nested #if's.  It is always
     137             :  * accessed via *ifptr.  The information is as follows:
     138             :  *      WAS_COMPILING   state of compiling flag at outer level.
     139             :  *      ELSE_SEEN       set TRUE when #else seen to prevent 2nd #else.
     140             :  *      TRUE_SEEN       set TRUE when #if or #elif succeeds
     141             :  * ifstack[0] holds the compiling flag.  It is TRUE if compilation
     142             :  * is currently enabled.  Note that this must be initialized TRUE.
     143             :  */
     144             : char ifstack[BLK_NEST] = { TRUE };   /* #if information      */
     145             : char* ifptr = ifstack;               /* -> current ifstack[] */
     146             : 
     147             : /*
     148             :  * incdir[] stores the -i directories (and the system-specific
     149             :  * #include <...> directories.
     150             :  */
     151             : char* incdir[NINCLUDE];              /* -i directories               */
     152             : char** incend = incdir;              /* -> free space in incdir[]    */
     153             : 
     154             : /*
     155             :  * This is the table used to predefine target machine and operating
     156             :  * system designators.  It may need hacking for specific circumstances.
     157             :  * Note: it is not clear that this is part of the Ansi Standard.
     158             :  * The -N option suppresses preset definitions.
     159             :  */
     160             : char* preset[] =
     161             : {                   /* names defined at cpp start   */
     162             : #ifdef  MACHINE
     163             :         MACHINE,
     164             : #endif
     165             : #ifdef  SYSTEM
     166             :         SYSTEM,
     167             : #endif
     168             : #ifdef  COMPILER
     169             :         COMPILER,
     170             : #endif
     171             : #if OSL_DEBUG_LEVEL > 1
     172             :         "decus_cpp",                    /* Ourselves!                   */
     173             : #endif
     174             :         NULL                            /* Must be last                 */
     175             : };
     176             : 
     177             : /*
     178             :  * The value of these predefined symbols must be recomputed whenever
     179             :  * they are evaluated.  The order must not be changed.
     180             :  */
     181             : char* magic[] =
     182             : {                    /* Note: order is important     */
     183             :         "__LINE__",
     184             :         "__FILE__",
     185             :         NULL                            /* Must be last                 */
     186             : };
     187             : 
     188             : static char* sharpfilename = NULL;
     189             : 
     190             : int nRunde = 0;
     191             : 
     192         361 : void InitCpp1()
     193             : {
     194             :     int i;
     195             :     /* in der LIB-Version muessen alle Variablen initialisiert werden */
     196             : 
     197         361 :     line = wrongline = errors = recursion = 0;
     198       46208 :     for( i = 0; i < IDMAX; i++ )
     199       45847 :         token[ i ] = 0;
     200             : 
     201    46208361 :     for( i = 0; i < NWORK; i++ )
     202    46208000 :         work[ i ] = 0;
     203             : 
     204       36461 :     for( i = 0; i < NINCLUDE; i++ )
     205       36100 :         incdir[ i ] = NULL;
     206             : 
     207         361 :     workp = NULL;
     208       11913 :     for( i = 0; i < BLK_NEST; i++ )
     209       11552 :         ifstack[ i ] = TRUE;
     210         361 :     ifptr = ifstack;
     211             : 
     212         361 :     pCppOut = stdout;
     213         361 :     pCppIn  = stdin;
     214             : #if OSL_DEBUG_LEVEL > 1
     215             :     debug = 0;
     216             :     bDumpDefs = 0;
     217             :     pDefOut = stdout;
     218             : #ifdef EVALDEFS
     219             :     bIsInEval = 0;
     220             :     for( i = 0; i < NEVALBUF; i++ )
     221             :         EvalBuf[ i ] = 0;
     222             :     nEvalOff = 0;
     223             : #endif
     224             : #endif
     225         361 :     rec_recover = TRUE;
     226         361 :     infile = NULL;
     227         361 :     instring = inmacro = keepcomments = cflag = eflag = FALSE;
     228         361 :     nflag = 0;
     229         361 :     incend = incdir;
     230         361 :     sharpfilename = NULL;
     231         361 : }
     232             : 
     233         361 : int MAIN(int argc, char** argv)
     234             : {
     235             :     int    i;
     236         361 :     char** useargv = 0;
     237         361 :     char** pfargv = NULL;
     238             : 
     239         361 :     if( nRunde == 0 )
     240             :     {
     241         361 :         pCppIn = stdin;
     242         361 :         pCppOut = stdout;
     243             :     }
     244             : 
     245         361 :     nRunde++;
     246         361 :     InitCpp1();
     247         361 :     InitCpp4();
     248         361 :     InitCpp6();
     249             : 
     250         361 :     initdefines();                          /* O.S. specific def's  */
     251         361 :     if ( argv[argc-1][0] == '@' )
     252             :     {
     253         361 :         i = readoptions( argv[1], &pfargv );    /* Command file */
     254         361 :         useargv=pfargv;
     255             :     }
     256             :     else
     257             :     {
     258           0 :         i = dooptions(argc, argv);              /* Command line -flags  */
     259           0 :         useargv=argv;
     260             :     }
     261         361 :     switch (i)
     262             :     {
     263             : #if OSL_DEBUG_LEVEL > 1
     264             :     case 4:
     265             :         if ( bDumpDefs )
     266             :         {
     267             :             /*
     268             :              * Get defBase file, "-" means use stdout.
     269             :              */
     270             :             if (!streq(useargv[3], "-"))
     271             :             {
     272             :                 pDefOut = fopen( useargv[3], "w" );
     273             :                 if( pDefOut == NULL )
     274             :                 {
     275             :                     perror(useargv[3]);
     276             :                     cerror("Can't open output file \"%s\"", useargv[3]);
     277             :                     exit(IO_ERROR);
     278             :                 }
     279             :             }                           /* Continue by opening output    */
     280             :         }
     281             : #endif
     282             :     case 3:
     283             :         /*
     284             :          * Get output file, "-" means use stdout.
     285             :          */
     286         361 :         if (!streq(useargv[2], "-"))
     287             :         {
     288         361 :             pCppOut = fopen( useargv[2], "w" );
     289         361 :             if( pCppOut == NULL )
     290             :             {
     291           0 :                 perror(useargv[2]);
     292           0 :                 cerror("Can't open output file \"%s\"", useargv[2]);
     293           0 :                 exit(IO_ERROR);
     294             :             }
     295             :         }                           /* Continue by opening input    */
     296             :     case 2:                         /* One file -> stdin            */
     297             :         /*
     298             :          * Open input file, "-" means use stdin.
     299             :          */
     300         361 :         if (!streq(useargv[1], "-"))
     301             :         {
     302         361 :             pCppIn = fopen( useargv[1], "r" );
     303         361 :             if( pCppIn == NULL)
     304             :             {
     305           0 :                 perror(useargv[1]);
     306           0 :                 cerror("Can't open input file \"%s\"", useargv[1]);
     307           0 :                 exit(IO_ERROR);
     308             :             }
     309         361 :             strncpy(work, useargv[1], NWORK);  /* Remember input filename      */
     310         361 :             break;
     311             :         }                           /* Else, just get stdin         */
     312             :     case 0:                         /* No args?                     */
     313             :     case 1:                         /* No files, stdin -> stdout    */
     314           0 :         work[0] = EOS;              /* Unix can't find stdin name   */
     315           0 :         break;
     316             : 
     317             :     default:
     318           0 :         exit(IO_ERROR);             /* Can't happen                 */
     319             :     }
     320             : 
     321         361 :     setincdirs();                   /* Setup -I include directories */
     322         361 :     addfile( pCppIn, work);           /* "open" main input file       */
     323             : #if OSL_DEBUG_LEVEL > 1
     324             :     if (debug > 0 || bDumpDefs)
     325             :         dumpdef("preset #define symbols");
     326             : #endif
     327         361 :     if( pCppIn != stdin )
     328         361 :         rewind( pCppIn );
     329             : 
     330         361 :     cppmain();                      /* Process main file            */
     331             : 
     332         361 :     if ((i = (ifptr - &ifstack[0])) != 0)
     333             :     {
     334           0 :         cierror("Inside #ifdef block at end of input, depth = %d", i);
     335             :     }
     336             : #if OSL_DEBUG_LEVEL > 1
     337             :     if( pDefOut != stdout && pDefOut != stderr )
     338             :         fclose( pDefOut );
     339             : #endif
     340         361 :     if( pCppOut != stdout && pCppOut != stderr )
     341         361 :         fclose( pCppOut );
     342             : 
     343         361 :     if (errors > 0)
     344             :     {
     345           0 :         fprintf(stderr, (errors == 1)
     346             :                 ? "%d error in preprocessor\n"
     347             :                 : "%d errors in preprocessor\n", errors);
     348           0 :         if (!eflag)
     349           0 :             exit(IO_ERROR);
     350             :     }
     351         361 :     if( pfargv )
     352         361 :         free(pfargv);
     353         361 :     return IO_NORMAL;
     354             : 
     355             : }
     356             : 
     357             : /*
     358             :  * Main process for cpp -- copies tokens from the current input
     359             :  * stream (main file, include file, or a macro) to the output
     360             :  * file.
     361             :  */
     362         361 : void cppmain()
     363             : {
     364             :     int c;              /* Current character    */
     365             :     int counter;        /* newlines and spaces  */
     366             : 
     367             :     /*
     368             :      * Explicitly output a #line at the start of cpp output so
     369             :      * that lint (etc.) knows the name of the original source
     370             :      * file.  If we don't do this explicitly, we may get
     371             :      * the name of the first #include file instead.
     372             :      * We also seem to need a blank line following that first #line.
     373             :      */
     374             : #ifdef EVALDEFS
     375             :     if ( !bIsInEval )
     376             : #endif
     377             :     {
     378         361 :         sharp();
     379         361 :         PUTCHAR('\n');
     380             :     }
     381             :     /*
     382             :      * This loop is started "from the top" at the beginning of each line
     383             :      * wrongline is set TRUE in many places if it is necessary to write
     384             :      * a #line record.  (But we don't write them when expanding macros.)
     385             :      *
     386             :      * The counter variable has two different uses:  at
     387             :      * the start of a line, it counts the number of blank lines that
     388             :      * have been skipped over.  These are then either output via
     389             :      * #line records or by outputting explicit blank lines.
     390             :      * When expanding tokens within a line, the counter remembers
     391             :      * whether a blank/tab has been output.  These are dropped
     392             :      * at the end of the line, and replaced by a single blank
     393             :      * within lines.
     394             :      */
     395             :     for (;;)
     396             :     {
     397       76314 :         counter = 0;                        /* Count empty lines    */
     398             :         for (;;)
     399             :         {                                   /* For each line, ...   */
     400     1104146 :             while (type[(c = get())] == SPA) /* Skip leading blanks */
     401             :                 ;                           /* in this line.        */
     402     1104146 :             if (c == '\n')                  /* If line's all blank, */
     403      313123 :                 ++counter;                  /* Do nothing now       */
     404      791023 :             else if (c == '#')
     405             :             {            /* Is 1st non-space '#' */
     406      714315 :                 keepcomments = FALSE;       /* Don't pass comments  */
     407      714315 :                 counter = control(counter); /* Yes, do a #command   */
     408      714315 :                 keepcomments = (cflag && compiling);
     409             :             }
     410       76708 :             else if (c == EOF_CHAR)         /* At end of file?      */
     411             :             {
     412         361 :                 break;
     413             :             }
     414       76347 :             else if (!compiling)
     415             :             {                               /* #ifdef false?        */
     416         394 :                 skipnl();                   /* Skip to newline      */
     417         394 :                 counter++;                  /* Count it, too.       */
     418             :             }
     419             :             else
     420             :             {
     421       75953 :                 break;                      /* Actual token         */
     422             :             }
     423     1027832 :         }
     424       76314 :         if (c == EOF_CHAR)                  /* Exit process at      */
     425         361 :             break;                          /* End of file          */
     426             :         /*
     427             :          * If the loop didn't terminate because of end of file, we
     428             :          * know there is a token to compile.  First, clean up after
     429             :          * absorbing newlines.  counter has the number we skipped.
     430             :          */
     431       75953 :         if ((wrongline && infile->fp != NULL) || counter > 4)
     432        1381 :             sharp();                        /* Output # line number */
     433             :         else
     434             :         {                                   /* If just a few, stuff */
     435      156225 :             while (--counter >= 0)          /* them out ourselves   */
     436        7081 :                 PUTCHAR('\n');
     437             :         }
     438             :         /*
     439             :          * Process each token on this line.
     440             :          */
     441       75953 :         unget();                            /* Reread the char.     */
     442             :         for (;;)
     443             :         {                                   /* For the whole line,  */
     444             :             do
     445             :             {                            /* Token concat. loop   */
     446     1312863 :                 for (counter = 0; type[(c = get())] == SPA;)
     447             :                 {
     448      235209 :                     counter++;              /* Skip over blanks     */
     449             : 
     450             :                 }
     451      538827 :                 if (c == EOF_CHAR || c == '\n')
     452             :                     goto end_line;          /* Exit line loop       */
     453      462874 :                 else if (counter > 0)       /* If we got any spaces */
     454      216976 :                     PUTCHAR(' ');           /* Output one space     */
     455      462874 :                 c = macroid(c);             /* Grab the token       */
     456             :             }
     457      462874 :             while (type[c] == LET && catenate());
     458             : 
     459      462874 :             if (c == EOF_CHAR || c == '\n') /* From macro exp error */
     460             :                 goto end_line;              /* Exit line loop       */
     461             : 
     462      462874 :             switch (type[c])
     463             :             {
     464             :             case LET:
     465       82877 :                 fputs(token, pCppOut);       /* Quite ordinary token */
     466             : #ifdef EVALDEFS
     467             :                 {
     468             :                     int len;
     469             :                     if ( bIsInEval
     470             :                          && nEvalOff + (len=strlen(token)) < NEVALBUF )
     471             :                     {
     472             :                         strcpy( &EvalBuf[nEvalOff], token );
     473             :                         nEvalOff += len;
     474             :                     }
     475             :                 }
     476             : #endif
     477       82877 :                 break;
     478             : 
     479             : 
     480             :             case DIG:                       /* Output a number      */
     481             :             case DOT:                       /* Dot may begin floats */
     482             : #ifdef EVALDEFS
     483             :                 if ( bIsInEval )
     484             :                     scannumber(c, outputEval);
     485             :                 else
     486             :                     scannumber(c, output);
     487             : #else
     488       59663 :                 scannumber(c, output);
     489             : #endif
     490       59663 :                 break;
     491             : 
     492             :             case QUO:                       /* char or string const */
     493       23627 :                 scanstring(c, output);      /* Copy it to output    */
     494       23627 :                 break;
     495             : 
     496             :             default:                        /* Some other character */
     497      296707 :                 cput(c);                    /* Just output it       */
     498             : #ifdef EVALDEFS
     499             :                 if ( bIsInEval && nEvalOff < NEVALBUF )
     500             :                     EvalBuf[nEvalOff++] = c;
     501             : #endif
     502      296707 :                 break;
     503             :             }                               /* Switch ends          */
     504      462874 :         }                                   /* Line for loop        */
     505             :       end_line:
     506       75953 :         if (c == '\n')
     507             :         {                                   /* Compiling at EOL?    */
     508       75953 :             PUTCHAR('\n');                  /* Output newline, if   */
     509       75953 :             if (infile->fp == NULL)         /* Expanding a macro,   */
     510           0 :                 wrongline = TRUE;           /* Output # line later  */
     511             :         }
     512       75953 :     }                                       /* Continue until EOF   */
     513             : #ifdef EVALDEFS
     514             :     if ( bIsInEval )
     515             :         EvalBuf[nEvalOff++] = '\0';
     516             : #endif
     517         361 : }
     518             : 
     519             : /*
     520             :  * Output one character to stdout -- output() is passed as an
     521             :  * argument to scanstring()
     522             :  */
     523      686139 : void output(int c)
     524             : {
     525      686139 :     if (c != TOK_SEP)
     526      686139 :         PUTCHAR(c);
     527      686139 : }
     528             : 
     529             : #ifdef EVALDEFS
     530             : /*
     531             :  * Output one character to stdout -- output() is passed as an
     532             :  * argument to scanstring()
     533             :  */
     534             : int outputEval(int c)
     535             : {
     536             :     if (c != TOK_SEP)
     537             :     {
     538             :         PUTCHAR(c);
     539             :         if ( bIsInEval && nEvalOff < NEVALBUF )
     540             :             EvalBuf[nEvalOff++] = c;
     541             :     }
     542             : }
     543             : #endif
     544             : 
     545             : 
     546             : /*
     547             :  * Output a line number line.
     548             :  */
     549        1742 : void sharp()
     550             : {
     551             :     char* name;
     552             : 
     553        1742 :     if (keepcomments)                       /* Make sure # comes on */
     554           0 :         PUTCHAR('\n');                      /* a fresh, new line.   */
     555             : 
     556        1742 :     fprintf( pCppOut, "#%s %d", LINE_PREFIX, line);
     557        1742 :     if (infile->fp != NULL)
     558             :     {
     559        1742 :         name = (infile->progname != NULL) ? infile->progname : infile->filename;
     560        3123 :         if (sharpfilename == NULL ||
     561        2762 :             (sharpfilename != NULL && !streq(name, sharpfilename)))
     562             :         {
     563         363 :             if (sharpfilename != NULL)
     564           2 :                 free(sharpfilename);
     565         363 :             sharpfilename = savestring(name);
     566         363 :             fprintf( pCppOut, " \"%s\"", name);
     567             :         }
     568             :     }
     569        1742 :     PUTCHAR('\n');
     570        1742 :     wrongline = FALSE;
     571        1742 : }
     572             : 
     573             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11