Back to index

salome-paravis  6.5.0
Classes | Defines | Typedefs | Enumerations | Functions
vtkParsePreprocess.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _MacroInfo
 Struct to describe a preprocessor symbol. More...
struct  _PreprocessInfo
 Contains all symbols defined thus far (including those defined in any included header files). More...

Defines

#define VTK_PARSE_FATAL_ERROR   0xF8
 Bitfield for fatal errors.

Typedefs

typedef long long preproc_int_t
 This file provides subroutines to assist in preprocessing C/C++ header files.
typedef unsigned long long preproc_uint_t
typedef struct _MacroInfo MacroInfo
 Struct to describe a preprocessor symbol.
typedef struct _PreprocessInfo PreprocessInfo
 Contains all symbols defined thus far (including those defined in any included header files).

Enumerations

enum  _preproc_platform_t { VTK_PARSE_NATIVE = 0 }
 Platforms. More...
enum  _preproc_return_t {
  VTK_PARSE_OK = 0, VTK_PARSE_SKIP = 1, VTK_PARSE_PREPROC_DOUBLE = 2, VTK_PARSE_PREPROC_FLOAT = 3,
  VTK_PARSE_PREPROC_STRING = 4, VTK_PARSE_MACRO_UNDEFINED = 5, VTK_PARSE_MACRO_REDEFINED = 6, VTK_PARSE_FILE_NOT_FOUND = 7,
  VTK_PARSE_FILE_OPEN_ERROR = 8, VTK_PARSE_FILE_READ_ERROR = 9, VTK_PARSE_MACRO_NUMARGS = 10, VTK_PARSE_SYNTAX_ERROR = 11
}
 Directive return values. More...

Functions

int vtkParsePreprocess_HandleDirective (PreprocessInfo *info, const char *directive)
 Handle a preprocessor directive.
int vtkParsePreprocess_EvaluateExpression (PreprocessInfo *info, const char *text, preproc_int_t *val, int *is_unsigned)
 Evaluate a preprocessor expression, providing an integer result in "val", and whether it is unsigned in "is_unsigned".
void vtkParsePreprocess_AddStandardMacros (PreprocessInfo *info, int platform)
 Add all standard preprocessor symbols.
int vtkParsePreprocess_AddMacro (PreprocessInfo *info, const char *name, const char *definition)
 Add a preprocessor symbol, including a definition.
int vtkParsePreprocess_RemoveMacro (PreprocessInfo *info, const char *name)
 Remove a preprocessor symbol.
MacroInfovtkParsePreprocess_GetMacro (PreprocessInfo *info, const char *name)
 Return a preprocessor symbol struct, or NULL if not found.
const char * vtkParsePreprocess_ExpandMacro (MacroInfo *macro, const char *argstring)
 Expand a function macro, given arguments in parentheses.
void vtkParsePreprocess_FreeExpandedMacro (const char *emacro)
 Free an expanded macro.
void vtkParsePreprocess_IncludeDirectory (PreprocessInfo *info, const char *name)
 Add an include directory.
const char * vtkParsePreprocess_FindIncludeFile (PreprocessInfo *info, const char *filename, int system_first, int *already_loaded)
 Find an include file in the path.
void vtkParsePreprocess_InitMacro (MacroInfo *symbol)
 Initialize a preprocessor symbol struct.
void vtkParsePreprocess_InitPreprocess (PreprocessInfo *info)
 Initialize a preprocessor struct.

Class Documentation

struct _MacroInfo

Struct to describe a preprocessor symbol.

Definition at line 63 of file vtkParsePreprocess.h.

Class Members
const char ** Arguments
const char * Comment
const char * Definition
int IsExternal
int IsFunction
const char * Name
int NumberOfArguments
struct _PreprocessInfo

Contains all symbols defined thus far (including those defined in any included header files).

Definition at line 78 of file vtkParsePreprocess.h.

Collaboration diagram for _PreprocessInfo:
Class Members
int ConditionalDepth
int ConditionalDone
const char * FileName
const char ** IncludeDirectories
const char ** IncludeFiles
int IsExternal
MacroInfo ** Macros
int NumberOfIncludeDirectories
int NumberOfIncludeFiles
int NumberOfMacros

Define Documentation

#define VTK_PARSE_FATAL_ERROR   0xF8

Bitfield for fatal errors.

Definition at line 120 of file vtkParsePreprocess.h.


Typedef Documentation

typedef struct _MacroInfo MacroInfo

Struct to describe a preprocessor symbol.

typedef long long preproc_int_t

This file provides subroutines to assist in preprocessing C/C++ header files.

It evaluates preprocessor directives and stores a list of all preprocessor macros.

The preprocessing is done in-line while the file is being parsed. Macros that are defined in the file are stored but are not automatically expanded. The parser can query the macro definitions, expand them into plain text, or ask the preprocessor to evaluate them and return an integer result.

The typical usage of this preprocessor is that the main parser will pass any lines that begin with '#' to the vtkParsePreprocess_HandleDirective() function, which will evaluate the line and provide a return code. The return code will tell the main parser if a syntax error or macro lookup error occurred, and will also let the parser know if an if or #else directive requires that the next block of code be skipped.

No checks are done for recursively-defined macros. If they occur, the preprocessor will crash. The preprocessor int type. Use the compiler's longest int type.

Definition at line 56 of file vtkParsePreprocess.h.

typedef unsigned long long preproc_uint_t

Definition at line 57 of file vtkParsePreprocess.h.

Contains all symbols defined thus far (including those defined in any included header files).


Enumeration Type Documentation

Platforms.

Always choose native unless crosscompiling.

Enumerator:
VTK_PARSE_NATIVE 

Definition at line 95 of file vtkParsePreprocess.h.

                         {
  VTK_PARSE_NATIVE = 0,
};

Directive return values.

Enumerator:
VTK_PARSE_OK 
VTK_PARSE_SKIP 
VTK_PARSE_PREPROC_DOUBLE 
VTK_PARSE_PREPROC_FLOAT 
VTK_PARSE_PREPROC_STRING 
VTK_PARSE_MACRO_UNDEFINED 
VTK_PARSE_MACRO_REDEFINED 
VTK_PARSE_FILE_NOT_FOUND 
VTK_PARSE_FILE_OPEN_ERROR 
VTK_PARSE_FILE_READ_ERROR 
VTK_PARSE_MACRO_NUMARGS 
VTK_PARSE_SYNTAX_ERROR 

Definition at line 102 of file vtkParsePreprocess.h.

                       {
  VTK_PARSE_OK = 0,
  VTK_PARSE_SKIP = 1,            /* skip next block */
  VTK_PARSE_PREPROC_DOUBLE = 2,  /* encountered a double */
  VTK_PARSE_PREPROC_FLOAT = 3,   /* encountered a float */
  VTK_PARSE_PREPROC_STRING = 4,  /* encountered a string */
  VTK_PARSE_MACRO_UNDEFINED = 5, /* macro lookup failed */
  VTK_PARSE_MACRO_REDEFINED = 6, /* attempt to redefine a macro */
  VTK_PARSE_FILE_NOT_FOUND = 7,  /* include file not found */
  VTK_PARSE_FILE_OPEN_ERROR = 8, /* include file not readable */
  VTK_PARSE_FILE_READ_ERROR = 9, /* error during read */
  VTK_PARSE_MACRO_NUMARGS = 10,  /* wrong number of args to func macro */
  VTK_PARSE_SYNTAX_ERROR = 11    /* any and all syntax errors */
};

Function Documentation

int vtkParsePreprocess_AddMacro ( PreprocessInfo info,
const char *  name,
const char *  definition 
)

Add a preprocessor symbol, including a definition.

Return values are VTK_PARSE_OK and VTK_PARSE_MACRO_REDEFINED.

Definition at line 2230 of file vtkParsePreprocess.c.

{
  int i;
  MacroInfo *macro;

  if (preproc_find_macro(info, name, &i))
    {
    return VTK_PARSE_MACRO_REDEFINED;
    }

  macro = preproc_add_macro_definition(info, name, definition);
  macro->IsExternal = 1;

  return VTK_PARSE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void vtkParsePreprocess_AddStandardMacros ( PreprocessInfo info,
int  platform 
)

Add all standard preprocessor symbols.

Use VTK_PARSE_NATIVE as the platform. In the future, other platform specifiers might be added to allow crosscompiling.

Add all standard preprocessor symbols.

Specify the platform.

Definition at line 2144 of file vtkParsePreprocess.c.

{
  int save_external = info->IsExternal;
  info->IsExternal = 1;

  /* a special macro to indicate that this is the wrapper */
  preproc_add_macro_definition(info, "__WRAP__", "1");

  /* language macros - assume that we are wrapping C++ code */
  preproc_add_macro_definition(info, "__cplusplus", "1");

  /* stdc version macros */
#ifdef __STDC__
  PREPROC_ADD_MACRO(info, __STDC__);
#endif
#ifdef __STDC_VERSION__
  PREPROC_ADD_MACRO(info, __STDC_VERSION__);
#endif
#ifdef __STDC_HOSTED__
  PREPROC_ADD_MACRO(info, __STDC_HOSTED__);
#endif

  if (platform == VTK_PARSE_NATIVE)
    {
#ifdef WIN32
    PREPROC_ADD_MACRO(info, WIN32);
#endif
#ifdef _WIN32
    PREPROC_ADD_MACRO(info, _WIN32);
#endif
#ifdef _MSC_VER
    PREPROC_ADD_MACRO(info, _MSC_VER);
#endif

#ifdef __BORLAND__
    PREPROC_ADD_MACRO(info, __BORLAND__);
#endif

#ifdef __CYGWIN__
    PREPROC_ADD_MACRO(info, __CYGWIN__);
#endif
#ifdef MINGW
    PREPROC_ADD_MACRO(info, MINGW);
#endif
#ifdef __MINGW32__
    PREPROC_ADD_MACRO(info, __MINGW32__);
#endif

#ifdef __linux__
    PREPROC_ADD_MACRO(info, __linux__);
#endif
#ifdef __LINUX__
    PREPROC_ADD_MACRO(info, __LINUX__);
#endif

#ifdef __APPLE__
    PREPROC_ADD_MACRO(info, __APPLE__);
#endif
#ifdef __MACH__
    PREPROC_ADD_MACRO(info, __MACH__);
#endif
#ifdef __DARWIN__
    PREPROC_ADD_MACRO(info, __DARWIN__);
#endif

#ifdef __GNUC__
    PREPROC_ADD_MACRO(info, __GNUC__);
#endif
#ifdef __LP64__
    PREPROC_ADD_MACRO(info, __LP64__);
#endif
#ifdef __BIG_ENDIAN__
    PREPROC_ADD_MACRO(info, __BIG_ENDIAN__);
#endif
#ifdef __LITTLE_ENDIAN__
    PREPROC_ADD_MACRO(info, __LITTLE_ENDIAN__);
#endif
    }

  info->IsExternal = save_external;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int vtkParsePreprocess_EvaluateExpression ( PreprocessInfo info,
const char *  text,
preproc_int_t val,
int *  is_unsigned 
)

Evaluate a preprocessor expression, providing an integer result in "val", and whether it is unsigned in "is_unsigned".

A return value of VTK_PARSE_OK means that no errors occurred, while VTK_PREPROC_DOUBLE, VTK_PREPROC_FLOAT, and VTK_PREPROC_STRING indicate that the preprocessor encountered a non-integer value. Error return values are VTK_PARSE_MACRO_UNDEFINED and VTK_PARSE_SYNTAX_ERRORS. Undefined macros evaluate to zero.

Evaluate a preprocessor expression, providing an integer result in "val", and whether it is unsigned in "is_unsigned".

If no errors occurred, the result will be VTK_PARSE_OK.

Definition at line 2125 of file vtkParsePreprocess.c.

{
  preproc_tokenizer tokens;
  preproc_init(&tokens, text);

  return preproc_evaluate_expression(info, &tokens, val, is_unsigned);
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* vtkParsePreprocess_ExpandMacro ( MacroInfo macro,
const char *  argstring 
)

Expand a function macro, given arguments in parentheses.

Returns a new string that was allocated with malloc, or NULL if the wrong number of arguments were given.

Definition at line 2280 of file vtkParsePreprocess.c.

{
  const char *cp = argstring;
  int n = 0;
  int j = 0;
  const char **values = NULL;
  const char *pp = NULL;
  const char *dp = NULL;
  char *rp = NULL;
  size_t rs = 0;
  size_t i = 0;
  size_t l = 0;
  size_t k = 0;
  int stringify = 0;
  int depth = 1;
  int c;

  if (argstring == NULL || *cp != '(')
    {
    return NULL;
    }

  /* break the string into individual argument values */
  values = (const char **)malloc(4*sizeof(const char **));

  cp++;
  values[n++] = cp;
  while (depth > 0 && *cp != '\0')
    {
    while (*cp != '\0')
      {
      if (*cp == '\"' || *cp == '\'')
        {
        preproc_skip_quotes(&cp);
        }
      else if (cp[0] == '/' && (cp[1] == '*' || cp[1] == '/'))
        {
        preproc_skip_comment(&cp);
        }
      else if (*cp == '(')
        {
        cp++;
        depth++;
        }
      else if (*cp == ')')
        {
        cp++;
        if (--depth == 0)
          {
          break;
          }
        }
      else if (*cp == ',')
        {
        cp++;
        if (depth == 1)
          {
          break;
          }
        }
      else if (*cp != '\0')
        {
        cp++;
        }
      }
    if (n >= 4 && (n & (n-1)) == 0)
      {
      values = (const char **)realloc(
        (char **)values, 2*n*sizeof(const char **));
      }

    values[n++] = cp;
    }
  --n;

  /* diagnostic: print out the values */
#if PREPROC_DEBUG
  for (j = 0; j < n; j++)
    {
    size_t m = values[j+1] - values[j] - 1;
    fprintf(stderr, "arg %i: %*.*s\n", (int)j, (int)m, (int)m, values[j]);
    }
#endif

  /* allow whitespace as "no argument" */
  if (macro->NumberOfArguments == 0 && n == 1)
    {
    cp = values[0];
    c = *cp;
    while (c == ' ' || c == '\n' || c == '\t' || c == '\r')
      {
      c = *(++cp);
      }
    if (cp + 1 == values[1])
      {
      n = 0;
      }
    }

  if (n != macro->NumberOfArguments)
    {
    free((char **)values);
#if PREPROC_DEBUG
    fprintf(stderr, "wrong number of macro args to %s, %d != %d\n",
            macro->Name, n, macro->NumberOfArguments);
#endif
    return NULL;
    }

  cp = macro->Definition;
  dp = cp;
  if (cp == NULL)
    {
    free((char **)values);
    return NULL;
    }

  rp = (char *)malloc(128);
  rp[0] = '\0';
  rs = 128;

  while (*cp != '\0')
    {
    pp = cp;
    stringify = 0;
    /* skip all chars that aren't part of a name */
    while ((*cp < 'a' || *cp > 'z') &&
           (*cp < 'A' || *cp > 'Z') &&
           *cp != '_' && *cp != '\0')
      {
      if (*cp == '\'' || *cp == '\"')
        {
        preproc_skip_quotes(&cp);
        dp = cp;
        }
      else if (*cp >= '0' && *cp <= '9')
        {
        preproc_skip_number(&cp);
        dp = cp;
        }
      else if (*cp == '/' && (cp[1] == '/' || cp[1] == '*'))
        {
        preproc_skip_comment(&cp);
        dp = cp;
        }
      else if (cp[0] == '#' && cp[1] == '#')
        {
        dp = cp;
        while (dp > pp && (dp[-1] == ' ' || dp[-1] == '\t' ||
                           dp[-1] == '\r' || dp[-1] == '\n'))
          {
          --dp;
          }
        cp += 2;
        while (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
          {
          cp++;
          }
        break;
        }
      else if (*cp == '#')
        {
        stringify = 1;
        dp = cp;
        cp++;
        while (*cp == ' ' || *cp == '\t' || *cp == '\r' || *cp == '\n')
          {
          cp++;
          }
        break;
        }
      else
        {
        cp++;
        dp = cp;
        }
      }
    l = dp - pp;
    if (l > 0)
      {
      if (i + l + 1 >= rs)
        {
        rs += rs + i + l + 1;
        rp = (char *)realloc(rp, rs);
        }
      strncpy(&rp[i], pp, l);
      i += l;
      rp[i] = '\0';
      }

    /* get the name */
    pp = cp;
    preproc_skip_name(&cp);
    l = cp - pp;
    if (l > 0)
      {
      for (j = 0; j < n; j++)
        {
        /* check whether the name matches an argument */
        if (strncmp(pp, macro->Arguments[j], l) == 0 &&
            macro->Arguments[j][l] == '\0')
          {
          /* substitute the argument value */
          l = values[j+1] - values[j] - 1;
          pp = values[j];
          /* remove leading whitespace from argument */
          c = *pp;
          while (c == ' ' || c == '\n' || c == '\t' || c == '\r')
            {
            c = *(++pp);
            l--;
            }
          /* remove trailing whitespace from argument */
          if (l > 0)
            {
            c = pp[l - 1];
            while (c == ' ' || c == '\n' || c == '\t' || c == '\r')
              {
              if (--l == 0)
                {
                break;
                }
              c = pp[l-1];
              }
            }
          break;
          }
        }
      if (stringify)
        {
        /* compute number of chars that will be added */
        stringify = 2;
        for (k = 0; k < l; k++)
          {
          c = pp[k];
          if (c == '\\' || c == '\"')
            {
            stringify++;
            }
          }
        }
      if (i + l + stringify + 1 >= rs)
        {
        rs += rs + i + l + 1;
        rp = (char *)realloc(rp, rs);
        }
      if (stringify)
        {
        rp[i++] = '\"';
        for (k = 0; k < l; k++)
          {
          c = pp[k];
          if (c == '\\' || c == '\"')
            {
            rp[i++] = '\\';
            }
          rp[i++] = c;
          }
        rp[i++] = '\"';
        }
      else
        {
        strncpy(&rp[i], pp, l);
        i += l;
        }
      rp[i] = '\0';
      }
    }

  free((char **)values);

  return rp;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* vtkParsePreprocess_FindIncludeFile ( PreprocessInfo info,
const char *  filename,
int  system_first,
int *  already_loaded 
)

Find an include file in the path.

If system_first is set, then the current directory is ignored unless it is explicitly in the path. A null return value indicates that the file was not found. If already_loaded is set, then the file was already loaded. This preprocessor never loads the same file twice.

If system_first is set, then the current directory is not searched.

Definition at line 2591 of file vtkParsePreprocess.c.

{
  const char *cp;
  cp = preproc_find_include_file(info, filename, system_first, 1);
  if (cp)
    {
    *already_loaded = 1;
    }

  *already_loaded = 0;
  return preproc_find_include_file(info, filename, system_first, 0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void vtkParsePreprocess_FreeExpandedMacro ( const char *  emacro)

Free an expanded macro.

Definition at line 2558 of file vtkParsePreprocess.c.

{
  free((char *)emacro);
}

Here is the caller graph for this function:

MacroInfo* vtkParsePreprocess_GetMacro ( PreprocessInfo info,
const char *  name 
)

Return a preprocessor symbol struct, or NULL if not found.

Definition at line 2250 of file vtkParsePreprocess.c.

{
  int i = 0;

  if (preproc_find_macro(info, name, &i))
    {
    return info->Macros[i];
    }

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int vtkParsePreprocess_HandleDirective ( PreprocessInfo info,
const char *  directive 
)

Handle a preprocessor directive.

Return value VTK_PARSE_OK means that no errors occurred, while VTK_PARSE_SKIP means that a conditional directive was encountered and the next code block should be skipped. The preprocessor has an internal state machine that keeps track of conditional if/else/endif directives. All other return values indicate errors, and it is up to the parser to decide which errors are fatal. The preprocessor only considers syntax errors and I/O errors to be fatal.

Handle a preprocessor directive.

Unrecognized directives are ignored.

Definition at line 2053 of file vtkParsePreprocess.c.

{
  int result = VTK_PARSE_OK;
  preproc_tokenizer tokens;

  preproc_init(&tokens, directive);

  if (tokens.tok != '#')
    {
    return VTK_PARSE_SYNTAX_ERROR;
    }

  preproc_next(&tokens);

  if (tokens.tok == TOK_ID)
    {
    if (strncmp("ifdef", tokens.text, tokens.len) == 0 ||
        strncmp("ifndef", tokens.text, tokens.len) == 0 ||
        strncmp("if", tokens.text, tokens.len) == 0 ||
        strncmp("elif", tokens.text, tokens.len) == 0 ||
        strncmp("else", tokens.text, tokens.len) == 0 ||
        strncmp("endif", tokens.text, tokens.len) == 0)
      {
      result = preproc_evaluate_if(info, &tokens);
      while (tokens.tok) { preproc_next(&tokens); }
#if PREPROC_DEBUG
        {
        size_t n = tokens.text - directive;

        if (result == VTK_PARSE_SKIP)
          {
          fprintf(stderr, "SKIP: ");
          }
        else if (result == VTK_PARSE_OK)
          {
          fprintf(stderr, "READ: ");
          }
        else
          {
          fprintf(stderr, "ERR%-2.2d ", result);
          }
        fprintf(stderr, "%*.*s\n", (int)n, (int)n, directive);
        }
#endif
      }
    else if (info->ConditionalDepth == 0)
      {
      if (strncmp("define", tokens.text, tokens.len) == 0 ||
          strncmp("undef", tokens.text, tokens.len) == 0)
        {
        result = preproc_evaluate_define(info, &tokens);
        }
      else if (strncmp("include", tokens.text, tokens.len) == 0)
        {
        result = preproc_evaluate_include(info, &tokens);
        }
      }
    }

  if (info->ConditionalDepth > 0)
    {
    return VTK_PARSE_SKIP;
    }

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void vtkParsePreprocess_IncludeDirectory ( PreprocessInfo info,
const char *  name 
)

Add an include directory.

The directories that were added first will be searched first.

Definition at line 2566 of file vtkParsePreprocess.c.

{
  int i, n;

  n = info->NumberOfIncludeDirectories;
  for (i = 0; i < n; i++)
    {
    if (strcmp(name, info->IncludeDirectories[i]) == 0)
      {
      return;
      }
    }

  info->IncludeDirectories = (const char **)preproc_array_check(
    (char **)info->IncludeDirectories, sizeof(char *),
    info->NumberOfIncludeDirectories);
  info->IncludeDirectories[info->NumberOfIncludeDirectories++] =
    preproc_strndup(name, strlen(name));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Initialize a preprocessor symbol struct.

Definition at line 2609 of file vtkParsePreprocess.c.

{
  macro->Name = NULL;
  macro->Definition = NULL;
  macro->Comment = NULL;
  macro->NumberOfArguments = 0;
  macro->Arguments = NULL;
  macro->IsFunction = 0;
  macro->IsExternal = 0;
}

Here is the caller graph for this function:

Initialize a preprocessor struct.

Definition at line 2623 of file vtkParsePreprocess.c.

{
  info->FileName = NULL;
  info->NumberOfMacros = 0;
  info->Macros = NULL;
  info->NumberOfIncludeDirectories = 0;
  info->IncludeDirectories = NULL;
  info->NumberOfIncludeFiles = 0;
  info->IncludeFiles = NULL;
  info->IsExternal = 0;
  info->ConditionalDepth = 0;
  info->ConditionalDone = 0;
}

Here is the caller graph for this function:

int vtkParsePreprocess_RemoveMacro ( PreprocessInfo info,
const char *  name 
)

Remove a preprocessor symbol.

Return values are VTK_PARSE_OK and VTK_PARSE_MACRO_UNDEFINED.

Definition at line 2266 of file vtkParsePreprocess.c.

{
  if (preproc_remove_macro(info, name))
    {
    return VTK_PARSE_OK;
    }

  return VTK_PARSE_MACRO_UNDEFINED;
}

Here is the call graph for this function:

Here is the caller graph for this function: