<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - clang_getCursorExtent yields invalid range for macro expansions"
   href="https://bugs.llvm.org/show_bug.cgi?id=44106">44106</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang_getCursorExtent yields invalid range for macro expansions
          </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>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>libclang
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>tagoo@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>klimek@google.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Currently libclang exposes `CXSourceRange clang_getCursorExtent(CXCursor C)`,
this function
(<a href="https://github.com/llvm/llvm-project/blob/master/clang/tools/libclang/CIndex.cpp#L6129">https://github.com/llvm/llvm-project/blob/master/clang/tools/libclang/CIndex.cpp#L6129</a>)
gets the raw extent and then calls cxloc::translateSourceRange.
cxloc::translateSourceRange
(<a href="https://github.com/llvm/llvm-project/blob/master/clang/tools/libclang/CIndex.cpp#L142">https://github.com/llvm/llvm-project/blob/master/clang/tools/libclang/CIndex.cpp#L142</a>)
currently has the following comment:

/// Translate a Clang source range into a CIndex source range.
///
/// Clang internally represents ranges where the end location points to the
/// start of the token at the end. However, for external clients it is more
/// useful to have a CXSourceRange be a proper half-open interval. This routine
/// does the appropriate translation.

This comment makes sense and gives an understanding that for an expression
like: `x + 2000` the range used by clang is `x + 2` and it will translate it to
be the full `x + 2000`.
However, the logic the method runs includes some special handling for macro
expansions that ultimately gives you a range of ‘macro start’ to ‘expansion
end’.
This conversion means that for a given macro the range you get back can include
thousands of in-between tokens that are unrelated.

An example of this is the following code where the range of the APPROX_PI
literal in the method starts at the 3 in the macro but ends just before the
semicolon in the method.
That is, the returned range is “Line 1, Column 19” to “Line 5, Column 21”; when
it is expected to be “Line 1, Column 19” to “Line 1, Column 26”.

                #define APPROX_PI 3.14159

double getApproxPI()
{
    return APPROX_PI;
}</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>