1
0
mirror of https://git.savannah.gnu.org/git/gperf.git synced 2025-12-02 13:09:22 +00:00

Remove all occurrences of reinterpret_cast<>.

* src/keyword-list.h (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Define as a typedef, not as a subclass.
(copy_list, delete_list, mergesort_list): Templatize accordingly.
* src/keyword-list.icc (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Remove method definitions.
* src/keyword-list.cc (Keyword_List): Add template parameter list <KT>.
(copy_list, mergesort_list): Remove definitions on subclass.
Add explicit template instantiation.
* src/keyword.h (Keyword_Factory): Add template parameter list <KT>.
* src/keyword.cc (Keyword_Factory): Likewise.
Add explicit template instantiation.
* src/input.h (Input): Add template parameter list <KT>.
* src/input.cc (Input): Likewise. Add explicit template instantiation.
* src/main.cc (KeywordExt_Factory): Define as a typedef, not as a
subclass.
(main): Update.
This commit is contained in:
Bruno Haible
2025-04-05 14:18:27 +02:00
parent 67c697622f
commit a041291c80
9 changed files with 1060 additions and 1019 deletions

View File

@@ -1,3 +1,23 @@
2025-04-05 Bruno Haible <bruno@clisp.org>
Remove all occurrences of reinterpret_cast<>.
* src/keyword-list.h (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Define as a typedef, not as a subclass.
(copy_list, delete_list, mergesort_list): Templatize accordingly.
* src/keyword-list.icc (Keyword_List): Add template parameter list <KT>.
(KeywordExt_List): Remove method definitions.
* src/keyword-list.cc (Keyword_List): Add template parameter list <KT>.
(copy_list, mergesort_list): Remove definitions on subclass.
Add explicit template instantiation.
* src/keyword.h (Keyword_Factory): Add template parameter list <KT>.
* src/keyword.cc (Keyword_Factory): Likewise.
Add explicit template instantiation.
* src/input.h (Input): Add template parameter list <KT>.
* src/input.cc (Input): Likewise. Add explicit template instantiation.
* src/main.cc (KeywordExt_Factory): Define as a typedef, not as a
subclass.
(main): Update.
2025-04-05 Bruno Haible <bruno@clisp.org> 2025-04-05 Bruno Haible <bruno@clisp.org>
Make output on Windows identical to output on Unix. Make output on Windows identical to output on Unix.

View File

@@ -1,5 +1,5 @@
/* Input routines. /* Input routines.
Copyright (C) 1989-1998, 2002-2004, 2011, 2017-2018 Free Software Foundation, Inc. Copyright (C) 1989-1998, 2002-2004, 2011, 2017-2018, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu> Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>. and Bruno Haible <bruno@clisp.org>.
@@ -28,7 +28,8 @@
#include "options.h" #include "options.h"
#include "getline.h" #include "getline.h"
Input::Input (FILE *stream, Keyword_Factory *keyword_factory) template <class KT>
Input<KT>::Input (FILE *stream, Keyword_Factory<KT> *keyword_factory)
: _stream (stream), _factory (keyword_factory) : _stream (stream), _factory (keyword_factory)
{ {
} }
@@ -226,8 +227,9 @@ is_define_declaration (const char *line, const char *line_end,
} }
/* Reads the entire input file. */ /* Reads the entire input file. */
template <class KT>
void void
Input::read_input () Input<KT>::read_input ()
{ {
/* The input file has the following structure: /* The input file has the following structure:
DECLARATIONS DECLARATIONS
@@ -774,7 +776,7 @@ Input::read_input ()
/* Parse the keywords section. */ /* Parse the keywords section. */
{ {
Keyword_List **list_tail = &_head; Keyword_List<KT> **list_tail = &_head;
const char *delimiters = option.get_delimiters (); const char *delimiters = option.get_delimiters ();
unsigned int lineno = keywords_lineno; unsigned int lineno = keywords_lineno;
bool charset_dependent = false; bool charset_dependent = false;
@@ -995,9 +997,9 @@ Input::read_input ()
} }
/* Allocate Keyword and add it to the list. */ /* Allocate Keyword and add it to the list. */
Keyword *new_kw = _factory->create_keyword (keyword, keyword_length, KT *new_kw = _factory->create_keyword (keyword, keyword_length,
rest, lineno); rest, lineno);
*list_tail = new Keyword_List (new_kw); *list_tail = new Keyword_List<KT> (new_kw);
list_tail = &(*list_tail)->rest(); list_tail = &(*list_tail)->rest();
} }
@@ -1021,7 +1023,8 @@ Input::read_input ()
_input_end = input_end; _input_end = input_end;
} }
Input::~Input () template <class KT>
Input<KT>::~Input ()
{ {
/* Free allocated memory. */ /* Free allocated memory. */
delete[] const_cast<char*>(_return_type); delete[] const_cast<char*>(_return_type);
@@ -1029,3 +1032,19 @@ Input::~Input ()
delete[] const_cast<char*>(_struct_decl); delete[] const_cast<char*>(_struct_decl);
delete[] _input; delete[] _input;
} }
/* ------------------------------------------------------------------------- */
/* Explicit template instantiations. Needed to avoid link-time errors.
C++ is just misdesigned: The most important aspect in building large
software packages is information hiding. (That's the point of having the
implementation of a .h file in a .cc file, isn't it? And of having
classes with private fields and methods, isn't it?) The fact that we
need the instantiation of the Input<KT> class only for KT = KeywordExt
comes from the code in main.cc. It is ugly that implementation details
of main.cc have an influence into this file here. */
template class Input<KeywordExt>;
/* ------------------------------------------------------------------------- */

View File

@@ -2,7 +2,7 @@
/* Input routines. /* Input routines.
Copyright (C) 1989-1998, 2002-2003 Free Software Foundation, Inc. Copyright (C) 1989-1998, 2002-2003, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu> Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>. and Bruno Haible <bruno@clisp.org>.
@@ -27,17 +27,18 @@
#include <stdio.h> #include <stdio.h>
#include "keyword-list.h" #include "keyword-list.h"
template <class KT>
class Input class Input
{ {
public: public:
Input (FILE *stream, Keyword_Factory *keyword_factory); Input (FILE *stream, Keyword_Factory<KT> *keyword_factory);
~Input (); ~Input ();
void read_input (); void read_input ();
private: private:
/* Input stream. */ /* Input stream. */
FILE * _stream; FILE * _stream;
/* Creates the keywords. */ /* Creates the keywords. */
Keyword_Factory * const _factory; Keyword_Factory<KT> * const _factory;
public: public:
/* Memory block containing the entire input. */ /* Memory block containing the entire input. */
char * _input; char * _input;
@@ -58,7 +59,7 @@ public:
/* Shorthand for user-defined struct tag type. */ /* Shorthand for user-defined struct tag type. */
const char * _struct_tag; const char * _struct_tag;
/* List of all keywords. */ /* List of all keywords. */
Keyword_List * _head; Keyword_List<KT> * _head;
/* Whether the keyword chars would have different values in a different /* Whether the keyword chars would have different values in a different
character set. */ character set. */
bool _charset_dependent; bool _charset_dependent;

View File

@@ -1,6 +1,6 @@
/* Keyword list. /* Keyword list.
Copyright (C) 2002-2003 Free Software Foundation, Inc. Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>. Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF. This file is part of GNU GPERF.
@@ -23,33 +23,27 @@
#include <stddef.h> #include <stddef.h>
/* -------------------------- Keyword_List class --------------------------- */ /* ------------------------ Keyword_List<KT> class ------------------------- */
/* Constructor. */ /* Constructor. */
Keyword_List::Keyword_List (Keyword *car) template <class KT>
Keyword_List<KT>::Keyword_List (KT *car)
: _cdr (NULL), _car (car) : _cdr (NULL), _car (car)
{ {
} }
/* ------------------------- KeywordExt_List class ------------------------- */ /* ---------------------- Keyword_List<KT> functions ----------------------- */
/* Constructor. */
KeywordExt_List::KeywordExt_List (KeywordExt *car)
: Keyword_List (car)
{
}
/* ------------------------ Keyword_List functions ------------------------- */
/* Copies a linear list, sharing the list elements. */ /* Copies a linear list, sharing the list elements. */
Keyword_List * template <class KT>
copy_list (Keyword_List *list) Keyword_List<KT> *
copy_list (Keyword_List<KT> *list)
{ {
Keyword_List *result; Keyword_List<KT> *result;
Keyword_List **lastp = &result; Keyword_List<KT> **lastp = &result;
while (list != NULL) while (list != NULL)
{ {
Keyword_List *new_cons = new Keyword_List (list->first()); Keyword_List<KT> *new_cons = new Keyword_List<KT> (list->first());
*lastp = new_cons; *lastp = new_cons;
lastp = &new_cons->rest(); lastp = &new_cons->rest();
list = list->rest(); list = list->rest();
@@ -58,34 +52,33 @@ copy_list (Keyword_List *list)
return result; return result;
} }
/* Copies a linear list, sharing the list elements. */
KeywordExt_List *
copy_list (KeywordExt_List *list)
{
return static_cast<KeywordExt_List *> (copy_list (static_cast<Keyword_List *> (list)));
}
/* Deletes a linear list, keeping the list elements in memory. */ /* Deletes a linear list, keeping the list elements in memory. */
template <class KT>
void void
delete_list (Keyword_List *list) delete_list (Keyword_List<KT> *list)
{ {
while (list != NULL) while (list != NULL)
{ {
Keyword_List *rest = list->rest(); Keyword_List<KT> *rest = list->rest();
delete list; delete list;
list = rest; list = rest;
} }
} }
/* Type of a comparison function. */ /* Type of a comparison function. */
typedef bool (*Keyword_Comparison) (Keyword *keyword1, Keyword *keyword2); #if 0 /* wishful thinking */
template <class KT>
typedef bool (*Keyword_Comparison) (KT *keyword1, KT *keyword2);
#endif
/* Merges two sorted lists together to form one sorted list. */ /* Merges two sorted lists together to form one sorted list. */
static Keyword_List * template <class KT>
merge (Keyword_List *list1, Keyword_List *list2, Keyword_Comparison less) static Keyword_List<KT> *
merge (Keyword_List<KT> *list1, Keyword_List<KT> *list2,
bool (*less) (KT *keyword1, KT *keyword2))
{ {
Keyword_List *result; Keyword_List<KT> *result;
Keyword_List **resultp = &result; Keyword_List<KT> **resultp = &result;
for (;;) for (;;)
{ {
if (!list1) if (!list1)
@@ -119,8 +112,10 @@ merge (Keyword_List *list1, Keyword_List *list2, Keyword_Comparison less)
/* Sorts a linear list, given a comparison function. /* Sorts a linear list, given a comparison function.
Note: This uses a variant of mergesort that is *not* a stable sorting Note: This uses a variant of mergesort that is *not* a stable sorting
algorithm. */ algorithm. */
Keyword_List * template <class KT>
mergesort_list (Keyword_List *list, Keyword_Comparison less) Keyword_List<KT> *
mergesort_list (Keyword_List<KT> *list,
bool (*less) (KT *keyword1, KT *keyword2))
{ {
if (list == NULL || list->rest() == NULL) if (list == NULL || list->rest() == NULL)
/* List of length 0 or 1. Nothing to do. */ /* List of length 0 or 1. Nothing to do. */
@@ -128,8 +123,8 @@ mergesort_list (Keyword_List *list, Keyword_Comparison less)
else else
{ {
/* Determine a list node in the middle. */ /* Determine a list node in the middle. */
Keyword_List *middle = list; Keyword_List<KT> *middle = list;
for (Keyword_List *temp = list->rest();;) for (Keyword_List<KT> *temp = list->rest();;)
{ {
temp = temp->rest(); temp = temp->rest();
if (temp == NULL) if (temp == NULL)
@@ -143,7 +138,7 @@ mergesort_list (Keyword_List *list, Keyword_Comparison less)
/* Cut the list into two halves. /* Cut the list into two halves.
If the list has n elements, the left half has ceiling(n/2) elements If the list has n elements, the left half has ceiling(n/2) elements
and the right half has floor(n/2) elements. */ and the right half has floor(n/2) elements. */
Keyword_List *right_half = middle->rest(); Keyword_List<KT> *right_half = middle->rest();
middle->rest() = NULL; middle->rest() = NULL;
/* Sort the two halves, then merge them. */ /* Sort the two halves, then merge them. */
@@ -153,16 +148,6 @@ mergesort_list (Keyword_List *list, Keyword_Comparison less)
} }
} }
KeywordExt_List *
mergesort_list (KeywordExt_List *list,
bool (*less) (KeywordExt *keyword1, KeywordExt *keyword2))
{
return
static_cast<KeywordExt_List *>
(mergesort_list (static_cast<Keyword_List *> (list),
reinterpret_cast<Keyword_Comparison> (less)));
}
#ifndef __OPTIMIZE__ #ifndef __OPTIMIZE__
@@ -171,3 +156,27 @@ mergesort_list (KeywordExt_List *list,
#undef INLINE #undef INLINE
#endif /* not defined __OPTIMIZE__ */ #endif /* not defined __OPTIMIZE__ */
/* ------------------------------------------------------------------------- */
/* Explicit template instantiations. Needed to avoid link-time errors.
C++ is just misdesigned: The most important aspect in building large
software packages is information hiding. (That's the point of having the
implementation of a .h file in a .cc file, isn't it? And of having
classes with private fields and methods, isn't it?) The fact that we
need the instantiation of the Keyword_List<KT> class and associate functions
only for KT = KeywordExt comes from the code in main.cc. It is ugly that
implementation details of main.cc have an influence into this file here. */
template class Keyword_List<KeywordExt>;
template Keyword_List<KeywordExt> *
copy_list<KeywordExt> (Keyword_List<KeywordExt> *list);
template void
delete_list<KeywordExt> (Keyword_List<KeywordExt> *list);
template Keyword_List<KeywordExt> *
mergesort_list<KeywordExt> (Keyword_List<KeywordExt> *list,
bool (*less) (KeywordExt *keyword1,
KeywordExt *keyword2));
/* ------------------------------------------------------------------------- */

View File

@@ -2,7 +2,7 @@
/* Keyword list. /* Keyword list.
Copyright (C) 2002-2003 Free Software Foundation, Inc. Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>. Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF. This file is part of GNU GPERF.
@@ -25,52 +25,42 @@
#include "keyword.h" #include "keyword.h"
/* List node of a linear list of Keyword. */ /* List node of a linear list of KT, where KT is a subclass of Keyword. */
template <class KT>
class Keyword_List class Keyword_List
{ {
public: public:
/* Constructor. */ /* Constructor. */
Keyword_List (Keyword *car); Keyword_List (KT *car);
/* Access to first element of list. */ /* Access to first element of list. */
Keyword * first () const; KT * first () const;
/* Access to next element of list. */ /* Access to next element of list. */
Keyword_List *& rest (); Keyword_List<KT> *& rest ();
protected: protected:
Keyword_List * _cdr; Keyword_List<KT> * _cdr;
Keyword * const _car; KT * const _car;
}; };
/* List node of a linear list of KeywordExt. */ /* List node of a linear list of KeywordExt. */
class KeywordExt_List : public Keyword_List typedef Keyword_List<KeywordExt> KeywordExt_List;
{
public:
/* Constructor. */
KeywordExt_List (KeywordExt *car);
/* Access to first element of list. */
KeywordExt * first () const;
/* Access to next element of list. */
KeywordExt_List *& rest ();
};
/* Copies a linear list, sharing the list elements. */ /* Copies a linear list, sharing the list elements. */
extern Keyword_List * copy_list (Keyword_List *list); template <class KT>
extern KeywordExt_List * copy_list (KeywordExt_List *list); extern Keyword_List<KT> * copy_list (Keyword_List<KT> *list);
/* Deletes a linear list, keeping the list elements in memory. */ /* Deletes a linear list, keeping the list elements in memory. */
extern void delete_list (Keyword_List *list); template <class KT>
extern void delete_list (Keyword_List<KT> *list);
/* Sorts a linear list, given a comparison function. /* Sorts a linear list, given a comparison function.
Note: This uses a variant of mergesort that is *not* a stable sorting Note: This uses a variant of mergesort that is *not* a stable sorting
algorithm. */ algorithm. */
extern Keyword_List * mergesort_list (Keyword_List *list, template <class KT>
bool (*less) (Keyword *keyword1, extern Keyword_List<KT> * mergesort_list (Keyword_List<KT> *list,
Keyword *keyword2)); bool (*less) (KT *keyword1,
extern KeywordExt_List * mergesort_list (KeywordExt_List *list, KT *keyword2));
bool (*less) (KeywordExt *keyword1,
KeywordExt *keyword2));
#ifdef __OPTIMIZE__ #ifdef __OPTIMIZE__

View File

@@ -1,6 +1,6 @@
/* Inline Functions for keyword-list.{h,cc}. /* Inline Functions for keyword-list.{h,cc}.
Copyright (C) 2002-2003 Free Software Foundation, Inc. Copyright (C) 2002-2003, 2025 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>. Written by Bruno Haible <bruno@clisp.org>.
This file is part of GNU GPERF. This file is part of GNU GPERF.
@@ -18,34 +18,20 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* -------------------------- Keyword_List class --------------------------- */ /* ------------------------ Keyword_List<KT> class ------------------------- */
/* Access to first element of list. */ /* Access to first element of list. */
INLINE Keyword * template <class KT>
Keyword_List::first () const INLINE KT *
Keyword_List<KT>::first () const
{ {
return _car; return _car;
} }
/* Access to next element of list. */ /* Access to next element of list. */
INLINE Keyword_List *& template <class KT>
Keyword_List::rest () INLINE Keyword_List<KT> *&
Keyword_List<KT>::rest ()
{ {
return _cdr; return _cdr;
} }
/* ------------------------- KeywordExt_List class ------------------------- */
/* Access to first element of list. */
INLINE KeywordExt *
KeywordExt_List::first () const
{
return static_cast<KeywordExt*>(_car);
}
/* Access to next element of list. */
INLINE KeywordExt_List *&
KeywordExt_List::rest ()
{
return *reinterpret_cast<KeywordExt_List**>(&_cdr);
}

View File

@@ -1,5 +1,5 @@
/* Keyword data. /* Keyword data.
Copyright (C) 1989-1998, 2000, 2002-2003 Free Software Foundation, Inc. Copyright (C) 1989-1998, 2000, 2002-2003, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu> Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>. and Bruno Haible <bruno@clisp.org>.
@@ -136,11 +136,13 @@ KeywordExt::delete_selchars ()
/* ------------------------- Keyword_Factory class ------------------------- */ /* ------------------------- Keyword_Factory class ------------------------- */
Keyword_Factory::Keyword_Factory () template <class KT>
Keyword_Factory<KT>::Keyword_Factory ()
{ {
} }
Keyword_Factory::~Keyword_Factory () template <class KT>
Keyword_Factory<KT>::~Keyword_Factory ()
{ {
} }
@@ -157,3 +159,19 @@ char empty_string[1] = "";
#undef INLINE #undef INLINE
#endif /* not defined __OPTIMIZE__ */ #endif /* not defined __OPTIMIZE__ */
/* ------------------------------------------------------------------------- */
/* Explicit template instantiations. Needed to avoid link-time errors.
C++ is just misdesigned: The most important aspect in building large
software packages is information hiding. (That's the point of having the
implementation of a .h file in a .cc file, isn't it? And of having
classes with private fields and methods, isn't it?) The fact that we
need the instantiation of the Keyword_Factory<KT> class only for
KT = KeywordExt comes from the code in main.cc. It is ugly that
implementation details of main.cc have an influence into this file here. */
template class Keyword_Factory<KeywordExt>;
/* ------------------------------------------------------------------------- */

View File

@@ -2,7 +2,7 @@
/* Keyword data. /* Keyword data.
Copyright (C) 1989-1998, 2000, 2002-2003, 2017 Free Software Foundation, Inc. Copyright (C) 1989-1998, 2000, 2002-2003, 2017, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu> Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>. and Bruno Haible <bruno@clisp.org>.
@@ -86,6 +86,7 @@ private:
This factory is used to make the Input class independent of the concrete This factory is used to make the Input class independent of the concrete
class KeywordExt. */ class KeywordExt. */
template <class KT>
class Keyword_Factory class Keyword_Factory
{ {
public: public:
@@ -95,9 +96,9 @@ public:
virtual ~Keyword_Factory (); virtual ~Keyword_Factory ();
/* Creates a new Keyword. */ /* Creates a new Keyword. */
virtual /*abstract*/ Keyword * virtual /*abstract*/ KT *
create_keyword (const char *allchars, int allchars_length, create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno) = 0; const char *rest, unsigned int lineno);
}; };
/* A statically allocated empty string. */ /* A statically allocated empty string. */

View File

@@ -1,5 +1,5 @@
/* Driver program for the hash function generator /* Driver program for the hash function generator
Copyright (C) 1989-1998, 2000, 2002-2003, 2009, 2017 Free Software Foundation, Inc. Copyright (C) 1989-1998, 2000, 2002-2003, 2009, 2017, 2025 Free Software Foundation, Inc.
Written by Douglas C. Schmidt <schmidt@ics.uci.edu> Written by Douglas C. Schmidt <schmidt@ics.uci.edu>
and Bruno Haible <bruno@clisp.org>. and Bruno Haible <bruno@clisp.org>.
@@ -31,14 +31,11 @@
/* This Keyword factory produces KeywordExt instances. */ /* This Keyword factory produces KeywordExt instances. */
class KeywordExt_Factory : public Keyword_Factory typedef Keyword_Factory<KeywordExt> KeywordExt_Factory;
{
virtual Keyword * create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno);
};
Keyword * template <>
KeywordExt_Factory::create_keyword (const char *allchars, int allchars_length, KeywordExt *
Keyword_Factory<KeywordExt>::create_keyword (const char *allchars, int allchars_length,
const char *rest, unsigned int lineno) const char *rest, unsigned int lineno)
{ {
return new KeywordExt (allchars, allchars_length, rest, lineno); return new KeywordExt (allchars, allchars_length, rest, lineno);
@@ -66,11 +63,11 @@ main (int argc, char *argv[])
{ {
/* Initialize the keyword list. */ /* Initialize the keyword list. */
KeywordExt_Factory factory; KeywordExt_Factory factory;
Input inputter (stdin, &factory); Input<KeywordExt> inputter (stdin, &factory);
inputter.read_input (); inputter.read_input ();
/* We can cast the keyword list to KeywordExt_List* because its list /* We can cast the keyword list to KeywordExt_List* because its list
elements were created by KeywordExt_Factory. */ elements were created by KeywordExt_Factory. */
KeywordExt_List* list = static_cast<KeywordExt_List*>(inputter._head); KeywordExt_List* list = inputter._head;
{ {
/* Search for a good hash function. */ /* Search for a good hash function. */