[cfe-commits] [PATCH] Type safety attributes (was: Compile-time MPI_Datatype checking)

Dmitri Gribenko gribozavr at gmail.com
Wed May 23 13:10:09 CDT 2012


Please find attached a patch that implements a generalization of my
previous attempts at compile-time MPI_Datatype checking.  Please

I'm including an excerpt from the patch (docs/LanguageExtensions.html)
with a description of proposed attributes and usage examples below.

<!-- ======================================================================= -->
<h2 id="type_safety">Type Safety Checking</h2>
<!-- ======================================================================= -->

<p>Clang supports additional attributes to enable checking type safety
properties that can't be enforced by C type system.  Usecases include:
<li>MPI library implementations, where these attributes enable checking that
    buffer type matches the passed <tt>MPI_Datatype</tt>;</li>
<li>for HDF5 library there is a similar usecase as MPI;</li>
<li>checking types of variadic functions' arguments for functions like
    <tt>fcntl()</tt> and <tt>ioctl()</tt>.</li>

<h3 id="argument_with_type_tag"><tt>argument_with_type_tag(...)</tt></h3>

<p>Use <tt>__attribute__((argument_with_type_tag(arg_kind, arg_idx,
type_tag_idx)))</tt> on a function declaration to specify that the function
accepts a type tag that determines the type of some other argument.
<tt>arg_kind</tt> is an identifier that should be used when annotating all
applicable type tags.</p>

<p>This attribute is primarily useful for checking arguments of variadic
functions (<tt>pointer_with_type_tag</tt> can be used in most of non-variadic

<p>For example:</p>
int fcntl(int fd, int cmd, ...)
      __attribute__(( argument_with_type_tag(fcntl,3,2) ));

<h3 id="pointer_with_type_tag"><tt>pointer_with_type_tag(...)</tt></h3>

<p>Use <tt>__attribute__((pointer_with_type_tag(ptr_kind, ptr_idx,
type_tag_idx)))</tt> on a function declaration to specify that the
function a type tag that determines the pointee type of some other pointer

<p>For example:</p>
int MPI_Send(void *buf, int count, MPI_Datatype datatype /*, other
args omitted */)
    __attribute__(( pointer_with_type_tag(mpi,1,3) ));

<h3 id="type_tag_for_datatype"><tt>type_tag_for_datatype(...)</tt></h3>

<p>Clang supports annotating type tags of two forms.</p>

<p><b>Type tag that is an expression containing a reference to some declared
identifier.</b> Use <tt>__attribute__((type_tag_for_datatype(kind, type)))</tt>
on a declaration with that identifier:</p>

extern struct mpi_datatype mpi_datatype_int
    __attribute__(( type_tag_for_datatype(mpi,int) ));
#define MPI_INT ((MPI_Datatype) &amp;mpi_datatype_int)

<p><b>Type tag that is an integral literal.</b>  Introduce a <tt>static
const</tt> variable with a corresponding initializer value and attach
<tt>__attribute__((type_tag_for_datatype(kind, type)))</tt> on that
declaration, for example</p>

#define MPI_INT ((MPI_Datatype) 42)
static const MPI_Datatype mpi_datatype_int
    __attribute__(( type_tag_for_datatype(mpi,int) )) = 42

<p>The attribute also accepts an optional third argument
<tt>layout_compatible<tt>, that will cause types to be compared according to
layout-compatibility rules.  This is implemented to support annotating types
like <tt>MPI_DOUBLE_INT</tt>.</p>

Dmitri Gribenko

(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mpi-datatype-check-v3.patch
Type: application/octet-stream
Size: 63889 bytes
Desc: not available
Url : http://lists.cs.uiuc.edu/pipermail/cfe-commits/attachments/20120523/1f4b2903/attachment-0001.obj 

More information about the cfe-commits mailing list