File: documentfinderhelpers.cpp

package info (click to toggle)
kdevelop 4%3A5.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 52,544 kB
  • sloc: cpp: 254,897; python: 3,380; sh: 1,271; ansic: 657; xml: 221; php: 95; makefile: 36; lisp: 13; sed: 12
file content (282 lines) | stat: -rw-r--r-- 8,124 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * This file is part of KDevelop
 *
 * Copyright 2014 Sergey Kalinichev <kalinichev.so.0@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include "documentfinderhelpers.h"

#include "duchain/clanghelpers.h"

#include <language/duchain/duchain.h>
#include <language/duchain/declaration.h>
#include <language/duchain/functiondeclaration.h>
#include <language/duchain/functiondefinition.h>
#include <language/duchain/duchainutils.h>

#include <KDesktopFile>

using namespace KDevelop;

namespace {

enum FileType {
    Unknown, ///< Doesn't belong to C++
    Header,  ///< Is a header file
    Source   ///< Is a C(++) file
};

class PotentialBuddyCollector : public DUChainUtils::DUChainItemFilter
{
public:
    enum BuddyMode {
        Header,
        Source
    };

    explicit PotentialBuddyCollector(BuddyMode mode)
     : mode(mode)
    {}

    bool accept(Declaration* decl) override
    {
        if (decl->range().isEmpty())
            return false;

        if (mode == Header && decl->isFunctionDeclaration()) {
            // Search for definitions of our declarations
            FunctionDefinition* def = FunctionDefinition::definition(decl);
            if (def) {
                vote(def->url().toUrl());
            }

            return true;
        }
        else if (mode == Source && decl->isFunctionDeclaration()) {
            FunctionDefinition* fdef = dynamic_cast<FunctionDefinition*>(decl);
            if (fdef) {
                Declaration* fdecl = fdef->declaration();
                if (fdecl) {
                    vote(fdecl->url().toUrl());
                }
            }

            return true;
        } else {
            return false;
        }
    }
    bool accept(DUContext* ctx) override
    {
        if (ctx->type() == DUContext::Class || ctx->type() == DUContext::Namespace || ctx->type() == DUContext::Global || ctx->type() == DUContext::Other || ctx->type() == DUContext::Helper ) {
            return true;
        } else {
            return false;
        }
    }

    QUrl bestBuddy() const
    {
        QUrl ret;
        int bestCount = 0;
        for (auto it = m_buddyFiles.begin(); it != m_buddyFiles.end(); ++it) {
            if(it.value() > bestCount) {
                bestCount = it.value();
                ret = it.key();
            }
        }

        return ret;
    }
private:
    BuddyMode mode;
    QHash<QUrl, int> m_buddyFiles;

    void vote(const QUrl& url)
    {
        m_buddyFiles[url]++;
    }
};

/**
 * Tries to find a buddy file to the given file by looking at the DUChain.
 *
 * The project might keep source files separate from headers. To cover
 * this situation, we examine DUChain for the most probable buddy file.
 * This of course only works if we have parsed the buddy file, but it is
 * better than nothing.
 *
 * @param url url of the source/header file to find a buddy for
 * @param type type of the file @p url
 *
 * @returns QUrl of the most probable buddy file, or an empty url
 **/
QUrl duchainBuddyFile(const QUrl& url, FileType type)
{
    DUChainReadLocker lock;

    auto ctx = DUChainUtils::standardContextForUrl(url);
    if (ctx) {
        PotentialBuddyCollector collector( type == Header ? PotentialBuddyCollector::Header : PotentialBuddyCollector::Source );
        DUChainUtils::collectItems(ctx, collector);

        return collector.bestBuddy();
    }

    return QUrl();
}

/**
 * Generates the base path (without extension) and the file type
 * for the specified url.
 *
 * @returns pair of base path and file type which has been found for @p url.
 */
QPair<QString,FileType> basePathAndTypeForUrl(const QUrl &url)
{
    QString path = url.toLocalFile();
    int idxSlash = path.lastIndexOf(QLatin1Char('/'));
    int idxDot = path.lastIndexOf(QLatin1Char('.'));
    FileType fileType = Unknown;
    QString basePath;
    if (idxSlash >= 0 && idxDot >= 0 && idxDot > idxSlash) {
        basePath = path.left(idxDot);
        if (idxDot + 1 < path.length()) {
            QString extension = path.mid(idxDot + 1);
            if (ClangHelpers::isHeader(extension)) {
                fileType = Header;
            } else if (ClangHelpers::isSource(extension)) {
                fileType = Source;
            }
        }
    } else {
        basePath = path;
    }

    return qMakePair(basePath, fileType);
}

}

namespace DocumentFinderHelpers {
QStringList mimeTypesList()
{
    static const QStringList mimeTypes = {
        QStringLiteral("text/x-chdr"),
        QStringLiteral("text/x-c++hdr"),
        QStringLiteral("text/x-csrc"),
        QStringLiteral("text/x-c++src"),
        QStringLiteral("text/x-objcsrc")
    };
    return mimeTypes;
}

bool areBuddies(const QUrl &url1, const QUrl& url2)
{
    auto type1 = basePathAndTypeForUrl(url1);
    auto type2 = basePathAndTypeForUrl(url2);

    QUrl headerPath;
    QUrl sourcePath;

    // Check that one file is a header, the other one is source
    if (type1.second == Header && type2.second == Source) {
        headerPath = url1;
        sourcePath = url2;
    } else if (type1.second == Source && type2.second == Header) {
        headerPath = url2;
        sourcePath = url1;
    } else {
        // Some other file constellation
        return false;
    }

    // The simplest directory layout is with header + source in one directory.
    // So check that first.
    if (type1.first == type2.first) {
        return true;
    }

    // Also check if the DUChain thinks this is likely
    if (duchainBuddyFile(sourcePath, Source) == headerPath) {
        return true;
    }

    return false;
}

bool buddyOrder(const QUrl &url1, const QUrl& url2)
{
    auto type1 = basePathAndTypeForUrl(url1);
    auto type2 = basePathAndTypeForUrl(url2);
    // Precondition is that the two URLs are buddies, so don't check it
    return(type1.second == Header && type2.second == Source);
}

QVector<QUrl> potentialBuddies(const QUrl& url, bool checkDUChain)
{
    auto type = basePathAndTypeForUrl(url);
    // Don't do anything for types we don't know
    if (type.second == Unknown) {
        return {};
    }

    // Depending on the buddy's file type we either generate source extensions (for headers)
    // or header extensions (for sources)
    const auto& extensions = ( type.second == Header ? ClangHelpers::sourceExtensions() : ClangHelpers::headerExtensions() );
    QVector< QUrl > buddies;
    buddies.reserve(extensions.size());
    for(const QString& extension : extensions) {
        if (!extension.contains(QLatin1Char('.'))) {
            buddies.append(QUrl::fromLocalFile(type.first + QLatin1Char('.') + extension));
        } else {
            buddies.append(QUrl::fromLocalFile(type.first + extension));
        }
    }

    if (checkDUChain) {
        // Also ask DUChain for a guess
        QUrl bestBuddy = duchainBuddyFile(url, type.second);
        if (!buddies.contains(bestBuddy)) {
            buddies.append(bestBuddy);
        }
    }

    return buddies;
}

QString sourceForHeader(const QString& headerPath)
{
    if (!ClangHelpers::isHeader(headerPath)) {
        return {};
    }

    QString targetUrl;
    auto buddies = DocumentFinderHelpers::potentialBuddies(QUrl::fromLocalFile(headerPath));
    for (const auto& buddy : buddies) {
        const auto local = buddy.toLocalFile();
        if (QFileInfo::exists(local)) {
            targetUrl = local;
            break;
        }
    }

    return targetUrl;
}

}