Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
340 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 800 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

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 200 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 1000 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 1000 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 800 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-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
One-stop solution for Exchange Administrators to address all MS Exchange Server issues, which is known by the name of Stellar Exchange Toolkit.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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…

715 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