/* ebrowse.c --- parsing files for the ebrowse C++ browser
-Copyright (C) 1992-2011 Free Software Foundation, Inc.
+Copyright (C) 1992-2012 Free Software Foundation, Inc.
This file is part of GNU Emacs.
#include <config.h>
#include <stdio.h>
-
-#ifdef HAVE_STDLIB_H
#include <stdlib.h>
-#endif
-
#include <string.h>
#include <ctype.h>
#include <assert.h>
#define streq(X, Y) (*(X) == *(Y) && strcmp ((X) + 1, (Y) + 1) == 0)
-/* The ubiquitous `max' and `min' macros. */
-
-#ifndef max
-#define max(X, Y) ((X) > (Y) ? (X) : (Y))
-#define min(X, Y) ((X) < (Y) ? (X) : (Y))
-#endif
+#include <min-max.h>
/* Files are read in chunks of this number of bytes. */
#define READ_CHUNK_SIZE (100 * 1024)
-/* The character used as a separator in path lists (like $PATH). */
-
-#if defined(__MSDOS__)
-#define PATH_LIST_SEPARATOR ';'
-#define FILENAME_EQ(X,Y) (strcasecmp(X,Y) == 0)
+#if defined (__MSDOS__)
+#define FILENAME_EQ(X,Y) (strcasecmp (X,Y) == 0)
#else
-#if defined(WINDOWSNT)
-#define PATH_LIST_SEPARATOR ';'
-#define FILENAME_EQ(X,Y) (stricmp(X,Y) == 0)
+#if defined (WINDOWSNT)
+#define FILENAME_EQ(X,Y) (stricmp (X,Y) == 0)
#else
-#define PATH_LIST_SEPARATOR ':'
-#define FILENAME_EQ(X,Y) (streq(X,Y))
+#define FILENAME_EQ(X,Y) (streq (X,Y))
#endif
#endif
/* The default output file name. */
#define TREE_HEADER_STRUCT "[ebrowse-hs "
#define TREE_STRUCT "[ebrowse-ts "
#define MEMBER_STRUCT "[ebrowse-ms "
-#define BROWSE_STRUCT "[ebrowse-bs "
#define CLASS_STRUCT "[ebrowse-cs "
/* The name of the symbol table entry for global functions, variables,
char *inbuffer;
char *in;
-int inbuffer_size;
+size_t inbuffer_size;
/* Return the current buffer position in the input file. */
/* Function prototypes. */
-int yylex (void);
-void yyparse (void);
-void re_init_parser (void);
-const char *token_string (int);
-char *matching_regexp (void);
-void init_sym (void);
-struct sym *add_sym (const char *, struct sym *);
-void add_link (struct sym *, struct sym *);
-void add_member_defn (struct sym *, char *, char *,
- int, unsigned, int, int, int);
-void add_member_decl (struct sym *, char *, char *, int,
- unsigned, int, int, int, int);
-void dump_roots (FILE *);
-void *xmalloc (int);
-void xfree (void *);
-void add_global_defn (char *, char *, int, unsigned, int, int, int);
-void add_global_decl (char *, char *, int, unsigned, int, int, int);
-void add_define (char *, char *, int);
-void mark_inherited_virtual (void);
-void leave_namespace (void);
-void enter_namespace (char *);
-void register_namespace_alias (char *, struct link *);
-void insert_keyword (const char *, int);
-void re_init_scanner (void);
-void init_scanner (void);
-void process_file (char *);
-void add_search_path (char *);
-FILE *open_file (char *);
-int process_pp_line (void);
-int dump_members (FILE *, struct member *);
-void dump_sym (FILE *, struct sym *);
-int dump_tree (FILE *, struct sym *);
-struct member *find_member (struct sym *, char *, int, int, unsigned);
-struct member *add_member (struct sym *, char *, int, int, unsigned);
-void mark_virtual (struct sym *);
-struct sym *make_namespace (char *, struct sym *);
-char *sym_scope (struct sym *);
-char *sym_scope_1 (struct sym *);
-int skip_to (int);
-void skip_matching (void);
-void member (struct sym *, int);
-void class_body (struct sym *, int);
-void class_definition (struct sym *, int, int, int);
-void declaration (int);
-unsigned parm_list (int *);
-char *operator_name (int *);
-struct sym *parse_classname (void);
-struct sym *parse_qualified_ident_or_type (char **);
-void parse_qualified_param_ident_or_type (char **);
-int globals (int);
-void yyerror (const char *, const char *);
-void usage (int) NO_RETURN;
-void version (void) NO_RETURN;
-
-
+static char *matching_regexp (void);
+static struct sym *add_sym (const char *, struct sym *);
+static void add_global_defn (char *, char *, int, unsigned, int, int, int);
+static void add_global_decl (char *, char *, int, unsigned, int, int, int);
+static struct member *add_member (struct sym *, char *, int, int, unsigned);
+static void class_definition (struct sym *, int, int, int);
+static char *operator_name (int *);
+static void parse_qualified_param_ident_or_type (char **);
\f
/***********************************************************************
Utilities
/* Print an error in a printf-like style with the current input file
name and line number. */
-void
+static void
yyerror (const char *format, const char *s)
{
fprintf (stderr, "%s:%d: ", filename, yyline);
/* Like malloc but print an error and exit if not enough memory is
available. */
-void *
-xmalloc (int nbytes)
+static void *
+xmalloc (size_t nbytes)
{
void *p = malloc (nbytes);
if (p == NULL)
/* Like realloc but print an error and exit if out of memory. */
-void *
-xrealloc (void *p, int sz)
+static void *
+xrealloc (void *p, size_t sz)
{
p = realloc (p, sz);
if (p == NULL)
/* Like strdup, but print an error and exit if not enough memory is
available.. If S is null, return null. */
-char *
+static char *
xstrdup (char *s)
{
if (s)
/* Initialize the symbol table. This currently only sets up the
special symbol for globals (`*Globals*'). */
-void
+static void
init_sym (void)
{
global_symbols = add_sym (GLOBALS_NAME, NULL);
If a symbol for NAME already exists, return that. Otherwise
create a new symbol and set it to default values. */
-struct sym *
+static struct sym *
add_sym (const char *name, struct sym *nested_in_class)
{
struct sym *sym;
/* Add links between superclass SUPER and subclass SUB. */
-void
+static void
add_link (struct sym *super, struct sym *sub)
{
struct link *lnk, *lnk2, *p, *prev;
parameter types of functions. Value is a pointer to the member
found or null if not found. */
-struct member *
+static struct member *
find_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
{
struct member **list;
a bit set giving additional information about the member (see the
F_* defines). */
-void
+static void
add_member_decl (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int vis, int flags)
{
struct member *m;
a bit set giving additional information about the member (see the
F_* defines). */
-void
+static void
add_member_defn (struct sym *cls, char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
{
struct member *m;
REGEXP is a regular expression matching the define in the source,
if it is non-null. POS is the position in the file. */
-void
+static void
add_define (char *name, char *regexp, int pos)
{
add_global_defn (name, regexp, pos, 0, 1, SC_FRIEND, F_DEFINE);
a bit set giving additional information about the member (see the
F_* defines). */
-void
+static void
add_global_defn (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
{
int i;
a bit set giving additional information about the member (see the
F_* defines). */
-void
+static void
add_global_decl (char *name, char *regexp, int pos, unsigned int hash, int var, int sc, int flags)
{
/* Add declaration only if not already declared. Header files must
member. HASH is a hash code for the parameter types of a function.
Value is a pointer to the member's structure. */
-struct member *
+static struct member *
add_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
{
struct member *m = (struct member *) xmalloc (sizeof *m + strlen (name));
recursively, marking functions as virtual that are declared virtual
in base classes. */
-void
+static void
mark_virtual (struct sym *r)
{
struct link *p;
/* For all roots of the class tree, mark functions as virtual that
are virtual because of a virtual declaration in a base class. */
-void
+static void
mark_inherited_virtual (void)
{
struct sym *r;
/* Create and return a symbol for a namespace with name NAME. */
-struct sym *
+static struct sym *
make_namespace (char *name, struct sym *context)
{
struct sym *s = (struct sym *) xmalloc (sizeof *s + strlen (name));
}
-/* Find the symbol for namespace NAME. If not found, retrun NULL */
+/* Find the symbol for namespace NAME. If not found, return NULL */
-struct sym *
+static struct sym *
check_namespace (char *name, struct sym *context)
{
struct sym *p = NULL;
/* Find the symbol for namespace NAME. If not found, add a new symbol
for NAME to all_namespaces. */
-struct sym *
+static struct sym *
find_namespace (char *name, struct sym *context)
{
struct sym *p = check_namespace (name, context);
/* Find namespace alias with name NAME. If not found return NULL. */
-struct link *
+static struct link *
check_namespace_alias (char *name)
{
struct link *p = NULL;
/* Register the name NEW_NAME as an alias for namespace list OLD_NAME. */
-void
+static void
register_namespace_alias (char *new_name, struct link *old_name)
{
unsigned h;
/* Enter namespace with name NAME. */
-void
+static void
enter_namespace (char *name)
{
struct sym *p = find_namespace (name, current_namespace);
/* Leave the current namespace. */
-void
+static void
leave_namespace (void)
{
assert (namespace_sp > 0);
/* Write string S to the output file FP in a Lisp-readable form.
If S is null, write out `()'. */
-#define PUTSTR(s, fp) \
- do { \
- if (!s) \
- { \
- putc ('(', fp); \
- putc (')', fp); \
- putc (' ', fp); \
- } \
- else \
- { \
- putc ('"', fp); \
- fputs (s, fp); \
- putc ('"', fp); \
- putc (' ', fp); \
- } \
- } while (0)
+static inline void
+putstr (const char *s, FILE *fp)
+{
+ if (!s)
+ {
+ putc ('(', fp);
+ putc (')', fp);
+ putc (' ', fp);
+ }
+ else
+ {
+ putc ('"', fp);
+ fputs (s, fp);
+ putc ('"', fp);
+ putc (' ', fp);
+ }
+}
/* A dynamically allocated buffer for constructing a scope name. */
/* Make sure scope_buffer has enough room to add LEN chars to it. */
-void
+static void
ensure_scope_buffer_room (int len)
{
if (scope_buffer_len + len >= scope_buffer_size)
namespaces to scope_buffer. Value is a pointer to the complete
scope name constructed. */
-char *
+static char *
sym_scope_1 (struct sym *p)
{
int len;
/* Return the scope of symbol P in printed representation, i.e.
as it would appear in a C*+ source file. */
-char *
+static char *
sym_scope (struct sym *p)
{
if (!scope_buffer)
/* Dump the list of members M to file FP. Value is the length of the
list. */
-int
+static int
dump_members (FILE *fp, struct member *m)
{
int n;
for (n = 0; m; m = m->next, ++n)
{
fputs (MEMBER_STRUCT, fp);
- PUTSTR (m->name, fp);
- PUTSTR (NULL, fp); /* FIXME? scope for globals */
+ putstr (m->name, fp);
+ putstr (NULL, fp); /* FIXME? scope for globals */
fprintf (fp, "%u ", (unsigned) m->flags);
- PUTSTR (m->filename, fp);
- PUTSTR (m->regexp, fp);
+ putstr (m->filename, fp);
+ putstr (m->regexp, fp);
fprintf (fp, "%u ", (unsigned) m->pos);
fprintf (fp, "%u ", (unsigned) m->vis);
putc (' ', fp);
- PUTSTR (m->def_filename, fp);
- PUTSTR (m->def_regexp, fp);
+ putstr (m->def_filename, fp);
+ putstr (m->def_regexp, fp);
fprintf (fp, "%u", (unsigned) m->def_pos);
putc (']', fp);
putc ('\n', fp);
/* Dump class ROOT to stream FP. */
-void
+static void
dump_sym (FILE *fp, struct sym *root)
{
fputs (CLASS_STRUCT, fp);
- PUTSTR (root->name, fp);
+ putstr (root->name, fp);
/* Print scope, if any. */
if (root->namesp)
- PUTSTR (sym_scope (root), fp);
+ putstr (sym_scope (root), fp);
else
- PUTSTR (NULL, fp);
+ putstr (NULL, fp);
/* Print flags. */
fprintf (fp, "%u", root->flags);
- PUTSTR (root->filename, fp);
- PUTSTR (root->regexp, fp);
+ putstr (root->filename, fp);
+ putstr (root->regexp, fp);
fprintf (fp, "%u", (unsigned) root->pos);
- PUTSTR (root->sfilename, fp);
+ putstr (root->sfilename, fp);
putc (']', fp);
putc ('\n', fp);
}
/* Dump class ROOT and its subclasses to file FP. Value is the
number of classes written. */
-int
+static int
dump_tree (FILE *fp, struct sym *root)
{
struct link *lk;
/* Dump the entire class tree to file FP. */
-void
+static void
dump_roots (FILE *fp)
{
int i, n = 0;
if (!f_append)
{
fputs (TREE_HEADER_STRUCT, fp);
- PUTSTR (EBROWSE_FILE_VERSION, fp);
+ putstr (EBROWSE_FILE_VERSION, fp);
putc ('\"', fp);
if (!f_structs)
/* Process a preprocessor line. Value is the next character from the
input buffer not consumed. */
-int
+static int
process_pp_line (void)
{
int in_comment = 0, in_string = 0;
/* Value is the next token from the input buffer. */
-int
+static int
yylex (void)
{
int c;
position in the input buffer, or maybe a bit more if that string is
shorter than min_regexp. */
-char *
+static char *
matching_regexp (void)
{
char *p;
/* Return a printable representation of token T. */
-const char *
+static const char *
token_string (int t)
{
static char b[3];
/* Reinitialize the scanner for a new input file. */
-void
+static void
re_init_scanner (void)
{
in = inbuffer;
}
-/* Insert a keyword NAME with token value TK into the keyword hash
+/* Insert a keyword NAME with token value TKV into the keyword hash
table. */
-void
-insert_keyword (const char *name, int tk)
+static void
+insert_keyword (const char *name, int tkv)
{
const char *s;
unsigned h = 0;
h %= KEYWORD_TABLE_SIZE;
k->name = name;
- k->tk = tk;
+ k->tk = tkv;
k->next = keyword_table[h];
keyword_table[h] = k;
}
/* Initialize the scanner for the first file. This sets up the
character class vectors and fills the keyword hash table. */
-void
+static void
init_scanner (void)
{
int i;
/* Skip forward until a given token TOKEN or YYEOF is seen and return
the current lookahead token after skipping. */
-int
+static int
skip_to (int token)
{
while (!LOOKING_AT2 (YYEOF, token))
/* Skip over pairs of tokens (parentheses, square brackets,
angle brackets, curly brackets) matching the current lookahead. */
-void
+static void
skip_matching (void)
{
int open, close, n;
}
}
-void
+static void
skip_initializer (void)
{
for (;;)
/* Build qualified namespace alias (A::B::c) and return it. */
-struct link *
+static struct link *
match_qualified_namespace_alias (void)
{
struct link *head = NULL;
/* Re-initialize the parser by resetting the lookahead token. */
-void
+static void
re_init_parser (void)
{
tk = -1;
Returns a hash code for the parameter types. This value is used to
distinguish between overloaded functions. */
-unsigned
+static unsigned
parm_list (int *flags)
{
unsigned hash = 0;
/* Print position info to stdout. */
-void
+static void
print_info (void)
{
if (info_position >= 0 && BUFFER_POS () <= info_position)
the access specifier for the member (private, protected,
public). */
-void
+static void
member (struct sym *cls, int vis)
{
char *id = NULL;
/* A function or class may follow. */
case TEMPLATE:
- MATCH();
+ MATCH ();
SET_FLAG (flags, F_TEMPLATE);
/* Skip over template argument list */
SKIP_MATCHING_IF ('<');
/* Parse the body of class CLS. TAG is the tag of the class (struct,
union, class). */
-void
+static void
class_body (struct sym *cls, int tag)
{
int vis = tag == CLASS ? PRIVATE : PUBLIC;
qualified ident has the form `X<..>::Y<...>::T<...>. Returns a
symbol for that class. */
-struct sym *
+static struct sym *
parse_classname (void)
{
struct sym *last_class = NULL;
implicitly static operator has been parsed. Value is a pointer to
a static buffer holding the constructed operator name string. */
-char *
+static char *
operator_name (int *sc)
{
- static int id_size = 0;
+ static size_t id_size = 0;
static char *id = NULL;
const char *s;
- int len;
+ size_t len;
MATCH ();
len = strlen (s) + 10;
if (len > id_size)
{
- int new_size = max (len, 2 * id_size);
+ size_t new_size = max (len, 2 * id_size);
id = (char *) xrealloc (id, new_size);
id_size = new_size;
}
}
else
{
- int tokens_matched = 0;
+ size_t tokens_matched = 0;
len = 20;
if (len > id_size)
len += strlen (s) + 2;
if (len > id_size)
{
- int new_size = max (len, 2 * id_size);
+ size_t new_size = max (len, 2 * id_size);
id = (char *) xrealloc (id, new_size);
id_size = new_size;
}
`X::Y::z'. This IDENT is returned in LAST_ID. Value is the
symbol structure for the ident. */
-struct sym *
+static struct sym *
parse_qualified_ident_or_type (char **last_id)
{
struct sym *cls = NULL;
}
while (enter--)
- leave_namespace();
+ leave_namespace ();
return cls;
}
`X::Y::z'. This IDENT is returned in LAST_ID. Value is the
symbol structure for the ident. */
-void
+static void
parse_qualified_param_ident_or_type (char **last_id)
{
struct sym *cls = NULL;
static char *id = NULL;
static int id_size = 0;
- while (LOOKING_AT (IDENT))
+ assert (LOOKING_AT (IDENT));
+
+ do
{
int len = strlen (yytext) + 1;
if (len > id_size)
else
break;
}
+ while (LOOKING_AT (IDENT));
}
Current lookahead is the class name. */
-void
+static void
class_definition (struct sym *containing, int tag, int flags, int nested)
{
struct sym *current;
the storage class of *ID. FLAGS is a bit set giving additional
information about the member (see the F_* defines). */
-void
+static void
add_declarator (struct sym **cls, char **id, int flags, int sc)
{
if (LOOKING_AT2 (';', ','))
/* Parse a declaration. */
-void
+static void
declaration (int flags)
{
char *id = NULL;
parsing in an `extern "C"' block. Value is 1 if EOF is reached, 0
otherwise. */
-int
+static int
globals (int start_flags)
{
int anonymous;
/* Parse the current input file. */
-void
+static void
yyparse (void)
{
while (globals (0) == 0)
/* Add the list of paths PATH_LIST to the current search path for
input files. */
-void
+static void
add_search_path (char *path_list)
{
while (*path_list)
char *start = path_list;
struct search_path *p;
- while (*path_list && *path_list != PATH_LIST_SEPARATOR)
+ while (*path_list && *path_list != SEPCHAR)
++path_list;
p = (struct search_path *) xmalloc (sizeof *p);
else
search_path = search_path_tail = p;
- while (*path_list == PATH_LIST_SEPARATOR)
+ while (*path_list == SEPCHAR)
++path_list;
}
}
opened. Try to find FILE in search_path first, then try the
unchanged file name. */
-FILE *
+static FILE *
open_file (char *file)
{
FILE *fp = NULL;
--version display version info\n\
"
-void
+static _Noreturn void
usage (int error)
{
puts (USAGE);
# define VERSION "21"
#endif
-void
+static _Noreturn void
version (void)
{
- /* Makes it easier to update automatically. */
- char emacs_copyright[] = "Copyright (C) 2011 Free Software Foundation, Inc.";
+ char emacs_copyright[] = COPYRIGHT;
printf ("ebrowse %s\n", VERSION);
puts (emacs_copyright);
/* Parse one input file FILE, adding classes and members to the symbol
table. */
-void
+static void
process_file (char *file)
{
FILE *fp;
fp = open_file (file);
if (fp)
{
- int nread, nbytes;
+ size_t nread, nbytes;
/* Give a progress indication if needed. */
if (f_very_verbose)
}
nbytes = fread (inbuffer + nread, 1, READ_CHUNK_SIZE, fp);
- if (nbytes <= 0)
+ if (nbytes == 0)
break;
nread += nbytes;
}
- if (nread < 0)
- nread = 0;
inbuffer[nread] = '\0';
/* Reinitialize scanner and parser for the new input file. */
containing its contents without the terminating newline. Value
is null when EOF is reached. */
-char *
+static char *
read_line (FILE *fp)
{
static char *buffer;