Merge Carl's preprocessor into the glcpp subdirectory.

This commit is contained in:
Kenneth Graunke
2010-06-21 11:22:11 -07:00
147 changed files with 3508 additions and 0 deletions

7
glcpp/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
glcpp
glcpp-lex.c
glcpp-parse.c
glcpp-parse.h
*.o
*~
tests/*.out

25
glcpp/Makefile Normal file
View File

@@ -0,0 +1,25 @@
# Debug symbols by default, but let the user avoid that with something
# like "make CFLAGS=-O2"
CFLAGS = -g
# But we use 'override' here so that "make CFLAGS=-O2" will still have
# all the warnings enabled.
override CFLAGS += -Wall -Wextra -Wwrite-strings -Wswitch-enum -Wno-unused
glcpp: glcpp.o glcpp-lex.o glcpp-parse.o hash_table.o xtalloc.o
gcc -o $@ -ltalloc -lm $^
%.c %.h: %.y
bison --debug --defines=$*.h --output=$*.c $^
%.c: %.l
flex --prefix=glcpp_ --outfile=$@ $<
glcpp-lex.c: glcpp-parse.h
test: glcpp
@(cd tests; ./glcpp-test)
clean:
rm -f glcpp glcpp-lex.c glcpp-parse.c *.o *~
rm -f tests/*.out tests/*~

30
glcpp/README Normal file
View File

@@ -0,0 +1,30 @@
glcpp -- GLSL "C" preprocessor
This is a simple preprocessor designed to provide the preprocessing
needs of the GLSL language. The requirements for this preprocessor are
specified in the GLSL 1.30 specification availble from:
http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.30.08.pdf
This specification is not precise on some semantics, (for example,
#define and #if), defining these merely "as is standard for C++
preprocessors". To fill in these details, I've been using the C99
standard (for which I had a convenient copy) as available from:
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
Known limitations
-----------------
Macro invocations cannot include embedded newlines.
The __LINE__, __FILE__, and __VERSION__ macros are not yet supported.
The argument of the 'defined' operator cannot yet include enclosing
parentheses.
The #error, #pragma, #extension, #version, and #line macros are not
yet supported.
A file that ends with a function-like macro name as the last
non-whitespace token will result in a parse error, (where it should be
passed through as is).

202
glcpp/glcpp-lex.l Normal file
View File

@@ -0,0 +1,202 @@
%{
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "glcpp.h"
#include "glcpp-parse.h"
%}
%option reentrant noyywrap
%option extra-type="glcpp_parser_t *"
SPACE [[:space:]]
NONSPACE [^[:space:]]
NEWLINE [\n]
HSPACE [ \t]
HASH ^{HSPACE}*#{HSPACE}*
IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*
PUNCTUATION [][(){}.&*~!/%<>^|;,=+-]
OTHER [^][(){}.&*~!/%<>^|;,=#[:space:]+-]+
DECIMAL_INTEGER [1-9][0-9]*[uU]?
OCTAL_INTEGER 0[0-7]*[uU]?
HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
NON_STARS_THEN_STARS [^*]*[*]+
%%
/* Single-line comments */
"//"[^\n]+\n {
return NEWLINE;
}
/* Multi-line comments */
"/*"({NON_STARS_THEN_STARS}[^*/])*{NON_STARS_THEN_STARS}"/" {
if (yyextra->space_tokens)
return SPACE;
}
{HASH}if/.*\n {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
return HASH_IF;
}
{HASH}elif/.*\n {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
return HASH_ELIF;
}
{HASH}else/.*\n {
yyextra->space_tokens = 0;
return HASH_ELSE;
}
{HASH}endif/.*\n {
yyextra->space_tokens = 0;
return HASH_ENDIF;
}
/* When skipping (due to an #if 0 or similar) consume anything
* up to a newline. We do this less priroty than any
* #if-related directive (#if, #elif, #else, #endif), but with
* more priority than any other directive or token to avoid
* any side-effects from skipped content.
*
* We use the lexing_if flag to avoid skipping any part of an
* if conditional expression. */
[^\n]+/\n {
if (yyextra->lexing_if ||
yyextra->skip_stack == NULL ||
yyextra->skip_stack->type == SKIP_NO_SKIP)
{
REJECT;
}
}
{HASH}define{HSPACE}+/{IDENTIFIER}"(" {
yyextra->space_tokens = 0;
return HASH_DEFINE_FUNC;
}
{HASH}define {
yyextra->space_tokens = 0;
return HASH_DEFINE_OBJ;
}
{HASH}undef {
yyextra->space_tokens = 0;
return HASH_UNDEF;
}
{HASH} {
yyextra->space_tokens = 0;
return HASH;
}
{DECIMAL_INTEGER} {
yylval.str = xtalloc_strdup (yyextra, yytext);
return INTEGER_STRING;
}
{OCTAL_INTEGER} {
yylval.str = xtalloc_strdup (yyextra, yytext);
return INTEGER_STRING;
}
{HEXADECIMAL_INTEGER} {
yylval.str = xtalloc_strdup (yyextra, yytext);
return INTEGER_STRING;
}
"<<" {
return LEFT_SHIFT;
}
">>" {
return RIGHT_SHIFT;
}
"<=" {
return LESS_OR_EQUAL;
}
">=" {
return GREATER_OR_EQUAL;
}
"==" {
return EQUAL;
}
"!=" {
return NOT_EQUAL;
}
"&&" {
return AND;
}
"||" {
return OR;
}
"##" {
return PASTE;
}
"defined" {
return DEFINED;
}
{IDENTIFIER} {
yylval.str = xtalloc_strdup (yyextra, yytext);
return IDENTIFIER;
}
{PUNCTUATION} {
return yytext[0];
}
{OTHER}+ {
yylval.str = xtalloc_strdup (yyextra, yytext);
return OTHER;
}
{HSPACE}+ {
if (yyextra->space_tokens) {
return SPACE;
}
}
\n {
yyextra->lexing_if = 0;
return NEWLINE;
}
%%

1602
glcpp/glcpp-parse.y Normal file

File diff suppressed because it is too large Load Diff

41
glcpp/glcpp.c Normal file
View File

@@ -0,0 +1,41 @@
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "glcpp.h"
extern int yydebug;
int
main (void)
{
glcpp_parser_t *parser;
int ret;
parser = glcpp_parser_create ();
ret = glcpp_parser_parse (parser);
glcpp_parser_destroy (parser);
return ret;
}

195
glcpp/glcpp.h Normal file
View File

@@ -0,0 +1,195 @@
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef GLCPP_H
#define GLCPP_H
#include <stdint.h>
#include <talloc.h>
#include "hash_table.h"
#define yyscan_t void*
/* Some data types used for parser values. */
typedef struct string_node {
const char *str;
struct string_node *next;
} string_node_t;
typedef struct string_list {
string_node_t *head;
string_node_t *tail;
} string_list_t;
typedef struct token token_t;
typedef struct token_list token_list_t;
typedef union YYSTYPE
{
intmax_t ival;
char *str;
string_list_t *string_list;
token_t *token;
token_list_t *token_list;
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
struct token {
int type;
YYSTYPE value;
};
typedef struct token_node {
token_t *token;
struct token_node *next;
} token_node_t;
struct token_list {
token_node_t *head;
token_node_t *tail;
token_node_t *non_space_tail;
};
typedef struct argument_node {
token_list_t *argument;
struct argument_node *next;
} argument_node_t;
typedef struct argument_list {
argument_node_t *head;
argument_node_t *tail;
} argument_list_t;
typedef struct glcpp_parser glcpp_parser_t;
typedef enum {
TOKEN_CLASS_IDENTIFIER,
TOKEN_CLASS_IDENTIFIER_FINALIZED,
TOKEN_CLASS_FUNC_MACRO,
TOKEN_CLASS_OBJ_MACRO
} token_class_t;
token_class_t
glcpp_parser_classify_token (glcpp_parser_t *parser,
const char *identifier,
int *parameter_index);
typedef struct {
int is_function;
string_list_t *parameters;
const char *identifier;
token_list_t *replacements;
} macro_t;
typedef struct expansion_node {
macro_t *macro;
token_node_t *replacements;
struct expansion_node *next;
} expansion_node_t;
typedef enum skip_type {
SKIP_NO_SKIP,
SKIP_TO_ELSE,
SKIP_TO_ENDIF
} skip_type_t;
typedef struct skip_node {
skip_type_t type;
struct skip_node *next;
} skip_node_t;
typedef struct active_list {
const char *identifier;
token_node_t *marker;
struct active_list *next;
} active_list_t;
struct glcpp_parser {
yyscan_t scanner;
struct hash_table *defines;
active_list_t *active;
int lexing_if;
int space_tokens;
int newline_as_space;
int in_control_line;
int paren_count;
skip_node_t *skip_stack;
token_list_t *lex_from_list;
token_node_t *lex_from_node;
};
glcpp_parser_t *
glcpp_parser_create (void);
int
glcpp_parser_parse (glcpp_parser_t *parser);
void
glcpp_parser_destroy (glcpp_parser_t *parser);
/* Generated by glcpp-lex.l to glcpp-lex.c */
int
glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
int
glcpp_lex (yyscan_t scanner);
int
glcpp_lex_destroy (yyscan_t scanner);
/* Generated by glcpp-parse.y to glcpp-parse.c */
int
yyparse (glcpp_parser_t *parser);
/* xtalloc - wrappers around talloc to check for out-of-memory */
#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
#define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
void *
xtalloc_named_const (const void *context, size_t size, const char *name);
char *
xtalloc_strdup (const void *t, const char *p);
char *
xtalloc_strndup (const void *t, const char *p, size_t n);
char *
xtalloc_asprintf (const void *t, const char *fmt, ...);
void *
_xtalloc_reference_loc (const void *context,
const void *ptr, const char *location);
#define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__)
#endif

159
glcpp/hash_table.c Normal file
View File

@@ -0,0 +1,159 @@
/*
* Copyright © 2008 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file hash_table.c
* \brief Implementation of a generic, opaque hash table data type.
*
* \author Ian Romanick <ian.d.romanick@intel.com>
*/
#include "main/imports.h"
#include "main/simple_list.h"
#include "hash_table.h"
struct node {
struct node *next;
struct node *prev;
};
struct hash_table {
hash_func_t hash;
hash_compare_func_t compare;
unsigned num_buckets;
struct node buckets[1];
};
struct hash_node {
struct node link;
const void *key;
void *data;
};
struct hash_table *
hash_table_ctor(unsigned num_buckets, hash_func_t hash,
hash_compare_func_t compare)
{
struct hash_table *ht;
unsigned i;
if (num_buckets < 16) {
num_buckets = 16;
}
ht = _mesa_malloc(sizeof(*ht) + ((num_buckets - 1)
* sizeof(ht->buckets[0])));
if (ht != NULL) {
ht->hash = hash;
ht->compare = compare;
ht->num_buckets = num_buckets;
for (i = 0; i < num_buckets; i++) {
make_empty_list(& ht->buckets[i]);
}
}
return ht;
}
void
hash_table_dtor(struct hash_table *ht)
{
hash_table_clear(ht);
_mesa_free(ht);
}
void
hash_table_clear(struct hash_table *ht)
{
struct node *node;
struct node *temp;
unsigned i;
for (i = 0; i < ht->num_buckets; i++) {
foreach_s(node, temp, & ht->buckets[i]) {
remove_from_list(node);
_mesa_free(node);
}
assert(is_empty_list(& ht->buckets[i]));
}
}
void *
hash_table_find(struct hash_table *ht, const void *key)
{
const unsigned hash_value = (*ht->hash)(key);
const unsigned bucket = hash_value % ht->num_buckets;
struct node *node;
foreach(node, & ht->buckets[bucket]) {
struct hash_node *hn = (struct hash_node *) node;
if ((*ht->compare)(hn->key, key) == 0) {
return hn->data;
}
}
return NULL;
}
void
hash_table_insert(struct hash_table *ht, void *data, const void *key)
{
const unsigned hash_value = (*ht->hash)(key);
const unsigned bucket = hash_value % ht->num_buckets;
struct hash_node *node;
node = _mesa_calloc(sizeof(*node));
node->data = data;
node->key = key;
insert_at_head(& ht->buckets[bucket], & node->link);
}
unsigned
hash_table_string_hash(const void *key)
{
const char *str = (const char *) key;
unsigned hash = 5381;
while (*str != '\0') {
hash = (hash * 33) + *str;
str++;
}
return hash;
}

125
glcpp/hash_table.h Normal file
View File

@@ -0,0 +1,125 @@
/*
* Copyright © 2008 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file hash_table.h
* \brief Implementation of a generic, opaque hash table data type.
*
* \author Ian Romanick <ian.d.romanick@intel.com>
*/
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
struct hash_table;
typedef unsigned (*hash_func_t)(const void *key);
typedef int (*hash_compare_func_t)(const void *key1, const void *key2);
/**
* Hash table constructor
*
* Creates a hash table with the specified number of buckets. The supplied
* \c hash and \c compare routines are used when adding elements to the table
* and when searching for elements in the table.
*
* \param num_buckets Number of buckets (bins) in the hash table.
* \param hash Function used to compute hash value of input keys.
* \param compare Function used to compare keys.
*/
extern struct hash_table *hash_table_ctor(unsigned num_buckets,
hash_func_t hash, hash_compare_func_t compare);
/**
* Release all memory associated with a hash table
*
* \warning
* This function cannot release memory occupied either by keys or data.
*/
extern void hash_table_dtor(struct hash_table *ht);
/**
* Flush all entries from a hash table
*
* \param ht Table to be cleared of its entries.
*/
extern void hash_table_clear(struct hash_table *ht);
/**
* Search a hash table for a specific element
*
* \param ht Table to be searched
* \param key Key of the desired element
*
* \return
* The \c data value supplied to \c hash_table_insert when the element with
* the matching key was added. If no matching key exists in the table,
* \c NULL is returned.
*/
extern void *hash_table_find(struct hash_table *ht, const void *key);
/**
* Add an element to a hash table
*/
extern void hash_table_insert(struct hash_table *ht, void *data,
const void *key);
/**
* Compute hash value of a string
*
* Computes the hash value of a string using the DJB2 algorithm developed by
* Professor Daniel J. Bernstein. It was published on comp.lang.c once upon
* a time. I was unable to find the original posting in the archives.
*
* \param key Pointer to a NUL terminated string to be hashed.
*
* \sa hash_table_string_compare
*/
extern unsigned hash_table_string_hash(const void *key);
/**
* Compare two strings used as keys
*
* This is just a macro wrapper around \c strcmp.
*
* \sa hash_table_string_hash
*/
#define hash_table_string_compare ((hash_compare_func_t) strcmp)
#ifdef __cplusplus
};
#endif
#endif /* HASH_TABLE_H */

6
glcpp/main/imports.h Normal file
View File

@@ -0,0 +1,6 @@
#include <assert.h>
#include <stdlib.h>
#define _mesa_malloc(x) malloc(x)
#define _mesa_free(x) free(x)
#define _mesa_calloc(x) calloc(1,x)

235
glcpp/main/simple_list.h Normal file
View File

@@ -0,0 +1,235 @@
/**
* \file simple_list.h
* Simple macros for type-safe, intrusive lists.
*
* Intended to work with a list sentinal which is created as an empty
* list. Insert & delete are O(1).
*
* \author
* (C) 1997, Keith Whitwell
*/
/*
* Mesa 3-D graphics library
* Version: 3.5
*
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _SIMPLE_LIST_H
#define _SIMPLE_LIST_H
struct simple_node {
struct simple_node *next;
struct simple_node *prev;
};
/**
* Remove an element from list.
*
* \param elem element to remove.
*/
#define remove_from_list(elem) \
do { \
(elem)->next->prev = (elem)->prev; \
(elem)->prev->next = (elem)->next; \
} while (0)
/**
* Insert an element to the list head.
*
* \param list list.
* \param elem element to insert.
*/
#define insert_at_head(list, elem) \
do { \
(elem)->prev = list; \
(elem)->next = (list)->next; \
(list)->next->prev = elem; \
(list)->next = elem; \
} while(0)
/**
* Insert an element to the list tail.
*
* \param list list.
* \param elem element to insert.
*/
#define insert_at_tail(list, elem) \
do { \
(elem)->next = list; \
(elem)->prev = (list)->prev; \
(list)->prev->next = elem; \
(list)->prev = elem; \
} while(0)
/**
* Move an element to the list head.
*
* \param list list.
* \param elem element to move.
*/
#define move_to_head(list, elem) \
do { \
remove_from_list(elem); \
insert_at_head(list, elem); \
} while (0)
/**
* Move an element to the list tail.
*
* \param list list.
* \param elem element to move.
*/
#define move_to_tail(list, elem) \
do { \
remove_from_list(elem); \
insert_at_tail(list, elem); \
} while (0)
/**
* Consatinate a cyclic list to a list
*
* Appends the sequence of nodes starting with \c tail to the list \c head.
* A "cyclic list" is a list that does not have a sentinal node. This means
* that the data pointed to by \c tail is an actual node, not a dataless
* sentinal. Note that if \c tail constist of a single node, this macro
* behaves identically to \c insert_at_tail
*
* \param head Head of the list to be appended to. This may or may not
* be a cyclic list.
* \param tail Head of the cyclic list to be appended to \c head.
* \param temp Temporary \c simple_list used by the macro
*
* \sa insert_at_tail
*/
#define concat_list_and_cycle(head, tail, temp) \
do { \
(head)->prev->next = (tail); \
(tail)->prev->next = (head); \
(temp) = (head)->prev; \
(head)->prev = (tail)->prev; \
(tail)->prev = (temp); \
} while (0)
#define concat_list(head, next_list) \
do { \
(next_list)->next->prev = (head)->prev; \
(next_list)->prev->next = (head); \
(head)->prev->next = (next_list)->next; \
(head)->prev = (next_list)->prev; \
} while (0)
/**
* Make a empty list empty.
*
* \param sentinal list (sentinal element).
*/
#define make_empty_list(sentinal) \
do { \
(sentinal)->next = sentinal; \
(sentinal)->prev = sentinal; \
} while (0)
/**
* Get list first element.
*
* \param list list.
*
* \return pointer to first element.
*/
#define first_elem(list) ((list)->next)
/**
* Get list last element.
*
* \param list list.
*
* \return pointer to last element.
*/
#define last_elem(list) ((list)->prev)
/**
* Get next element.
*
* \param elem element.
*
* \return pointer to next element.
*/
#define next_elem(elem) ((elem)->next)
/**
* Get previous element.
*
* \param elem element.
*
* \return pointer to previous element.
*/
#define prev_elem(elem) ((elem)->prev)
/**
* Test whether element is at end of the list.
*
* \param list list.
* \param elem element.
*
* \return non-zero if element is at end of list, or zero otherwise.
*/
#define at_end(list, elem) ((elem) == (list))
/**
* Test if a list is empty.
*
* \param list list.
*
* \return non-zero if list empty, or zero otherwise.
*/
#define is_empty_list(list) ((list)->next == (list))
/**
* Walk through the elements of a list.
*
* \param ptr pointer to the current element.
* \param list list.
*
* \note It should be followed by a { } block or a single statement, as in a \c
* for loop.
*/
#define foreach(ptr, list) \
for( ptr=(list)->next ; ptr!=list ; ptr=(ptr)->next )
/**
* Walk through the elements of a list.
*
* Same as #foreach but lets you unlink the current value during a list
* traversal. Useful for freeing a list, element by element.
*
* \param ptr pointer to the current element.
* \param t temporary pointer.
* \param list list.
*
* \note It should be followed by a { } block or a single statement, as in a \c
* for loop.
*/
#define foreach_s(ptr, t, list) \
for(ptr=(list)->next,t=(ptr)->next; list != ptr; ptr=t, t=(t)->next)
#endif

View File

@@ -0,0 +1 @@
this is four tokens

View File

@@ -0,0 +1 @@
this is four tokens

2
glcpp/tests/001-define.c Normal file
View File

@@ -0,0 +1,2 @@
#define foo 1
foo

View File

@@ -0,0 +1,2 @@
1

View File

@@ -0,0 +1,3 @@
#define foo 1
#define bar foo
bar

View File

@@ -0,0 +1,3 @@
1

View File

@@ -0,0 +1,3 @@
#define bar foo
#define foo 1
bar

View File

@@ -0,0 +1,3 @@
1

View File

@@ -0,0 +1,6 @@
#define foo bar
#define bar baz
#define baz foo
foo
bar
baz

View File

@@ -0,0 +1,6 @@
foo
bar
baz

View File

@@ -0,0 +1,3 @@
#define foo 1
#define bar a foo
bar

View File

@@ -0,0 +1,3 @@
a 1

View File

@@ -0,0 +1,3 @@
#define bar a foo
#define foo 1
bar

View File

@@ -0,0 +1,3 @@
a 1

View File

@@ -0,0 +1,6 @@
#define foo a bar
#define bar b baz
#define baz c foo
foo
bar
baz

View File

@@ -0,0 +1,6 @@
a b c foo
b c a bar
c a b baz

View File

@@ -0,0 +1,2 @@
#define foo
foo

View File

@@ -0,0 +1,2 @@

4
glcpp/tests/009-undef.c Normal file
View File

@@ -0,0 +1,4 @@
#define foo 1
foo
#undef foo
foo

View File

@@ -0,0 +1,4 @@
1
foo

View File

@@ -0,0 +1,6 @@
#define foo 1
foo
#undef foo
foo
#define foo 2
foo

View File

@@ -0,0 +1,6 @@
1
foo
2

View File

@@ -0,0 +1,2 @@
#define foo()
foo()

View File

@@ -0,0 +1,2 @@

View File

@@ -0,0 +1,2 @@
#define foo() bar
foo()

View File

@@ -0,0 +1,2 @@
bar

View File

@@ -0,0 +1,2 @@
#define foo(x) 1
foo(bar)

View File

@@ -0,0 +1,2 @@
1

View File

@@ -0,0 +1,2 @@
#define foo(x,y) 1
foo(bar,baz)

View File

@@ -0,0 +1,2 @@
1

View File

@@ -0,0 +1,4 @@
#define foo ()1
foo()
#define bar ()2
bar()

View File

@@ -0,0 +1,4 @@
()1()
()2()

View File

@@ -0,0 +1,2 @@
#define foo(x) ((x)+1)
foo(bar)

View File

@@ -0,0 +1,2 @@
((bar)+1)

View File

@@ -0,0 +1,2 @@
#define foo(x,y) ((x)*(y))
foo(bar,baz)

View File

@@ -0,0 +1,2 @@
((bar)*(baz))

View File

@@ -0,0 +1,3 @@
#define x 0
#define foo(x) x
foo(1)

View File

@@ -0,0 +1,3 @@
1

View File

@@ -0,0 +1,2 @@
#define foo(x) (x)
foo(this is more than one word)

View File

@@ -0,0 +1,2 @@
(this is more than one word)

View File

@@ -0,0 +1,2 @@
#define foo(x,y) x,two fish,red fish,y
foo(one fish, blue fish)

View File

@@ -0,0 +1,2 @@
one fish,two fish,red fish,blue fish

View File

@@ -0,0 +1,3 @@
#define bar(x) (1+(x))
#define foo(y) (2*(y))
foo(bar(3))

View File

@@ -0,0 +1,3 @@
(2*((1+(3))))

View File

@@ -0,0 +1,2 @@
#define foo(x) (x)
foo(argument(including parens)for the win)

View File

@@ -0,0 +1,2 @@
(argument(including parens)for the win)

View File

@@ -0,0 +1,8 @@
#define noargs() 1
# define onearg(foo) foo
# define twoargs( x , y ) x y
# define threeargs( a , b , c ) a b c
noargs ( )
onearg ( 2 )
twoargs ( 3 , 4 )
threeargs ( 5 , 6 , 7 )

View File

@@ -0,0 +1,8 @@
1
2
3 4
5 6 7

View File

@@ -0,0 +1,3 @@
#define foo foo
#define bar foo
bar

View File

@@ -0,0 +1,3 @@
foo

View File

@@ -0,0 +1,2 @@
#define foo(bar) bar
foo bar

View File

@@ -0,0 +1,2 @@
foo bar

View File

@@ -0,0 +1,6 @@
#define foo(a) bar
foo
(
1
)

View File

@@ -0,0 +1,3 @@
bar

View File

@@ -0,0 +1,3 @@
#define failure() success
#define foo failure()
foo

View File

@@ -0,0 +1,3 @@
success

View File

@@ -0,0 +1,3 @@
#define success() failure
#define foo success
foo

View File

@@ -0,0 +1,3 @@
success

View File

@@ -0,0 +1,3 @@
#define bar(failure) failure
#define foo bar(success)
foo

View File

@@ -0,0 +1,3 @@
success

View File

@@ -0,0 +1,4 @@
#define baz(failure) failure
#define bar(failure) failure
#define foo bar(baz(success))
foo

View File

@@ -0,0 +1,4 @@
success

View File

@@ -0,0 +1,4 @@
#define baz(failure) failure
#define bar(failure) failure
#define foo() bar(baz(success))
foo()

View File

@@ -0,0 +1,4 @@
success

View File

@@ -0,0 +1,2 @@
#define foo(a) foo(2*(a))
foo(3)

View File

@@ -0,0 +1,2 @@
foo(2*(3))

View File

@@ -0,0 +1,2 @@
#define foo(a) foo(2*(a))
foo(foo(3))

View File

@@ -0,0 +1,2 @@
foo(2*(foo(2*(3))))

View File

@@ -0,0 +1,2 @@
#define foo(bar) bar
foo(foo)

View File

@@ -0,0 +1,2 @@
foo

View File

@@ -0,0 +1,2 @@
#define foo(bar) bar
foo(1+foo)

View File

@@ -0,0 +1,3 @@
#define bar success
#define foo(x) x
foo(more bar)

View File

@@ -0,0 +1,3 @@
more success

View File

@@ -0,0 +1,3 @@
#define expand(x) expand(x once)
#define foo(x) x
foo(expand(just))

View File

@@ -0,0 +1,3 @@
expand(just once)

View File

@@ -0,0 +1,2 @@
#define foo(x) success
foo(argument (with,embedded , commas) -- tricky)

View File

@@ -0,0 +1,2 @@
success

View File

@@ -0,0 +1,3 @@
#define foo(a) (a)
#define bar two,words
foo(bar)

View File

@@ -0,0 +1,3 @@
(two,words)

View File

@@ -0,0 +1,2 @@
#define paste(a,b) a ## b
paste(one , token)

View File

@@ -0,0 +1,2 @@
onetoken

5
glcpp/tests/041-if-0.c Normal file
View File

@@ -0,0 +1,5 @@
success_1
#if 0
failure
#endif
success_2

View File

@@ -0,0 +1,5 @@
success_1
success_2

5
glcpp/tests/042-if-1.c Normal file
View File

@@ -0,0 +1,5 @@
success_1
#if 1
success_2
#endif
success_3

View File

@@ -0,0 +1,5 @@
success_1
success_2
success_3

View File

@@ -0,0 +1,7 @@
success_1
#if 0
failure
#else
success_2
#endif
success_3

View File

@@ -0,0 +1,7 @@
success_1
success_2
success_3

View File

@@ -0,0 +1,7 @@
success_1
#if 1
success_2
#else
failure
#endif
success_3

Some files were not shown because too many files have changed in this diff Show More