<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Cannot find the right back trace of the macro expansion"
   href="https://llvm.org/bugs/show_bug.cgi?id=24592">24592</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Cannot find the right back trace of the macro expansion
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>zhengkai@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, rtrieu@google.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In /llvm/tools/clang/test/Misc/caret-diags-macros.c
#line 204
#define sprintf2(str, ...) \
  __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
#define Cstrlen(a)  strlen_test(a)
#define Csprintf    sprintf2
void f(char* pMsgBuf, char* pKeepBuf) {
Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ",
Cstrlen(pKeepBuf));

The error message is :
warning: format specifies type 'int' but the argument has type 'unsigned long'
Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ",
Cstrlen(pKeepBuf));
                                                    ~~~      ^
                                                    %1lu

Should be 

warning: format specifies type 'int' but the argument has type 'unsigned long'
Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ",
Cstrlen(pKeepBuf));
                                                    ~~~      ^~~~~~~~~~~~~~~~~
                                                    %1lu


This bug is caused in /llvm/tools/clang/lib/Frontend/DiagnosticRenderer.cpp

in the procedure void DiagnosticRenderer::emitCaret, the procedure used
function void mapDiagnosticRanges.
This function doesn't map the right spelling ranges for the ranges given.

In the new version of this function (), the output is:

warning: format specifies type 'int' but the argument has type 'unsigned long'
Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ",
Cstrlen(pKeepBuf));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
                                                    %1lu

This is caused by the argument Ranges which is passed to the procedure 
void DiagnosticRenderer::emitCaret indicates that the underlined part should be 
#define Cstrlen(a)  strlen_test(a)
                    ~~~~~~~~~~~~~~

But there is a kind of high-level macro invocation here. Csprintf is a macro
and it expand to sprintf2 which is a new macro name. So it continues to expand
the macro sprintf2.
So when  I trace back the range in the macro expansion stack, the range map
backs to the 
#define Csprintf    sprintf2
        ~~~~~~~~
So it finally map backs to the entire macro invocation.

I think the essential cause of this bug is that the back trace algorithm of the
macro expansions is not right in the SourceManager Class.

May be due to a similar reason with the bug:
<a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Macro expansion backtrace fails to mention a parameterless macro aliasing other macro's name"
   href="show_bug.cgi?id=16799">https://llvm.org/bugs/show_bug.cgi?id=16799</a></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>