Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile-time detection of deprecated API calls?

Is there any new, cool feature in C++11 that allows us to detect at compile time whether an API now marked as deprecated is actually called by someone?

From what I've read about the new static_assert feature it doesn't seem flexible enough to be used in that kind of analysis.

But is there anything else we could use?

Optionally, is there anything in boost allowing that kind of compile-time checking?

like image 234
cacau Avatar asked Feb 03 '14 11:02

cacau


People also ask

Which annotation can cancel out a warning on a method using the deprecated API at compile time?

You can use the @SuppressWarnings annotation to suppress warnings whenever that code is compiled. Place the @SuppressWarnings annotation at the declaration of the class, method, field, or local variable that uses a deprecated API.

What is a deprecated API?

Deprecation means that we've ended official support for the APIs, but they will continue to remain available to developers. This page highlights some of the deprecations in this release of Android. To see other deprecations, refer to the API diff report.

How to remove deprecated error in Java?

To do so, right-click on your project and go to Properties > Java Compiler > Errors/Warnings. Click Enable project specific settings and then unfold Deprecated and restricted API and select Ignore for Deprecated API. This will disable all deprecated warnings though. +1.


2 Answers

With C++14 you will have that option :

#include <iostream>

void foo( int v ) { std::cout << v << " "; }

[[deprecated("foo with float is deprecated")]]
void foo( float v ) { std::cout << v << " "; }

[[deprecated("you should not use counter anymore")]]
int counter {};

int main() {
  foo( ++counter );
  foo( 3.14f );
}

Clang gives the compilation output (here) :

main.cpp:12:10: warning: 'counter' is deprecated [-Wdeprecated-declarations]
  foo( ++counter );
         ^
main.cpp:9:5: note: 'counter' has been explicitly marked deprecated here
int counter {};
    ^
main.cpp:13:3: warning: 'foo' is deprecated [-Wdeprecated-declarations]
  foo( 3.14f );
  ^
main.cpp:6:6: note: 'foo' has been explicitly marked deprecated here
void foo( float v ) { std::cout << v << " "; }
     ^
2 warnings generated.
like image 134
galop1n Avatar answered Sep 29 '22 01:09

galop1n


Whether static_assert is too inflexible depends on requirements that you haven't specified, but if you want to disallow calls to deprecated APIs in your library, and those functions are templates, then it's perfect.

More likely you wish to emit some sort of mere warning when such calls are made and, to my knowledge, there is no new C++11 feature to do that.

Generally, C++ doesn't provide fine-grained control over specific compiler diagnostics/output, only "can compile" and "cannot compile" (though this is a gross over-simplification, the principle holds).

Instead, you will need to rely on compiler-specific features such as __declspec and __attribute__.

like image 26
Lightness Races in Orbit Avatar answered Sep 29 '22 01:09

Lightness Races in Orbit