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
335 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
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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ā€¦
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Viewers will learn how to use the Hootsuite Dashboard.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now