Pelles C forum

Pelles C => General discussions => Topic started by: czerny on December 09, 2012, 02:26:17 PM

Title: pragma warn
Post by: czerny on December 09, 2012, 02:26:17 PM
Hello,

I have a header file:
Code: [Select]
...
#pragma warn (disable : 2145)
typedef int (*DataFunc)(void *);
...
which defines a generic function pointer 'DataFunc' which itself needs a void pointer as parameter.

This function pointer is later realized for example by a function:
Code: [Select]
int worker(int *p)
{
printf("%d\n", *p);
return 1;
}
Without the above #pragma, the compiler gives a warning #2145: Assignment of 'int __cdecl function(int *)' to 'int __cdecl function(void *)'.
With the above #pragma the compiler disables the warning globally, which I do not want.
Is there a way to disable the warning 2145 just for this single function pointer definition?

I have seen the #pragma warn (push|pop) but could not get what I am looking for.

czerny
Fertig.
Title: Re: pragma warn
Post by: laurro on December 09, 2012, 03:04:26 PM
With :

Code: [Select]
...
#pragma warn (disable : 2145)
typedef int (*DataFunc)(void *);
#pragma warn (enable: 2145)
...

Laur.
Title: Re: pragma warn
Post by: aardvajk on December 09, 2012, 09:50:32 PM
Quote
Is there a way to disable the warning 2145 just for this single function pointer definition?
I don't think so, and you wouldn't want to anyway since casting function pointers and using them is undefined behaviour.

What is defined (for data pointers) is casting to void* and back to the original type. So, a solution that's guaranteed to work and shouldn't cause any warnings is to change worker's definition to
int worker(void*)
and cast the argument back to int* inside the function.
Title: Re: pragma warn
Post by: czerny on December 09, 2012, 09:59:07 PM
With :

Code: [Select]
...
#pragma warn (disable : 2145)
typedef int (*DataFunc)(void *);
#pragma warn (enable: 2145)
...

Laur.

No!

See the following example:
Code: [Select]
#pragma warn (disable : 2145)
typedef int  (*DataFunc1)(void *);
#pragma warn (enable : 2145)
typedef int  (*DataFunc2)(void *, void *);

int Func1(int *p){}
int Func2(int *p1, int *p2){}

int main(int argc, char **argv)
{
DataFunc1 f1 = Func1;
DataFunc2 f2 = Func2;
return 1;
}

czerny
Title: Re: pragma warn
Post by: czerny on December 09, 2012, 10:01:39 PM
I don't think so, and you wouldn't want to anyway since casting function pointers and using them is undefined behaviour.
I do not want to cast a function pointer.

czerny
Title: Re: pragma warn
Post by: laurro on December 10, 2012, 10:49:05 AM
try this way:
Code: [Select]
#include <stdio.h>

 typedef int  (*DataFunc1)(void *);
 typedef int  (*DataFunc2)(void *, void *);

  int Func1(int *p){return *p*2;}
  int Func2(int *p1, int *p2){return *p1+*p2;}

#pragma warn (disable : 2145 )
DataFunc1 f1 = Func1;

#pragma warn (enable : 2145 )
DataFunc2 f2 = Func2;


 int main(int argc, char **argv)
{
int a = 10;
int b = f1(&a);
printf("%d\n\n", b);

return f2(&a,&b);
}

Laur
Title: Re: pragma warn
Post by: czerny on December 10, 2012, 12:13:40 PM
try this way:
Sorry! This solves the problem at the wrong place!

In reallity I have to solve this in a multi source file situation. The function pointers are declared in a separate module (*.c,*h) and the user (the writer of the main program per example) should not set any #pragmas which are  caused by that module.
Title: Re: pragma warn
Post by: jj2007 on March 27, 2013, 10:04:24 AM
Hi,
I am using
#pragma warn(disable:2007)        // assembly not portable
#pragma warn(disable:2215)        // conversion ... loss of data
#pragma warn(disable:2216)        // retval never used
#pragma warn(disable:2218)        // para not referenced

Works fine except for the 2218 which gets happily ignored by PellesC. Is that a known behaviour?
Title: Re: pragma warn
Post by: CLR on March 27, 2013, 10:37:43 AM
You mean 2118?
Title: Re: pragma warn
Post by: jj2007 on March 27, 2013, 10:57:29 AM
Yes - stupid typo!! Thanxalot, it was driving me mad :)