Solved

Can I add a comment or line of code to a function or above so when the application is built it throws warnings if the function is used?

Posted on 2008-06-12
11
337 Views
Last Modified: 2013-12-13
I have seen something like this for Visual Studio, but have not tested it:
#pragma deprecated
http://msdn.microsoft.com/en-us/library/8wsycdzs(VS.71).aspx

Is there something similar for GCC or other compilers or do they recognize this?

It would be very useful to get warnings when we are using deprecated functions.
0
Comment
Question by:ctangent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 21769931
Some versions of gcc will warn if you use deprecated C/C++ features. Unfortunately, Microsoft went a bit mad and marked functions as deprecated when they are still valid Posix/ANSI CC++ functions.

For more on gcc see this thread: { http:Q_23478482.html }
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 21770482
I think ctangent means that he wants to do this for his own functions. I don't know of any gcc #pragma that would do that. You could do something like this though :

        void fun() {
            /* do something */
        }

        #define fun _Pragma("\"Warning: deprecated function 'fun' called\"") fun

        int main(void) {
            fun();                      /* <--- will generate the above warning */
            return 0;
        }
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21770771
>> I think ctangent means that he wants to do this for his own functions
Maybe, but I was responding to this point : "It would be very useful to get warnings when we are using deprecated functions.", which I interpreted to mean those deprecated in the standard.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:ctangent
ID: 21771119
evilrix, infinity is correct. I'm interested in calling my own functions deprecated.

Infinity08, would that method work for all compilers?

So at the top of any function I could do:

#define myfunction _Pragma("WARNING HERE") myfunction
void myfunction()
{

}
for any function and it would work (even if function takes arguments etc.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 21771257
>>would that method work for all compilers?

#pragma directives are very compiler specific and not covered by any standard. GCC even discourages their usage (http://gcc.gnu.org/onlinedocs/gcc/Pragmas.html#Pragmas). If you want something portable, maybe the #error preprocessor directive would help.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21771315
>> I'm interested in calling my own functions deprecated.
Try __attribute__ ((deprecated))
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 21771325
Odd... it didn't post my example! I'll try again :)

>> I'm interested in calling my own functions deprecated.
Try __attribute__ ((deprecated))

int main()
{
        float f= 1.1f;
}
rcormier@satan:~/tmp$ cat x.cpp
#include <iostream>
 
void myfunc(int i) __attribute__ ((deprecated));
 
void myfunc(int i)
{
        std::cout << "hello" << std::endl;
}
 
int main()
{
        myfunc(0);
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21771332
Arrr, now I've posted too much... undo undo!

>> I'm interested in calling my own functions deprecated.
Try __attribute__ ((deprecated))

#include <iostream>
 
void myfunc(int i) __attribute__ ((deprecated));
 
void myfunc(int i)
{
        std::cout << "hello" << std::endl;
}
 
int main()
{
        myfunc(0);
}

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 21771337
>> __attribute__ ((deprecated))
Documentation for all function attributes: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 21773954
>> Infinity08, would that method work for all compilers?

Difficult to say, since pragma's are completely implementation defined. It does for gcc - that I tested (and that was what your question was about if I'm not mistaken). Note that the _Pragma operator is part of C99.


>> So at the top of any function I could do:

No, it should not be seen by the function definition, or the function definition itself will generate the warning. Ie. you should place it in the header file AFTER the function declaration.


>> (even if function takes arguments etc.

Yes. It just uses the function name.



But, evilrix's __attribute__ ((deprecated)) solution is nicer :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21774112
>> evilrix's __attribute__ ((deprecated)) solution is nicer
Thanks :)

Yours was pretty neat too -- but I just decided to RTM ;)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let’s list some of the technologies that enable smooth teleworking. 
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Using Adobe Premiere Pro, the viewer will learn how to set up a sequence with proper settings, importing pictures, rendering, and exporting the finished product.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question