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
|
/*
* 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 "todoextractor.h"
#include "../util/clangtypes.h"
#include <language/duchain/problem.h>
#include <language/duchain/stringhelpers.h>
#include <language/editor/documentrange.h>
#include <interfaces/icore.h>
#include <interfaces/ilanguagecontroller.h>
#include <interfaces/icompletionsettings.h>
#include <QStringList>
#include <QDir>
#include <algorithm>
#include <limits>
using namespace KDevelop;
namespace {
inline int findEndOfLineOrEnd(const QString& str, int from = 0)
{
const int index = str.indexOf(QLatin1Char('\n'), from);
return (index == -1 ? str.length() : index);
}
inline int findBeginningOfLineOrStart(const QString& str, int from = 0)
{
const int index = str.lastIndexOf(QLatin1Char('\n'), from);
return (index == -1 ? 0 : index+1);
}
inline int findEndOfCommentOrEnd(const QString& str, int from = 0)
{
const int index = str.indexOf(QLatin1String("*/"), from);
return (index == -1 ? str.length() : index);
}
/**
* @brief Class for parsing to-do items out of a given comment string
*
* Make sure this class is very performant as it will be used for every comment string
* throughout the code base.
*
* So: No unnecessary deep-copies of strings if possible!
*/
class CommentTodoParser
{
public:
struct Result {
/// Description of the problem
QString description;
/// Range within the comment
KTextEditor::Range localRange;
};
CommentTodoParser(const QString& str, const QStringList& markerWords)
: m_str(str)
, m_todoMarkerWords(markerWords)
{
skipUntilMarkerWord();
}
QVector<Result> results() const
{
return m_results;
}
private:
void skipUntilMarkerWord()
{
// in the most-cases, we won't find a to-do item
// make sure this case is sufficiently fast
// for each to-do marker, scan the comment text
foreach (const QString& todoMarker, m_todoMarkerWords) {
int offset = m_str.indexOf(todoMarker, m_offset);
if (offset != -1) {
m_offset = offset;
parseTodoMarker();
}
}
}
void skipUntilNewline()
{
m_offset = findEndOfLineOrEnd(m_str, m_offset);
}
void parseTodoMarker()
{
// okay, we've found something
// m_offset points to the start of the to-do item
const int lineStart = findBeginningOfLineOrStart(m_str, m_offset);
const int lineEnd = findEndOfLineOrEnd(m_str, m_offset);
Q_ASSERT(lineStart <= m_offset);
Q_ASSERT(lineEnd > m_offset);
QString text = m_str.mid(m_offset, lineEnd - m_offset);
Q_ASSERT(!text.contains(QLatin1Char('\n')));
// there's nothing to be stripped on the left side, hence ignore that
text.chop(text.length() - findEndOfCommentOrEnd(text));
text = text.trimmed(); // remove additional whitespace from the end
// check at what line within the comment we are by just counting the newlines until now
const int line = std::count(m_str.constBegin(), m_str.constBegin() + m_offset, QLatin1Char('\n'));
KTextEditor::Cursor start = {line, m_offset - lineStart};
KTextEditor::Cursor end = {line, start.column() + text.length()};
m_results << Result{text, {start, end}};
skipUntilNewline();
skipUntilMarkerWord();
}
inline bool atEnd() const
{
return m_offset < m_str.length();
}
private:
const QString m_str;
const QStringList m_todoMarkerWords;
int m_offset = 0;
QVector<Result> m_results;
};
}
Q_DECLARE_TYPEINFO(CommentTodoParser::Result, Q_MOVABLE_TYPE);
TodoExtractor::TodoExtractor(CXTranslationUnit unit, CXFile file)
: m_unit(unit)
, m_file(file)
, m_todoMarkerWords(KDevelop::ICore::self()->languageController()->completionSettings()->todoMarkerWords())
{
extractTodos();
}
void TodoExtractor::extractTodos()
{
using uintLimits = std::numeric_limits<uint>;
auto start = clang_getLocation(m_unit, m_file, 1, 1);
auto end = clang_getLocation(m_unit, m_file, uintLimits::max(), uintLimits::max());
auto range = clang_getRange(start, end);
IndexedString path(QDir(ClangString(clang_getFileName(m_file)).toString()).canonicalPath());
if(clang_Range_isNull(range)){
return;
}
const ClangTokens tokens(m_unit, range);
for (CXToken token : tokens) {
CXTokenKind tokenKind = clang_getTokenKind(token);
if (tokenKind != CXToken_Comment) {
continue;
}
CXString tokenSpelling = clang_getTokenSpelling(m_unit, token);
auto tokenRange = ClangRange(clang_getTokenExtent(m_unit, token)).toRange();
const QString text = ClangString(tokenSpelling).toString();
CommentTodoParser parser(text, m_todoMarkerWords);
const auto parserResults = parser.results();
m_problems.reserve(m_problems.size() + parserResults.size());
for (const CommentTodoParser::Result& result : parserResults) {
ProblemPointer problem(new Problem);
problem->setDescription(result.description);
problem->setSeverity(IProblem::Hint);
problem->setSource(IProblem::ToDo);
// move the local range to the correct location
// note: localRange is the range *within* the comment only
auto localRange = result.localRange;
KTextEditor::Range todoRange{
tokenRange.start().line() + localRange.start().line(),
localRange.start().column(),
tokenRange.start().line() + localRange.end().line(),
localRange.end().column()};
problem->setFinalLocation({path, todoRange});
m_problems << problem;
}
}
}
QList< ProblemPointer > TodoExtractor::problems() const
{
return m_problems;
}
|