Clang: Refactor TokenInfos

Remove code duplication.

Change-Id: Ib1859f2c3a04f66d0f0b669b4e93a7fc06ab8e61
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-02-06 15:48:24 +01:00
parent 2a016d05b2
commit d01b474b46
24 changed files with 394 additions and 596 deletions

View File

@@ -52,15 +52,14 @@ HEADERS += \
$$PWD/diagnosticsetiterator.h \
$$PWD/fixit.h \
$$PWD/fulltokeninfo.h \
$$PWD/fulltokeninfos.h \
$$PWD/projectpart.h \
$$PWD/projects.h \
$$PWD/skippedsourceranges.h \
$$PWD/sourcelocation.h \
$$PWD/sourcerange.h \
$$PWD/tokeninfo.h \
$$PWD/tokeninfos.h \
$$PWD/tokeninfositerator.h \
$$PWD/tokenprocessor.h \
$$PWD/tokenprocessoriterator.h \
$$PWD/unsavedfile.h \
$$PWD/unsavedfiles.h \
$$PWD/utf8positionfromlinecolumn.h
@@ -111,14 +110,12 @@ SOURCES += \
$$PWD/diagnosticset.cpp \
$$PWD/fixit.cpp \
$$PWD/fulltokeninfo.cpp \
$$PWD/fulltokeninfos.cpp \
$$PWD/projectpart.cpp \
$$PWD/projects.cpp \
$$PWD/skippedsourceranges.cpp \
$$PWD/sourcelocation.cpp \
$$PWD/sourcerange.cpp \
$$PWD/tokeninfo.cpp \
$$PWD/tokeninfos.cpp \
$$PWD/unsavedfile.cpp \
$$PWD/unsavedfiles.cpp \
$$PWD/utf8positionfromlinecolumn.cpp

View File

@@ -30,7 +30,7 @@
#include "clangfilesystemwatcher.h"
#include "codecompleter.h"
#include "diagnosticset.h"
#include "tokeninfos.h"
#include "tokenprocessor.h"
#include "clangexceptions.h"
#include "skippedsourceranges.h"

View File

@@ -26,7 +26,7 @@
#include "clangdocuments.h"
#include <diagnosticset.h>
#include <tokeninfos.h>
#include <tokenprocessor.h>
#include <clangexceptions.h>
#include <projects.h>
#include <skippedsourceranges.h>

View File

@@ -31,13 +31,12 @@
#include "clangtranslationunitupdater.h"
#include "clangfollowsymbol.h"
#include "clangfollowsymboljob.h"
#include "tokenprocessor.h"
#include <codecompleter.h>
#include <cursor.h>
#include <diagnosticcontainer.h>
#include <diagnosticset.h>
#include <tokeninfos.h>
#include <fulltokeninfos.h>
#include <skippedsourceranges.h>
#include <sourcelocation.h>
#include <sourcerange.h>
@@ -201,34 +200,24 @@ Cursor TranslationUnit::cursor() const
return clang_getTranslationUnitCursor(m_cxTranslationUnit);
}
TokenInfos TranslationUnit::tokenInfos() const
TokenProcessor<TokenInfo> TranslationUnit::tokenInfos() const
{
return tokenInfosInRange(cursor().sourceRange());
}
TokenInfos TranslationUnit::tokenInfosInRange(const SourceRange &range) const
TokenProcessor<TokenInfo> TranslationUnit::tokenInfosInRange(const SourceRange &range) const
{
CXToken *cxTokens = 0;
uint cxTokensCount = 0;
clang_tokenize(m_cxTranslationUnit, range, &cxTokens, &cxTokensCount);
return TokenInfos(m_cxTranslationUnit, cxTokens, cxTokensCount);
return TokenProcessor<TokenInfo>(m_cxTranslationUnit, range);
}
FullTokenInfos TranslationUnit::fullTokenInfos() const
TokenProcessor<FullTokenInfo> TranslationUnit::fullTokenInfos() const
{
return fullTokenInfosInRange(cursor().sourceRange());
}
FullTokenInfos TranslationUnit::fullTokenInfosInRange(const SourceRange &range) const
TokenProcessor<FullTokenInfo> TranslationUnit::fullTokenInfosInRange(const SourceRange &range) const
{
CXToken *cxTokens = 0;
uint cxTokensCount = 0;
clang_tokenize(m_cxTranslationUnit, range, &cxTokens, &cxTokensCount);
return FullTokenInfos(m_cxTranslationUnit, cxTokens, cxTokensCount);
return TokenProcessor<FullTokenInfo>(m_cxTranslationUnit, range);
}
SkippedSourceRanges TranslationUnit::skippedSourceRanges() const

View File

@@ -25,6 +25,9 @@
#pragma once
#include "fulltokeninfo.h"
#include "tokenprocessor.h"
#include <clangsupport/codecompletion.h>
#include <clang-c/Index.h>
@@ -34,9 +37,6 @@ namespace ClangBackEnd {
class Cursor;
class DiagnosticContainer;
class DiagnosticSet;
class TokenInfoContainer;
class TokenInfos;
class FullTokenInfos;
class ReferencesResult;
class SkippedSourceRanges;
class SourceLocation;
@@ -101,11 +101,11 @@ public:
Cursor cursorAt(const Utf8String &filePath, uint line, uint column) const;
Cursor cursor() const;
TokenInfos tokenInfos() const;
TokenInfos tokenInfosInRange(const SourceRange &range) const;
TokenProcessor<TokenInfo> tokenInfos() const;
TokenProcessor<TokenInfo> tokenInfosInRange(const SourceRange &range) const;
FullTokenInfos fullTokenInfos() const;
FullTokenInfos fullTokenInfosInRange(const SourceRange &range) const;
TokenProcessor<FullTokenInfo> fullTokenInfos() const;
TokenProcessor<FullTokenInfo> fullTokenInfosInRange(const SourceRange &range) const;
SkippedSourceRanges skippedSourceRanges() const;
SourceRangeContainer followSymbol(uint line, uint column) const;

View File

@@ -24,7 +24,6 @@
****************************************************************************/
#include "clangupdateextradocumentannotationsjob.h"
#include "fulltokeninfos.h"
#include <clangsupport/clangsupportdebugutils.h>
#include <clangsupport/clangcodemodelclientinterface.h>

View File

@@ -1,93 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2018 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 "fulltokeninfos.h"
#include <clangsupport/tokeninfocontainer.h>
#include <QVector>
namespace ClangBackEnd {
FullTokenInfos::FullTokenInfos(CXTranslationUnit cxTranslationUnit, CXToken *tokens, uint tokensCount)
: cxTranslationUnit(cxTranslationUnit),
cxTokens(tokens),
cxTokenCount(tokensCount)
{
cxCursors.resize(tokensCount);
clang_annotateTokens(cxTranslationUnit, cxTokens, cxTokenCount, cxCursors.data());
}
FullTokenInfos::~FullTokenInfos()
{
clang_disposeTokens(cxTranslationUnit, cxTokens, cxTokenCount);
}
QVector<TokenInfoContainer> FullTokenInfos::toTokenInfoContainers() const
{
QVector<TokenInfoContainer> containers;
containers.reserve(static_cast<int>(size()));
const auto isValidTokenInfo = [] (const TokenInfo &tokenInfo) {
// Do not exclude StringLiteral because it can be a filename for an #include
return !tokenInfo.hasInvalidMainType()
&& !tokenInfo.hasMainType(HighlightingType::NumberLiteral)
&& !tokenInfo.hasMainType(HighlightingType::Comment);
};
for (size_t index = 0; index < cxCursors.size(); ++index) {
FullTokenInfo fullTokenInfo = (*this)[index];
if (isValidTokenInfo(fullTokenInfo))
containers.push_back(fullTokenInfo);
}
return containers;
}
bool FullTokenInfos::isEmpty() const
{
return cxTokenCount == 0;
}
bool FullTokenInfos::isNull() const
{
return cxTokens == nullptr;
}
size_t FullTokenInfos::size() const
{
return cxTokenCount;
}
FullTokenInfo FullTokenInfos::operator[](size_t index) const
{
FullTokenInfo tokenInfo(cxCursors[index],
cxTokens + index,
cxTranslationUnit,
currentOutputArgumentRanges);
tokenInfo.evaluate();
return tokenInfo;
}
} // namespace ClangBackEnd

View File

@@ -1,59 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2018 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.
**
****************************************************************************/
#pragma once
#include "fulltokeninfo.h"
#include <clang-c/Index.h>
#include <vector>
namespace ClangBackEnd {
class FullTokenInfos
{
public:
FullTokenInfos() = default;
FullTokenInfos(CXTranslationUnit cxTranslationUnit, CXToken *tokens, uint tokensCount);
~FullTokenInfos();
bool isEmpty() const;
bool isNull() const;
size_t size() const;
FullTokenInfo operator[](size_t index) const;
QVector<TokenInfoContainer> toTokenInfoContainers() const;
private:
mutable std::vector<CXSourceRange> currentOutputArgumentRanges;
CXTranslationUnit cxTranslationUnit = nullptr;
CXToken *const cxTokens = nullptr;
const uint cxTokenCount = 0;
std::vector<CXCursor> cxCursors;
};
} // namespace ClangBackEnd

View File

@@ -1,111 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 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 "tokeninfos.h"
#include "tokeninfocontainer.h"
#include <QVector>
namespace ClangBackEnd {
TokenInfos::TokenInfos(CXTranslationUnit cxTranslationUnit, CXToken *tokens, uint tokensCount)
: cxTranslationUnit(cxTranslationUnit),
cxTokens(tokens),
cxTokenCount(tokensCount)
{
cxCursors.resize(tokensCount);
clang_annotateTokens(cxTranslationUnit, cxTokens, cxTokenCount, cxCursors.data());
}
TokenInfos::~TokenInfos()
{
clang_disposeTokens(cxTranslationUnit, cxTokens, cxTokenCount);
}
TokenInfos::const_iterator TokenInfos::begin() const
{
return const_iterator(cxCursors.cbegin(),
cxTokens,
cxTranslationUnit,
currentOutputArgumentRanges);
}
TokenInfos::const_iterator TokenInfos::end() const
{
return const_iterator(cxCursors.cend(),
cxTokens + cxTokenCount,
cxTranslationUnit,
currentOutputArgumentRanges);
}
QVector<TokenInfoContainer> TokenInfos::toTokenInfoContainers() const
{
QVector<TokenInfoContainer> containers;
containers.reserve(size());
const auto isValidTokenInfo = [] (const TokenInfo &tokenInfo) {
return !tokenInfo.hasInvalidMainType()
&& !tokenInfo.hasMainType(HighlightingType::NumberLiteral)
&& !tokenInfo.hasMainType(HighlightingType::Comment);
};
for (const TokenInfo &tokenInfo : *this)
if (isValidTokenInfo(tokenInfo))
containers.push_back(tokenInfo);
return containers;
}
bool TokenInfos::currentOutputArgumentRangesAreEmpty() const
{
return currentOutputArgumentRanges.empty();
}
bool TokenInfos::isEmpty() const
{
return cxTokenCount == 0;
}
bool ClangBackEnd::TokenInfos::isNull() const
{
return cxTokens == nullptr;
}
uint TokenInfos::size() const
{
return cxTokenCount;
}
TokenInfo TokenInfos::operator[](size_t index) const
{
TokenInfo tokenInfo(cxCursors[index],
cxTokens + index,
cxTranslationUnit,
currentOutputArgumentRanges);
tokenInfo.evaluate();
return tokenInfo;
}
} // namespace ClangBackEnd

View File

@@ -1,72 +0,0 @@
/****************************************************************************
**
** Copyright (C) 2016 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.
**
****************************************************************************/
#pragma once
#include "tokeninfositerator.h"
#include <clang-c/Index.h>
#include <vector>
namespace ClangBackEnd {
using uint = unsigned int;
class TokenInfoContainer;
class TokenInfos
{
public:
using const_iterator = TokenInfosIterator;
using value_type = TokenInfo;
public:
TokenInfos() = default;
TokenInfos(CXTranslationUnit cxTranslationUnit, CXToken *tokens, uint tokensCount);
~TokenInfos();
bool isEmpty() const;
bool isNull() const;
uint size() const;
TokenInfo operator[](size_t index) const;
const_iterator begin() const;
const_iterator end() const;
QVector<TokenInfoContainer> toTokenInfoContainers() const;
bool currentOutputArgumentRangesAreEmpty() const;
private:
mutable std::vector<CXSourceRange> currentOutputArgumentRanges;
CXTranslationUnit cxTranslationUnit = nullptr;
CXToken *const cxTokens = nullptr;
const uint cxTokenCount = 0;
std::vector<CXCursor> cxCursors;
};
} // namespace ClangBackEnd

View File

@@ -0,0 +1,137 @@
/****************************************************************************
**
** Copyright (C) 2016 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.
**
****************************************************************************/
#pragma once
#include "sourcerange.h"
#include "tokenprocessoriterator.h"
#include "tokeninfocontainer.h"
#include <clang-c/Index.h>
#include <QVector>
#include <vector>
namespace ClangBackEnd {
using uint = unsigned int;
template<class T>
class TokenProcessor
{
static_assert (std::is_base_of<TokenInfo, T>::value,
"Use TokenProcessor only with classes derived from TokenInfo");
public:
using const_iterator = TokenProcessorIterator<T>;
using value_type = T;
public:
TokenProcessor() = default;
TokenProcessor(CXTranslationUnit cxTranslationUnit, const SourceRange &range)
: cxTranslationUnit(cxTranslationUnit)
{
uint cxTokensCount = 0;
clang_tokenize(cxTranslationUnit, range, &cxTokens, &cxTokensCount);
cxCursors.resize(cxTokensCount);
clang_annotateTokens(cxTranslationUnit, cxTokens, cxTokensCount, cxCursors.data());
}
~TokenProcessor()
{
clang_disposeTokens(cxTranslationUnit, cxTokens, cxCursors.size());
}
bool isEmpty() const
{
return cxCursors.empty();
}
bool isNull() const
{
return cxTokens == nullptr;
}
uint size() const
{
return cxCursors.size();
}
const_iterator begin() const
{
return const_iterator(cxCursors.cbegin(),
cxTokens,
cxTranslationUnit,
currentOutputArgumentRanges);
}
const_iterator end() const
{
return const_iterator(cxCursors.cend(),
cxTokens + cxCursors.size(),
cxTranslationUnit,
currentOutputArgumentRanges);
}
T operator[](size_t index) const
{
T tokenInfo(cxCursors[index], cxTokens + index, cxTranslationUnit,
currentOutputArgumentRanges);
tokenInfo.evaluate();
return tokenInfo;
}
QVector<TokenInfoContainer> toTokenInfoContainers() const
{
QVector<TokenInfoContainer> containers;
containers.reserve(size());
const auto isValidTokenInfo = [] (const T &tokenInfo) {
return !tokenInfo.hasInvalidMainType()
&& !tokenInfo.hasMainType(HighlightingType::NumberLiteral)
&& !tokenInfo.hasMainType(HighlightingType::Comment);
};
for (size_t index = 0; index < cxCursors.size(); ++index) {
T tokenInfo = (*this)[index];
if (isValidTokenInfo(tokenInfo))
containers.push_back(tokenInfo);
}
return containers;
}
bool currentOutputArgumentRangesAreEmpty() const
{
return currentOutputArgumentRanges.empty();
}
private:
mutable std::vector<CXSourceRange> currentOutputArgumentRanges;
CXTranslationUnit cxTranslationUnit = nullptr;
CXToken *cxTokens = nullptr;
std::vector<CXCursor> cxCursors;
};
} // namespace ClangBackEnd

View File

@@ -39,20 +39,21 @@ using uint = unsigned int;
class DiagnosticSet;
class Diagnostic;
class TokenInfosIterator : public std::iterator<std::forward_iterator_tag, TokenInfo, uint>
template<class T>
class TokenProcessorIterator : public std::iterator<std::forward_iterator_tag, TokenInfo, uint>
{
public:
TokenInfosIterator(std::vector<CXCursor>::const_iterator cxCursorIterator,
CXToken *cxToken,
CXTranslationUnit cxTranslationUnit,
std::vector<CXSourceRange> &currentOutputArgumentRanges)
TokenProcessorIterator(std::vector<CXCursor>::const_iterator cxCursorIterator,
CXToken *cxToken,
CXTranslationUnit cxTranslationUnit,
std::vector<CXSourceRange> &currentOutputArgumentRanges)
: cxCursorIterator(cxCursorIterator),
cxToken(cxToken),
cxTranslationUnit(cxTranslationUnit),
currentOutputArgumentRanges(currentOutputArgumentRanges)
{}
TokenInfosIterator& operator++()
TokenProcessorIterator& operator++()
{
++cxCursorIterator;
++cxToken;
@@ -60,30 +61,27 @@ public:
return *this;
}
TokenInfosIterator operator++(int)
TokenProcessorIterator operator++(int)
{
return TokenInfosIterator(cxCursorIterator++,
cxToken++,
cxTranslationUnit,
currentOutputArgumentRanges);
return TokenProcessorIterator(cxCursorIterator++,
cxToken++,
cxTranslationUnit,
currentOutputArgumentRanges);
}
bool operator==(TokenInfosIterator other) const
bool operator==(TokenProcessorIterator other) const
{
return cxCursorIterator == other.cxCursorIterator;
}
bool operator!=(TokenInfosIterator other) const
bool operator!=(TokenProcessorIterator other) const
{
return cxCursorIterator != other.cxCursorIterator;
}
TokenInfo operator*()
T operator*()
{
TokenInfo tokenInfo(*cxCursorIterator,
cxToken,
cxTranslationUnit,
currentOutputArgumentRanges);
T tokenInfo(*cxCursorIterator, cxToken, cxTranslationUnit, currentOutputArgumentRanges);
tokenInfo.evaluate();
return tokenInfo;
}