[asterisk-bugs] [JIRA] (ASTERISK-20850) nested functions aren't portable

Rusty Newton (JIRA) noreply at issues.asterisk.org
Fri Apr 25 17:48:18 CDT 2014


    [ https://issues.asterisk.org/jira/browse/ASTERISK-20850?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=210846#comment-210846 ] 

Rusty Newton edited comment on ASTERISK-20850 at 4/25/14 5:46 PM:
------------------------------------------------------------------

Example of a possible clang solution (using blocks & cleanup):

===============================
#include <stdio.h>
#include <malloc.h>

#ifndef __has_feature
#define __has_feature(f) 0
#endif

#if __has_feature(blocks)
typedef void (^_raii_cleanup_block_t)(void);
static inline void _raii_cleanup_block(_raii_cleanup_block_t *b)
{ (*b)(); }

#define RAII_VAR(vartype, varname, initval, dtor) \
raii_cleanup_block_t raii_cleanup ## varname __attribute((cleanup(_raii_cleanup_block),unused)) = NULL; \
vartype varname = initval; \
_error_cleanup ## varname = ^
{ dtor(varname); }

#elif GNUC
#define RAII_VAR(vartype, varname, initval, dtor) \
auto void dtor ## varname (vartype * v); \
void dtor ## varname (vartype * v)
{ dtor(*v); }

\
vartype varname _attribute((cleanup(_dtor ## varname))) = (initval)
#else
#warning "Your compiler is not supported"
#endif

struct teststruct
{ int testA; char testB[10]; }

;

int main (void)
{ RAII_VAR(struct teststruct *, testvar, malloc(sizeof(struct teststruct)), free); testvar->testA=10; printf("testvar->testA: %d\n", testvar->testA); return 0; }

===============================
Compile using:
clang -fblocks
Or:
clang -fblocks -lBlocksRuntime

Can someone please look into this, and see if this is a viable solution. If it is, it would make clang/scan-build possible compilers for the asterisk project again. scan-build might actually be very helpfull.


[Edit by Rusty - Updating this comment based on the comment on the clone ASTERISK-23666 that was erroneously created. Plus, re-opening this issue.]


was (Author: dkdegroot):
Example of a possible clang solution (using blocks & cleanup):

===============================
#include <stdio.h>
#include <malloc.h>

#ifndef __has_feature
#define __has_feature(f) 0
#endif

#if __has_feature(blocks)
typedef void (^_raii_cleanup_block_t)(void);
static inline void _raii_cleanup_block(_raii_cleanup_block_t *b) { (*b)(); }
#define RAII_VAR(vartype, varname, initval, dtor)                                                                       \
    _raii_cleanup_block_t _raii_cleanup ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = NULL;      \
    vartype varname = initval;                                                                                          \
    _error_cleanup ## varname = ^{ dtor(varname); }
#elif __GNUC__
#define RAII_VAR(vartype, varname, initval, dtor)                                                                       \
    auto void _dtor_ ## varname (vartype * v);                                                                          \
    void _dtor_ ## varname (vartype * v) { dtor(*v); }                                                                  \
    vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
#else   
    #warning "Your compiler is not supported"
#endif

struct teststruct {
    int testA;
    char testB[10];
};

int main (void) {
    RAII_VAR(struct teststruct *, testvar, malloc(sizeof(struct teststruct)), free);
    testvar->testA=10;
    printf("testvar->testA: %d\n", testvar->testA);
    return 0;
}
===============================
Compile using:
clang -fblocks 
Or:
clang -fblocks -lBlocksRuntime 

> nested functions aren't portable
> --------------------------------
>
>                 Key: ASTERISK-20850
>                 URL: https://issues.asterisk.org/jira/browse/ASTERISK-20850
>             Project: Asterisk
>          Issue Type: Bug
>      Security Level: None
>          Components: General
>    Affects Versions: 11.1.0
>         Environment: CLANG
>            Reporter: John Nemeth
>
> Nested functions are a GCC extension that is not sanctioned by C standards, thus the use of them is non-portable.  Specifically, CLANG does not support them.  CLANG is used by OSX, current versions of FreeBSD, and NetBSD is experimenting with it.  The RAII_VAR macro in include/asterisk/utils.h creates nested functions.  This prevents Asterisk 11.* from compiling with CLANG.



--
This message was sent by Atlassian JIRA
(v6.2#6252)



More information about the asterisk-bugs mailing list