aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/clangbackend/source/clangfollowsymbol.cpp
blob: 81aaeec5c89fc1ce5fa9c617cad88b82bf9d3f9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "clangfollowsymbol.h"
#include "clangfollowsymboljob.h"
#include "commandlinearguments.h"
#include "cursor.h"
#include "clangstring.h"
#include "sourcerange.h"
#include "token.h"
#include "clangsupportdebugutils.h"

#include <utils/qtcassert.h>

#include <future>

namespace ClangBackEnd {

static SourceRange getOperatorRange(const Tokens &tokens,
                                    std::vector<Token>::const_iterator currentToken)
{
    const SourceLocation start = currentToken->location();
    currentToken += 2;
    while (currentToken != tokens.cend() && !(currentToken->spelling() == "("))
        ++currentToken;

    return SourceRange(start, currentToken->location());
}

static SourceRangeContainer extractMatchingTokenRange(const Cursor &cursor,
                                                      const Utf8String &tokenStr)
{
    Tokens tokens(cursor.sourceRange());
    for (auto it = tokens.cbegin(); it != tokens.cend(); ++it) {
        const Token &currentToken = *it;
        if (!(tokenStr == currentToken.spelling()))
            continue;

        if (cursor.isFunctionLike() || cursor.isConstructorOrDestructor()) {
            if (tokenStr == "operator")
                return getOperatorRange(tokens, it);

            auto nextIt = it + 1;
            if (nextIt == tokens.cend() || !(nextIt->spelling() == "("))
                continue;
        }
        return currentToken.extent();
    }
    return SourceRangeContainer();
}

FollowSymbolResult FollowSymbol::followSymbol(CXTranslationUnit tu,
                                              const Cursor &fullCursor,
                                              uint line,
                                              uint column)
{
    Tokens tokens(fullCursor.sourceRange());

    if (!tokens.size()) {
        const Cursor tuCursor(clang_getTranslationUnitCursor(tu));
        tokens = Tokens(tuCursor.sourceRange());
    }

    if (!tokens.size())
        return SourceRangeContainer();

    std::vector<Cursor> cursors = tokens.annotate();
    const int tokenIndex = tokens.getTokenIndex(tu, line, column);
    QTC_ASSERT(tokenIndex >= 0, return SourceRangeContainer());

    const Utf8String tokenSpelling = tokens[tokenIndex].spelling();
    if (tokenSpelling.isEmpty())
        return SourceRangeContainer();

    Cursor cursor{cursors[tokenIndex]};
    if (cursor.kind() == CXCursor_CXXThisExpr && tokenSpelling != "this") { // QTCREATORBUG-25342
        cursor.semanticParent().visit([&cursor](CXCursor current, CXCursor parent) {
            if (current == cursor && parent.kind == CXCursor_MemberRefExpr
                    && cursor.sourceLocation() == Cursor(parent).sourceLocation()) {
                cursor = parent;
                return CXChildVisit_Break;
            }
            return CXChildVisit_Recurse;
        });
    }

    if (cursor.kind() == CXCursor_InclusionDirective) {
        CXFile file = clang_getIncludedFile(cursors[tokenIndex].cx());
        const ClangString filename(clang_getFileName(file));
        const SourceLocation loc(tu, clang_getLocation(tu, file, 1, 1));
        FollowSymbolResult result;
        result.range = SourceRangeContainer(SourceRange(loc, loc));
        // CLANG-UPGRADE-CHECK: Remove if we don't use empty generated ui_* headers anymore.
        if (Utf8String(filename).contains("ui_"))
            result.isResultOnlyForFallBack = true;
        return result;
    }

    // For definitions we can always find a declaration in current TU
    if (cursor.isDefinition()) {
        if (tokenSpelling == "auto") {
            Type type = cursor.type().pointeeType();
            if (!type.isValid())
                type = cursor.type();
            const Cursor declCursor = type.declaration();
            return extractMatchingTokenRange(declCursor, declCursor.spelling());
        }

        const Cursor declCursor = cursor.canonical();
        FollowSymbolResult result;
        result.range = extractMatchingTokenRange(declCursor, tokenSpelling);
        result.isResultOnlyForFallBack = cursor.isFunctionLike() && declCursor == cursor;
        return result;
    }

    if (!cursor.isDeclaration()) {
        // This is the symbol usage
        // We want to return definition
        cursor = cursor.referenced();
        if (cursor.isNull())
            return SourceRangeContainer();

        FollowSymbolResult result;
        // We can't find definition in this TU or it's a virtual method call
        if (!cursor.isDefinition() || cursor.isVirtualMethod())
            result.isResultOnlyForFallBack = true;

        result.range = extractMatchingTokenRange(cursor, tokenSpelling);
        return result;
    }

    const bool isFunction = cursor.isFunctionLike();
    cursor = cursor.definition();
    // If we are able to find a definition in current TU
    if (!cursor.isNull())
        return extractMatchingTokenRange(cursor, tokenSpelling);

    return FollowSymbolResult({}, isFunction);
}

} // namespace ClangBackEnd