Preprocessing simple integer arithmetic with cpp - c++

I have a following input file:
#define __SIZE_K(x) (x * 1024)
#define DT_FLASH_SIZE __SIZE_K(128)
reg = <0x08000000 DT_FLASH_SIZE>;
If I run that through a preprocessor I get this:
$ cpp -x assembler-with-cpp input.dts -E -P
reg = <0x08000000 (128 * 1024)>;
If it possible to get the macro fully evaluated? I would like to have:
reg = <0x08000000 131072>;
I would like to have devicetree source files "fully-preprocessed" and I would prefer to do this entirely in the preprocessor, but I'm not sure this is possible... The final devicetree consists of multiple files, some of which define the layout, some are headers with macros and various values depending on selected chip.

If it possible to get the macro fully evaluated?
Theoretically, yes. But it's not usable in the context you want to use it.
Preprocessor is a simple text replacement tool. It replaces one text for the other. First, you have to implement a table of all possible combinations of replacements:
#define MUL_1_1 1
#define MUL_1_2 2
#define MUL_1_3 3
// etc. for **billions** of lines
#define MUL_128_1024 the_result_here
// etc.
After those bilions of lines, you can finally write:
#define MUL_(a, b) MUL_##a##_##b
#define MUL(a, b) MUL_(a, b)
After that you can do:
#define __SIZE_K(x) MUL(x, 1024)
You can use a library - like P99_MUL or BOOST_PP_MUL - and they are basically implemented in almost the same way, with a lot of optimizations to shorten the list.
There is no point in using specifically C preprocessor in this context anyway. Use M4 or Python's Jinja2 or PHP - a full programming language, instead of some limited preprocessing C tool.

Related

Track preprocessor's replacements

In my C++ project, I have a header with a line like this:
enum { OK, ERROR_1, ERROR_2 };
When compiling with GCC (v 9.4.0), I get
error: expected identifier before '(' token
Examining the preprocessor output gives
enum {
# 53 "/path/to/file.h" 3 4
(0)
# 53 "/path/to/file.h"
, ERROR_1, ERROR_2 };
I searched my project for a macro that would define OK and replace it with (0) but to no avail. So my question is how can I track where this (0) comes from? I read the docs on preprocessor output, but haven't found anything that would aid me in my problem.
You can use for example -E -fdirectives-only as options to GCC. It will give you a preprocessor output with all #includes resolved and including file/line markers, but with the macro definitions still in place and unexpanded.
Then simply search for #define OK in the output and search upwards for a # N marker where N is an integer. The marker will refer to the file/line from where the definition originates.
(By the way, you are looking on the wrong page of documentation. For the possible command line options affecting the preprocessor see https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html.)

Resharper : subtituate macro with multi-line code

Is it possible to make Resharper Substituate macro call in separate-lines mode?
Example
I have this code :-
#define TEST(T) int T=5; \
T++;
TEST(apple);
If I click
Substituate macro call and all nested calls like this :-
The line TEST(apple); will become :-
int apple=5; apple++;;
I hope there is an option to make the result be 2 separate lines :-
int apple=5;
apple++;;
Other notes
I know that macro with \ is finally interpreted as a single line,
but it would be nice if there is an option to show it as many lines for eye-candy.
(Even it may break the macro behavior, e.g. those with __LINE__ )
It would be useful for debugging for a 10+ lines macro.
It's not possible at the moment, but you can always select the resulting code after a macro substitution and invoke "Reformat Code" (Ctrl+Alt+Enter) to make it readable.

Remove pressed key from vim input buffer

I'm writing a vim auto-command for cpp files where I want an abbreviation for #include lines.
The goal is for the abbreviation to expand to #include <▐> with the pipe being the location of the cursor after expansion.
Since naturally the abbreviation is to be followed by a space I'm trying to remove the input space from the register and move on.
However even after exiting insert mode and returning, or any trick I could think of: deleting next key, keying in bs etc, the space is being entered after whatever series of commands the iabbrev includes.
Therefore I'm looking for something that will remove the space from the register AND put me still in insert mode.
Thanks.
Add this to your .vimrc:
autocmd FileType cpp iab <buffer> #i #include <><Left><C-R>=Eatchar('\s')<CR>
func Eatchar(pat)
let c = nr2char(getchar(0))
return (c =~ a:pat) ? '' : c
endfunc
In lh-cpp, I have in a C ftplugin (the place where such definitions should be)
iab <buffer> #n <C-R>=lh#map#no_context("#n ",'\<esc\>0i#include')<CR>
:Brackets < > -open=function('lh#cpp#brackets#lt') -visual=0
With lh#map#no_context() that prevents the expansion of #n within string and comment contexts -- sometimes, we want to be able to type auto s = "#n";, and lh#cpp#brackets#lt that recognizes a few contexts in which I can expand my < into <> (includes, templates, castings, etc).
As you see I don't automatically append "" nor <> here as I want to be able to choose between #include <foo> and #include "foo"
Back to abbreviations that don't eat the space characters, the answer is in the help, see Menix's answer.
To simplify my task, I've defined a command that automatically applies such function (in lh-brackets this time):
command! -narg=+ Iabbr execute "iabbr " <q-args>."<C-R>=lh#map#eat_char('\\s')<CR>"
command! -narg=+ Inoreabbr
\ execute "inoreabbr " <q-args>."<C-R>=lh#map#eat_char('\\s')<CR>"
Thus, I would define your abbreviation this way
:Inoreabbr <silent> <buffer> #n< #include <><left>
:Inoreabbr <silent> <buffer> #n" #include ""<left>
Or even better to not see these abbreviations triggerred in string and comment contexts:
:Inoreab <buffer> <silent> #n< <C-R>=lh#map#no_context("#n< ",'\<esc\>0i#include <>\<left\>')<CR>
" and so on
As you can see from #LucHermitte's answer (and the similar attempt by #Meninx before), eating that character is a bit involved.
Instead, I would recommend you upgrade to a snippets plugin (if you don't use one already). Snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
Depending on the snippet plugin, the expansion may need to be explicitly triggered, others may already support insertion without the trailing space. Usually, you can configure a default for the included filename (that can be overwritten easily) - this functionality cannot be had via Vim abbreviations alone.
alternative
If you don't want full snippets functionality just for this, I would recommend to define the abbreviation based on triggering it with <Enter> instead of <Space>. This should fit well for this example (the next code / #include will be on a new line, anyway), and so you don't have to artificially remove anything.

(C/C++) how to create a macro that accepts the increment operator

to make my question precise i would like to create a macro that accepts as variable ++ and pastes it with another variable. In code:
#define MYMACRO(A, OP) /* .... */
and then if i write in the source file
MYMACRO(this->var, ++)
the preprocessor should paste in the code
this->var++;
When i am trying to define the macro as A##OP , it provides the following error:
pasting "++" and "var" does not give a valid preprocessing token
Is it possible to do what i am trying?
thanks in advance for the answer
You don't need the ## token pasting operator, because you're not trying to combine the parameters into a single token. Just use:
#define MYMACRO(a, op) (a)op;
You just need to combine the tokens like this:
#define MYMACRO(a, incrdecr) (a)incrdecr

#define directive with multiple replacements?

I am relatively new to programming, and I am trying to learn to use wxWidgets in C++ (with Visual Studio 2010).
I was looking through the wxWidgets header file "app.h" and I see some #define directives that I can't understand. Here is an example:
#define wxIMPLEMENT_APP(appname) \
wxIMPLEMENT_WX_THEME_SUPPORT \
wxIMPLEMENT_APP_NO_THEMES(appname)"
I'm used to seeing #define with one "identifier" and one "replacement", so I can't understand if this macro has two "identifiers" (wxIMPLEMENT_APP(appname) and wxIMPLEMENT_WX_THEME_SUPPORT) and one "replacement" (wxIMPLEMENT_APP_NO_THEMES (appname)), or one "identifier" (wxIMPLEMENT_APP(appname)) and two "replacements" (wxIMPLEMENT_WX_THEME_SUPPORT and wxIMPLEMENT_APP_NO_THEMES(appname)).
How am I to understand this macro?
I tried looking online and in text books, searching under "macros", "pre-processor directives", "text replacement macros", "#define directive", and similar, but I could not find any examples with explanation that look like the one I have here.
This preprocessor macro has a single replacement split across multiple lines. The \ at the end of the line lets you write a single "logical" line on multiple lines of text.
Everything that follows wxIMPLEMENT_APP(appname) will be placed in the text of the program when wxIMPLEMENT_APP(appname) pattern is matched; presumably, both these definitions will be further processed by the preprocessor, because they look like references to other macro definitions.