'Option to add line-markers for all lines in preprocessed file

I am analysing a preprocessed C file to generate a mapping between the source file line-numbers and preprocessed file line-numbers.

Is there a way to generate the preprocessed file such that it has line markers for all lines in the preprocessed file.

 gcc -E  test.c -o test.i

This doesnt generate line-markers for all lines. It only has line-markers for the include lines. So need to keep track of the last line marker to find the filename and derive the line number.

Is there a better approach than this?

Example: Test.c Source:

1. #include "test.h"
2. #define ADD(x) x + x;
3. 
4. void fun () {
5.    return 10;
6. }
7. int main () {
8.     foo(1);
9.     ADD (10);
10.    return 0;
11. }

Test.h Source:

1. void foo(int i) {
2.     return i + 10;
3. }

Preprocess command:

gcc -E test.c -o test.i

test.i Source:

1. # 1 "test.c"
2. # 1 "<built-in>"
3. # 1 "<command-line>"
4. # 1 "/usr/include/stdc-predef.h" 1 3 4
5. # 1 "<command-line>" 2
6. # 1 "test.c"
7. # 1 "test.h" 1
8. void foo(int i) {
9.     return i + 10;
10 }
11. # 2 "test.c" 2
12. 
13. 
14. void fun () {
15.    return 10;
16. }
17. int main () {
18.     foo(1);
19.     10 + 10;;
20.     return 0;
21. }

test.c: Source_line - Preprocessed_line Map

{
"4": "14",
"5": "15",
"6": "16",
"7": "17",
"8": "18",
"9": "19",
"10: "20",
"11":"21"
}

test.h Source_line - Preprocessed_line Map

{
"1": "8",
"2": "9",
"3": "10"
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source