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
339 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
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

 

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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

622 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