kateprojectcodeanalysistoolclazy.cpp 4.33 KB
Newer Older
Waqar Ahmed's avatar
Waqar Ahmed committed
1
2
3
4
5
6
/*  This file is part of the Kate project.
 *
 *  SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com>
 *
 *  SPDX-License-Identifier: LGPL-2.0-or-later
 */
Waqar Ahmed's avatar
Waqar Ahmed committed
7
8
9
#include "kateprojectcodeanalysistoolclazy.h"

#include <KLocalizedString>
Waqar Ahmed's avatar
Waqar Ahmed committed
10
11

#include <QDir>
Waqar Ahmed's avatar
Waqar Ahmed committed
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
#include <QRegularExpression>

KateProjectCodeAnalysisToolClazy::KateProjectCodeAnalysisToolClazy(QObject *parent)
    : KateProjectCodeAnalysisTool(parent)
{
}

QString KateProjectCodeAnalysisToolClazy::name() const
{
    return i18n("Clazy (Qt/C++)");
}

QString KateProjectCodeAnalysisToolClazy::description() const
{
    return i18n("Clazy is a static analysis tool for Qt/C++ code");
}

QString KateProjectCodeAnalysisToolClazy::fileExtensions() const
{
    return QStringLiteral("cpp|cxx|cc|c++|tpp|txx");
}

QStringList KateProjectCodeAnalysisToolClazy::filter(const QStringList &files) const
{
    // c++ files
    return files.filter(
        QRegularExpression(QStringLiteral("\\.(") + fileExtensions().replace(QStringLiteral("+"), QStringLiteral("\\+")) + QStringLiteral(")$")));
}

QString KateProjectCodeAnalysisToolClazy::path() const
{
    return QStringLiteral("clazy-standalone");
}

Waqar Ahmed's avatar
Waqar Ahmed committed
46
47
48
49
50
51
52
static QString buildDirectory(const QVariantMap &projectMap)
{
    const QVariantMap buildMap = projectMap[QStringLiteral("build")].toMap();
    const QString buildDir = buildMap[QStringLiteral("directory")].toString();
    return buildDir;
}

Waqar Ahmed's avatar
Waqar Ahmed committed
53
54
55
56
57
58
QStringList KateProjectCodeAnalysisToolClazy::arguments()
{
    if (!m_project) {
        return {};
    }

Waqar Ahmed's avatar
Waqar Ahmed committed
59
    QString compileCommandsDir = compileCommandsDirectory();
Waqar Ahmed's avatar
Waqar Ahmed committed
60
61

    QStringList args;
Waqar Ahmed's avatar
Waqar Ahmed committed
62
63
    if (!compileCommandsDir.isEmpty()) {
        args = QStringList{QStringLiteral("-p"), compileCommandsDir};
Waqar Ahmed's avatar
Waqar Ahmed committed
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
    }

    auto &&fileList = filter(m_project->files());
    setActualFilesCount(fileList.size());
    QString files = fileList.join(QLatin1Char(' '));

    return args << fileList;
}

QString KateProjectCodeAnalysisToolClazy::notInstalledMessage() const
{
    return i18n("Please install 'clazy'.");
}

QStringList KateProjectCodeAnalysisToolClazy::parseLine(const QString &line) const
{
    //"/path/kate/kate/kateapp.cpp:529:10: warning: Missing reference in range-for with non trivial type (QJsonValue) [-Wclazy-range-loop]"
    int idxColon = line.indexOf(QLatin1Char(':'));
    if (idxColon < 0) {
        return {};
    }
    QString file = line.mid(0, idxColon);
    idxColon++;
    int nextColon = line.indexOf(QLatin1Char(':'), idxColon);
    QString lineNo = line.mid(idxColon, nextColon - idxColon);

    int spaceIdx = line.indexOf(QLatin1Char(' '), nextColon);
    if (spaceIdx < 0) {
        return {};
    }

    idxColon = line.indexOf(QLatin1Char(':'), spaceIdx);
    if (idxColon < 0) {
        return {};
    }

    QString severity = line.mid(spaceIdx + 1, idxColon - (spaceIdx + 1));

    idxColon++;

    QString msg = line.mid(idxColon);

    return {file, lineNo, severity, msg};
}

QString KateProjectCodeAnalysisToolClazy::stdinMessages()
{
    return QString();
}
Waqar Ahmed's avatar
Waqar Ahmed committed
113
114
115
116
117
118

QString KateProjectCodeAnalysisToolClazy::compileCommandsDirectory() const
{
    QString buildDir = buildDirectory(m_project->projectMap());
    const QString compCommandsFile = QStringLiteral("compile_commands.json");

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    if (buildDir.startsWith(QLatin1String("./"))) {
        buildDir = buildDir.mid(2);
    }

    // clang-format off
    /**
     * list of absoloute paths to check compile commands
     */
    const QString possiblePaths[4] =
    {
        /** Absoloute build path in .kateproject e.g from cmake */
        buildDir,
        /** Relative path in .kateproject e.g */
        m_project->baseDir() + (buildDir.startsWith(QLatin1Char('/')) ? buildDir : QLatin1Char('/') + buildDir),
        /** Check for the commonly existing "build/" directory */
        m_project->baseDir() + QStringLiteral("/build"),
        /** Project base, maybe it has a symlink to compile_commands.json file */
        m_project->baseDir()
    };
    // clang-format on

    /**
     * Check all paths one by one for compile_commands.json and exit when found
     */
Waqar Ahmed's avatar
Waqar Ahmed committed
143
    QString compileCommandsDir;
144
145
146
    for (const QString &path : possiblePaths) {
        const QString guessedPath = QDir(path).filePath(compCommandsFile);
        const bool dirHasCompileComds = QFile::exists(guessedPath);
147
        if (dirHasCompileComds) {
148
            compileCommandsDir = guessedPath;
149
            break;
Waqar Ahmed's avatar
Waqar Ahmed committed
150
151
        }
    }
152

Waqar Ahmed's avatar
Waqar Ahmed committed
153
154
    return compileCommandsDir;
}