<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/106878>106878</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Wrong line number from clang -E for macro expansion.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mrolle45
      </td>
    </tr>
</table>

<pre>
    When a macro call contains embedded #line directives, clang writes these directives, then writes the expansion of the macro.  It needs to re-establish the presumed line number before the expansion.
In some cases, the re-establishment is missing.
It depends on whether the macro call is the first token on its line or it follows something else.
It is missing when the macro call is first on its line.
Without any line directives inside the call, it is correct in both cases, since a re-establishment is not needed.

Here's an example:
```
File t.c:
#define str(...) # __VA_ARGS__
__LINE__ Line directives inside macro
#line 94
__LINE__
X   str(   // "Hello world" at line 95 column 5
 // Expansion indented at original line number after line directives
 Hello
    world
#line 400
#line 72
)
__LINE__
#line 114
__LINE__
  str(   // "Hello world 2" at line 115

 Hello
    world 2
#line 500
#line 81
)
__LINE__    // 94
X str(   // "Hello world" at line 95 column 5

    Hello
 world
)
__LINE__
#line 114
__LINE__
  str(   // "Hello world 2" at line 115

    Hello
    world 2
)
__LINE__    // 120

Output from clang -xc t.c -E:


2 Line directives inside macro
# 94 "t.c"
94
X
# 400 "t.c"
# 72 "t.c"
 -- Needs # 95 "t.c"  here
 "Hello world"
 -- Needs # 73 "t.c"  here
73
# 114 "t.c"
114
# 500 "t.c"
# 81 "t.c"
# 115 "t.c"
  "Hello world 2"
# 82 "t.c"
82
X "Hello world"




88
# 114 "t.c"
114
  "Hello world 2"




120

```

# Suggestion

Look in `PrintProprocessedOutput.cpp` method `PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok()`.  This is where the missing line directive should be written in the first case, and where it is actually written in the second case.

Note, if the # 95 is written here, this will change the presumed line number, and the following # 73 should be written later.

</pre>
<img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