I want to develop a tool that helps in transforming a code like this:

#ifdef MYLIB_ENABLE_DEPRECATED_CODE ... some deprecated code ... #endif 

into:

#ifdef MYLIB_ENABLE_DEPRECATED_CODE MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH() ... some deprecated code ... MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_POP() #endif 

Basically, I am refactoring code to deprecate parts of it. I wrap the deprecate the code with #ifdef MYLIB_ENABLE_DEPRECATED_CODE and #endif but sometimes forget the custom macros MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH() and MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_POP(). I need a way to identify places where those custom macros are missing. Right now, I identify them during build time via CI failures etc, which is painful. How to go about developing such a tool in Clang?

5 Replies 5

You might be able to do this with a script, though probably a LLM (copilot) can help you do this too since it is a predictable pattern

A simple "search and replace" seems also possible, simpler than clang tooling if you are not familiar with it.

@Jarod42 A simple text based search may also work. This is quite a large open source project. I have started digging into how this tool can be developed.

I understand that this MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH() should appear or vanish depending on a context. Basically Op wants to sometimes disable deprecation for given symbol and in other case to have it.

IMO we need a solid example. ... some deprecated code ... can mean lots of different things and which could be done in multiple ways.

For example deprecated function should not be used at all. The only place where they can be invoked are test, but in case of test you can disable deprecation warning.

Other case is deprecating enum entry (or a constant), this value quite often must be used in production code, but you address this in different way.

There are lots of cases where issues can be address differently. So this question is to general to answer.

@Marek R Thanks for your message. What we are trying to do is this. Suppose we have some logic that needs to be deprecated, we want to wrap it this way:

#ifdef MYLIB_ENABLE_DEPRECATED_CODE MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH() ... some deprecated code ... MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_POP() #endif 

The deprecated code could be anything. MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH() and MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_POP() are based on #pragma GCC diagnostic push and #pragma GCC diagnostic pop. Suppose we only do this:

#ifdef MYLIB_ENABLE_DEPRECATED_CODE ... some deprecated code ... #endif 

Then warnings could be emitted from the deprecated code and they are caught by -Werror. So my goal really comes down to identifying blocks marked by #ifdef MYLIB_ENABLE_DEPRECATED_CODE and #endif and adding MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_PUSH() and MYLIB_IMPL_DISABLE_DEPRECATED_WARNINGS_POP() if they are missing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.