[llvm-bugs] [Bug 44106] New: clang_getCursorExtent yields invalid range for macro expansions

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Nov 21 13:36:49 PST 2019


https://bugs.llvm.org/show_bug.cgi?id=44106

            Bug ID: 44106
           Summary: clang_getCursorExtent yields invalid range for macro
                    expansions
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: libclang
          Assignee: unassignedclangbugs at nondot.org
          Reporter: tagoo at outlook.com
                CC: klimek at google.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Currently libclang exposes `CXSourceRange clang_getCursorExtent(CXCursor C)`,
this function
(https://github.com/llvm/llvm-project/blob/master/clang/tools/libclang/CIndex.cpp#L6129)
gets the raw extent and then calls cxloc::translateSourceRange.
cxloc::translateSourceRange
(https://github.com/llvm/llvm-project/blob/master/clang/tools/libclang/CIndex.cpp#L142)
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;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20191121/ee49377a/attachment.html>


More information about the llvm-bugs mailing list