Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do predefined macro's __FILE__, __LINE__, __func__ ,stringify(#) work?

Tags:

c++

macros

If my compiler supports these predefined macros (__FILE__, __LINE__, __func__) then I can safely use them and assume it will always report the right file and line according to : __FILE__, __LINE__, and __FUNCTION__ usage in C++

I also read that variable names, line numbers and such aren't relevant to the assembly end-code that is executed (the actual .exe file produced from .cpp code), if this is the case then how do these macros work when used in code? How is a __LINE__ macro represented in assembly code? How can the .exe file know the correct line number of the original .cpp source when it comes across a __LINE__ macro?

like image 323
Hatted Rooster Avatar asked Dec 14 '22 12:12

Hatted Rooster


2 Answers

How is a LINE macro represented in assembly code?

It's not. These are preprocessor macros. Once the preprocessor runs, they are replaced with literals.

For example, if you had this code:

void foo() {
    printf("%d", __LINE__);
}

The preprocessor will turn it into this:

void foo() {
    printf("%d", 2);
}
like image 200
Colonel Thirty Two Avatar answered Dec 27 '22 16:12

Colonel Thirty Two


1) "macros" like __FILE__ and __LINE__ are expanded by the compilation system's "preprocessor".. The source text is modified before the compiler itself actually sees it.

2) This is true of build-in macros like "__LINE__". It's equally true for any macro you create, with the #define statement.

3) Things like "line numbers" and "function names" are important to debuggers. Debug information is generated by the compiler itself (not the preprocessor). It's related to - but different from - either macro expansion or assembly code generation.

Debug information is usually saved directly in your executable file (along with assembler instructions, string literals, etc) as "metadata".

You can read more here: "How Debuggers Work"

ADDENDUM:

Like debugger metadata (above), func is similar - but different.

From the C99 standard:

6.4.2.2 Predefined identifiers

Semantics:

  1. The identifier __func_ _ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

    static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

  1. This name is encoded as if the implicit declaration had been written in the source character set and then translated into the execution character set as indicated in translation phase 5.

  2. EXAMPLE Consider the code fragment

    #include <stdio.h> void myfunc(void) { printf("%s\n", __func__); /* ... */ }

Each time the function is called, it will print to the standard output stream:

myfunc

You'll also see __FUNCTION__ in many C compilers, but it was never standard.

You can read more here:

__FILE__, __LINE__, and __FUNCTION__ usage in C++

like image 34
paulsm4 Avatar answered Dec 27 '22 17:12

paulsm4