Search code examples
c++ceclipseideeclipse-cdt

Eclipse CDT: doesn't show name a macro appears in


When searching for macro references, Eclipse displays file+line the macro is referenced it. I would like to file+line+function.

Searching for a other type of data (e.g. function) will display file+line+function as expected, so maybe something should be tweaked in Eclipse configuration for macros?

Any ideas?


Solution

  • Update - Jan 2017

    The next release of CDT (CDT 9.3, part of Eclipse Oxygen to be released in June 2017) will have support for showing function containing the macro reference. See Bug 508216 for more details.

    The rest of this answer is the original answer.

    TL;DR

    There is no way in Eclipse CDT to display the function a macro is referenced in because such information is not included in the index when the index is built.

    Pictures

    To ensure that we are talking about the same thing I provide some visuals.

    Given a simplified C file containing functions, macros and globals:

    Example Code

    #define MACRO(X) ((X) + 2)
    int function(int);
    int global;
    
    int function_results_are_in(void) {
        int i = 0;
        i = MACRO(i);
        i = function(i);
        i += global;
        return i;
    }
    

    Doing a search using one of the following methods that uses the C/C++ Index (as opposed to a file/grep style search):

    Setup

    • References in Workspace of selection using one of:
      • Shift+Ctrl+G
      • Right-click -> References -> Workspace
    • Search menu -> C/C++ Search and search for references, like this picture: enter image description here

    Results - Function search

    As you can see with searches for functions, the result shows the containing function name:

    function search

    Results - Macro search

    But for the macro search, there is no containing function name:

    macro search

    Under the hood

    Each search result in the C/C++ Search results is LineSearchElement.Match. If its fEnclosingElement is null then there is no function to display.

    Taking one step back you can see that the match is created from a matching IIndexName. The Match.fEnclosingElement field is filled from the result of IIndexName.getEnclosingDefinition().

    In the case of a macro reference the concrete type of IIndexName is a PDOMMacroReferenceName, and the implementation of getEnclosingDefinition is simply return null.