NO

Author Topic: pragma warn  (Read 7597 times)

czerny

  • Guest
pragma warn
« 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.

laurro

  • Guest
Re: pragma warn
« Reply #1 on: December 09, 2012, 03:04:26 PM »
With :

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

Laur.

aardvajk

  • Guest
Re: pragma warn
« Reply #2 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.

czerny

  • Guest
Re: pragma warn
« Reply #3 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

czerny

  • Guest
Re: pragma warn
« Reply #4 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

laurro

  • Guest
Re: pragma warn
« Reply #5 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

czerny

  • Guest
Re: pragma warn
« Reply #6 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.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: pragma warn
« Reply #7 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?

CLR

  • Guest
Re: pragma warn
« Reply #8 on: March 27, 2013, 10:37:43 AM »
You mean 2118?

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: pragma warn
« Reply #9 on: March 27, 2013, 10:57:29 AM »
Yes - stupid typo!! Thanxalot, it was driving me mad :)