Macronator - Demacrofy C++ Code

Macronator: Detected transformation candidate

The Macronator is a plug-in for Eclipse CDT that assists to remove macros from C++ code. It leverages language features introduced with the C++11 standard to transform macros into semantically identical, type safe C++ code.

A simple example: the macro definition PI

#define PI 3.1415
can be transformed to the following C++ statement
constexpr auto PI = 3.1415;

This software is based on the paper The demacrofier by Kumar, Sutton, and Stroustrup.

Motivation

The usage of macros often leads to semantic errors that are hard to debug. Macros are preprocessed and expanded before compilation on a textual level. Therefore, macros cannot be type checked by the compiler. Also, programmers have to mentally transform macros into C++ code in order to find errors, which gets more and more difficult as the macros become sophisticated and complex.

The C++11 standard introduced new language features that can be used to transform a large portion of macros (but not all!) into semantically identical C++ statements. The Macronator detects possible transformation candidates automatically and provides a quickfix that applies the appropriate transformation.

Examples

In addition to object-like macros, the Macronator can also transform function-like macros. For example the SQUARE macro

#define SQUARE(X) (X) * (X)
gets transformed to the following inline function
template<typename T1>
inline auto SQUARE(T1&& X) -> decltype((X) * (X)) {
    return ((X) * (X));
}

For more detailed examples and explanations refer to the feature descriptions below.

Features

User Guide

macronator-1.jpg (22.5 KB) Anonymous, 20 Mar 2014 10:24

macronator-2.jpg (15.6 KB) Anonymous, 20 Mar 2014 10:24

macronator-6.png (20.9 KB) Anonymous, 20 Mar 2014 13:16

macronator-7.png (11.5 KB) Anonymous, 20 Mar 2014 13:16