From 5e872c0fcd229a540f84f846504e8d874112c8eb Mon Sep 17 00:00:00 2001 From: Ivan Donchevskii Date: Tue, 6 Feb 2018 08:19:42 +0100 Subject: [PATCH] Clang: Introduce OverviewModel for clang Change-Id: I1473e3f679f4345a04c55f1ee80cfe35e21a4347 Reviewed-by: Marco Bubke Reviewed-by: hjk --- src/plugins/clangcodemodel/clangcodemodel.pro | 6 +- .../clangmodelmanagersupport.cpp | 6 +- .../clangcodemodel/clangoverviewmodel.cpp | 245 ++++++++++++++++++ .../clangcodemodel/clangoverviewmodel.h | 66 +++++ src/plugins/cpptools/abstractoverviewmodel.h | 7 +- src/plugins/cpptools/cppeditoroutline.cpp | 15 +- 6 files changed, 330 insertions(+), 15 deletions(-) create mode 100644 src/plugins/clangcodemodel/clangoverviewmodel.cpp create mode 100644 src/plugins/clangcodemodel/clangoverviewmodel.h diff --git a/src/plugins/clangcodemodel/clangcodemodel.pro b/src/plugins/clangcodemodel/clangcodemodel.pro index 6541f55f3cc..decfacf8bdc 100644 --- a/src/plugins/clangcodemodel/clangcodemodel.pro +++ b/src/plugins/clangcodemodel/clangcodemodel.pro @@ -40,7 +40,8 @@ SOURCES += \ clangrefactoringengine.cpp \ clangtextmark.cpp \ clanguiheaderondiskmanager.cpp \ - clangutils.cpp + clangutils.cpp \ + clangoverviewmodel.cpp HEADERS += \ clangactivationsequencecontextprocessor.h \ @@ -79,7 +80,8 @@ HEADERS += \ clangrefactoringengine.h \ clangtextmark.h \ clanguiheaderondiskmanager.h \ - clangutils.h + clangutils.h \ + clangoverviewmodel.h FORMS += clangprojectsettingswidget.ui diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index 8ce75e5fdc1..ce7505a3dcf 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -33,12 +33,10 @@ #include "clangprojectsettings.h" #include "clangrefactoringengine.h" #include "clangcurrentdocumentfilter.h" +#include "clangoverviewmodel.h" #include -// TODO: replace with clang based overview model -#include - #include #include #include @@ -153,7 +151,7 @@ CppTools::RefactoringEngineInterface &ModelManagerSupportClang::refactoringEngin std::unique_ptr ModelManagerSupportClang::createOverviewModel() { - return std::make_unique(); + return std::make_unique(); } CppTools::BaseEditorDocumentProcessor *ModelManagerSupportClang::createEditorDocumentProcessor( diff --git a/src/plugins/clangcodemodel/clangoverviewmodel.cpp b/src/plugins/clangcodemodel/clangoverviewmodel.cpp new file mode 100644 index 00000000000..d9cdebc4398 --- /dev/null +++ b/src/plugins/clangcodemodel/clangoverviewmodel.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** +** +** 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 "clangoverviewmodel.h" + +#include "clangeditordocumentprocessor.h" +#include "clangutils.h" + +#include + +#include +#include +#include + +using TokenContainer = ClangBackEnd::TokenInfoContainer; +using TokenContainers = QVector; + +namespace ClangCodeModel { +namespace Internal { + +static bool contains(const ClangBackEnd::SourceRangeContainer &range, + unsigned line, + unsigned column) +{ + if (line < range.start().line() || line > range.end().line()) + return false; + if (line == range.start().line() && column < range.start().column()) + return false; + if (line == range.end().line() && column > range.end().column()) + return false; + return true; +} + +static bool contains(const ClangBackEnd::SourceRangeContainer &range, + const ClangBackEnd::SourceLocationContainer &location) +{ + return contains(range, location.line(), location.column()); +} + +void buildTree(TokenContainers::const_iterator containersBegin, + TokenContainers::const_iterator containersEnd, + TokenTreeItem *parent, bool isRoot = false) +{ + for (auto it = containersBegin; it != containersEnd;) { + if (!it->extraInfo().declaration) { + ++it; + continue; + } + if (it->types().mainHighlightingType == ClangBackEnd::HighlightingType::LocalVariable) { + ++it; + continue; + } + + auto *item = new TokenTreeItem(*it); + parent->appendChild(item); + const auto &range = it->extraInfo().cursorRange; + + ++it; + auto innerIt = it; + for (; innerIt != containersEnd; ++innerIt) { + if (!innerIt->extraInfo().declaration) + continue; + if (innerIt->types().mainHighlightingType + == ClangBackEnd::HighlightingType::LocalVariable) { + continue; + } + const auto &start = innerIt->extraInfo().cursorRange.start(); + if (!contains(range, start)) { + break; + } + } + if (innerIt != it) { + buildTree(it, innerIt, item); + it = innerIt; + } + } + + if (isRoot) { + ClangBackEnd::ExtraInfo extraInfo; + if (!parent->childCount()) { + extraInfo.token = Utf8String::fromString( + QString(QT_TRANSLATE_NOOP("ClangCodeModel", ""))); + } else { + extraInfo.token = Utf8String::fromString( + QString(QT_TRANSLATE_NOOP("ClangCodeModel", "