pre.l 87.1 KB
Newer Older
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1 2
/******************************************************************************
 *
3
 * 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
4
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
5
 * Copyright (C) 1997-2013 by Dimitri van Heesch.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
6 7 8 9 10 11 12
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
13 14
 * Documents produced by Doxygen are derivative works derived from the
 * input used in their production; they are not affected by this license.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
15 16 17 18 19 20 21 22 23 24 25 26
 *
 */

%{

/*
 *	includes
 */

#include <stdio.h>
#include <assert.h>
#include <ctype.h>
27
#include <errno.h>
Dimitri van Heesch's avatar
Dimitri van Heesch committed
28 29 30 31 32 33 34

#include <qarray.h>
#include <qstack.h>
#include <qfile.h>
#include <qstrlist.h>
#include <qdict.h>
#include <qregexp.h>
35
#include <qfileinfo.h>
36
#include <qdir.h>
Dimitri van Heesch's avatar
Dimitri van Heesch committed
37
  
38
#include "pre.h"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
39 40 41 42 43
#include "constexp.h"
#include "define.h"
#include "doxygen.h"
#include "message.h"
#include "util.h"
44
#include "defargs.h"
45
#include "debug.h"
46
#include "bufstr.h"
47
#include "portable.h"
48
#include "bufstr.h"
49 50
#include "arguments.h"
#include "entry.h"
51 52 53 54 55
#include "condparser.h"
#include "config.h"
#include "filedef.h"
#include "memberdef.h"
#include "membername.h"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
56

57 58 59 60
// Toggle for some debugging info
//#define DBG_CTX(x) fprintf x
#define DBG_CTX(x) do { } while(0)

Dimitri van Heesch's avatar
Dimitri van Heesch committed
61
#define YY_NEVER_INTERACTIVE 1
62 63 64 65 66 67 68 69 70 71

struct CondCtx
{
  CondCtx(int line,QCString id,bool b) 
    : lineNr(line),sectionId(id), skip(b) {}
  int lineNr;
  QCString sectionId;
  bool skip;
};

Dimitri van Heesch's avatar
Dimitri van Heesch committed
72 73
struct FileState
{
74 75
  FileState(int size) : fileBuf(size), 
                        oldFileBuf(0), oldFileBufPos(0) {}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
76
  int lineNr;
77 78 79
  BufStr fileBuf;
  BufStr *oldFileBuf;
  int oldFileBufPos;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
80
  YY_BUFFER_STATE bufState;
81
  QCString fileName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
82 83
};  

84 85 86
/** @brief Singleton that manages the defines available while 
 *  proprocessing files. 
 */
87 88
class DefineManager
{
89
  /** Local class used to hold the defines for a single file */
90 91 92
  class DefinesPerFile
  {
    public:
93
      /** Creates an empty container for defines */
94 95 96 97
      DefinesPerFile() : m_defines(257), m_includedFiles(17)
      {
        m_defines.setAutoDelete(TRUE);
      }
98
      /** Destroys the object */
99 100 101
      virtual ~DefinesPerFile()
      {
      }
102 103 104 105
      /** Adds a define in the context of a file. Will replace 
       *  an existing define with the same name (redefinition)
       *  @param def The Define object to add.
       */
106 107 108 109 110 111 112 113 114
      void addDefine(Define *def)
      {
	Define *d = m_defines.find(def->name);
	if (d!=0) // redefine
	{
	  m_defines.remove(d->name);
	}
	m_defines.insert(def->name,def);
      }
115 116 117
      /** Adds an include file for this file 
       *  @param fileName The name of the include file
       */
118 119 120 121 122 123 124 125 126 127 128 129
      void addInclude(const char *fileName)
      {
	m_includedFiles.insert(fileName,(void*)0x8);
      }
      void collectDefines(DefineDict *dict,QDict<void> &includeStack);
    private:
      DefineDict m_defines;
      QDict<void> m_includedFiles;
  };

  public:
    friend class DefinesPerFile;
130
    /** Returns a reference to the singleton */
131 132 133 134 135
    static DefineManager &instance()
    {
      if (theInstance==0) theInstance = new DefineManager;
      return *theInstance;
    }
136
    /** Deletes the singleton */
137 138 139 140 141
    static void deleteInstance()
    {
      delete theInstance;
      theInstance = 0;
    }
142 143 144 145 146
    /** Starts a context in which defines are collected. 
     *  Called at the start of a new file that is preprocessed.
     *  @param fileName the name of the file to process.
     */
    void startContext(const char *fileName)
147 148 149
    {
      //printf("DefineManager::startContext()\n");
      m_contextDefines.clear();
150 151 152 153 154 155 156 157
      if (fileName==0) return;
      DefinesPerFile *dpf = m_fileMap.find(fileName);
      if (dpf==0)
      {
	//printf("New file!\n");
	dpf = new DefinesPerFile;
	m_fileMap.insert(fileName,dpf);
      }
158
    }
159 160 161
    /** Ends the context started with startContext() freeing any
     *  defines collected within in this context.
     */
162 163 164 165 166
    void endContext()
    {
      //printf("DefineManager::endContext()\n");
      m_contextDefines.clear();
    }
167 168 169 170 171
    /** Add an included file to the current context.
     *  If the file has been pre-processed already, all defines are added
     *  to the context.
     *  @param fileName The name of the include file to add to the context.
     */
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    void addFileToContext(const char *fileName)
    {
      if (fileName==0) return;
      //printf("DefineManager::addFileToContext(%s)\n",fileName);
      DefinesPerFile *dpf = m_fileMap.find(fileName);
      if (dpf==0)
      {
	//printf("New file!\n");
	dpf = new DefinesPerFile;
	m_fileMap.insert(fileName,dpf);
      }
      else
      {
	//printf("existing file!\n");
	QDict<void> includeStack(17);
	dpf->collectDefines(&m_contextDefines,includeStack);
      }
    }
190 191 192 193 194

    /** Add a define to the manager object.
     *  @param fileName The file in which the define was found
     *  @param def The Define object to add.
     */
195 196 197 198 199 200 201 202 203 204
    void addDefine(const char *fileName,Define *def)
    {
      if (fileName==0) return;
      //printf("DefineManager::addDefine(%s,%s)\n",fileName,def->name.data());
      Define *d = m_contextDefines.find(def->name);
      if (d!=0) // redefine
      {
	m_contextDefines.remove(d->name);
      }
      m_contextDefines.insert(def->name,def);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
205 206 207 208 209 210 211

      DefinesPerFile *dpf = m_fileMap.find(fileName);
      if (dpf==0)
      {
	dpf = new DefinesPerFile;
      }
      dpf->addDefine(def);
212
    }
213 214 215 216 217

    /** Add an include relation to the manager object.
     *  @param fromFileName file name in which the include was found.
     *  @param toFileName file name that is included.
     */
218 219 220 221 222 223 224 225 226 227 228
    void addInclude(const char *fromFileName,const char *toFileName)
    {
      //printf("DefineManager::addInclude(%s,%s)\n",fromFileName,toFileName);
      if (fromFileName==0 || toFileName==0) return;
      DefinesPerFile *dpf = m_fileMap.find(fromFileName);
      if (dpf==0)
      {
	dpf = new DefinesPerFile;
      }
      dpf->addInclude(toFileName);
    }
229 230 231
    /** Returns a Define object given its name or 0 if the Define does
     *  not exist.
     */
232 233
    Define *isDefined(const char *name) const
    {
234
      Define *d = m_contextDefines.find(name);
235
      if (d && d->undef) d=0;
236 237
      //printf("isDefined(%s)=%p\n",name,d);
      return d;
238
    }
239
    /** Returns a reference to the defines found in the current context. */
240 241 242 243 244 245
    const DefineDict &defineContext() const
    {
      return m_contextDefines;
    }
  private:
    static DefineManager *theInstance;
246 247

    /** Helper function to collect all define for a given file */
248 249 250 251 252 253 254 255 256 257
    void collectDefinesForFile(const char *fileName,DefineDict *dict)
    {
      if (fileName==0) return;
      DefinesPerFile *dpf = m_fileMap.find(fileName);
      if (dpf)
      {
	QDict<void> includeStack(17);
	dpf->collectDefines(dict,includeStack);
      }
    }
258 259

    /** Helper function to return the DefinesPerFile object for a given file name. */
260 261 262 263 264
    DefinesPerFile *find(const char *fileName) const
    {
      if (fileName==0) return 0;
      return m_fileMap.find(fileName);
    }
265 266

    /** Creates a new DefineManager object */
267 268 269 270
    DefineManager() : m_fileMap(1009), m_contextDefines(1009)
    {
      m_fileMap.setAutoDelete(TRUE);
    }
271 272

    /** Destroys the object */
273 274 275
    virtual ~DefineManager() 
    {
    }
276

277 278 279 280
    QDict<DefinesPerFile> m_fileMap;
    DefineDict m_contextDefines;
};

281
/** Singleton instance */
282 283
DefineManager *DefineManager::theInstance = 0;

284 285 286 287 288 289 290 291 292
/** Collects all defines for a file and all files that the file includes.
 *  This function will recursively call itself for each file.
 *  @param dict The dictionary to fill with the defines. A redefine will
 *         replace a previous definition.
 *  @param includeStack The stack of includes, used to stop recursion in
 *         case there is a cyclic include dependency.
 */
void DefineManager::DefinesPerFile::collectDefines(
                     DefineDict *dict,QDict<void> &includeStack)
293
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
294
  //printf("DefinesPerFile::collectDefines #defines=%d\n",m_defines.count());
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  {
    QDictIterator<void> di(m_includedFiles);
    for (di.toFirst();(di.current());++di)
    {
      QCString incFile = di.currentKey();
      DefinesPerFile *dpf = DefineManager::instance().find(incFile);
      if (dpf && includeStack.find(incFile)==0) 
      {
        //printf("  processing include %s\n",incFile.data());
	includeStack.insert(incFile,(void*)0x8);
	dpf->collectDefines(dict,includeStack);
      }
    }
  }
  {
    QDictIterator<Define> di(m_defines);
    Define *def;
    for (di.toFirst();(def=di.current());++di)
    {
      Define *d = dict->find(def->name);
      if (d!=0) // redefine
      {
	dict->remove(d->name);
      }
      dict->insert(def->name,def);
      //printf("  adding define %s\n",def->name.data());
    }
  }
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
325 326
/* -----------------------------------------------------------------
 *
Dimitri van Heesch's avatar
Dimitri van Heesch committed
327
 *	scanner's state
Dimitri van Heesch's avatar
Dimitri van Heesch committed
328 329
 */

Dimitri van Heesch's avatar
Dimitri van Heesch committed
330
static int                g_yyLineNr   = 1;
331 332
static int                g_yyMLines   = 1;
static int                g_yyColNr   = 1;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
333 334
static QCString           g_yyFileName;
static FileDef           *g_yyFileDef;
335
static FileDef           *g_inputFileDef;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
336 337 338 339 340 341 342 343 344
static int                g_ifcount    = 0;
static QStrList          *g_pathList = 0;  
static QStack<FileState>  g_includeStack;
static QDict<int>        *g_argDict;
static int                g_defArgs = -1;
static QCString           g_defName;
static QCString           g_defText;
static QCString           g_defLitText;
static QCString           g_defArgsStr;
345
static QCString           g_defExtraSpacing;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
346 347 348 349 350
static bool               g_defVarArgs;
static int                g_level;
static int                g_lastCContext;
static int                g_lastCPPContext;
static QArray<int>        g_levelGuard;
351 352
static BufStr            *g_inputBuf;
static int                g_inputBufPos;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
353 354 355 356 357
static BufStr            *g_outputBuf;
static int                g_roundCount;
static bool               g_quoteArg;
static DefineDict        *g_expandedDict;
static int                g_findDefArgContext;
358
static bool               g_expectGuard;
359
static QCString           g_guardName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
360 361 362
static QCString           g_lastGuardName;
static QCString           g_incName;
static QCString           g_guardExpr;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
363
static int                g_curlyCount;
364
static bool               g_nospaces; // add extra spaces during macro expansion
365

366 367
static bool               g_macroExpansion; // from the configuration
static bool               g_expandOnlyPredef; // from the configuration
368
static int                g_commentCount;
369
static bool               g_insideComment;
370
static bool               g_isImported;
371
static QCString           g_blockName;
372 373
static int                g_condCtx;
static bool               g_skip;
374
static QStack<CondCtx>    g_condStack;
375
static bool               g_insideCS; // C# has simpler preprocessor
Dimitri van Heesch's avatar
Dimitri van Heesch committed
376
static bool               g_isSource;
377

378
static bool               g_lexInit = FALSE;
379
static int                g_fenceSize = 0;
380
static bool               g_ccomment;
381

382 383 384 385
//DefineDict* getGlobalDefineDict() 
//{
//  return g_globalDefineDict;
//}
386

Dimitri van Heesch's avatar
Dimitri van Heesch committed
387 388 389
static void setFileName(const char *name)
{
  bool ambig;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
390
  QFileInfo fi(name);
391
  g_yyFileName=fi.absFilePath().utf8();
392
  g_yyFileDef=findFileDef(Doxygen::inputNameDict,g_yyFileName,ambig);
393 394 395 396 397
  if (g_yyFileDef==0) // if this is not an input file check if it is an
                      // include file
  {
    g_yyFileDef=findFileDef(Doxygen::includeNameDict,g_yyFileName,ambig);
  }
398 399
  //printf("setFileName(%s) g_yyFileName=%s g_yyFileDef=%p\n",
  //    name,g_yyFileName.data(),g_yyFileDef);
400
  if (g_yyFileDef && g_yyFileDef->isReference()) g_yyFileDef=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
401
  g_insideCS = getLanguageFromFileName(g_yyFileName)==SrcLangExt_CSharp;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
402
  g_isSource = guessSection(g_yyFileName);
403
}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
404 405 406

static void incrLevel()
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
407 408 409 410
  g_level++;
  g_levelGuard.resize(g_level);
  g_levelGuard[g_level-1]=FALSE;
  //printf("%s line %d: incrLevel %d\n",g_yyFileName.data(),g_yyLineNr,g_level);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
411 412 413 414
}

static void decrLevel()
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
415 416
  //printf("%s line %d: decrLevel %d\n",g_yyFileName.data(),g_yyLineNr,g_level);
  if (g_level > 0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
417
  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
418 419
    g_level--;
    g_levelGuard.resize(g_level);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
420 421 422
  }
  else
  {
423
    warn(g_yyFileName,g_yyLineNr,"More #endif's than #if's found.\n");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
424 425 426 427 428
  }
}

static bool otherCaseDone()
{
429 430
  if (g_level==0)
  {
431
    warn(g_yyFileName,g_yyLineNr,"Found an #else without a preceding #if.\n");
432 433 434 435 436 437
    return TRUE;
  }
  else
  {
    return g_levelGuard[g_level-1];
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
438 439 440 441
}

static void setCaseDone(bool value)
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
442
  g_levelGuard[g_level-1]=value;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
443 444
}

445 446
static QDict<void> g_allIncludes(10009);

Dimitri van Heesch's avatar
Dimitri van Heesch committed
447
static FileState *checkAndOpenFile(const QCString &fileName,bool &alreadyIncluded)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
448
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
449
  alreadyIncluded = FALSE;
450
  FileState *fs = 0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
451
  //printf("checkAndOpenFile(%s)\n",fileName.data());
452
  QFileInfo fi(fileName);
453 454
  if (fi.exists() && fi.isFile())
  {
455 456 457
    static QStrList &exclPatterns = Config_getList("EXCLUDE_PATTERNS");
    if (patternMatch(fi,&exclPatterns)) return 0;

458
    QCString absName = fi.absFilePath().utf8();
459 460

    // global guard
Dimitri van Heesch's avatar
Dimitri van Heesch committed
461 462
    if (g_curlyCount==0) // not #include inside { ... }
    {
463
      if (g_allIncludes.find(absName)!=0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
464
      {
465 466 467
        alreadyIncluded = TRUE;
        //printf("  already included 1\n");
        return 0; // already done
Dimitri van Heesch's avatar
Dimitri van Heesch committed
468 469 470
      }
      g_allIncludes.insert(absName,(void *)0x8);
    }
471
    // check include stack for absName
472 473 474 475 476 477 478 479 480 481 482 483 484 485

    QStack<FileState> tmpStack;
    g_includeStack.setAutoDelete(FALSE);
    while ((fs=g_includeStack.pop()))
    {
      if (fs->fileName==absName) alreadyIncluded=TRUE;
      tmpStack.push(fs);
    }
    while ((fs=tmpStack.pop()))
    {
      g_includeStack.push(fs);
    }
    g_includeStack.setAutoDelete(TRUE);

486
    if (alreadyIncluded)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
487 488
    {
      //printf("  already included 2\n");
489
      return 0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
490
    }
491
    //printf("#include %s\n",absName.data());
492

493
    fs = new FileState(fi.size()+4096);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
494
    alreadyIncluded = FALSE;
495 496 497 498 499 500 501 502 503 504 505
    if (!readInputFile(absName,fs->fileBuf))
    { // error
      //printf("  error reading\n");
      delete fs;
      fs=0;
    }
    else
    {
      fs->oldFileBuf    = g_inputBuf;
      fs->oldFileBufPos = g_inputBufPos;
    }
506
  }
507
  return fs;
508 509
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
510
static FileState *findFile(const char *fileName,bool localInclude,bool &alreadyIncluded)
511
{
512 513
  //printf("** findFile(%s,%d) g_yyFileName=%s\n",fileName,localInclude,g_yyFileName.data());
  if (localInclude && !g_yyFileName.isEmpty())
514
  {
515 516
    QFileInfo fi(g_yyFileName);
    if (fi.exists())
517
    {
518
      QCString absName = QCString(fi.dirPath(TRUE).data())+"/"+fileName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
519
      FileState *fs = checkAndOpenFile(absName,alreadyIncluded);
520
      if (fs)
521
      {
522
	setFileName(absName);
523
	g_yyLineNr=1;
524
	return fs;
525
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
526 527 528 529
      else if (alreadyIncluded)
      {
	return 0;
      }
530 531
    }
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
532
  if (g_pathList==0) 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
533 534 535
  {
    return 0;
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
536
  char *s=g_pathList->first();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
537 538
  while (s)
  {
539
    QCString absName = (QCString)s+"/"+fileName;
540
    //printf("  Looking for %s in %s\n",fileName,s);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
541
    FileState *fs = checkAndOpenFile(absName,alreadyIncluded);
542
    if (fs)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
543
    {
544
      setFileName(absName);
545
      g_yyLineNr=1;
546
      //printf("  -> found it\n");
547
      return fs;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
548
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
549 550 551 552
    else if (alreadyIncluded)
    {
      return 0;
    }
553

Dimitri van Heesch's avatar
Dimitri van Heesch committed
554
    s=g_pathList->next();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
555 556 557 558
  } 
  return 0;
}

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
static QCString extractTrailingComment(const char *s)
{
  if (s==0) return "";
  int i=strlen(s)-1;
  while (i>=0)
  {
    char c=s[i];
    switch (c)
    {
      case '/':
	{
	  i--;
	  if (i>=0 && s[i]=='*') // end of a comment block
	  {
	    i--;
	    while (i>0 && !(s[i-1]=='/' && s[i]=='*')) i--;
575 576 577 578 579 580 581
	    if (i==0) 
	    {
	      i++;
	    }
	    // only /*!< or /**< are treated as a comment for the macro name,
	    // otherwise the comment is treated as part of the macro definition
	    return ((s[i+1]=='*' || s[i+1]=='!') && s[i+2]=='<') ? &s[i-1] : ""; 
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	  }
	  else
	  {
	    return "";
	  }
	} 
	break;
	// whitespace or line-continuation
      case ' ':
      case '\t': 
      case '\r':
      case '\n':
      case '\\':
	break;
      default:
	return "";
    }
    i--;
  }
  return "";
}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
603

604 605 606 607
static int getNextChar(const QCString &expr,QCString *rest,uint &pos);
static int getCurrentChar(const QCString &expr,QCString *rest,uint pos);
static void unputChar(const QCString &expr,QCString *rest,uint &pos,char c);
static void expandExpression(QCString &expr,QCString *rest,int pos);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
608

609
static QCString stringize(const QCString &s)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
610
{
611
  QCString result;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
  uint i=0;
  bool inString=FALSE;
  bool inChar=FALSE;
  char c,pc;
  while (i<s.length())
  {
    if (!inString && !inChar)
    {
      while (i<s.length() && !inString && !inChar)
      {
	c=s.at(i++);
	if (c=='"')
	{
	  result+="\\\"";
	  inString=TRUE;
	}
	else if (c=='\'')
	{
	  result+=c;
	  inChar=TRUE;
	}
	else
	{
	  result+=c;
	}
      }
    }
    else if (inChar)
    {
      while (i<s.length() && inChar)
      {
	c=s.at(i++);
	if (c=='\'')
	{
	  result+='\'';
	  inChar=FALSE;
	}
	else if (c=='\\')
	{
	  result+="\\\\";
	}
	else
	{
	  result+=c;
	}
      }
    }
    else
    {
      pc=0;
      while (i<s.length() && inString)
      {
	char c=s.at(i++);
	if (c=='"') 
	{
	  result+="\\\"";
	  inString= pc=='\\';
	}
	else if (c=='\\')
	  result+="\\\\";
	else
	  result+=c;
	pc=c;
      }
    }
  }
  //printf("stringize `%s'->`%s'\n",s.data(),result.data());
  return result;
}

/*! Execute all ## operators in expr. 
 * If the macro name before or after the operator contains a no-rescan 
 * marker (@-) then this is removed (before the concatenated macro name
 * may be expanded again.
 */
687
static void processConcatOperators(QCString &expr)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
688
{
689
  //printf("processConcatOperators: in=`%s'\n",expr.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
690 691
  QRegExp r("[ \\t\\n]*##[ \\t\\n]*"); 
  int l,n,i=0;
692
  if (expr.isEmpty()) return;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
693 694
  while ((n=r.match(expr,i,&l))!=-1)
  {
695
    //printf("Match: `%s'\n",expr.data()+i);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
696 697 698 699 700
    if (n+l+1<(int)expr.length() && expr.at(n+l)=='@' && expr.at(n+l+1)=='-')
    {
      // remove no-rescan marker after ID
      l+=2;
    }
701
    //printf("found `%s'\n",expr.mid(n,l).data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
702 703 704 705 706 707 708 709 710 711
    // remove the ## operator and the surrounding whitespace
    expr=expr.left(n)+expr.right(expr.length()-n-l);
    int k=n-1;
    while (k>=0 && isId(expr.at(k))) k--; 
    if (k>0 && expr.at(k)=='-' && expr.at(k-1)=='@')
    {
      // remove no-rescan marker before ID
      expr=expr.left(k-1)+expr.right(expr.length()-k-1);
      n-=2;
    }
712
    i=n;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
713
  }
714
  //printf("processConcatOperators: out=`%s'\n",expr.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
715 716
}

717 718 719 720 721 722
static void yyunput (int c,char *buf_ptr  );
static void returnCharToStream(char c)
{
  unput(c);
}

723 724 725
static inline void addTillEndOfString(const QCString &expr,QCString *rest,
                                       uint &pos,char term,QCString &arg)
{
726
  int cc;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
727
  while ((cc=getNextChar(expr,rest,pos))!=EOF && cc!=0)
728
  {
729
    if (cc=='\\') arg+=(char)cc,cc=getNextChar(expr,rest,pos);
730
    else if (cc==term) return;
731
    arg+=(char)cc;
732 733 734
  }
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
735 736 737
/*! replaces the function macro \a def whose argument list starts at
 * \a pos in expression \a expr. 
 * Notice that this routine may scan beyond the \a expr string if needed.
738
 * In that case the characters will be read from the input file.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
739 740 741
 * The replacement string will be returned in \a result and the 
 * length of the (unexpanded) argument list is stored in \a len.
 */ 
742
static bool replaceFunctionMacro(const QCString &expr,QCString *rest,int pos,int &len,const Define *def,QCString &result)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
743
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
744
  //printf("replaceFunctionMacro(expr=%s,rest=%s,pos=%d,def=%s) level=%d\n",expr.data(),rest ? rest->data() : 0,pos,def->name.data(),g_level);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
745 746 747 748
  uint j=pos;
  len=0;
  result.resize(0);
  int cc;
749
  while ((cc=getCurrentChar(expr,rest,j))!=EOF && isspace(cc)) 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
750 751 752 753 754 755 756 757 758 759 760
  { 
    len++; 
    getNextChar(expr,rest,j); 
  }
  if (cc!='(') 
  { 
    unputChar(expr,rest,j,' '); 
    return FALSE; 
  }
  getNextChar(expr,rest,j); // eat the `(' character

761
  QDict<QCString> argTable;  // list of arguments
Dimitri van Heesch's avatar
Dimitri van Heesch committed
762
  argTable.setAutoDelete(TRUE);
763
  QCString arg;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
764 765
  int argCount=0;
  bool done=FALSE;
766
  
767
  // PHASE 1: read the macro arguments
768 769
  if (def->nargs==0)
  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
770
    while ((cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
771 772 773 774 775 776
    {
      char c = (char)cc;
      if (c==')') break;
    }
  }
  else
Dimitri van Heesch's avatar
Dimitri van Heesch committed
777
  {
778
    while (!done && (argCount<def->nargs || def->varArgs) && 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
779
	((cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
780
	  )
Dimitri van Heesch's avatar
Dimitri van Heesch committed
781
    {
782 783
      char c=(char)cc;
      if (c=='(') // argument is a function => search for matching )
Dimitri van Heesch's avatar
Dimitri van Heesch committed
784
      {
785 786
	int level=1;
	arg+=c;
787
	//char term='\0';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
788
	while ((cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
789
	{
790
	  char c=(char)cc;
791
	  //printf("processing %c: term=%c (%d)\n",c,term,term);
792 793
	  if (c=='\'' || c=='\"') // skip ('s and )'s inside strings
	  {
794 795
	    arg+=c;
	    addTillEndOfString(expr,rest,j,c,arg);
796
	  }
797
	  if (c==')')
798 799 800 801
	  {
	    level--;
	    arg+=c;
	    if (level==0) break;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
802
	  }
803
	  else if (c=='(')
804 805 806 807 808 809
	  {
	    level++;
	    arg+=c;
	  }
	  else
	    arg+=c;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
810
	}
811 812 813 814
      }
      else if (c==')' || c==',') // last or next argument found
      {
	if (c==',' && argCount==def->nargs-1 && def->varArgs)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
815
	{
816 817
	  arg=arg.stripWhiteSpace();
	  arg+=',';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
818
	}
819
	else
Dimitri van Heesch's avatar
Dimitri van Heesch committed
820
	{
821 822 823 824 825 826 827 828 829 830
	  QCString argKey;
	  argKey.sprintf("@%d",argCount++); // key name
	  arg=arg.stripWhiteSpace();
	  // add argument to the lookup table
	  argTable.insert(argKey, new QCString(arg));
	  arg.resize(0);
	  if (c==')') // end of the argument list
	  {
	    done=TRUE;
	  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
831
	}
832 833
      } 
      else if (c=='\"') // append literal strings
Dimitri van Heesch's avatar
Dimitri van Heesch committed
834
      {
835 836
	arg+=c; 
	bool found=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
837
	while (!found && (cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
838
	{
839 840 841 842 843
	  found = cc=='"';
	  if (cc=='\\')
	  {
	    c=(char)cc;	  
	    arg+=c;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
844
	    if ((cc=getNextChar(expr,rest,j))==EOF || cc==0) break;
845
	  }
846 847
	  c=(char)cc;	  
	  arg+=c;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
848 849
	}
      }
850
      else if (c=='\'') // append literal characters
Dimitri van Heesch's avatar
Dimitri van Heesch committed
851 852
      {
	arg+=c;
853
	bool found=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
854
	while (!found && (cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
855
	{
856 857 858 859 860
	  found = cc=='\'';
	  if (cc=='\\')
	  {
	    c=(char)cc;	  
	    arg+=c;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
861
	    if ((cc=getNextChar(expr,rest,j))==EOF || cc==0) break;
862
	  }
863 864 865 866 867
	  c=(char)cc;
	  arg+=c;
	}
      }	    
      else // append other characters
Dimitri van Heesch's avatar
Dimitri van Heesch committed
868 869 870 871 872 873
      {
	arg+=c;
      }
    }
  }

874
  // PHASE 2: apply the macro function
Dimitri van Heesch's avatar
Dimitri van Heesch committed
875 876 877 878 879
  if (argCount==def->nargs || 
      (argCount>def->nargs && def->varArgs)) // matching parameters lists
  {
    uint k=0;
    // substitution of all formal arguments
880 881
    QCString resExpr;
    const QCString d=def->definition.stripWhiteSpace();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
882
    //printf("Macro definition: %s\n",d.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    bool inString=FALSE;
    while (k<d.length())
    {
      if (d.at(k)=='@') // maybe a marker, otherwise an escaped @
      {
	if (d.at(k+1)=='@') // escaped @ => copy it (is unescaped later)
	{
	  k+=2;
	  resExpr+="@@"; // we unescape these later
	}
	else if (d.at(k+1)=='-') // no-rescan marker
	{
	  k+=2;
	  resExpr+="@-";
	}
	else // argument marker => read the argument number
	{
900 901
	  QCString key="@";
	  QCString *subst=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
	  bool hash=FALSE;
	  int l=k-1;
	  // search for ## backward
	  if (l>=0 && d.at(l)=='"') l--;
	  while (l>=0 && d.at(l)==' ') l--;
	  if (l>0 && d.at(l)=='#' && d.at(l-1)=='#') hash=TRUE;
	  k++;
	  // scan the number
	  while (k<d.length() && d.at(k)>='0' && d.at(k)<='9') key+=d.at(k++);
	  if (!hash) 
	  {
	    // search for ## forward
	    l=k;
	    if (l<(int)d.length() && d.at(l)=='"') l++;
	    while (l<(int)d.length() && d.at(l)==' ') l++;
	    if (l<(int)d.length()-1 && d.at(l)=='#' && d.at(l+1)=='#') hash=TRUE;
	  }
919
	  //printf("request key %s result %s\n",key.data(),argTable[key]->data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
920 921
	  if (key.length()>1 && (subst=argTable[key])) 
	  {
922
	    QCString substArg=*subst;
923
	    //printf("substArg=`%s'\n",substArg.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
924 925 926 927 928 929 930 931 932 933 934 935 936
	    // only if no ## operator is before or after the argument
	    // marker we do macro expansion.
	    if (!hash) expandExpression(substArg,0,0);
	    if (inString)
	    {
	      //printf("`%s'=stringize(`%s')\n",stringize(*subst).data(),subst->data());

	      // if the marker is inside a string (because a # was put 
	      // before the macro name) we must escape " and \ characters
	      resExpr+=stringize(substArg);
	    }
	    else
	    {
937 938 939 940 941
	      if (hash && substArg.isEmpty())
	      {
		resExpr+="@E"; // empty argument will be remove later on
	      }
	      else if (g_nospaces)
942 943 944 945 946 947 948
	      {
	        resExpr+=substArg;
	      }
	      else
	      {
	        resExpr+=" "+substArg+" ";
	      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
949 950 951 952 953 954 955 956 957 958
	    }
	  }
	}
      }
      else // no marker, just copy
      {
	if (!inString && d.at(k)=='\"') 
	{
	  inString=TRUE; // entering a literal string
	}
959
	else if (inString && d.at(k)=='\"' && (d.at(k-1)!='\\' || d.at(k-2)=='\\'))
Dimitri van Heesch's avatar
Dimitri van Heesch committed
960 961 962 963 964 965 966 967 968 969 970 971
	{
	  inString=FALSE; // leaving a literal string
	}
	resExpr+=d.at(k++);
      }
    }
    len=j-pos;
    result=resExpr;
    //printf("result after substitution `%s' expr=`%s'\n",
    //       result.data(),expr.mid(pos,len).data());
    return TRUE;
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
972
  return FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
973 974 975 976 977 978 979
}


/*! returns the next identifier in string \a expr by starting at position \a p.
 * The position of the identifier is returned (or -1 if nothing is found)
 * and \a l is its length. Any quoted strings are skipping during the search.
 */
980
static int getNextId(const QCString &expr,int p,int *l)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
981 982 983 984 985
{
  int n;
  while (p<(int)expr.length())
  {
    char c=expr.at(p++);
986 987 988 989 990
    if (isdigit(c)) // skip number
    {
      while (p<(int)expr.length() && isId(expr.at(p))) p++;
    }
    else if (isalpha(c) || c=='_') // read id
Dimitri van Heesch's avatar
Dimitri van Heesch committed
991 992
    {
      n=p-1;
993
      while (p<(int)expr.length() && isId(expr.at(p))) p++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
994 995 996 997 998
      *l=p-n;
      return n; 
    }
    else if (c=='"') // skip string
    {
999
      char ppc=0,pc=c;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1000
      if (p<(int)expr.length()) c=expr.at(p);
1001 1002
      while (p<(int)expr.length() && (c!='"' || (pc=='\\' && ppc!='\\'))) 
	// continue as long as no " is found, but ignoring \", but not \\"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1003
      {
1004
	ppc=pc;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1005 1006 1007 1008
	pc=c;
	c=expr.at(p);
	p++;
      }
1009
      if (p<(int)expr.length()) ++p; // skip closing quote
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1010
    }
1011 1012
    else if (c=='/') // skip C Comment
    {
1013
      //printf("Found C comment at p=%d\n",p);
1014 1015 1016
      char pc=c;
      if (p<(int)expr.length()) 
      {
1017
	c=expr.at(p);
1018 1019
        if (c=='*')  // Start of C comment
        { 
1020
	  p++;
1021 1022 1023
  	  while (p<(int)expr.length() && !(pc=='*' && c=='/'))
	  {
	    pc=c;
1024
	    c=expr.at(p++);
1025 1026 1027
	  }
        }
      }
1028
      //printf("Found end of C comment at p=%d\n",p);
1029
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  }
  return -1;
}

/*! preforms recursive macro expansion on the string \a expr
 *  starting at position \a pos.
 *  May read additional characters from the input while re-scanning!
 *  If \a expandAll is \c TRUE then all macros in the expression are
 *  expanded, otherwise only the first is expanded.
 */
1040
static void expandExpression(QCString &expr,QCString *rest,int pos)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1041 1042
{
  //printf("expandExpression(%s,%s)\n",expr.data(),rest ? rest->data() : 0);
1043 1044
  QCString macroName;
  QCString expMacro;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1045
  bool definedTest=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1046 1047 1048 1049 1050
  int i=pos,l,p,len;
  while ((p=getNextId(expr,i,&l))!=-1) // search for an macro name
  {
    bool replaced=FALSE;
    macroName=expr.mid(p,l);
1051
    //printf("macroName=%s\n",macroName.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1052 1053
    if (p<2 || !(expr.at(p-2)=='@' && expr.at(p-1)=='-')) // no-rescan marker?
    {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1054
      if (g_expandedDict->find(macroName)==0) // expand macro
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1055
      {
1056
	Define *def=DefineManager::instance().isDefined(macroName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1057 1058 1059 1060 1061 1062 1063 1064
	if (definedTest) // macro name was found after defined 
	{
	  if (def) expMacro = " 1 "; else expMacro = " 0 ";
	  replaced=TRUE;
	  len=l;
	  definedTest=FALSE;
	}
	else if (def && def->nargs==-1) // simple macro
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1065 1066
	{
	  // substitute the definition of the macro
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1067
	  //printf("macro `%s'->`%s'\n",macroName.data(),def->definition.data());
1068 1069 1070 1071 1072 1073 1074 1075 1076
	  if (g_nospaces)
	  {
	    expMacro=def->definition.stripWhiteSpace();
	  }
	  else
	  {
	    expMacro=" "+def->definition.stripWhiteSpace()+" ";
	  }
	  //expMacro=def->definition.stripWhiteSpace();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1077 1078 1079 1080
	  replaced=TRUE;
	  len=l;
	  //printf("simple macro expansion=`%s'->`%s'\n",macroName.data(),expMacro.data());
	}
1081
	else if (def && def->nargs>=0) // function macro
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1082 1083 1084 1085
	{
	  replaced=replaceFunctionMacro(expr,rest,p+l,len,def,expMacro);
	  len+=l;
	}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1086 1087 1088 1089 1090
        else if (macroName=="defined")
        {
  	  //printf("found defined inside macro definition '%s'\n",expr.right(expr.length()-p).data());
	  definedTest=TRUE;
        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1091 1092 1093

	if (replaced) // expand the macro and rescan the expression
	{
1094
	    
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1095
	  //printf("replacing `%s'->`%s'\n",expr.mid(p,len).data(),expMacro.data());
1096 1097
	  QCString resultExpr=expMacro;
	  QCString restExpr=expr.right(expr.length()-len-p);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1098
	  processConcatOperators(resultExpr);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1099
	  if (def && !def->nonRecursive)
1100
	  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1101
	    g_expandedDict->insert(macroName,def);
1102
	    expandExpression(resultExpr,&restExpr,0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1103
	    g_expandedDict->remove(macroName);
1104
	  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	  expr=expr.left(p)+resultExpr+restExpr;
	  i=p;
	  //printf("new expression: %s\n",expr.data());
	}
	else // move to the next macro name
	{
	  //printf("moving to the next macro old=%d new=%d\n",i,p+l);
	  i=p+l;
	}
      }
      else // move to the next macro name
      {
	expr=expr.left(p)+"@-"+expr.right(expr.length()-p);
	//printf("macro already expanded, moving to the next macro expr=%s\n",expr.data());
	i=p+l+2;
	//i=p+l;
      }
    }
    else // no re-scan marker found, skip the macro name
    {
      //printf("skipping marked macro\n");
      i=p+l;
    }
  }
}

1131 1132
/*! replaces all occurrences of @@@@ in \a s by @@
 *  and removes all occurrences of @@E.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1133 1134
 *  All identifiers found are replaced by 0L
 */
1135
QCString removeIdsAndMarkers(const char *s)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1136 1137 1138 1139 1140
{
  //printf("removeIdsAndMarkers(%s)\n",s);
  const char *p=s;
  char c;
  bool inNum=FALSE;
1141
  QCString result;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1142 1143 1144 1145
  if (p)
  {
    while ((c=*p))
    {
1146
      if (c=='@') // replace @@ with @ and remove @E
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1147 1148 1149 1150 1151
      {
	if (*(p+1)=='@')
	{
	  result+=c; 
	}
1152 1153 1154 1155
	else if (*(p+1)=='E')
	{
	  // skip
	}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1156 1157
	p+=2;
      }
1158
      else if (isdigit(c)) // number
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1159 1160 1161 1162 1163
      {
	result+=c;
	p++;
        inNum=TRUE;	
      }
1164
      else if (c=='d' && !inNum) // identifier starting with a `d'
1165
      {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1166
	if (qstrncmp(p,"defined ",8)==0 || qstrncmp(p,"defined(",8)==0) 
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	           // defined keyword
	{
	  p+=7; // skip defined
	}
	else
	{
	  result+="0L";
	  p++;
	  while ((c=*p) && isId(c)) p++;
	}
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1178 1179 1180 1181 1182
      else if ((isalpha(c) || c=='_') && !inNum) // replace identifier with 0L
      {
	result+="0L";
	p++;
	while ((c=*p) && isId(c)) p++;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	if (*p=='(') // undefined function macro
	{
	  p++;
	  int count=1;
	  while ((c=*p++))
	  {
	    if (c=='(') count++;
	    else if (c==')')
	    {
	      count--;
	      if (count==0) break;
	    }
	    else if (c=='/')
	    {
	      char pc=c;
	      c=*++p;
	      if (c=='*') // start of C comment
	      {
		while (*p && !(pc=='*' && c=='/')) // search end of comment
		{
		  pc=c;
		  c=*++p;
		}
		p++;
	      }
	    }
	  }
	}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1211
      }
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
      else if (c=='/') // skip C comments
      {
	char pc=c;
	c=*++p;
	if (c=='*') // start of C comment
	{ 
	  while (*p && !(pc=='*' && c=='/')) // search end of comment
	  {
	    pc=c;
	    c=*++p;
	  }
	  p++;
	}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1225 1226 1227 1228 1229
	else // oops, not comment but division
	{
	  result+=pc;
	  goto nextChar;
	}
1230
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1231 1232
      else 
      {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1233
nextChar:
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1234 1235
	result+=c;
	char lc=tolower(c);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1236
	if (!isId(lc) && lc!='.' /*&& lc!='-' && lc!='+'*/) inNum=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1237 1238 1239 1240
	p++;
      }
    }
  }
1241
  //printf("removeIdsAndMarkers(%s)=%s\n",s,result.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1242 1243 1244 1245 1246 1247 1248
  return result;
}

/*! replaces all occurrences of @@ in \a s by @
 *  \par assumption: 
 *   \a s only contains pairs of @@'s
 */
1249
QCString removeMarkers(const char *s)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1250 1251 1252
{
  const char *p=s;
  char c;
1253
  QCString result;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1254 1255 1256 1257
  if (p)
  {
    while ((c=*p))
    {
1258
      switch(c)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1259
      {
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	case '@': // replace @@ with @
	  {
	    if (*(p+1)=='@')
	    {
	      result+=c; 
	    }
	    p+=2;
	  }
	  break;
	case '/': // skip C comments
1270 1271
	  {
	    result+=c;
1272
	    char pc=c;
1273
	    c=*++p;
1274 1275 1276 1277
	    if (c=='*') // start of C comment
	    { 
	      while (*p && !(pc=='*' && c=='/')) // search end of comment
	      {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1278 1279 1280 1281
		if (*p=='@' && *(p+1)=='@') 
		  result+=c,p++;
		else 
		  result+=c;
1282 1283 1284
		pc=c;
		c=*++p;
	      }
1285
	      if (*p) result+=c,p++;
1286
	    }
1287
	  }
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	  break;
	case '"': // skip string literals
	  {
	    result+=c;
	    char pc=c;
	    c=*++p;
	    while (*p && (c!='"' || pc=='\\')) // no end quote
	    {
	      result+=c;
	      c=*++p;
	    }
1299
	    if (*p) result+=c,p++; 
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	  }
	  break;
	case '\'': // skip char literals
	  {
	    result+=c;
	    char pc=c;
	    c=*++p;
	    while (*p && (c!='\'' || pc=='\\')) // no end quote
	    {
	      result+=c;
	      c=*++p;
	    }
1312
	    if (*p) result+=c,p++; 
1313 1314 1315 1316 1317 1318 1319 1320
	  }
	  break;
	default:
	  {
	    result+=c;
	    p++;
	  }
	  break;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1321 1322 1323
      }
    }
  }
1324
  //printf("RemoveMarkers(%s)=%s\n",s,result.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1325 1326 1327 1328 1329 1330 1331
  return result;
}

/*! compute the value of the expression in string \a expr.
 *  If needed the function may read additional characters from the input.
 */

1332
bool computeExpression(const QCString &expr)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1333
{
1334
  QCString e=expr;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1335
  expandExpression(e,0,0);
1336
  //printf("after expansion `%s'\n",e.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1337
  e = removeIdsAndMarkers(e);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1338
  if (e.isEmpty()) return FALSE;
1339
  //printf("parsing `%s'\n",e.data());
1340
  return parseCppExpression(g_yyFileName,g_yyLineNr,e);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1341 1342 1343 1344 1345 1346
}

/*! expands the macro definition in \a name
 *  If needed the function may read additional characters from the input
 */

1347
QCString expandMacro(const QCString &name)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1348
{
1349
  QCString n=name;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1350 1351 1352 1353 1354 1355 1356 1357 1358
  expandExpression(n,0,0);
  n=removeMarkers(n);
  //printf("expandMacro `%s'->`%s'\n",name.data(),n.data());
  return n;
}

Define *newDefine()
{
  Define *def=new Define;
1359
  def->name       = g_defName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1360
  def->definition = g_defText.stripWhiteSpace();
1361 1362 1363
  def->nargs      = g_defArgs;
  def->fileName   = g_yyFileName; 
  def->fileDef    = g_yyFileDef;
1364 1365
  def->lineNr     = g_yyLineNr-g_yyMLines;
  def->columnNr   = g_yyColNr;
1366
  def->varArgs    = g_defVarArgs;
1367 1368
  //printf("newDefine: %s %s file: %s\n",def->name.data(),def->definition.data(),
  //    def->fileDef ? def->fileDef->name().data() : def->fileName.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1369
  //printf("newDefine: `%s'->`%s'\n",def->name.data(),def->definition.data());
1370
  if (!def->name.isEmpty() && Doxygen::expandAsDefinedDict[def->name])
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1371 1372 1373
  {
    def->isPredefined=TRUE;
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1374 1375 1376 1377 1378
  return def;
}

void addDefine()
{
1379
  if (g_skip) return; // do not add this define as it is inside a 
1380 1381
                      // conditional section (cond command) that is disabled.
  if (!Doxygen::gatherDefines) return;
1382

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1383
  //printf("addDefine %s %s\n",g_defName.data(),g_defArgsStr.data());
1384 1385
  //ArgumentList *al = new ArgumentList;
  //stringToArgumentList(g_defArgsStr,al);
1386
  MemberDef *md=new MemberDef(
1387
      g_yyFileName,g_yyLineNr-g_yyMLines,g_yyColNr,
1388
      "#define",g_defName,g_defArgsStr,0,
1389
      Public,Normal,FALSE,Member,MemberType_Define,0,0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1390 1391 1392 1393 1394 1395 1396 1397
  if (!g_defArgsStr.isEmpty())
  {
    ArgumentList *argList = new ArgumentList;
    //printf("addDefine() g_defName=`%s' g_defArgsStr=`%s'\n",g_defName.data(),g_defArgsStr.data());
    stringToArgumentList(g_defArgsStr,argList);
    md->setArgumentList(argList);
  }
  //printf("Setting initializer for `%s' to `%s'\n",g_defName.data(),g_defText.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1398 1399 1400
  int l=g_defLitText.find('\n');
  if (l>0 && g_defLitText.left(l).stripWhiteSpace()=="\\")
  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1401
    // strip first line if it only contains a slash
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1402 1403
    g_defLitText = g_defLitText.right(g_defLitText.length()-l-1);
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1404
  else if (l>0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1405 1406 1407 1408 1409 1410 1411 1412
  {
    // align the items on the first line with the items on the second line
    int k=l+1;
    const char *p=g_defLitText.data()+k;
    char c;
    while ((c=*p++) && (c==' ' || c=='\t')) k++;
    g_defLitText=g_defLitText.mid(l+1,k-l-1)+g_defLitText.stripWhiteSpace();
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1413
  md->setInitializer(g_defLitText.stripWhiteSpace());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1414

1415
  //printf("pre.l: md->setFileDef(%p)\n",g_inputFileDef);
1416
  md->setFileDef(g_inputFileDef);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1417
  md->setDefinition("#define "+g_defName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1418

1419
  MemberName *mn=Doxygen::functionNameSDict->find(g_defName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1420 1421
  if (mn==0)
  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1422
    mn = new MemberName(g_defName);
1423
    Doxygen::functionNameSDict->append(g_defName,mn);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1424 1425
  }
  mn->append(md);
1426 1427 1428 1429
  if (g_yyFileDef) 
  {
    g_yyFileDef->insertMember(md);
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1430

1431 1432
  //Define *d;
  //if ((d=defineDict[g_defName])==0) defineDict.insert(g_defName,newDefine()); 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1433 1434
}

1435
static inline void outputChar(char c)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1436
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1437
  if (g_includeStack.isEmpty() || g_curlyCount>0) g_outputBuf->addChar(c);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1438
}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1439

1440
static inline void outputArray(const char *a,int len)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1441
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1442
  if (g_includeStack.isEmpty() || g_curlyCount>0) g_outputBuf->addArray(a,len);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1443 1444
}

1445
static void readIncludeFile(const QCString &inc)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1446
{
1447
  static bool searchIncludes = Config_getBool("SEARCH_INCLUDES");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1448
  uint i=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1449

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1450
  // find the start of the include file name
1451
  while (i<inc.length() &&
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1452
         (inc.at(i)==' ' || inc.at(i)=='"' || inc.at(i)=='<')
1453
        ) i++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1454
  uint s=i;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1455

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1456 1457
  // was it a local include?
  bool localInclude = s>0 && inc.at(s-1)=='"';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1458

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1459
  // find the end of the include file name
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1460
  while (i<inc.length() && inc.at(i)!='"' && inc.at(i)!='>') i++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1461

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1462 1463
  if (s<inc.length() && i>s) // valid include file name found
  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1464
    // extract include path+name
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1465
    QCString incFileName=inc.mid(s,i-s).stripWhiteSpace();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1466

1467
    QCString dosExt = incFileName.right(4);
1468
    if (dosExt==".exe" || dosExt==".dll" || dosExt==".tlb")
1469 1470 1471 1472 1473
    {
      // skip imported binary files (e.g. M$ type libraries)
      return;
    }

1474
    QCString oldFileName = g_yyFileName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1475
    FileDef *oldFileDef  = g_yyFileDef;
1476
    int oldLineNr        = g_yyLineNr;
1477
    //printf("Searching for `%s'\n",incFileName.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1478

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    // absIncFileName avoids difficulties for incFileName starting with "../" (bug 641336)
    QCString absIncFileName = incFileName;
    {
      QFileInfo fi(g_yyFileName);
      if (fi.exists())
      {
	QCString absName = QCString(fi.dirPath(TRUE).data())+"/"+incFileName;
        QFileInfo fi2(absName);
        if (fi2.exists())
        {
1489
	  absIncFileName=fi2.absFilePath().utf8();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	}
	else if (searchIncludes) // search in INCLUDE_PATH as well
	{
	  QStrList &includePath = Config_getList("INCLUDE_PATH");
	  char *s=includePath.first();
	  while (s)
	  {
	    QFileInfo fi(s);
	    if (fi.exists() && fi.isDir())
	    {
1500
	      QCString absName = QCString(fi.absFilePath().utf8())+"/"+incFileName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1501 1502 1503 1504
	      //printf("trying absName=%s\n",absName.data());
	      QFileInfo fi2(absName);
	      if (fi2.exists())
	      {
1505
		absIncFileName=fi2.absFilePath().utf8();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1506 1507 1508 1509
		break;
	      }
	      //printf( "absIncFileName = %s\n", absIncFileName.data() );
	    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1510
	    s=includePath.next();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1511 1512 1513
	  }
	}
	//printf( "absIncFileName = %s\n", absIncFileName.data() );
1514 1515
      }
    }
1516 1517
    DefineManager::instance().addInclude(g_yyFileName,absIncFileName);
    DefineManager::instance().addFileToContext(absIncFileName);
1518

1519
    // findFile will overwrite g_yyFileDef if found
1520
    FileState *fs;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1521 1522 1523
    bool alreadyIncluded = FALSE;
    //printf("calling findFile(%s)\n",incFileName.data());
    if ((fs=findFile(incFileName,localInclude,alreadyIncluded))) // see if the include file can be found
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1524
    {
1525
      //printf("Found include file!\n");
1526 1527
      if (Debug::isFlagSet(Debug::Preprocessor))
      {
1528 1529 1530 1531
        for (i=0;i<g_includeStack.count();i++) 
        {
          Debug::print(Debug::Preprocessor,0,"  ");
        }
1532
        //msg("#include %s: parsing...\n",incFileName.data());
1533
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1534
      if (oldFileDef)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1535
      {
1536
        // add include dependency to the file in which the #include was found
1537
	bool ambig;
1538 1539 1540
	// change to absolute name for bug 641336 
        FileDef *incFd = findFileDef(Doxygen::inputNameDict,absIncFileName,ambig);
        oldFileDef->addIncludeDependency(ambig ? 0 : incFd,incFileName,localInclude,g_isImported,FALSE);
1541
        // add included by dependency
1542 1543
        if (g_yyFileDef)
        {
1544 1545
          //printf("Adding include dependency %s->%s\n",oldFileDef->name().data(),incFileName.data());
          g_yyFileDef->addIncludedByDependency(oldFileDef,oldFileDef->docName(),localInclude,g_isImported);
1546
        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1547
      }
1548 1549 1550 1551
      else if (g_inputFileDef)
      {
        g_inputFileDef->addIncludeDependency(0,absIncFileName,localInclude,g_isImported,TRUE);
      }
1552 1553 1554
      fs->bufState = YY_CURRENT_BUFFER;
      fs->lineNr   = oldLineNr;
      fs->fileName = oldFileName;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1555
      // push the state on the stack
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1556
      g_includeStack.push(fs);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1557
      // set the scanner to the include file
1558

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1559
      // Deal with file changes due to 
1560
      // #include's within { .. } blocks
1561
      QCString lineStr(g_yyFileName.length()+20);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1562 1563
      lineStr.sprintf("# 1 \"%s\" 1\n",g_yyFileName.data());
      outputArray(lineStr.data(),lineStr.length());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1564

1565
      DBG_CTX((stderr,"Switching to include file %s\n",incFileName.data()));
1566
      g_expectGuard=TRUE;
1567
      g_inputBuf   = &fs->fileBuf;
1568 1569
      g_inputBufPos=0;
      yy_switch_to_buffer(yy_create_buffer(0, YY_BUF_SIZE));
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1570
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1571 1572
    else
    {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1573 1574
      //printf("  calling findFile(%s) alreadyInc=%d\n",incFileName.data(),alreadyIncluded);
      if (oldFileDef)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1575
      {
1576
	bool ambig;
1577 1578 1579 1580 1581 1582 1583
	//QCString absPath = incFileName;
	//if (QDir::isRelativePath(incFileName))
	//{
	//  absPath = QDir::cleanDirPath(oldFileDef->getPath()+"/"+incFileName);
	//  //printf("%s + %s -> resolved path %s\n",oldFileDef->getPath().data(),incFileName.data(),absPath.data());
	//}

1584 1585
	// change to absolute name for bug 641336 
	FileDef *fd = findFileDef(Doxygen::inputNameDict,absIncFileName,ambig);
1586
	//printf("%s::findFileDef(%s)=%p\n",oldFileDef->name().data(),incFileName.data(),fd);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1587
	// add include dependency to the file in which the #include was found
1588
	oldFileDef->addIncludeDependency(ambig ? 0 : fd,incFileName,localInclude,g_isImported,FALSE);
1589 1590 1591
	// add included by dependency
        if (fd)
        {
1592
          //printf("Adding include dependency (2) %s->%s ambig=%d\n",oldFileDef->name().data(),fd->name().data(),ambig);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1593
          fd->addIncludedByDependency(oldFileDef,oldFileDef->docName(),localInclude,g_isImported);
1594
        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1595
      }
1596 1597 1598 1599
      else if (g_inputFileDef)
      {
        g_inputFileDef->addIncludeDependency(0,absIncFileName,localInclude,g_isImported,TRUE);
      }
1600 1601
      if (Debug::isFlagSet(Debug::Preprocessor))
      {
1602 1603
	if (alreadyIncluded)
	{
1604
          Debug::print(Debug::Preprocessor,0,"#include %s: already included! skipping...\n",incFileName.data());
1605 1606 1607
	}
	else
	{
1608
          Debug::print(Debug::Preprocessor,0,"#include %s: not found! skipping...\n",incFileName.data());
1609
	}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1610
        //printf("error: include file %s not found\n",yytext);
1611
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1612
      if (g_curlyCount>0 && !alreadyIncluded) // failed to find #include inside { ... }
1613
      {
1614
	warn(g_yyFileName,g_yyLineNr,"include file %s not found, perhaps you forgot to add its directory to INCLUDE_PATH?",incFileName.data());
1615
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1616 1617 1618 1619 1620 1621
    }
  }
}

/* ----------------------------------------------------------------- */

1622 1623
static void startCondSection(const char *sectId)
{
1624
  //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
1625 1626
  CondParser prs;
  bool expResult = prs.parse(g_yyFileName,g_yyLineNr,sectId);
1627
  g_condStack.push(new CondCtx(g_yyLineNr,sectId,g_skip));
1628
  if (!expResult)
1629
  {
1630
    g_skip=TRUE;
1631
  }
1632
  //printf("  expResult=%d skip=%d\n",expResult,g_skip);
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
}

static void endCondSection()
{
  if (g_condStack.isEmpty())
  {
    g_skip=FALSE;
  }
  else
  {
1643 1644
    CondCtx *ctx = g_condStack.pop();
    g_skip=ctx->skip;
1645
  }
1646
  //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
1647 1648
}

1649 1650 1651 1652 1653 1654 1655 1656 1657
static void forceEndCondSection()
{
  while (!g_condStack.isEmpty())
  {
    g_condStack.pop();
  }
  g_skip=FALSE;
}

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
static QCString escapeAt(const char *text)
{
  QCString result;
  if (text)
  {
    char c;
    const char *p=text;
    while ((c=*p++))
    {
      if (c=='@') result+="@@"; else result+=c;
    }
  }
  return result;
}

static char resolveTrigraph(char c)
{
  switch (c)
  {
    case '=': return '#';
    case '/': return '\\';
    case '\'': return '^';
    case '(': return '[';
    case ')': return ']';
    case '!': return '|';
    case '<': return '{';
    case '>': return '}';
    case '-': return '~';
  }
  return '?';
}
1689 1690 1691 1692

/* ----------------------------------------------------------------- */

#undef  YY_INPUT
1693 1694 1695 1696
#define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size);

static int yyread(char *buf,int max_size)
{
1697 1698 1699 1700 1701
  int bytesInBuf = g_inputBuf->curPos()-g_inputBufPos;
  int bytesToCopy = QMIN(max_size,bytesInBuf);
  memcpy(buf,g_inputBuf->data()+g_inputBufPos,bytesToCopy);
  g_inputBufPos+=bytesToCopy;
  return bytesToCopy;
1702 1703 1704 1705
}

/* ----------------------------------------------------------------- */

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1706 1707
%}

1708
ID	[a-z_A-Z\x80-\xFF][a-z_A-Z0-9\x80-\xFF]*
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1709 1710
B       [ \t]
BN	[ \t\r\n]
1711
CHARLIT   (("'"\\[0-7]{1,3}"'")|("'"\\."'")|("'"[^'\\\n]{1,4}"'"))
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1712

1713 1714
%option noyywrap

Dimitri van Heesch's avatar
Dimitri van Heesch committed
1715 1716 1717 1718
%x      Start
%x	Command
%x	SkipCommand
%x	SkipLine
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1719
%x	SkipString
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1720
%x	CopyLine
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1721
%x	CopyString
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1722 1723
%x      Include
%x      IncludeID
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1724
%x      EndImport
1725
%x	DefName
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1726 1727 1728 1729 1730 1731
%x	DefineArg
%x	DefineText
%x      SkipCPPBlock
%x      Ifdef
%x      Ifndef
%x	SkipCComment
1732
%x	ArgCopyCComment
1733
%x	CopyCComment
1734
%x	SkipVerbatim
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1735 1736 1737 1738 1739 1740 1741 1742
%x	SkipCPPComment
%x	RemoveCComment
%x	RemoveCPPComment
%x	Guard
%x	DefinedExpr1
%x	DefinedExpr2
%x	SkipDoubleQuote
%x	SkipSingleQuote
1743
%x	UndefName
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1744 1745 1746
%x	IgnoreLine
%x	FindDefineArgs
%x	ReadString
1747 1748
%x	CondLineC
%x	CondLineCpp
1749
%x      SkipCond
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1750 1751 1752 1753 1754 1755

%%

<*>\x06					
<*>\x00
<*>\r
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1756 1757 1758
<*>"??"[=/'()!<>-]			{ // Trigraph
  					  unput(resolveTrigraph(yytext[2]));
  					}
1759
<Start>^{B}*"#"				{ BEGIN(Command); g_yyColNr+=yyleng; g_yyMLines=0;}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1760
<Start>^{B}*/[^#]			{
1761
 					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1762 1763
  					  BEGIN(CopyLine); 
					}
1764
<Start>^{B}*[a-z_A-Z\x80-\xFF][a-z_A-Z0-9\x80-\xFF]+{B}*"("[^\)\n]*")"/{BN}{1,10}*[:{] { // constructors?
1765
					  int i;
1766
					  for (i=(int)yyleng-1;i>=0;i--)
1767 1768 1769 1770 1771
					  {
					    unput(yytext[i]);
					  }
					  BEGIN(CopyLine);
                                        }
1772 1773
<Start>^{B}*[_A-Z][_A-Z0-9]+{B}*"("[^\(\)\n]*"("[^\)\n]*")"[^\)\n]*")"{B}*\n | // function list macro with one (...) argument, e.g. for K_GLOBAL_STATIC_WITH_ARGS
<Start>^{B}*[_A-Z][_A-Z0-9]+{B}*"("[^\)\n]*")"{B}*\n { // function like macro
1774 1775 1776 1777 1778 1779
  					  static bool skipFuncMacros = Config_getBool("SKIP_FUNCTION_MACROS");
					  QCString name(yytext);
					  name=name.left(name.find('(')).stripWhiteSpace();

					  Define *def=0;
					  if (skipFuncMacros && 
1780
					      name!="Q_PROPERTY" &&
1781 1782 1783
					      !(
					         (g_includeStack.isEmpty() || g_curlyCount>0) &&
					         g_macroExpansion &&
1784 1785
					         (def=DefineManager::instance().isDefined(name)) &&
						 /*macroIsAccessible(def) &&*/
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
					         (!g_expandOnlyPredef || def->isPredefined)
					       )
					     )
					  {
					    outputChar('\n');
					    g_yyLineNr++;
					  }
					  else // don't skip
					  {
					    int i;
1796
					    for (i=(int)yyleng-1;i>=0;i--)
1797 1798 1799 1800 1801
					    {
					      unput(yytext[i]);
					    }
					    BEGIN(CopyLine);
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1802
  					}
1803
<CopyLine>"extern"{BN}{0,80}"\"C\""*{BN}{0,80}"{"	{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1804 1805
                                          QCString text=yytext;
  					  g_yyLineNr+=text.contains('\n');
1806
					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1807
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1808 1809
<CopyLine>"{"				{ // count brackets inside the main file
  					  if (g_includeStack.isEmpty()) 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1810
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1811
					    g_curlyCount++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1812
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1813 1814 1815
					  outputChar(*yytext);
  					}
<CopyLine>"}"				{ // count brackets inside the main file
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1816 1817
  					  if (g_includeStack.isEmpty() && g_curlyCount>0) 
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1818
					    g_curlyCount--;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1819
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1820 1821 1822
					  outputChar(*yytext);
  					}
<CopyLine>"'"\\[0-7]{1,3}"'"		{ 
1823
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1824 1825
					}
<CopyLine>"'"\\."'"			{ 
1826
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1827 1828
					}
<CopyLine>"'"."'"			{ 
1829
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1830 1831 1832 1833 1834
					}
<CopyLine>\"				{
					  outputChar(*yytext);
					  BEGIN( CopyString );
					}
1835
<CopyString>[^\"\\\r\n]+		{
1836
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1837 1838
					}
<CopyString>\\.				{
1839
					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1840 1841 1842 1843 1844
					}
<CopyString>\"				{
					  outputChar(*yytext);
					  BEGIN( CopyLine );
					}
1845 1846
<CopyLine>{ID}/{BN}{0,80}"("		{
  					  g_expectGuard = FALSE;
1847
  					  Define *def=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1848
					  //def=g_globalDefineDict->find(yytext);
1849
					  //def=DefineManager::instance().isDefined(yytext);
1850 1851 1852 1853 1854 1855
					  //printf("Search for define %s found=%d g_includeStack.isEmpty()=%d "
					  //       "g_curlyCount=%d g_macroExpansion=%d g_expandOnlyPredef=%d "
					  //	 "isPreDefined=%d\n",yytext,def ? 1 : 0,
					  //	 g_includeStack.isEmpty(),g_curlyCount,g_macroExpansion,g_expandOnlyPredef,
					  //	 def ? def->isPredefined : -1
					  //	);
1856
					  if ((g_includeStack.isEmpty() || g_curlyCount>0) &&
1857
					      g_macroExpansion &&
1858 1859
					      (def=DefineManager::instance().isDefined(yytext)) &&
				              /*(def->isPredefined || macroIsAccessible(def)) && */
1860
					      (!g_expandOnlyPredef || def->isPredefined)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1861 1862
					     )
					  {
1863
					    //printf("Found it! #args=%d\n",def->nargs);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1864 1865
					    g_roundCount=0;
					    g_defArgsStr=yytext;
1866 1867
					    if (def->nargs==-1) // no function macro
					    {
1868
					      QCString result = def->isPredefined ? def->definition : expandMacro(g_defArgsStr);
1869 1870 1871 1872
					      outputArray(result,result.length());
					    }
					    else // zero or more arguments
					    {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1873
					      g_findDefArgContext = CopyLine;
1874 1875
					      BEGIN(FindDefineArgs);
					    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1876 1877 1878
					  }
					  else
					  {
1879
					    outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1880 1881 1882 1883
					  }
  					}
<CopyLine>{ID}				{
                                          Define *def=0;
1884
  					  if ((g_includeStack.isEmpty() || g_curlyCount>0) && 
1885
					      g_macroExpansion &&
1886
					      (def=DefineManager::instance().isDefined(yytext)) &&
1887
					      def->nargs==-1 &&
1888
				              /*(def->isPredefined || macroIsAccessible(def)) &&*/
1889
					      (!g_expandOnlyPredef || def->isPredefined)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1890 1891
					     )
					  {
1892
					    QCString result=def->isPredefined ? def->definition : expandMacro(yytext); 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1893 1894 1895 1896
					    outputArray(result,result.length());
					  }
					  else
					  {
1897
					    outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1898 1899
					  }
  					}
1900 1901
<CopyLine>"\\"\r?/\n			{ // strip line continuation characters
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1902 1903 1904 1905 1906 1907
<CopyLine>.				{
  					  outputChar(*yytext);
  					}
<CopyLine>\n				{
  					  outputChar('\n');
					  BEGIN(Start);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1908
					  g_yyLineNr++;
1909
					  g_yyColNr=1;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1910 1911
  					}
<FindDefineArgs>"("			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1912 1913
  					  g_defArgsStr+='(';
  					  g_roundCount++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1914 1915
  					}
<FindDefineArgs>")"			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1916 1917 1918
  					  g_defArgsStr+=')';
					  g_roundCount--;
					  if (g_roundCount==0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1919
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1920
					    QCString result=expandMacro(g_defArgsStr);
1921
					    //printf("g_defArgsStr=`%s'->`%s'\n",g_defArgsStr.data(),result.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1922
					    if (g_findDefArgContext==CopyLine)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1923 1924
					    {
					      outputArray(result,result.length());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1925
					      BEGIN(g_findDefArgContext);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1926
					    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1927
					    else // g_findDefArgContext==IncludeID
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1928 1929
					    {
					      readIncludeFile(result);
1930
					      g_nospaces=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1931 1932 1933 1934 1935 1936
					      BEGIN(Start);
					    }
					  }
  					}
  /*
<FindDefineArgs>")"{B}*"("		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1937
  					  g_defArgsStr+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1938 1939
  					}
  */
1940 1941 1942
<FindDefineArgs>{CHARLIT}		{
  					  g_defArgsStr+=yytext;
  					}
1943 1944 1945 1946
<FindDefineArgs>"/*"[*]?                {
                                          g_defArgsStr+=yytext;
                                          BEGIN(ArgCopyCComment);
                                        }
1947
<FindDefineArgs>\"			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1948
  					  g_defArgsStr+=*yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1949 1950 1951
  					  BEGIN(ReadString);
  					}
<FindDefineArgs>\n			{
1952
                                          g_defArgsStr+=' ';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1953
  					  g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1954 1955 1956
					  outputChar('\n');
  					}
<FindDefineArgs>"@"			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1957
  					  g_defArgsStr+="@@";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1958 1959
  					}
<FindDefineArgs>.			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1960
  					  g_defArgsStr+=*yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1961
  					}
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
<ArgCopyCComment>[^*\n]+		{
					  g_defArgsStr+=yytext;
  					}
<ArgCopyCComment>"*/"			{
					  g_defArgsStr+=yytext;
  					  BEGIN(FindDefineArgs);
  					}
<ArgCopyCComment>\n			{ 
                                          g_defArgsStr+=' ';
  					  g_yyLineNr++;
					  outputChar('\n');
  					}
<ArgCopyCComment>.			{ 
                                          g_defArgsStr+=yytext;
                                        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1977
<ReadString>"\""			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1978
  					  g_defArgsStr+=*yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1979 1980
					  BEGIN(FindDefineArgs);
  					}
1981
<ReadString>"//"|"/*"			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1982
  					  g_defArgsStr+=yytext;
1983
  					}
1984 1985 1986
<ReadString>\\.				{
  					  g_defArgsStr+=yytext;
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1987
<ReadString>.				{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1988
  					  g_defArgsStr+=*yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1989
  					}
1990 1991
<Command>("include"|"import"){B}+/{ID}	{
  					  g_isImported = yytext[1]=='m';
1992
  					  if (g_macroExpansion) 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1993 1994
					    BEGIN(IncludeID);
  					}
1995 1996
<Command>("include"|"import"){B}*[<"]	{ 
  					  g_isImported = yytext[1]=='m';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
1997 1998 1999
					  char c[2];
					  c[0]=yytext[yyleng-1];c[1]='\0';
					  g_incName=c;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2000 2001
  					  BEGIN(Include); 
					}
2002
<Command>("cmake")?"define"{B}+		{ 
2003
  			                  //printf("!!!DefName\n"); 
2004
					  g_yyColNr+=yyleng;
2005
  					  BEGIN(DefName); 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2006 2007 2008
					}
<Command>"ifdef"/{B}*"("		{
  					  incrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2009
					  g_guardExpr.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2010 2011 2012 2013 2014
  					  BEGIN(DefinedExpr2);
  					}
<Command>"ifdef"/{B}+			{
  					  //printf("Pre.l: ifdef\n");
  					  incrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2015
					  g_guardExpr.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2016 2017 2018 2019
  					  BEGIN(DefinedExpr1);
  					}
<Command>"ifndef"/{B}*"("		{
  					  incrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2020
					  g_guardExpr="! ";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2021 2022 2023 2024
  					  BEGIN(DefinedExpr2);
					}
<Command>"ifndef"/{B}+			{
  					  incrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2025
					  g_guardExpr="! ";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2026 2027
  					  BEGIN(DefinedExpr1);
  					}
2028
<Command>"if"/[ \t(!]			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2029
  					  incrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2030
					  g_guardExpr.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2031 2032
					  BEGIN(Guard);
					}
2033
<Command>("elif"|"else"{B}*"if")/[ \t(!]	{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2034 2035
  					  if (!otherCaseDone())
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2036
					    g_guardExpr.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2037 2038 2039 2040
					    BEGIN(Guard);  
					  }
					  else
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2041
					    g_ifcount=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2042 2043 2044
					    BEGIN(SkipCPPBlock);
					  }
  					}
2045
<Command>"else"/[^a-z_A-Z0-9\x80-\xFF]		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2046
					  //printf("else g_levelGuard[%d]=%d\n",g_level-1,g_levelGuard[g_level-1]);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2047 2048
  					  if (otherCaseDone())
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2049
					    g_ifcount=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2050 2051 2052 2053 2054
					    BEGIN(SkipCPPBlock);
					  }
					  else
					  {
					    setCaseDone(TRUE);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2055
					    //g_levelGuard[g_level-1]=TRUE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2056 2057 2058
					  } 
  					}
<Command>"undef"{B}+			{
2059
  					  BEGIN(UndefName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2060
  					}
2061
<Command>("elif"|"else"{B}*"if")/[ \t(!]	{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2062 2063
  					  if (!otherCaseDone())
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2064
					    g_guardExpr.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2065 2066 2067
  					    BEGIN(Guard);
					  }
  					}
2068
<Command>"endif"/[^a-z_A-Z0-9\x80-\xFF]		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2069 2070 2071 2072 2073 2074
  					  //printf("Pre.l: #endif\n");
  					  decrLevel();
  					}
<Command,IgnoreLine>\n			{
  					  outputChar('\n');
  					  BEGIN(Start);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2075
					  g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2076
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2077 2078 2079
<Command>"pragma"{B}+"once"             {
                                          g_expectGuard = FALSE;
                                        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2080 2081 2082
<Command>{ID}				{ // unknown directive
					  BEGIN(IgnoreLine);
					}
2083 2084 2085 2086
<IgnoreLine>\\[\r]?\n			{
  					  outputChar('\n');
					  g_yyLineNr++;
					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2087
<IgnoreLine>.
2088
<Command>. {g_yyColNr+=yyleng;}
2089
<UndefName>{ID}				{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2090
  					  Define *def;
2091
  					  if ((def=DefineManager::instance().isDefined(yytext)) 
2092
					      /*&& !def->isPredefined*/
2093
					      && !def->nonRecursive
2094
					     )
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2095 2096 2097 2098 2099 2100
					  {
					    //printf("undefining %s\n",yytext);
					    def->undef=TRUE;
					  }
					  BEGIN(Start);
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2101
<Guard>\\[\r]?\n			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2102
  					  outputChar('\n');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2103 2104
  					  g_guardExpr+=' ';
					  g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2105 2106 2107 2108 2109 2110 2111
  					}
<Guard>"defined"/{B}*"("		{
    					  BEGIN(DefinedExpr2);
    					}
<Guard>"defined"/{B}+			{
    					  BEGIN(DefinedExpr1);
    					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2112
<Guard>{ID}				{ g_guardExpr+=yytext; }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2113
<Guard>.				{ g_guardExpr+=*yytext; }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2114
<Guard>\n				{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2115
  					  unput(*yytext);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2116
  					  //printf("Guard: `%s'\n",
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2117 2118
					  //    g_guardExpr.data());
					  bool guard=computeExpression(g_guardExpr);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2119
					  setCaseDone(guard);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2120
					  //printf("if g_levelGuard[%d]=%d\n",g_level-1,g_levelGuard[g_level-1]);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2121 2122 2123 2124 2125 2126
					  if (guard)
					  {
					    BEGIN(Start);
					  } 
					  else
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2127
					    g_ifcount=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2128 2129 2130
					    BEGIN(SkipCPPBlock);
					  }
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2131
<DefinedExpr1,DefinedExpr2>\\\n		{ g_yyLineNr++; outputChar('\n'); }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2132
<DefinedExpr1>{ID}			{
2133
  					  if (DefineManager::instance().isDefined(yytext) || g_guardName==yytext)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2134
					    g_guardExpr+=" 1L ";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2135
					  else
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2136 2137
					    g_guardExpr+=" 0L ";
					  g_lastGuardName=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2138 2139 2140
					  BEGIN(Guard);
  					}
<DefinedExpr2>{ID}			{
2141
  					  if (DefineManager::instance().isDefined(yytext) || g_guardName==yytext)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2142
					    g_guardExpr+=" 1L ";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2143
					  else
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2144
					    g_guardExpr+=" 0L ";
2145
					  g_lastGuardName=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2146 2147
  					}
<DefinedExpr1,DefinedExpr2>\n		{ // should not happen, handle anyway
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2148
                                          g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2149
  					  g_ifcount=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2150 2151 2152 2153 2154 2155 2156 2157
 					  BEGIN(SkipCPPBlock); 
					}
<DefinedExpr2>")"			{
  					  BEGIN(Guard);
  					}
<DefinedExpr1,DefinedExpr2>.
<SkipCPPBlock>^{B}*"#"			{ BEGIN(SkipCommand); }
<SkipCPPBlock>^{B}*/[^#]		{ BEGIN(SkipLine); }
2158
<SkipCPPBlock>\n			{ g_yyLineNr++; outputChar('\n'); }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2159
<SkipCPPBlock>.
2160
<SkipCommand>"if"(("n")?("def"))?/[ \t(!]	{ 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2161
  					  incrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2162 2163
                                          g_ifcount++; 
  					  //printf("#if... depth=%d\n",g_ifcount);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2164
					}
2165
<SkipCommand>"else"			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2166 2167
					  //printf("Else! g_ifcount=%d otherCaseDone=%d\n",g_ifcount,otherCaseDone());
  					  if (g_ifcount==0 && !otherCaseDone())
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2168 2169 2170 2171 2172 2173
					  {
					    setCaseDone(TRUE);
  					    //outputChar('\n');
					    BEGIN(Start);
					  }
  					}
2174
<SkipCommand>("elif"|"else"{B}*"if")/[ \t(!]		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2175
  					  if (g_ifcount==0) 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2176 2177 2178
					  {
  					    if (!otherCaseDone())
					    {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2179 2180
					      g_guardExpr.resize(0);
					      g_lastGuardName.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2181 2182 2183 2184
  					      BEGIN(Guard);
					    }
					    else
					    {
2185
					      BEGIN(SkipCPPBlock);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2186 2187 2188
					    }
					  }
					}
2189
<SkipCommand>"endif"			{ 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2190
					  g_expectGuard = FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2191
  					  decrLevel();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2192
  				          if (--g_ifcount<0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2193 2194 2195 2196 2197 2198 2199
  					  {
  					    //outputChar('\n');
					    BEGIN(Start);
					  }
					}
<SkipCommand>\n				{ 
  					  outputChar('\n');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2200
  					  g_yyLineNr++; 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2201 2202 2203 2204 2205 2206
					  BEGIN(SkipCPPBlock);
					}
<SkipCommand>{ID}			{ // unknown directive 
  					  BEGIN(SkipLine); 
					}
<SkipCommand>.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2207 2208 2209 2210 2211
<SkipLine>[^'"/\n]+			
<SkipLine>{CHARLIT}			{ }
<SkipLine>\"				{
					  BEGIN(SkipString);
					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2212
<SkipLine>.
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2213 2214 2215
<SkipString>"//"/[^\n]*                 { 
                                        }
<SkipLine,SkipCommand,SkipCPPBlock>"//"[^\n]* {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2216
  					  g_lastCPPContext=YY_START;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2217 2218
  					  BEGIN(RemoveCPPComment);
					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2219 2220 2221
<SkipString>"/*"/[^\n]*                 { 
                                        }
<SkipLine,SkipCommand,SkipCPPBlock>"/*"/[^\n]* {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2222
					  g_lastCContext=YY_START;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2223 2224 2225 2226
  					  BEGIN(RemoveCComment);
  					}
<SkipLine>\n				{
  					  outputChar('\n');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2227
					  g_yyLineNr++;  
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2228 2229
					  BEGIN(SkipCPPBlock);
					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2230 2231 2232 2233 2234 2235
<SkipString>[^"\\\n]+			{ }
<SkipString>\\.				{ }
<SkipString>\"				{
  					  BEGIN(SkipLine);
  					}
<SkipString>.				{ }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2236
<IncludeID>{ID}{B}*/"("			{
2237
  					  g_nospaces=TRUE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2238 2239 2240
				          g_roundCount=0;
					  g_defArgsStr=yytext;
					  g_findDefArgContext = IncludeID;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2241 2242 2243
					  BEGIN(FindDefineArgs);
					}
<IncludeID>{ID}				{
2244
  					  g_nospaces=TRUE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2245 2246 2247 2248
                                          readIncludeFile(expandMacro(yytext));
					  BEGIN(Start);
  					}
<Include>[^\">\n]+[\">]			{ 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2249 2250
					  g_incName+=yytext;
					  readIncludeFile(g_incName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2251 2252 2253 2254 2255 2256 2257 2258 2259
					  if (g_isImported)
					  {
					    BEGIN(EndImport);
					  }
					  else
					  {
					    BEGIN(Start);
					  }
  					}
2260
<EndImport>[^\\\n]*/\n			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2261 2262 2263 2264
  					  BEGIN(Start);
  					}
<EndImport>\\[\r]?"\n"			{ 
					  outputChar('\n');
2265
					  g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2266 2267
					}
<EndImport>.				{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2268
  					}
2269
<DefName>{ID}/("\\\n")*"("		{ // define with argument
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2270
  					  //printf("Define() `%s'\n",yytext);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2271 2272 2273 2274 2275 2276 2277 2278
					  g_argDict = new QDict<int>(31);
					  g_argDict->setAutoDelete(TRUE);
					  g_defArgs = 0; 
                                          g_defArgsStr.resize(0);
					  g_defText.resize(0);
					  g_defLitText.resize(0);
					  g_defName = yytext;
					  g_defVarArgs = FALSE;
2279
					  g_defExtraSpacing.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2280 2281
					  BEGIN(DefineArg);
  					}
2282
<DefName>{ID}{B}+"1"/[ \r\t\n]		{ // special case: define with 1 -> can be "guard"
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2283
  					  //printf("Define `%s'\n",yytext);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2284 2285 2286 2287
  					  g_argDict = 0;
					  g_defArgs = -1;
                                          g_defArgsStr.resize(0);
					  g_defName = yytext;
2288
					  g_defName = g_defName.left(g_defName.length()-1).stripWhiteSpace();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2289
					  g_defVarArgs = FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2290 2291
					  //printf("Guard check: %s!=%s || %d\n",
					  //    g_defName.data(),g_lastGuardName.data(),g_expectGuard);
2292
					  if (g_curlyCount>0 || g_defName!=g_lastGuardName || !g_expectGuard)
2293 2294 2295 2296 2297
					  { // define may appear in the output
					    QCString tmp=(QCString)"#define "+g_defName;
					    outputArray(tmp.data(),tmp.length());
					    g_quoteArg=FALSE;
					    g_insideComment=FALSE;
2298 2299 2300
					    g_lastGuardName.resize(0);
				            g_defText="1"; 
					    g_defLitText="1"; 
2301 2302 2303 2304
					    BEGIN(DefineText); 
					  }
					  else // define is a guard => hide
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2305
					    //printf("Found a guard %s\n",yytext);
2306 2307
					    g_defText.resize(0);
					    g_defLitText.resize(0);
2308 2309
					    BEGIN(Start);
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2310
					  g_expectGuard=FALSE;
2311
  					}
2312
<DefName>{ID}/{B}*"\n"			{ // empty define
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2313 2314 2315 2316 2317 2318 2319
  					  g_argDict = 0;
					  g_defArgs = -1;
					  g_defName = yytext;
                                          g_defArgsStr.resize(0);
					  g_defText.resize(0);
					  g_defLitText.resize(0);
					  g_defVarArgs = FALSE;
2320 2321
					  //printf("Guard check: %s!=%s || %d\n",
					  //    g_defName.data(),g_lastGuardName.data(),g_expectGuard);
2322
					  if (g_curlyCount>0 || g_defName!=g_lastGuardName || !g_expectGuard)
2323
					  { // define may appear in the output
2324
					    QCString tmp=(QCString)"#define "+g_defName;
2325
					    outputArray(tmp.data(),tmp.length());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2326
					    g_quoteArg=FALSE;
2327
					    g_insideComment=FALSE;
2328
					    if (g_insideCS) g_defText="1"; // for C#, use "1" as define text
2329 2330 2331 2332 2333
					    BEGIN(DefineText);
					  }
					  else // define is a guard => hide
					  {
					    //printf("Found a guard %s\n",yytext);
2334
					    g_guardName = yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2335
					    g_lastGuardName.resize(0);
2336 2337
					    BEGIN(Start);
					  }
2338
					  g_expectGuard=FALSE;
2339
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354
<DefName>{ID}/{B}*			{ // define with content
  					  //printf("Define `%s'\n",yytext);
  					  g_argDict = 0;
					  g_defArgs = -1;
                                          g_defArgsStr.resize(0);
					  g_defText.resize(0);
					  g_defLitText.resize(0);
					  g_defName = yytext;
					  g_defVarArgs = FALSE;
					  QCString tmp=(QCString)"#define "+g_defName+g_defArgsStr;
					  outputArray(tmp.data(),tmp.length());
					  g_quoteArg=FALSE;
					  g_insideComment=FALSE;
					  BEGIN(DefineText); 
  					}
2355 2356 2357 2358
<DefineArg>"\\\n"                       {
  					  g_defExtraSpacing+="\n";
					  g_yyLineNr++;
                                        }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2359 2360
<DefineArg>","{B}*			{ g_defArgsStr+=yytext; }
<DefineArg>"("{B}*                      { g_defArgsStr+=yytext; }
2361
<DefineArg>{B}*")"{B}*			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2362
                                          g_defArgsStr+=yytext; 
2363
					  QCString tmp=(QCString)"#define "+g_defName+g_defArgsStr+g_defExtraSpacing;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2364
					  outputArray(tmp.data(),tmp.length());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2365
					  g_quoteArg=FALSE;
2366
					  g_insideComment=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2367 2368
  					  BEGIN(DefineText);
  					}
2369 2370 2371 2372 2373 2374
<DefineArg>"..."			{ // Variadic macro
					  g_defVarArgs = TRUE;
					  g_defArgsStr+=yytext;
					  g_argDict->insert("__VA_ARGS__",new int(g_defArgs));
					  g_defArgs++;
  					}
2375
<DefineArg>{ID}{B}*("..."?)		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2376
  					  //printf("Define addArg(%s)\n",yytext);
2377
  					  QCString argName=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2378 2379
  					  g_defVarArgs = yytext[yyleng-1]=='.';
					  if (g_defVarArgs) // strip ellipsis
2380
					  {
2381
					    argName=argName.left(argName.length()-3);
2382
					  }
2383
					  argName = argName.stripWhiteSpace();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2384 2385 2386
                                          g_defArgsStr+=yytext;
					  g_argDict->insert(argName,new int(g_defArgs)); 
					  g_defArgs++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2387
  					}
2388 2389
  /*
<DefineText>"/ **"|"/ *!"			{
2390 2391
  					  g_defText+=yytext;
					  g_defLitText+=yytext;
2392 2393
					  g_insideComment=TRUE;
  					}
2394
<DefineText>"* /"			{
2395 2396 2397
  					  g_defText+=yytext;
					  g_defLitText+=yytext;
					  g_insideComment=FALSE;
2398
  					}
2399
  */
2400
<DefineText>"/*"[!*]?			{
2401 2402
					  g_defText+=yytext;
					  g_defLitText+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2403
					  g_lastCContext=YY_START;
2404
					  g_commentCount=1;
2405
  					  BEGIN(CopyCComment);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2406
  					}
2407
<DefineText>"//"[!/]?			{
2408
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2409 2410
  					  g_lastCPPContext=YY_START;
					  g_defLitText+=' ';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2411 2412
  					  BEGIN(SkipCPPComment);
  					}
2413 2414
<SkipCComment>[/]?"*/"			{
  					  if (yytext[0]=='/') outputChar('/');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2415
  					  outputChar('*');outputChar('/');
2416 2417
					  if (--g_commentCount<=0)
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2418 2419 2420 2421 2422 2423
					    if (g_lastCContext==Start) 
					      // small hack to make sure that ^... rule will
					      // match when going to Start... Example: "/*...*/ some stuff..."
					    {
					      YY_CURRENT_BUFFER->yy_at_bol=1;
					    }
2424 2425
  					    BEGIN(g_lastCContext);  
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2426
  					}
2427
<SkipCComment>"//"("/")*		{
2428
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2429 2430 2431
  					}
<SkipCComment>"/*"			{
  					  outputChar('/');outputChar('*');
2432
					  //g_commentCount++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2433
  					}
2434
<SkipCComment>[\\@][\\@]("f{"|"f$"|"f[") {
2435
  					  outputArray(yytext,(int)yyleng);
2436
  					}
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450
<SkipCComment>"~~~"[~]*                 {
                                          static bool markdownSupport = Config_getBool("MARKDOWN_SUPPORT");
                                          if (!markdownSupport)
                                          {
                                            REJECT;
                                          }
                                          else
                                          {
  					    outputArray(yytext,(int)yyleng);
                                            g_fenceSize=yyleng;
                                            BEGIN(SkipVerbatim);
                                          }
                                        }
<SkipCComment>[\\@][\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly"|"dot"|"code"("{"[^}]*"}")?){BN}+ {
2451
  					  outputArray(yytext,(int)yyleng);
2452 2453
  					  g_yyLineNr+=QCString(yytext).contains('\n');
  					}
2454
<SkipCComment>[\\@]("verbatim"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly"|"dot"|"code"("{"[^}]*"}")?){BN}+	{
2455
  					  outputArray(yytext,(int)yyleng);
2456
  					  g_yyLineNr+=QCString(yytext).contains('\n');
2457
                                          g_fenceSize=0;
2458 2459 2460 2461 2462 2463
					  if (yytext[1]=='f')
					  {
					    g_blockName="f";
					  }
					  else
					  {
2464 2465 2466 2467
                                            QCString bn=&yytext[1];
                                            int i = bn.find('{'); // for \code{.c}
                                            if (i!=-1) bn=bn.left(i);
					    g_blockName=bn.stripWhiteSpace();
2468
					  }
2469
					  BEGIN(SkipVerbatim);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2470
  					}
2471 2472 2473
<SkipCComment,SkipCPPComment>[\\@][\\@]"cond"[ \t]+ { // escaped @cond
  					  outputArray(yytext,(int)yyleng);
                                        }
2474 2475
<SkipCPPComment>[\\@]"cond"[ \t]+	{ // conditional section
                                          g_ccomment=TRUE;  
2476
                                          g_condCtx=YY_START;
2477 2478 2479 2480
  					  BEGIN(CondLineCpp);
  					}
<SkipCComment>[\\@]"cond"[ \t]+	{ // conditional section
                                          g_ccomment=FALSE;  
2481
                                          g_condCtx=YY_START;
2482
  					  BEGIN(CondLineC);
2483
  					}
2484
<CondLineC,CondLineCpp>[!()&| \ta-z_A-Z0-9\x80-\xFF.\-]+      {
2485
  				          startCondSection(yytext);
2486 2487
                                          if (g_skip)
                                          {
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
                                            if (YY_START==CondLineC)
                                            {
                                              // end C comment
  					      outputArray("*/",2);
                                              g_ccomment=TRUE;
                                            }
                                            else
                                            {
                                              g_ccomment=FALSE;
                                            }
2498 2499 2500 2501 2502 2503
                                            BEGIN(SkipCond);
                                          }
                                          else
                                          {
  					    BEGIN(g_condCtx);
                                          }
2504
  					}
2505
<CondLineC,CondLineCpp>.		{ // non-guard character
2506
  					  unput(*yytext);
2507
  					  startCondSection(" ");
2508 2509
                                          if (g_skip)
                                          {
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
                                            if (YY_START==CondLineC)
                                            {
                                              // end C comment
  					      outputArray("*/",2);
                                              g_ccomment=TRUE;
                                            }
                                            else
                                            {
                                              g_ccomment=FALSE;
                                            }
2520 2521 2522 2523 2524 2525 2526
                                            BEGIN(SkipCond);
                                          }
                                          else
                                          {
					    BEGIN(g_condCtx);
                                          }
  					}
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
<SkipCComment,SkipCPPComment>[\\@]"cond"[ \t\r]*/\n { // no guard
                                          if (YY_START==SkipCComment)
                                          {
                                            g_ccomment=TRUE;
                                            // end C comment
  					    outputArray("*/",2);
                                          }
                                          else
                                          {
                                            g_ccomment=FALSE;
                                          }
2538
                                          g_condCtx=YY_START;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2539
                                          startCondSection(" ");
2540 2541
                                          BEGIN(SkipCond);
  					}
2542 2543 2544
<SkipCond>\n                            { g_yyLineNr++; outputChar('\n'); }
<SkipCond>.                             { }
<SkipCond>[^\/\!*\\@\n]+                { }
2545 2546
<SkipCond>"//"[/!]                      { g_ccomment=FALSE; }
<SkipCond>"/*"[*!]                      { g_ccomment=TRUE; }
2547
<SkipCond,SkipCComment,SkipCPPComment>[\\@][\\@]"endcond"/[^a-z_A-Z0-9\x80-\xFF] {
2548 2549 2550 2551 2552
                                          if (!g_skip)
                                          {
  					    outputArray(yytext,(int)yyleng);
                                          }
                                        }
2553
<SkipCond>[\\@]"endcond"/[^a-z_A-Z0-9\x80-\xFF]  { 
2554
                                          bool oldSkip = g_skip;
2555
                                          endCondSection(); 
2556
                                          if (oldSkip && !g_skip)
2557 2558 2559 2560 2561 2562 2563
                                          {
                                            if (g_ccomment)
                                            {
                                              outputArray("/** ",4);
                                            }
                                            BEGIN(g_condCtx);
                                          }
2564
                                        }
2565
<SkipCComment,SkipCPPComment>[\\@]"endcond"/[^a-z_A-Z0-9\x80-\xFF] {
2566
                                          bool oldSkip = g_skip;
2567
  					  endCondSection();
2568
                                          if (oldSkip && !g_skip) 
2569 2570 2571
                                          {
                                            BEGIN(g_condCtx);
                                          }
2572
  					}
2573
<SkipVerbatim>[\\@]("endverbatim"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"enddocbookonly"|"endrtfonly"|"endmanonly"|"enddot"|"endcode"|"f$"|"f]"|"f}") { /* end of verbatim block */
2574
  					  outputArray(yytext,(int)yyleng);
2575 2576 2577 2578 2579
					  if (yytext[1]=='f' && g_blockName=="f")
					  {
					    BEGIN(SkipCComment);
					  }
					  else if (&yytext[4]==g_blockName)
2580 2581 2582
					  {
					    BEGIN(SkipCComment);
					  }
2583
  					}
2584 2585 2586 2587 2588 2589 2590
<SkipVerbatim>"~~~"[~]*                 {
  					  outputArray(yytext,(int)yyleng);
                                          if (g_fenceSize==yyleng)
                                          {
                                            BEGIN(SkipCComment);
                                          }
                                        }
2591
<SkipVerbatim>"*/"|"/*"			{
2592
  					  outputArray(yytext,(int)yyleng);
2593
  					}
2594
<SkipCComment,SkipVerbatim>[^*\\@\x06~\n\/]+ {
2595
  					  outputArray(yytext,(int)yyleng);
2596 2597
  					}
<SkipCComment,SkipVerbatim>\n		{ 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2598
  					  g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2599 2600
  					  outputChar('\n');
  					}
2601
<SkipCComment,SkipVerbatim>.		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2602 2603
  					  outputChar(*yytext);
  					}
2604
<CopyCComment>[^*a-z_A-Z\x80-\xFF\n]+		{
2605
					  g_defLitText+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2606
					  g_defText+=escapeAt(yytext);
2607 2608 2609 2610 2611 2612 2613 2614
  					}
<CopyCComment>"*/"			{
					  g_defLitText+=yytext;
					  g_defText+=yytext;
  					  BEGIN(g_lastCContext);
  					}
<CopyCComment>\n			{ 
  					  g_yyLineNr++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2615
  					  outputChar('\n');
2616
					  g_defLitText+=yytext;
2617
					  g_defText+=' ';
2618
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2619 2620 2621 2622 2623 2624 2625 2626 2627 2628
<RemoveCComment>"*/"{B}*"#"	        { // see bug 594021 for a usecase for this rule
                                          if (g_lastCContext==SkipCPPBlock)
					  {
					    BEGIN(SkipCommand);
					  }
					  else
					  {
					    REJECT;
					  }
					}
2629
<RemoveCComment>"*/"		        { BEGIN(g_lastCContext); }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2630 2631
<RemoveCComment>"//"			
<RemoveCComment>"/*"
2632
<RemoveCComment>[^*\x06\n]+
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2633
<RemoveCComment>\n			{ g_yyLineNr++; outputChar('\n'); }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2634
<RemoveCComment>.			
2635
<SkipCPPComment>[^\n\/\\@]+		{
2636
  					  outputArray(yytext,(int)yyleng);
2637
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2638 2639
<SkipCPPComment,RemoveCPPComment>\n	{
  					  unput(*yytext);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2640
  					  BEGIN(g_lastCPPContext);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2641 2642 2643 2644 2645 2646 2647
  					}
<SkipCPPComment>"/*"			{
  					  outputChar('/');outputChar('*');
  					}
<SkipCPPComment>"//"			{
  					  outputChar('/');outputChar('/');
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2648
<SkipCPPComment>[^\x06\@\\\n]+		{
2649
  					  outputArray(yytext,(int)yyleng);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2650 2651 2652 2653 2654 2655
  					}
<SkipCPPComment>.			{
  					  outputChar(*yytext);
  					}
<RemoveCPPComment>"/*"
<RemoveCPPComment>"//"
2656
<RemoveCPPComment>[^\x06\n]+
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2657 2658
<RemoveCPPComment>.
<DefineText>"#"				{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2659 2660
  					  g_quoteArg=TRUE;
					  g_defLitText+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2661
  					}
2662
<DefineText,CopyCComment>{ID}		{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2663 2664
					  g_defLitText+=yytext;
  					  if (g_quoteArg)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2665
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2666
					    g_defText+="\"";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2667
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2668
					  if (g_defArgs>0)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2669 2670
					  {
					    int *n;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2671
					    if ((n=(*g_argDict)[yytext]))
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2672
					    {
2673
					      //if (!g_quoteArg) g_defText+=' ';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2674
					      g_defText+='@';
2675
					      QCString numStr;
2676
					      numStr.sprintf("%d",*n);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2677
					      g_defText+=numStr;
2678
					      //if (!g_quoteArg) g_defText+=' ';
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2679 2680 2681
					    }
					    else
					    {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2682
					      g_defText+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2683 2684 2685 2686
					    }
					  }
					  else
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2687
					    g_defText+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2688
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2689
					  if (g_quoteArg)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2690
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2691
					    g_defText+="\"";
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2692
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2693
					  g_quoteArg=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2694
  					}
2695 2696 2697 2698
<CopyCComment>.				{
					  g_defLitText+=yytext;
					  g_defText+=yytext;
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2699 2700
<DefineText>\\[\r]?\n			{ 
					  g_defLitText+=yytext;
2701
					  outputChar('\n');
2702 2703 2704
					  g_defText += ' ';
					  g_yyLineNr++;
					  g_yyMLines++;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2705 2706
					}
<DefineText>\n				{
2707
					  QCString comment=extractTrailingComment(g_defLitText);
2708
					  g_defLitText+=yytext;
2709 2710 2711
					  if (!comment.isEmpty())
					  {
					    outputArray(comment,comment.length());
2712
					    g_defLitText=g_defLitText.left(g_defLitText.length()-comment.length()-1);
2713
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2714 2715
  					  outputChar('\n');
  					  Define *def=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2716
					  //printf("Define name=`%s' text=`%s' litTexti=`%s'\n",g_defName.data(),g_defText.data(),g_defLitText.data());
2717
					  if (g_includeStack.isEmpty() || g_curlyCount>0) 
2718 2719 2720
					  {
					    addDefine();
					  }
2721
					  def=DefineManager::instance().isDefined(g_defName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2722
					  if (def==0) // new define
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2723
					  {
2724
					    //printf("new define '%s'!\n",g_defName.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2725
					    Define *nd = newDefine();
2726 2727
					    DefineManager::instance().addDefine(g_yyFileName,nd);

Dimitri van Heesch's avatar
Dimitri van Heesch committed
2728
					    // also add it to the local file list if it is a source file
2729 2730 2731 2732
					    //if (g_isSource && g_includeStack.isEmpty())
					    //{
					    //  g_fileDefineDict->insert(g_defName,nd);
					    //}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2733
					  }
2734
					  else if (def /*&& macroIsAccessible(def)*/)
2735
					       // name already exists
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2736
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2737
					    //printf("existing define!\n");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2738 2739 2740 2741
					    //printf("define found\n");
					    if (def->undef) // undefined name
					    {
					      def->undef = FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2742 2743 2744 2745
					      def->name = g_defName;
					      def->definition = g_defText.stripWhiteSpace();
					      def->nargs = g_defArgs;
					      def->fileName = g_yyFileName.copy(); 
2746 2747
					      def->lineNr = g_yyLineNr-g_yyMLines;
					      def->columnNr = g_yyColNr;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2748 2749 2750
					    }
					    else
					    {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2751
					      //printf("error: define %s is defined more than once!\n",g_defName.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2752 2753
					    }
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2754 2755
					  delete g_argDict; g_argDict=0;
					  g_yyLineNr++;
2756
					  g_yyColNr=1;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2757
					  g_lastGuardName.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2758 2759
					  BEGIN(Start);
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2760 2761 2762
<DefineText>{B}*			{ g_defText += ' '; g_defLitText+=yytext; }
<DefineText>{B}*"##"{B}*		{ g_defText += "##"; g_defLitText+=yytext; }
<DefineText>"@"				{ g_defText += "@@"; g_defLitText+=yytext; }
2763 2764
<DefineText>\"				{ 
                                          g_defText += *yytext; 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2765
  					  g_defLitText+=yytext; 
2766 2767 2768 2769
					  if (!g_insideComment)
					  {
					    BEGIN(SkipDoubleQuote);
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2770
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2771 2772
<DefineText>\'				{ g_defText += *yytext;
  					  g_defLitText+=yytext; 
2773 2774 2775 2776
					  if (!g_insideComment)
					  {
  					    BEGIN(SkipSingleQuote);
					  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2777
					}
2778
<SkipDoubleQuote>"//"[/]?		{ g_defText += yytext; g_defLitText+=yytext; }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2779
<SkipDoubleQuote>"/*"			{ g_defText += yytext; g_defLitText+=yytext; }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2780
<SkipDoubleQuote>\"			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2781
  					  g_defText += *yytext; g_defLitText+=yytext; 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2782 2783
					  BEGIN(DefineText);
  					}
2784
<SkipSingleQuote,SkipDoubleQuote>\\.	{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2785
  					  g_defText += yytext; g_defLitText+=yytext;
2786
					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2787
<SkipSingleQuote>\'			{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2788
  					  g_defText += *yytext; g_defLitText+=yytext;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2789 2790
					  BEGIN(DefineText);
  					}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2791 2792 2793
<SkipDoubleQuote>.			{ g_defText += *yytext; g_defLitText+=yytext; }
<SkipSingleQuote>.			{ g_defText += *yytext; g_defLitText+=yytext; }
<DefineText>.				{ g_defText += *yytext; g_defLitText+=yytext; }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2794
<<EOF>>					{
2795
                                          DBG_CTX((stderr,"End of include file\n"));
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2796 2797
					  //printf("Include stack depth=%d\n",g_includeStack.count());
  					  if (g_includeStack.isEmpty())
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2798
					  {
2799
					    DBG_CTX((stderr,"Terminating scanner!\n"));
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2800 2801 2802 2803
					    yyterminate();
					  }
					  else
					  {
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2804 2805
					    FileState *fs=g_includeStack.pop();
					    //fileDefineCache->merge(g_yyFileName,fs->fileName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2806 2807 2808
					    YY_BUFFER_STATE oldBuf = YY_CURRENT_BUFFER;
					    yy_switch_to_buffer( fs->bufState );
					    yy_delete_buffer( oldBuf );
2809
					    g_yyLineNr    = fs->lineNr;
2810
                                            //preYYin = fs->oldYYin;
2811
                                            g_inputBuf    = fs->oldFileBuf;
2812
					    g_inputBufPos = fs->oldFileBufPos;
2813
					    setFileName(fs->fileName);
2814
					    DBG_CTX((stderr,"######## FileName %s\n",g_yyFileName.data()));
2815
					    
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2816
                                            // Deal with file changes due to 
2817
                                            // #include's within { .. } blocks
2818
                                            QCString lineStr(15+g_yyFileName.length());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2819 2820
                                            lineStr.sprintf("# %d \"%s\" 2",g_yyLineNr,g_yyFileName.data());
                                            outputArray(lineStr.data(),lineStr.length());
2821
					    
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2822
					    delete fs; fs=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2823 2824
					  }
  					}
2825
<*>"/*"/"*/"				|
2826
<*>"/*"[*]?				{
2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
                                          if (YY_START==SkipVerbatim || YY_START==SkipCond)
                                          {
                                            REJECT;
                                          }
                                          else
                                          {
					    outputArray(yytext,(int)yyleng);
  					    g_lastCContext=YY_START;
					    g_commentCount=1;
					    if (yyleng==3) g_lastGuardName.resize(0); // reset guard in case the #define is documented!
					    BEGIN(SkipCComment);
                                          }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2839
  					}
2840
<*>"//"[/]?				{
2841
                                          if (YY_START==SkipVerbatim || YY_START==SkipCond)
2842 2843 2844 2845 2846 2847 2848 2849 2850 2851
                                          {
                                            REJECT;
                                          }
                                          else
                                          {
					    outputArray(yytext,(int)yyleng);
  					    g_lastCPPContext=YY_START;
					    if (yyleng==3) g_lastGuardName.resize(0); // reset guard in case the #define is documented!
					    BEGIN(SkipCPPComment);
                                          }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2852 2853 2854
					}
<*>\n					{ 
  					  outputChar('\n');
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2855
  					  g_yyLineNr++; 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2856 2857
					}
<*>.				        {
2858
  					  g_expectGuard = FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2859 2860 2861 2862 2863 2864 2865 2866
  					  outputChar(*yytext);
  					}

%%

/*@ ----------------------------------------------------------------------------
 */

2867
static int getNextChar(const QCString &expr,QCString *rest,uint &pos)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2868 2869 2870 2871 2872 2873 2874
{
  //printf("getNextChar(%s,%s,%d)\n",expr.data(),rest ? rest->data() : 0,pos);
  if (pos<expr.length())
  {
    //printf("%c=expr()\n",expr.at(pos));
    return expr.at(pos++);
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2875
  else if (rest && !rest->isEmpty())
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2876 2877 2878 2879 2880 2881 2882 2883 2884
  {
    int cc=rest->at(0);
    *rest=rest->right(rest->length()-1);
    //printf("%c=rest\n",cc);
    return cc;
  }
  else
  {
    int cc=yyinput();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2885
    //printf("%d=yyinput() %d\n",cc,EOF);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2886 2887 2888 2889
    return cc;
  }
}
 
2890
static int getCurrentChar(const QCString &expr,QCString *rest,uint pos)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2891 2892 2893 2894 2895 2896 2897
{
  //printf("getCurrentChar(%s,%s,%d)\n",expr.data(),rest ? rest->data() : 0,pos);
  if (pos<expr.length())
  {
    //printf("%c=expr()\n",expr.at(pos));
    return expr.at(pos);
  }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2898
  else if (rest && !rest->isEmpty())
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2899 2900 2901 2902 2903 2904 2905
  {
    int cc=rest->at(0);
    //printf("%c=rest\n",cc);
    return cc;
  }
  else
  {
2906 2907 2908
    int cc=yyinput();
    returnCharToStream(cc);
    //unput((char)cc);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2909 2910 2911 2912 2913
    //printf("%c=yyinput()\n",cc);
    return cc;
  }
}

2914
static void unputChar(const QCString &expr,QCString *rest,uint &pos,char c)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
{
  //printf("unputChar(%s,%s,%d,%c)\n",expr.data(),rest ? rest->data() : 0,pos,c);
  if (pos<expr.length())
  {
    pos++;
  }
  else if (rest)
  {
    //printf("Prepending to rest!\n");
    char cs[2];cs[0]=c;cs[1]='\0';
    rest->prepend(cs);
  }
  else
  {
2929 2930
    //unput(c);
    returnCharToStream(c);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2931 2932 2933 2934 2935 2936 2937
  }
  //printf("result: unputChar(%s,%s,%d,%c)\n",expr.data(),rest ? rest->data() : 0,pos,c);
}

void addSearchDir(const char *dir)
{
  QFileInfo fi(dir);
2938
  if (fi.isDir()) g_pathList->append(fi.absFilePath().utf8());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2939 2940 2941 2942
} 

void initPreprocessor()
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2943
  g_pathList = new QStrList;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2944
  addSearchDir(".");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2945
  g_expandedDict = new DefineDict(17);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2946 2947
}

2948
void cleanUpPreprocessor()
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2949
{
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2950 2951
  delete g_expandedDict; g_expandedDict=0;
  delete g_pathList; g_pathList=0;
2952
  DefineManager::deleteInstance();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2953 2954 2955
}


2956
void preprocessFile(const char *fileName,BufStr &input,BufStr &output)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2957 2958
{
  uint orgOffset=output.curPos();
2959 2960
  //printf("##########################\n%s\n####################\n",
  //    input.data());
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2961

2962 2963
  g_macroExpansion = Config_getBool("MACRO_EXPANSION");
  g_expandOnlyPredef = Config_getBool("EXPAND_ONLY_PREDEF");
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2964
  g_skip=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2965
  g_curlyCount=0;
2966
  g_nospaces=FALSE;
2967 2968
  g_inputBuf=&input;
  g_inputBufPos=0;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2969 2970 2971 2972 2973
  g_outputBuf=&output;
  g_includeStack.setAutoDelete(TRUE);
  g_includeStack.clear();
  g_expandedDict->setAutoDelete(FALSE);
  g_expandedDict->clear();
2974 2975
  g_condStack.clear();
  g_condStack.setAutoDelete(TRUE);
2976 2977 2978 2979
  //g_fileDefineDict->clear();

  setFileName(fileName);
  g_inputFileDef = g_yyFileDef;
2980
  DefineManager::instance().startContext(g_yyFileName);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2981
  
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2982 2983
  static bool firstTime=TRUE;
  if (firstTime)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2984
  {
2985 2986 2987 2988 2989
    // add predefined macros
    char *defStr;
    QStrList &predefList = Config_getList("PREDEFINED");
    QStrListIterator sli(predefList);
    for (sli.toFirst();(defStr=sli.current());++sli)
Dimitri van Heesch's avatar
Dimitri van Heesch committed
2990
    {
2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
      QCString ds = defStr;
      int i_equals=ds.find('=');
      int i_obrace=ds.find('(');
      int i_cbrace=ds.find(')');
      bool nonRecursive = i_equals>0 && ds.at(i_equals-1)==':';

      if (i_obrace==0) continue; // no define name

      if (i_obrace<i_equals && i_cbrace<i_equals && 
	  i_obrace!=-1      && i_cbrace!=-1      && 
	  i_obrace<i_cbrace
	 ) // predefined function macro definition
3003
      {
3004
	//printf("predefined function macro '%s'\n",defStr);
3005
	QRegExp reId("[a-z_A-Z\x80-\xFF][a-z_A-Z0-9\x80-\xFF]*"); // regexp matching an id
3006 3007 3008 3009 3010
	QDict<int> argDict(17);
	argDict.setAutoDelete(TRUE);
	int i=i_obrace+1,p,l,count=0;
	// gather the formal arguments in a dictionary 
	while (i<i_cbrace && (p=reId.match(ds,i,&l)))
3011
	{
3012 3013
	  argDict.insert(ds.mid(p,l),new int(count++));
	  i=p+l;
3014
	}
3015 3016 3017 3018 3019 3020 3021
	// strip definition part
	QCString tmp=ds.right(ds.length()-i_equals-1);
	QCString definition;
	i=0;
	// substitute all occurrences of formal arguments by their 
	// corresponding markers
	while ((p=reId.match(tmp,i,&l))!=-1)
3022
	{
3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035
	  if (p>i) definition+=tmp.mid(i,p-i);
	  int *argIndex;
	  if ((argIndex=argDict[tmp.mid(p,l)])!=0)
	  {
	    QCString marker;
	    marker.sprintf(" @%d ",*argIndex);
	    definition+=marker;
	  }
	  else
	  {
	    definition+=tmp.mid(p,l);
	  }
	  i=p+l;
3036
	}
3037 3038 3039 3040 3041 3042 3043
	if (i<(int)tmp.length()) definition+=tmp.mid(i,tmp.length()-i);

	// add define definition to the dictionary of defines for this file
	QCString dname = ds.left(i_obrace);
	if (!dname.isEmpty())
	{
	  Define *def = new Define;
3044 3045 3046
	  def->name         = dname;
	  def->definition   = definition; 
	  def->nargs        = count;
3047 3048
	  def->isPredefined = TRUE;
	  def->nonRecursive = nonRecursive;
3049 3050
	  def->fileDef      = g_yyFileDef;
	  def->fileName     = fileName;
3051
	  DefineManager::instance().addDefine(g_yyFileName,def);
3052 3053 3054 3055
	}

	//printf("#define `%s' `%s' #nargs=%d\n",
	//  def->name.data(),def->definition.data(),def->nargs);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3056
      }
3057 3058 3059 3060
      else if ((i_obrace==-1 || i_obrace>i_equals) &&
	  (i_cbrace==-1 || i_cbrace>i_equals) &&
	  !ds.isEmpty() && (int)ds.length()>i_equals
	  ) // predefined non-function macro definition
3061
      {
3062
	//printf("predefined normal macro '%s'\n",defStr);
3063
	Define *def = new Define;
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
	if (i_equals==-1) // simple define without argument
	{
	  def->name = ds;
	  def->definition = "1"; // substitute occurrences by 1 (true)
	}
	else // simple define with argument
	{
	  int ine=i_equals - (nonRecursive ? 1 : 0);
	  def->name = ds.left(ine);
	  def->definition = ds.right(ds.length()-i_equals-1);
	}
	if (!def->name.isEmpty())
	{
	  def->nargs = -1;
	  def->isPredefined = TRUE;
	  def->nonRecursive = nonRecursive;
3080 3081
	  def->fileDef      = g_yyFileDef;
	  def->fileName     = fileName;
3082
	  DefineManager::instance().addDefine(g_yyFileName,def);
3083 3084 3085 3086 3087
	}
	else
	{
	  delete def;
	}
3088

3089 3090
	//printf("#define `%s' `%s' #nargs=%d\n",
	//  def->name.data(),def->definition.data(),def->nargs);
3091
      }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3092
    }
3093
    //firstTime=FALSE;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3094 3095
  }
 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3096
  g_yyLineNr = 1;
3097
  g_yyColNr  = 1;
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3098 3099
  g_level    = 0;
  g_ifcount  = 0;
3100

Dimitri van Heesch's avatar
Dimitri van Heesch committed
3101
  BEGIN( Start );
3102
  
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3103
  g_expectGuard = guessSection(fileName)==Entry::HEADER_SEC;
3104
  g_guardName.resize(0);
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3105 3106
  g_lastGuardName.resize(0);
  g_guardExpr.resize(0);
3107
  
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3108
  preYYlex();
3109
  g_lexInit=TRUE;
3110

3111 3112 3113 3114 3115 3116 3117 3118
  while (!g_condStack.isEmpty())
  {
    CondCtx *ctx = g_condStack.pop();
    QCString sectionInfo = " ";
    if (ctx->sectionId!=" ") sectionInfo.sprintf(" with label %s ",ctx->sectionId.data()); 
    warn(fileName,ctx->lineNr,"Conditional section%sdoes not have "
	"a corresponding \\endcond command within this file.",sectionInfo.data());
  }
3119 3120 3121
  // make sure we don't extend a \cond with missing \endcond over multiple files (see bug 624829)
  forceEndCondSection();

Dimitri van Heesch's avatar
Dimitri van Heesch committed
3122
  // remove locally defined macros so they can be redefined in another source file
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
  //if (g_fileDefineDict->count()>0)
  //{
  //  QDictIterator<Define> di(*g_fileDefineDict);
  //  Define *d;
  //  for (di.toFirst();(d=di.current());++di)
  //  {
  //    g_globalDefineDict->remove(di.currentKey());
  //  }
  //  g_fileDefineDict->clear();
  //}
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3133

3134
  if (Debug::isFlagSet(Debug::Preprocessor))
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3135
  {
3136 3137
    char *orgPos=output.data()+orgOffset;
    char *newPos=output.data()+output.curPos();
3138
    Debug::print(Debug::Preprocessor,0,"Preprocessor output (size: %d bytes):\n",newPos-orgPos);
3139
    int line=1;
3140
    Debug::print(Debug::Preprocessor,0,"---------\n00001 ");
3141 3142 3143
    while (orgPos<newPos) 
    {
      putchar(*orgPos);
3144
      if (*orgPos=='\n') Debug::print(Debug::Preprocessor,0,"%05d ",++line);
3145 3146
      orgPos++;
    }
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160
    Debug::print(Debug::Preprocessor,0,"\n---------\n");
    if (DefineManager::instance().defineContext().count()>0)
    {
      Debug::print(Debug::Preprocessor,0,"Macros accessible in this file:\n");
      Debug::print(Debug::Preprocessor,0,"---------\n");
      QDictIterator<Define> di(DefineManager::instance().defineContext());
      Define *def;
      for (di.toFirst();(def=di.current());++di)
      {
        Debug::print(Debug::Preprocessor,0,"%s ",def->name.data());
      }
      Debug::print(Debug::Preprocessor,0,"\n---------\n");
    }
    else
3161
    {
3162
      Debug::print(Debug::Preprocessor,0,"No macros accessible in this file.\n");
3163
    }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3164
  }
3165
  DefineManager::instance().endContext();
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3166 3167
}

3168 3169 3170 3171 3172 3173 3174 3175 3176 3177
void preFreeScanner()
{
#if defined(YY_FLEX_SUBMINOR_VERSION) 
  if (g_lexInit)
  {
    preYYlex_destroy();
  }
#endif
}

3178
#if !defined(YY_FLEX_SUBMINOR_VERSION) 
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3179
extern "C" { // some bogus code to keep the compiler happy
3180
//  int  preYYwrap() { return 1 ; }
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3181 3182
  void preYYdummy() { yy_flex_realloc(0,0); } 
}
3183 3184
#endif