File: clangdiagnosticevaluator.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 (131 lines) | stat: -rw-r--r-- 4,640 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
/*
 * Copyright 2014 Kevin Funk <kfunk@kde.org>
 * 
 * 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) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 * 
 * 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, see <http://www.gnu.org/licenses/>.
 * 
 */

#include "clangdiagnosticevaluator.h"
#include "unknowndeclarationproblem.h"
#include "missingincludepathproblem.h"
#include "util/clangtypes.h"

namespace
{

/**
 * Check whether the problem stated in @p diagnostic may be caused by a missing include
 *
 * @return True if this may be fixable by adding a include, false otherwise
 */
bool isDeclarationProblem(const QByteArray& description)
{
    /* libclang does not currently expose an enum or any other way to query
     * what specific semantic error we're dealing with. Instead, we have to
     * parse the clang error message and guess if a missing include could be
     * the reason for the error
     *
     * There is no nice way of determining what identifier we're looking at either,
     * so we have to read that from the diagnostic too. Hopefully libclang will
     * get these features in the future.
     *
     * I have suggested this feature to clang devs. For reference, see:
     * http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-March/036036.html
     */

    return description.startsWith( QByteArrayLiteral("use of undeclared identifier") )
           || description.startsWith( QByteArrayLiteral("no member named") )
           || description.startsWith( QByteArrayLiteral("unknown type name") )
           || description.startsWith( QByteArrayLiteral("variable has incomplete type") )
           || description.startsWith( QByteArrayLiteral("member access into incomplete type") );
}

/// @return true if @p diagnostic says that include file not found
bool isIncludeFileNotFound(const QByteArray& description)
{
    return description.endsWith(QByteArrayLiteral("file not found"));
}

bool isReplaceWithDotProblem(const QByteArray& description)
{
    // TODO: The diagnostic message depends on LibClang version.
    static const QByteArray diagnosticMessages[] = {
        QByteArrayLiteral("did you mean to use '.'?"),
        QByteArrayLiteral("maybe you meant to use '.'?")
    };

    for (const auto& diagnStr : diagnosticMessages) {
        if (description.endsWith(diagnStr)) {
            return true;
        }
    }

    return false;
}

bool isReplaceWithArrowProblem(const QByteArray& description)
{
    // TODO: The diagnostic message depends on LibClang version.
    static const QByteArray diagnosticMessages[] = {
        QByteArrayLiteral("did you mean to use '->'?"),
        QByteArrayLiteral("maybe you meant to use '->'?")
    };

    for (const auto& diagnStr : diagnosticMessages) {
        if (description.endsWith(diagnStr)) {
            return true;
        }
    }

    return false;
}

}

ClangDiagnosticEvaluator::DiagnosticType ClangDiagnosticEvaluator::diagnosticType(CXDiagnostic diagnostic)
{
    const ClangString str(clang_getDiagnosticSpelling(diagnostic));
    const auto description = QByteArray::fromRawData(str.c_str(), qstrlen(str.c_str()));

    if (isDeclarationProblem(description)) {
        return UnknownDeclarationProblem;
    } else if (isIncludeFileNotFound(description)) {
        return IncludeFileNotFoundProblem;
    } else if (isReplaceWithDotProblem(description)) {
        return ReplaceWithDotProblem;
    } else if (isReplaceWithArrowProblem(description)) {
        return ReplaceWithArrowProblem;
    }

    return Unknown;
}

ClangProblem* ClangDiagnosticEvaluator::createProblem(CXDiagnostic diagnostic, CXTranslationUnit unit)
{
    switch (diagnosticType(diagnostic)) {
    case IncludeFileNotFoundProblem:
        return new MissingIncludePathProblem(diagnostic, unit);
        break;
    case UnknownDeclarationProblem:
        return new class UnknownDeclarationProblem(diagnostic, unit);
        break;
    default:
        return new ClangProblem(diagnostic, unit);
        break;
    }
}