Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I force a compiler error if certain functions are called?

I have v1 and v2 versions of my software. v1 uses the registry to save settings, with lots of calls to GetProfileInt, etc. v2 now uses an sqlite db to save settings.

We are currently developing both branches and are merging new features from v1 to the v2 branch. We currently have to remember to update any registry calls to use the new config db and this has been missed a few times.

What I would like is to throw a compiler error if any of the GetProfile... or WriteProfile... functions are used in v2.

We're using C++ in Visual Studio 2010. If there's nothing built in can I use the output from a script to throw a compiler error somehow?

like image 714
jacobsee Avatar asked May 23 '12 20:05

jacobsee


People also ask

What produces a compiler error?

A compile error happens when the compiler reports something wrong with your program, and does not produce a machine-language translation. You will get compile errors.

Can a program compile with errors?

Compilation error refers to a state when a compiler fails to compile a piece of computer program source code, either due to errors in the code, or, more unusually, due to errors in the compiler itself. A compilation error message often helps programmers debugging the source code.

Which errors are thrown by compiler?

Compile-time errors are the errors that occurred when we write the wrong syntax. If we write the wrong syntax or semantics of any programming language, then the compile-time errors will be thrown by the compiler. The compiler will not allow to run the program until all the errors are removed from the program.

What type of error is a compiler error?

Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can be compiled. All these errors are detected by compiler and thus are known as compile-time errors.


2 Answers

Since this answer is accepted I might as well include the solution the asker actually used:

jacobsee discovered the deprecated pragma

#pragma deprecated(GetProfileInt)

Original answer:

You may be able to declare them as deprecated using __declspec(deprecated). It would look like this:

UINT __declspec(deprecated) WINAPI GetProfileInt(
  __in  LPCTSTR lpAppName,
  __in  LPCTSTR lpKeyName,
  __in  INT nDefault
);

You'll have to do so from a header that is included in every translation unit you care about. Doing so will result in a warning any time a translation unit that includes the deprecated declaration uses that function.

If you want a compiler error and if your project doesn't already treat warnings as errors then you'll have to turn that on, and maybe fix all the warnings you've been ignoring. (These are good practices whether you use this solution or not.)

like image 172
bames53 Avatar answered Oct 03 '22 00:10

bames53


Promoting my comment to an answer:

You can use a macro to redefine them to something that won't compile:

#define GetProfile  HAHA_Nice_try_This_will_not_compile!!!

The catch is that you need to make sure that it isn't (legitimately) called outside your code.
(So you should put the macro after all your includes.)

like image 25
Mysticial Avatar answered Oct 03 '22 01:10

Mysticial