How to Generate a Newline in a Cpp MACro

How to include a newline in a C++ macro or how to use C++ templates to do the same?

I solved the above problem using GNU m4 preprocessor successfully.

m4_define('DEFINE_FUNCTION','
__asm rtype nt_$2(void*) {
str lr, [sp, $3];
bl vThunk_$1;
ldr lr, [sp,$3];
bx lr;
}
void vThunk_$2(void*)')

DEFINE_FUNCTION(void*, mov_lt, 0x04)
{
}

Preprocessing the above code using m4 solved my problem of newline requirement in code. Ran m4 -P as a prebuild event so that source file is processed even before C preprocessor and compilation stage comes into picture.

Thanks for the help and sorry for confusing a lot. But there is really a scope for good macro pre-processor in latest C++ compilers.

How to implement a logging macro with automatic newline in C++

I think the idea is to return a temp wrapper object:

#include <iostream>

struct Log
{
~Log(void) { ::std::cout << ::std::endl; }
};

template<typename T> Log &&
operator <<(Log && wrap, T const & whatever)
{
::std::cout << whatever;
return ::std::move(wrap);
}

int main()
{
Log() << "whatever";
Log() << "more";
return 0;
}

online compiler

Note that the macro can be used here to execute conditional branching in the beginning of logging. That is skip logging if severity level is low.

#define LOG(level) \
if(g_log_lelevel <= level) Log()

Multi line preprocessor macros

You use \ as a line continuation escape character.

#define swap(a, b) {               \
(a) ^= (b); \
(b) ^= (a); \
(a) ^= (b); \
}

EDIT: As @abelenky pointed out in the comments, the \ character must be the last character on the line. If it is not (even if it is just white space afterward) you will get confusing error messages on each line after it.

How can I make the preprocessor insert linebreaks into the macro expansion result?

You can't do it. The replacement text is until the end of the line where it is #defined, so it will not have newlines in it. If your problems with compilation are infrequent, you could run the preprocessed file through indent or something like that before compiling when that happens to help you get more readable code.

Using Backslash-Newline outside of a macro in C

"Splices" -- backslash newline sequences -- are removed before the preprocessor processes the program text. At least that's the theory, bearing in mind that the C standard does not actually define a process called the "preprocessor".

What it does define is a procedure for converting the program text into a stream of tokens which can be parsed, and then turning that into an executable. The procedure consists of eight translation phases, and the compiler must produce the same result as would be produced if the phases were executed one at a time, each one taking as input the output of the previous phase. (Most of the inputs and output are streams of tokens, rather than character strings. So the output GCC produces when run with the -E flag doesn't correspond to anything in the standard, allowing GCC to basically produce whatever output it finds convenient. Or that its authors thought you would find convenient.)

The "as if" clause means that a particular compiler can combine phases or execute them in pieces, as long as it doesn't change the result. So you can really only look at the process as the abstract description of an algorithm. Still, it's useful to understand. The full text is found in §5.1.1.2 of the standard.

A highly condensed and commented description of the phases, which is incomplete and somewhat imprecise in its details, in the hopes that it's easier to digest than the language in the standard. But do read it in the original.

  1. Remove trigraphs (which are now deprecated, so don't worry if you don't know what they are) and, if necessary, convert the program text to whatever character encoding the compiler requires.

  2. Remove splices. All backslash-newline sequences are simply removed from the program text, leaving nothing behind. (OK, that's the theory. In practice, most compilers still know the original source line number of every bit of text. But this information is only used for producing diagnostics.)

  3. Split the text into tokens and whitespace sequences, and replace all comments with a single space character.

  4. "Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed". This is as close as the standard gets to defining the preprocessor, so it's reasonable to say that the "preprocessor" is the execution of phase 4. #include directives are preprocessor directives, and processing the include directive starts with passing the included file through phases 1-3 before inserting it into the token stream to be further preprocessed.

  5. Replace all the escape sequences in character and string literals with the actual characters (possibly wide characters) which will be used during execution.

  6. Concatenate adjacent string literals.

  7. Remove all whitespace, leaving only tokens. Convert preprocessing tokens into syntactic tokens. Parse the resulting token stream and convert it into a "translation unit". Or, in other words, compile the program into an object file (although that's way more specific than the language in the standard).

  8. Combine all the translation units and necessary library modules into a single executable image. Informally, this is the linking phase and the result is something you can hand to the operating system for execution.

That's what the standard mandates. But real-world compilers do lots of other stuff, like generate more or less readable error messages; rearrange the code in ways that might make it execute faster and/or occupy less space; insert debugging information into the executable; and produce whatever additional analyses and reports the user has requested (none of which are standardised). This, for example, includes the -E and/or -S outputs. The compiler does these things as a favour to you, and they can be helpful in understanding the way your program was compiled. But you shouldn't take them too seriously, since the official result of the compilation process is the actual executable.

Most compilation toolchains can also produce libraries, so it is not the case that all programs are immediately fully processed into executable images. But that's the only outcome which is standardised. Although the standard refers to libraries, particularly the standard library, it does not make any assumptions about how libraries come into existence.

The standard libraries (and headers) don't even have to exist in the filesystem; it's enough that the compiler recognises their names and responds appropriately. Some of the stuff the standard library has to implement cannot be written in portable C, so it is quite possible that the standard library source code, if it exists, is not all in the form of a standard C program. Standard library headers might include constructs which receive special handling by the compiler, and thus cannot be used by other compilers or copied directly into your program.

This might all seem too much in the air, but the intention was to make it possible to have C implementations which run on extremely limited processors, including processors without any external storage at all. (And it is still quite common to target embedded systems which might be missing lots of things you normally take for granted.) And, on the whole, it's served us pretty well over the years.



Related Topics



Leave a reply



Submit