Type-checking casts for GNU C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To the C-programming community,


this is word to let the world know that I have found a way to make the 
C++ new-style casts, specifically static_cast<> and const_cast<>, in 
GNU C (that's right, not C++).

I had seen annotation-only "casts" in C projects (and not just my own) 
before. Just grepping -Pin '\w+_cast\(' in all .tar.gz/.tar.bz2 in just 
a bunch of .src.rpms ([gklmnop]* was my run) of the distro already 
reveal quite a bit. It looks like

---<8--- from libjasper
/* The below macro is intended to be used for type casts.  By using this 
  macro, type casts can be easily located in the source code with 
  tools like "grep". */ 
#define JAS_CAST(t, e) \ 
        ((t) (e))
--->8---

---<8--- from mc (midnight commander)
/* C++ style type casts */ 
#define const_cast(m_type, m_expr) ((m_type) (m_expr)) 
--->8---

Essentially it boils down to just the plain old cast, and it does not do 
much except the annotation -- you can still have Freudian slips with 
these.

But the presence of the ARRAY_SIZE/BUILD_BUG_ON macros in the Linux 
kernel source([1]) inspired me to try something similarly with GCC's 
extensions, for fun and profit.

[1] http://lwn.net/Articles/226007/

Result are some macros that actually _do_ type-checking instead of just 
annotation and giving something to grep for. (Trying to reach for the 
art market.) I am not aware of this having been done before, as it is 
pretty impossible to grep or google for such without turning up lots of 
C++ code; it is further my assumption that since the GCC code written by 
those who understand it thoroughly, but which only uses "CONST_CAST" in 
annotation form only without type checking, that my approach could 
indeed be novel.

The reference implementation is in libHX starting from 2.0.
Linux distro packages will be doing an update soon.

Direct view of defs.h: http://tinyurl.com/7grobb
FM project page with URLs: http://freshmeat.net/projects/libHX/

It is recommended to have a read at the documentation (reachable
via the "Demo" link on the FM project page) to get the idea of
the numbered const_castN.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux