CppEditor: Clean up cpptoolsreuse.{h,cpp}

A lot of code had a more suitable place to live in, some was even
unused.

Change-Id: I36478ef473aa14395a182c9ac5e3738fc93134b6
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-06-18 17:43:21 +02:00
parent e3d3af81cb
commit c0bfd9bbd6
29 changed files with 455 additions and 460 deletions

View File

@@ -14,7 +14,10 @@
#include <optional>
namespace Core { class SearchResult; }
namespace Core {
class HelpItem;
class SearchResult;
}
namespace CppEditor { class CppEditorWidget; }
namespace LanguageServerProtocol { class Range; }
namespace ProjectExplorer {

View File

@@ -6,6 +6,8 @@
#include "clangdclient.h"
#include "clangmodelmanagersupport.h"
#include <coreplugin/editormanager/editormanager.h>
#include <cppeditor/cppeditorconstants.h>
#include <cppeditor/cppeditortr.h>
#include <cppeditor/cpplocatorfilter.h>

View File

@@ -47,7 +47,7 @@ Project *projectForCurrentEditor()
return nullptr;
if (ProjectPart::ConstPtr projectPart = projectPartForFile(filePath))
return projectForProjectPart(*projectPart);
return projectPart->project();
return nullptr;
}
@@ -80,7 +80,7 @@ void disableDiagnosticInCurrentProjectConfig(const ClangDiagnostic &diagnostic)
// Get config
ClangDiagnosticConfig config = diagnosticConfig();
ClangDiagnosticConfigsModel configsModel = CppEditor::diagnosticConfigsModel();
ClangDiagnosticConfigsModel configsModel = ClangdSettings::diagnosticConfigsModel();
// Create copy if needed
if (config.isReadOnly()) {

View File

@@ -5,6 +5,7 @@
#include "clangcodemodeltr.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <cppeditor/baseeditordocumentparser.h>

View File

@@ -38,12 +38,12 @@
#include <QSortFilterProxyModel>
#include <QStackedWidget>
#include <QStringListModel>
#include <QTextEdit>
#include <QTreeView>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QUuid>
using namespace CppEditor;
using namespace Utils;

View File

@@ -22,6 +22,7 @@
#include <coreplugin/messagemanager.h>
#include <coreplugin/modemanager.h>
#include <cppeditor/cppprojectfile.h>
#include <cppeditor/cpptoolsreuse.h>
#include <debugger/analyzer/analyzerconstants.h>

View File

@@ -6,8 +6,10 @@
#include "cppeditor_global.h"
#include "cpptoolsreuse.h"
#include "cppworkingcopy.h"
#include "projectpart.h"
#include <projectexplorer/project.h>
#include <utils/cpplanguage_details.h>
#include <QObject>
#include <QMutex>

View File

@@ -12,6 +12,7 @@
#include <cplusplus/CppDocument.h>
#include <cplusplus/Macro.h>
#include <cplusplus/Overview.h>
#include <cplusplus/TranslationUnit.h>
#include <utils/async.h>
@@ -26,6 +27,51 @@ namespace CppEditor {
using SemanticUses = QList<SemanticInfo::Use>;
namespace {
static bool isOwnershipRAIIName(const QString &name)
{
static QSet<QString> knownNames;
if (knownNames.isEmpty()) {
// Qt
knownNames.insert(QLatin1String("QScopedPointer"));
knownNames.insert(QLatin1String("QScopedArrayPointer"));
knownNames.insert(QLatin1String("QMutexLocker"));
knownNames.insert(QLatin1String("QReadLocker"));
knownNames.insert(QLatin1String("QWriteLocker"));
// Standard C++
knownNames.insert(QLatin1String("auto_ptr"));
knownNames.insert(QLatin1String("unique_ptr"));
// Boost
knownNames.insert(QLatin1String("scoped_ptr"));
knownNames.insert(QLatin1String("scoped_array"));
}
return knownNames.contains(name);
}
static bool isOwnershipRAIIType(Symbol *symbol, const LookupContext &context)
{
if (!symbol)
return false;
// This is not a "real" comparison of types. What we do is to resolve the symbol
// in question and then try to match its name with already known ones.
if (symbol->asDeclaration()) {
Declaration *declaration = symbol->asDeclaration();
const NamedType *namedType = declaration->type()->asNamedType();
if (namedType) {
ClassOrNamespace *clazz = context.lookupType(namedType->name(),
declaration->enclosingScope());
if (clazz && !clazz->symbols().isEmpty()) {
Overview overview;
Symbol *symbol = clazz->symbols().at(0);
return isOwnershipRAIIName(overview.prettyName(symbol->name()));
}
}
}
return false;
}
CursorInfo::Range toRange(const SemanticInfo::Use &use)
{
return {use.line, use.column, use.length};

View File

@@ -4,6 +4,7 @@
#include "clangdiagnosticconfigsmodel.h"
#include "cppeditorconstants.h"
#include "cppeditortr.h"
#include "cpptoolsreuse.h"
#include <utils/algorithm.h>
@@ -88,6 +89,34 @@ QStringList ClangDiagnosticConfigsModel::globalDiagnosticOptions()
};
}
void ClangDiagnosticConfigsModel::addBuiltinConfigs()
{
ClangDiagnosticConfig config;
// Questionable constructs
config = ClangDiagnosticConfig();
config.setId(Constants::CPP_CLANG_DIAG_CONFIG_QUESTIONABLE);
config.setDisplayName(Tr::tr("Checks for questionable constructs"));
config.setIsReadOnly(true);
config.setClangOptions({
"-Wall",
"-Wextra",
});
config.setClazyMode(ClangDiagnosticConfig::ClazyMode::UseCustomChecks);
config.setClangTidyMode(ClangDiagnosticConfig::TidyMode::UseCustomChecks);
appendOrUpdate(config);
// Warning flags from build system
config = ClangDiagnosticConfig();
config.setId(Constants::CPP_CLANG_DIAG_CONFIG_BUILDSYSTEM);
config.setDisplayName(Tr::tr("Build-system warnings"));
config.setIsReadOnly(true);
config.setClazyMode(ClangDiagnosticConfig::ClazyMode::UseCustomChecks);
config.setClangTidyMode(ClangDiagnosticConfig::TidyMode::UseCustomChecks);
config.setUseBuildSystemWarnings(true);
appendOrUpdate(config);
}
int ClangDiagnosticConfigsModel::indexOfConfig(const Utils::Id &id) const
{
return Utils::indexOf(m_diagnosticConfigs, [&](const ClangDiagnosticConfig &config) {

View File

@@ -34,6 +34,8 @@ public:
const QString &displayName);
static QStringList globalDiagnosticOptions();
void addBuiltinConfigs();
private:
ClangDiagnosticConfigs m_diagnosticConfigs;
};

View File

@@ -11,6 +11,7 @@
#include "cpptoolsreuse.h"
#include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/session.h>
#include <projectexplorer/project.h>
@@ -201,6 +202,16 @@ void ClangdSettings::setCustomDiagnosticConfigs(const ClangDiagnosticConfigs &co
instance().saveSettings();
}
ClangDiagnosticConfigsModel ClangdSettings::diagnosticConfigsModel()
{
const ClangDiagnosticConfigs &customConfigs = instance().customDiagnosticConfigs();
ClangDiagnosticConfigsModel model;
model.addBuiltinConfigs();
for (const ClangDiagnosticConfig &config : customConfigs)
model.appendOrUpdate(config);
return model;
}
FilePath ClangdSettings::clangdFilePath() const
{
if (!m_data.executableFilePath.isEmpty())
@@ -237,7 +248,7 @@ Id ClangdSettings::diagnosticConfigId() const
ClangDiagnosticConfig ClangdSettings::diagnosticConfig() const
{
return diagnosticConfigsModel(customDiagnosticConfigs()).configWithId(diagnosticConfigId());
return diagnosticConfigsModel().configWithId(diagnosticConfigId());
}
ClangdSettings::Granularity ClangdSettings::granularity() const
@@ -761,7 +772,7 @@ ClangdSettingsWidget::ClangdSettingsWidget(const ClangdSettings::Data &settingsD
m_configSelectionWidget = new ClangDiagnosticConfigsSelectionWidget(formLayout);
m_configSelectionWidget->refresh(
diagnosticConfigsModel(settings.customDiagnosticConfigs()),
ClangdSettings::diagnosticConfigsModel(),
settings.diagnosticConfigId(),
[](const ClangDiagnosticConfigs &configs, const Utils::Id &configToSelect) {
return new CppEditor::ClangDiagnosticConfigsWidget(configs, configToSelect);

View File

@@ -13,6 +13,7 @@ namespace ProjectExplorer { class Project; }
namespace Utils { class MacroExpander; }
namespace CppEditor {
class ClangDiagnosticConfigsModel;
// TODO: Can we move this to ClangCodeModel?
class CPPEDITOR_EXPORT ClangdSettings : public QObject
@@ -96,6 +97,8 @@ public:
static void setDefaultClangdPath(const Utils::FilePath &filePath);
static void setCustomDiagnosticConfigs(const ClangDiagnosticConfigs &configs);
static ClangDiagnosticConfigsModel diagnosticConfigsModel();
Utils::FilePath clangdFilePath() const;
IndexingPriority indexingPriority() const { return m_data.indexingPriority; }
Utils::FilePath projectIndexPath(const Utils::MacroExpander &expander) const;

View File

@@ -29,6 +29,7 @@ using namespace Utils;
namespace CppEditor::Internal {
namespace {
class CppHoverHandler : public TextEditor::BaseHoverHandler
{
private:

View File

@@ -1913,7 +1913,7 @@ void CppCodeModelInspectorDialog::updateProjectPartData(const ProjectPart::Const
QString projectFilePath = QLatin1String("<None>");
if (part->hasProject()) {
projectFilePath = part->topLevelProject.toUserOutput();
if (const ProjectExplorer::Project * const project = projectForProjectPart(*part))
if (const ProjectExplorer::Project * const project = part->project())
projectName = project->displayName();
}
const QString callGroupId = part->callGroupId.isEmpty() ? QString::fromLatin1("<None>")

View File

@@ -505,7 +505,7 @@ void Dumper::dumpProjectInfos(const QList<ProjectInfo::ConstPtr> &projectInfos)
QString projectFilePath = "<None>";
if (part->hasProject()) {
projectFilePath = part->topLevelProject.toUserOutput();
if (const ProjectExplorer::Project * const project = projectForProjectPart(*part))
if (const ProjectExplorer::Project * const project = part->project())
projectName = project->displayName();
}
if (!part->projectConfigFile.isEmpty())

View File

@@ -250,7 +250,7 @@ FilePath correspondingHeaderOrSource(const FilePath &filePath, bool *wasHeader,
} else {
const QList<ProjectInfo::ConstPtr> projectInfos = CppModelManager::projectInfos();
for (const ProjectInfo::ConstPtr &projectInfo : projectInfos) {
const Project *project = projectForProjectInfo(*projectInfo);
const Project *project = projectInfo->project();
if (project == currentProject)
continue; // We have already checked the current project.

View File

@@ -7,6 +7,7 @@
#include "cpptoolsreuse.h"
#include <texteditor/textdocumentlayout.h>
#include <utils/algorithm.h>
#include <utils/textutils.h>
#include <cplusplus/SimpleLexer.h>

View File

@@ -1573,7 +1573,7 @@ QFuture<void> CppModelManager::updateProjectInfo(const ProjectInfo::ConstPtr &ne
QStringList removedProjectParts;
bool filesRemoved = false;
Project * const project = projectForProjectInfo(*newProjectInfo);
Project * const project = newProjectInfo->project();
if (!project)
return {};

View File

@@ -3,7 +3,7 @@
#pragma once
#include "cpptoolsreuse.h"
#include "projectpart.h"
#include <QAbstractListModel>
#include <QComboBox>

View File

@@ -3,20 +3,16 @@
#include "cpptoolsreuse.h"
#include "clangdiagnosticconfigsmodel.h"
#include "clangdsettings.h"
#include "cppautocompleter.h"
#include "cppcanonicalsymbol.h"
#include "cppcodemodelsettings.h"
#include "cppcompletionassist.h"
#include "cppeditorconstants.h"
#include "cppeditorwidget.h"
#include "cppeditortr.h"
#include "cppfilesettingspage.h"
#include "cpphighlighter.h"
#include "cppqtstyleindenter.h"
#include "cpprefactoringchanges.h"
#include "projectinfo.h"
#include "quickfixes/cppquickfixassistant.h"
#include <coreplugin/documentmanager.h>
@@ -144,51 +140,6 @@ void moveCursorToStartOfIdentifier(QTextCursor *tc)
skipCharsBackward(tc, isValidIdentifierChar);
}
static bool isOwnershipRAIIName(const QString &name)
{
static QSet<QString> knownNames;
if (knownNames.isEmpty()) {
// Qt
knownNames.insert(QLatin1String("QScopedPointer"));
knownNames.insert(QLatin1String("QScopedArrayPointer"));
knownNames.insert(QLatin1String("QMutexLocker"));
knownNames.insert(QLatin1String("QReadLocker"));
knownNames.insert(QLatin1String("QWriteLocker"));
// Standard C++
knownNames.insert(QLatin1String("auto_ptr"));
knownNames.insert(QLatin1String("unique_ptr"));
// Boost
knownNames.insert(QLatin1String("scoped_ptr"));
knownNames.insert(QLatin1String("scoped_array"));
}
return knownNames.contains(name);
}
bool isOwnershipRAIIType(Symbol *symbol, const LookupContext &context)
{
if (!symbol)
return false;
// This is not a "real" comparison of types. What we do is to resolve the symbol
// in question and then try to match its name with already known ones.
if (symbol->asDeclaration()) {
Declaration *declaration = symbol->asDeclaration();
const NamedType *namedType = declaration->type()->asNamedType();
if (namedType) {
ClassOrNamespace *clazz = context.lookupType(namedType->name(),
declaration->enclosingScope());
if (clazz && !clazz->symbols().isEmpty()) {
Overview overview;
Symbol *symbol = clazz->symbols().at(0);
return isOwnershipRAIIName(overview.prettyName(symbol->name()));
}
}
}
return false;
}
bool isValidAsciiIdentifierChar(const QChar &ch)
{
return ch.isLetterOrNumber() || ch == QLatin1Char('_');
@@ -377,245 +328,6 @@ bool fileSizeExceedsLimit(const FilePath &filePath, int sizeLimitInMb)
return false;
}
static void addBuiltinConfigs(ClangDiagnosticConfigsModel &model)
{
ClangDiagnosticConfig config;
// Questionable constructs
config = ClangDiagnosticConfig();
config.setId(Constants::CPP_CLANG_DIAG_CONFIG_QUESTIONABLE);
config.setDisplayName(Tr::tr("Checks for questionable constructs"));
config.setIsReadOnly(true);
config.setClangOptions({
"-Wall",
"-Wextra",
});
config.setClazyMode(ClangDiagnosticConfig::ClazyMode::UseCustomChecks);
config.setClangTidyMode(ClangDiagnosticConfig::TidyMode::UseCustomChecks);
model.appendOrUpdate(config);
// Warning flags from build system
config = ClangDiagnosticConfig();
config.setId(Constants::CPP_CLANG_DIAG_CONFIG_BUILDSYSTEM);
config.setDisplayName(Tr::tr("Build-system warnings"));
config.setIsReadOnly(true);
config.setClazyMode(ClangDiagnosticConfig::ClazyMode::UseCustomChecks);
config.setClangTidyMode(ClangDiagnosticConfig::TidyMode::UseCustomChecks);
config.setUseBuildSystemWarnings(true);
model.appendOrUpdate(config);
}
ClangDiagnosticConfigsModel diagnosticConfigsModel(const ClangDiagnosticConfigs &customConfigs)
{
ClangDiagnosticConfigsModel model;
addBuiltinConfigs(model);
for (const ClangDiagnosticConfig &config : customConfigs)
model.appendOrUpdate(config);
return model;
}
ClangDiagnosticConfigsModel diagnosticConfigsModel()
{
return diagnosticConfigsModel(ClangdSettings::instance().customDiagnosticConfigs());
}
NSVisitor::NSVisitor(const CppRefactoringFile *file, const QStringList &namespaces, int symbolPos)
: ASTVisitor(file->cppDocument()->translationUnit()),
m_file(file),
m_remainingNamespaces(namespaces),
m_symbolPos(symbolPos)
{}
bool NSVisitor::preVisit(AST *ast)
{
if (!m_firstToken)
m_firstToken = ast;
if (m_file->startOf(ast) >= m_symbolPos)
m_done = true;
return !m_done;
}
bool NSVisitor::visit(NamespaceAST *ns)
{
if (!m_firstNamespace)
m_firstNamespace = ns;
if (m_remainingNamespaces.isEmpty()) {
m_done = true;
return false;
}
QString name;
const Identifier * const id = translationUnit()->identifier(ns->identifier_token);
if (id)
name = QString::fromUtf8(id->chars(), id->size());
if (name != m_remainingNamespaces.first())
return false;
if (!ns->linkage_body) {
m_done = true;
return false;
}
m_enclosingNamespace = ns;
m_remainingNamespaces.removeFirst();
return !m_remainingNamespaces.isEmpty();
}
void NSVisitor::postVisit(AST *ast)
{
if (ast == m_enclosingNamespace)
m_done = true;
}
/**
* @brief The NSCheckerVisitor class checks which namespaces are missing for a given list
* of enclosing namespaces at a given position
*/
NSCheckerVisitor::NSCheckerVisitor(const CppRefactoringFile *file, const QStringList &namespaces,
int symbolPos)
: ASTVisitor(file->cppDocument()->translationUnit())
, m_file(file)
, m_remainingNamespaces(namespaces)
, m_symbolPos(symbolPos)
{}
bool NSCheckerVisitor::preVisit(AST *ast)
{
if (m_file->startOf(ast) >= m_symbolPos)
m_done = true;
return !m_done;
}
void NSCheckerVisitor::postVisit(AST *ast)
{
if (!m_done && m_file->endOf(ast) > m_symbolPos)
m_done = true;
}
bool NSCheckerVisitor::visit(NamespaceAST *ns)
{
if (m_remainingNamespaces.isEmpty())
return false;
QString name = getName(ns);
if (name != m_remainingNamespaces.first())
return false;
m_enteredNamespaces.push_back(ns);
m_remainingNamespaces.removeFirst();
// if we reached the searched namespace we don't have to search deeper
return !m_remainingNamespaces.isEmpty();
}
bool NSCheckerVisitor::visit(UsingDirectiveAST *usingNS)
{
// example: we search foo::bar and get 'using namespace foo;using namespace foo::bar;'
const QString fullName = Overview{}.prettyName(usingNS->name->name);
const QStringList namespaces = fullName.split("::");
if (namespaces.length() > m_remainingNamespaces.length())
return false;
// from other using namespace statements
const auto curList = m_usingsPerNamespace.find(currentNamespace());
const bool isCurListValid = curList != m_usingsPerNamespace.end();
const bool startEqual = std::equal(namespaces.cbegin(),
namespaces.cend(),
m_remainingNamespaces.cbegin());
if (startEqual) {
if (isCurListValid) {
if (namespaces.length() > curList->second.length()) {
// eg. we already have 'using namespace foo;' and
// now get 'using namespace foo::bar;'
curList->second = namespaces;
}
// the other case: first 'using namespace foo::bar;' and now 'using namespace foo;'
} else
m_usingsPerNamespace.emplace(currentNamespace(), namespaces);
} else if (isCurListValid) {
// ex: we have already 'using namespace foo;' and get 'using namespace bar;' now
QStringList newlist = curList->second;
newlist.append(namespaces);
if (newlist.length() <= m_remainingNamespaces.length()) {
const bool startEqual = std::equal(newlist.cbegin(),
newlist.cend(),
m_remainingNamespaces.cbegin());
if (startEqual)
curList->second.append(namespaces);
}
}
return false;
}
void NSCheckerVisitor::endVisit(NamespaceAST *ns)
{
// if the symbolPos was in the namespace and the
// namespace has no children, m_done should be true
postVisit(ns);
if (!m_done && currentNamespace() == ns) {
// we were not succesfull in this namespace, so undo all changes
m_remainingNamespaces.push_front(getName(currentNamespace()));
m_usingsPerNamespace.erase(currentNamespace());
m_enteredNamespaces.pop_back();
}
}
void NSCheckerVisitor::endVisit(TranslationUnitAST *)
{
// the last node, create the final result
// we must handle like the following: We search for foo::bar and have:
// using namespace foo::bar;
// namespace foo {
// // cursor/symbolPos here
// }
if (m_remainingNamespaces.empty()) {
// we are already finished
return;
}
// find the longest combination of normal namespaces + using statements
int longestNamespaceList = 0;
int enteredNamespaceCount = 0;
// check 'using namespace ...;' statements in the global scope
const auto namespaces = m_usingsPerNamespace.find(nullptr);
if (namespaces != m_usingsPerNamespace.end())
longestNamespaceList = namespaces->second.length();
for (auto ns : m_enteredNamespaces) {
++enteredNamespaceCount;
const auto namespaces = m_usingsPerNamespace.find(ns);
int newListLength = enteredNamespaceCount;
if (namespaces != m_usingsPerNamespace.end())
newListLength += namespaces->second.length();
longestNamespaceList = std::max(newListLength, longestNamespaceList);
}
m_remainingNamespaces.erase(m_remainingNamespaces.begin(),
m_remainingNamespaces.begin() + longestNamespaceList
- m_enteredNamespaces.size());
}
QString NSCheckerVisitor::getName(NamespaceAST *ns)
{
const Identifier *const id = translationUnit()->identifier(ns->identifier_token);
if (id)
return QString::fromUtf8(id->chars(), id->size());
return {};
}
NamespaceAST *NSCheckerVisitor::currentNamespace()
{
return m_enteredNamespaces.empty() ? nullptr : m_enteredNamespaces.back();
}
ProjectExplorer::Project *projectForProjectPart(const ProjectPart &part)
{
return ProjectExplorer::ProjectManager::projectWithProjectFilePath(part.topLevelProject);
}
ProjectExplorer::Project *projectForProjectInfo(const ProjectInfo &info)
{
return ProjectExplorer::ProjectManager::projectWithProjectFilePath(info.projectFilePath());
}
void openEditor(const Utils::FilePath &filePath, bool inNextSplit, Utils::Id editorId)
{
using Core::EditorManager;

View File

@@ -5,25 +5,21 @@
#include "cppeditor_global.h"
#include "clangdiagnosticconfig.h"
#include "projectpart.h"
#include <texteditor/quickfix.h>
#include <texteditor/texteditor.h>
#include <utils/searchresultitem.h>
#include <cplusplus/ASTVisitor.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/Token.h>
#include <texteditor/quickfix.h>
#include <utils/id.h>
#include <utils/searchresultitem.h>
namespace CPlusPlus {
class Macro;
class Symbol;
class LookupContext;
} // namespace CPlusPlus
namespace TextEditor { class AssistInterface; }
namespace ProjectExplorer { class Project; }
namespace TextEditor {
class AssistInterface;
class TextEditorWidget;
}
namespace Utils { namespace Text { class Range; } }
namespace CppEditor {
@@ -49,9 +45,6 @@ int CPPEDITOR_EXPORT activeArgumenForPrefix(const QString &prefix);
QStringList CPPEDITOR_EXPORT identifierWordsUnderCursor(const QTextCursor &tc);
QString CPPEDITOR_EXPORT identifierUnderCursor(QTextCursor *cursor);
bool CPPEDITOR_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
const CPlusPlus::LookupContext &context);
const CPlusPlus::Macro CPPEDITOR_EXPORT *findCanonicalMacro(const QTextCursor &cursor,
CPlusPlus::Document::Ptr document);
@@ -89,114 +82,6 @@ QList<Utils::Text::Range> CPPEDITOR_EXPORT symbolOccurrencesInDeclarationComment
bool fileSizeExceedsLimit(const Utils::FilePath &filePath, int sizeLimitInMb);
ProjectExplorer::Project CPPEDITOR_EXPORT *projectForProjectInfo(const ProjectInfo &info);
ProjectExplorer::Project CPPEDITOR_EXPORT *projectForProjectPart(const ProjectPart &part);
class ClangDiagnosticConfigsModel;
ClangDiagnosticConfigsModel CPPEDITOR_EXPORT diagnosticConfigsModel();
ClangDiagnosticConfigsModel CPPEDITOR_EXPORT
diagnosticConfigsModel(const ClangDiagnosticConfigs &customConfigs);
class CPPEDITOR_EXPORT SymbolInfo
{
public:
int startLine = 0;
int startColumn = 0;
int endLine = 0;
int endColumn = 0;
QString fileName;
bool isResultOnlyForFallBack = false;
};
class ProjectPartInfo {
public:
enum Hint {
NoHint = 0,
IsFallbackMatch = 1 << 0,
IsAmbiguousMatch = 1 << 1,
IsPreferredMatch = 1 << 2,
IsFromProjectMatch = 1 << 3,
IsFromDependenciesMatch = 1 << 4,
};
Q_DECLARE_FLAGS(Hints, Hint)
ProjectPartInfo() = default;
ProjectPartInfo(const ProjectPart::ConstPtr &projectPart,
const QList<ProjectPart::ConstPtr> &projectParts,
Hints hints)
: projectPart(projectPart)
, projectParts(projectParts)
, hints(hints)
{
}
public:
ProjectPart::ConstPtr projectPart;
QList<ProjectPart::ConstPtr> projectParts; // The one above as first plus alternatives.
Hints hints = NoHint;
};
QStringList CPPEDITOR_EXPORT getNamespaceNames(const CPlusPlus::Namespace *firstNamespace);
QStringList CPPEDITOR_EXPORT getNamespaceNames(const CPlusPlus::Symbol *symbol);
class CPPEDITOR_EXPORT NSVisitor : public CPlusPlus::ASTVisitor
{
public:
NSVisitor(const CppRefactoringFile *file, const QStringList &namespaces, int symbolPos);
const QStringList remainingNamespaces() const { return m_remainingNamespaces; }
const CPlusPlus::NamespaceAST *firstNamespace() const { return m_firstNamespace; }
const CPlusPlus::AST *firstToken() const { return m_firstToken; }
const CPlusPlus::NamespaceAST *enclosingNamespace() const { return m_enclosingNamespace; }
private:
bool preVisit(CPlusPlus::AST *ast) override;
bool visit(CPlusPlus::NamespaceAST *ns) override;
void postVisit(CPlusPlus::AST *ast) override;
const CppRefactoringFile * const m_file;
const CPlusPlus::NamespaceAST *m_enclosingNamespace = nullptr;
const CPlusPlus::NamespaceAST *m_firstNamespace = nullptr;
const CPlusPlus::AST *m_firstToken = nullptr;
QStringList m_remainingNamespaces;
const int m_symbolPos;
bool m_done = false;
};
class CPPEDITOR_EXPORT NSCheckerVisitor : public CPlusPlus::ASTVisitor
{
public:
NSCheckerVisitor(const CppRefactoringFile *file, const QStringList &namespaces, int symbolPos);
/**
* @brief returns the names of the namespaces that are additionally needed at the symbolPos
* @return A list of namespace names, the outermost namespace at index 0 and the innermost
* at the last index
*/
const QStringList remainingNamespaces() const { return m_remainingNamespaces; }
private:
bool preVisit(CPlusPlus::AST *ast) override;
void postVisit(CPlusPlus::AST *ast) override;
bool visit(CPlusPlus::NamespaceAST *ns) override;
bool visit(CPlusPlus::UsingDirectiveAST *usingNS) override;
void endVisit(CPlusPlus::NamespaceAST *ns) override;
void endVisit(CPlusPlus::TranslationUnitAST *) override;
QString getName(CPlusPlus::NamespaceAST *ns);
CPlusPlus::NamespaceAST *currentNamespace();
const CppRefactoringFile *const m_file;
QStringList m_remainingNamespaces;
const int m_symbolPos;
std::vector<CPlusPlus::NamespaceAST *> m_enteredNamespaces;
// track 'using namespace ...' statements
std::unordered_map<CPlusPlus::NamespaceAST *, QStringList> m_usingsPerNamespace;
bool m_done = false;
};
namespace Internal {
void decorateCppEditor(TextEditor::TextEditorWidget *editor);
} // namespace Internal

View File

@@ -11,6 +11,8 @@
#include <cplusplus/ASTPath.h>
#include <cplusplus/LookupContext.h>
#include <cplusplus/Symbol.h>
#include <cplusplus/Overview.h>
#include <utils/qtcassert.h>
@@ -18,6 +20,7 @@ using namespace CPlusPlus;
using namespace Utils;
namespace CppEditor {
using namespace Internal;
namespace {
static int ordering(InsertionPointLocator::AccessSpec xsSpec)
@@ -719,39 +722,6 @@ static QStringList getListOfMissingNamespacesForLocation(const CppRefactoringFil
return visitor.remainingNamespaces();
}
/**
* @brief getNamespaceNames Returns a list of namespaces for an enclosing namespaces of a
* namespace (contains the namespace itself)
* @param firstNamespace the starting namespace (included in the list)
* @return the enclosing namespaces, the outermost namespace is at the first index, the innermost
* at the last index
*/
QStringList getNamespaceNames(const Namespace *firstNamespace)
{
QStringList namespaces;
for (const Namespace *scope = firstNamespace; scope; scope = scope->enclosingNamespace()) {
if (scope->name() && scope->name()->identifier()) {
namespaces.prepend(QString::fromUtf8(scope->name()->identifier()->chars(),
scope->name()->identifier()->size()));
} else {
namespaces.prepend(""); // an unnamed namespace
}
}
namespaces.pop_front(); // the "global namespace" is one namespace, but not an unnamed
return namespaces;
}
/**
* @brief getNamespaceNames Returns a list of enclosing namespaces for a symbol
* @param symbol a symbol from which we want the enclosing namespaces
* @return the enclosing namespaces, the outermost namespace is at the first index, the innermost
* at the last index
*/
QStringList getNamespaceNames(const Symbol *symbol)
{
return getNamespaceNames(symbol->enclosingNamespace());
}
InsertionLocation insertLocationForMethodDefinition(Symbol *symbol,
const bool useSymbolFinder,
NamespaceHandling namespaceHandling,
@@ -834,4 +804,226 @@ InsertionLocation insertLocationForMethodDefinition(Symbol *symbol,
return InsertionLocation(filePath, prefix, suffix, line, column);
}
namespace Internal {
NSVisitor::NSVisitor(const CppRefactoringFile *file, const QStringList &namespaces, int symbolPos)
: ASTVisitor(file->cppDocument()->translationUnit()),
m_file(file),
m_remainingNamespaces(namespaces),
m_symbolPos(symbolPos)
{}
bool NSVisitor::preVisit(AST *ast)
{
if (!m_firstToken)
m_firstToken = ast;
if (m_file->startOf(ast) >= m_symbolPos)
m_done = true;
return !m_done;
}
bool NSVisitor::visit(NamespaceAST *ns)
{
if (!m_firstNamespace)
m_firstNamespace = ns;
if (m_remainingNamespaces.isEmpty()) {
m_done = true;
return false;
}
QString name;
const Identifier * const id = translationUnit()->identifier(ns->identifier_token);
if (id)
name = QString::fromUtf8(id->chars(), id->size());
if (name != m_remainingNamespaces.first())
return false;
if (!ns->linkage_body) {
m_done = true;
return false;
}
m_enclosingNamespace = ns;
m_remainingNamespaces.removeFirst();
return !m_remainingNamespaces.isEmpty();
}
void NSVisitor::postVisit(AST *ast)
{
if (ast == m_enclosingNamespace)
m_done = true;
}
/**
* @brief The NSCheckerVisitor class checks which namespaces are missing for a given list
* of enclosing namespaces at a given position
*/
NSCheckerVisitor::NSCheckerVisitor(const CppRefactoringFile *file, const QStringList &namespaces,
int symbolPos)
: ASTVisitor(file->cppDocument()->translationUnit())
, m_file(file)
, m_remainingNamespaces(namespaces)
, m_symbolPos(symbolPos)
{}
bool NSCheckerVisitor::preVisit(AST *ast)
{
if (m_file->startOf(ast) >= m_symbolPos)
m_done = true;
return !m_done;
}
void NSCheckerVisitor::postVisit(AST *ast)
{
if (!m_done && m_file->endOf(ast) > m_symbolPos)
m_done = true;
}
bool NSCheckerVisitor::visit(NamespaceAST *ns)
{
if (m_remainingNamespaces.isEmpty())
return false;
QString name = getName(ns);
if (name != m_remainingNamespaces.first())
return false;
m_enteredNamespaces.push_back(ns);
m_remainingNamespaces.removeFirst();
// if we reached the searched namespace we don't have to search deeper
return !m_remainingNamespaces.isEmpty();
}
bool NSCheckerVisitor::visit(UsingDirectiveAST *usingNS)
{
// example: we search foo::bar and get 'using namespace foo;using namespace foo::bar;'
const QString fullName = Overview{}.prettyName(usingNS->name->name);
const QStringList namespaces = fullName.split("::");
if (namespaces.length() > m_remainingNamespaces.length())
return false;
// from other using namespace statements
const auto curList = m_usingsPerNamespace.find(currentNamespace());
const bool isCurListValid = curList != m_usingsPerNamespace.end();
const bool startEqual = std::equal(namespaces.cbegin(),
namespaces.cend(),
m_remainingNamespaces.cbegin());
if (startEqual) {
if (isCurListValid) {
if (namespaces.length() > curList->second.length()) {
// eg. we already have 'using namespace foo;' and
// now get 'using namespace foo::bar;'
curList->second = namespaces;
}
// the other case: first 'using namespace foo::bar;' and now 'using namespace foo;'
} else
m_usingsPerNamespace.emplace(currentNamespace(), namespaces);
} else if (isCurListValid) {
// ex: we have already 'using namespace foo;' and get 'using namespace bar;' now
QStringList newlist = curList->second;
newlist.append(namespaces);
if (newlist.length() <= m_remainingNamespaces.length()) {
const bool startEqual = std::equal(newlist.cbegin(),
newlist.cend(),
m_remainingNamespaces.cbegin());
if (startEqual)
curList->second.append(namespaces);
}
}
return false;
}
void NSCheckerVisitor::endVisit(NamespaceAST *ns)
{
// if the symbolPos was in the namespace and the
// namespace has no children, m_done should be true
postVisit(ns);
if (!m_done && currentNamespace() == ns) {
// we were not succesfull in this namespace, so undo all changes
m_remainingNamespaces.push_front(getName(currentNamespace()));
m_usingsPerNamespace.erase(currentNamespace());
m_enteredNamespaces.pop_back();
}
}
void NSCheckerVisitor::endVisit(TranslationUnitAST *)
{
// the last node, create the final result
// we must handle like the following: We search for foo::bar and have:
// using namespace foo::bar;
// namespace foo {
// // cursor/symbolPos here
// }
if (m_remainingNamespaces.empty()) {
// we are already finished
return;
}
// find the longest combination of normal namespaces + using statements
int longestNamespaceList = 0;
int enteredNamespaceCount = 0;
// check 'using namespace ...;' statements in the global scope
const auto namespaces = m_usingsPerNamespace.find(nullptr);
if (namespaces != m_usingsPerNamespace.end())
longestNamespaceList = namespaces->second.length();
for (auto ns : m_enteredNamespaces) {
++enteredNamespaceCount;
const auto namespaces = m_usingsPerNamespace.find(ns);
int newListLength = enteredNamespaceCount;
if (namespaces != m_usingsPerNamespace.end())
newListLength += namespaces->second.length();
longestNamespaceList = std::max(newListLength, longestNamespaceList);
}
m_remainingNamespaces.erase(m_remainingNamespaces.begin(),
m_remainingNamespaces.begin() + longestNamespaceList
- m_enteredNamespaces.size());
}
QString NSCheckerVisitor::getName(NamespaceAST *ns)
{
const Identifier *const id = translationUnit()->identifier(ns->identifier_token);
if (id)
return QString::fromUtf8(id->chars(), id->size());
return {};
}
NamespaceAST *NSCheckerVisitor::currentNamespace()
{
return m_enteredNamespaces.empty() ? nullptr : m_enteredNamespaces.back();
}
/**
* @brief getNamespaceNames Returns a list of namespaces for an enclosing namespaces of a
* namespace (contains the namespace itself)
* @param firstNamespace the starting namespace (included in the list)
* @return the enclosing namespaces, the outermost namespace is at the first index, the innermost
* at the last index
*/
QStringList getNamespaceNames(const Namespace *firstNamespace)
{
QStringList namespaces;
for (const Namespace *scope = firstNamespace; scope; scope = scope->enclosingNamespace()) {
if (scope->name() && scope->name()->identifier()) {
namespaces.prepend(QString::fromUtf8(scope->name()->identifier()->chars(),
scope->name()->identifier()->size()));
} else {
namespaces.prepend(""); // an unnamed namespace
}
}
namespaces.pop_front(); // the "global namespace" is one namespace, but not an unnamed
return namespaces;
}
/**
* @brief getNamespaceNames Returns a list of enclosing namespaces for a symbol
* @param symbol a symbol from which we want the enclosing namespaces
* @return the enclosing namespaces, the outermost namespace is at the first index, the innermost
* at the last index
*/
QStringList getNamespaceNames(const Symbol *symbol)
{
return getNamespaceNames(symbol->enclosingNamespace());
}
} // namespace Internal
} // namespace CppEditor;

View File

@@ -113,4 +113,67 @@ insertLocationForMethodDefinition(CPlusPlus::Symbol *symbol,
const Utils::FilePath &fileName,
QStringList *insertedNamespaces = nullptr);
namespace Internal {
class NSVisitor : public CPlusPlus::ASTVisitor
{
public:
NSVisitor(const CppRefactoringFile *file, const QStringList &namespaces, int symbolPos);
const QStringList remainingNamespaces() const { return m_remainingNamespaces; }
const CPlusPlus::NamespaceAST *firstNamespace() const { return m_firstNamespace; }
const CPlusPlus::AST *firstToken() const { return m_firstToken; }
const CPlusPlus::NamespaceAST *enclosingNamespace() const { return m_enclosingNamespace; }
private:
bool preVisit(CPlusPlus::AST *ast) override;
bool visit(CPlusPlus::NamespaceAST *ns) override;
void postVisit(CPlusPlus::AST *ast) override;
const CppRefactoringFile * const m_file;
const CPlusPlus::NamespaceAST *m_enclosingNamespace = nullptr;
const CPlusPlus::NamespaceAST *m_firstNamespace = nullptr;
const CPlusPlus::AST *m_firstToken = nullptr;
QStringList m_remainingNamespaces;
const int m_symbolPos;
bool m_done = false;
};
class NSCheckerVisitor : public CPlusPlus::ASTVisitor
{
public:
NSCheckerVisitor(const CppRefactoringFile *file, const QStringList &namespaces, int symbolPos);
/**
* @brief returns the names of the namespaces that are additionally needed at the symbolPos
* @return A list of namespace names, the outermost namespace at index 0 and the innermost
* at the last index
*/
const QStringList remainingNamespaces() const { return m_remainingNamespaces; }
private:
bool preVisit(CPlusPlus::AST *ast) override;
void postVisit(CPlusPlus::AST *ast) override;
bool visit(CPlusPlus::NamespaceAST *ns) override;
bool visit(CPlusPlus::UsingDirectiveAST *usingNS) override;
void endVisit(CPlusPlus::NamespaceAST *ns) override;
void endVisit(CPlusPlus::TranslationUnitAST *) override;
QString getName(CPlusPlus::NamespaceAST *ns);
CPlusPlus::NamespaceAST *currentNamespace();
const CppRefactoringFile *const m_file;
QStringList m_remainingNamespaces;
const int m_symbolPos;
std::vector<CPlusPlus::NamespaceAST *> m_enteredNamespaces;
// track 'using namespace ...' statements
std::unordered_map<CPlusPlus::NamespaceAST *, QStringList> m_usingsPerNamespace;
bool m_done = false;
};
QStringList getNamespaceNames(const CPlusPlus::Namespace *firstNamespace);
QStringList getNamespaceNames(const CPlusPlus::Symbol *symbol);
} // namespace Internal
} // namespace CppEditor

View File

@@ -6,6 +6,7 @@
#include <projectexplorer/abi.h>
#include <projectexplorer/kitaspects.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projectmanager.h>
#include <projectexplorer/rawprojectpart.h>
#include <projectexplorer/toolchain.h>
@@ -25,6 +26,11 @@ ProjectInfo::ConstPtr ProjectInfo::cloneWithNewSettings(const ConstPtr &pi,
return ConstPtr(new ProjectInfo(pi, settings));
}
ProjectExplorer::Project *ProjectInfo::project() const
{
return ProjectExplorer::ProjectManager::projectWithProjectFilePath(projectFilePath());
}
bool ProjectInfo::operator ==(const ProjectInfo &other) const
{
return m_projectName == other.m_projectName

View File

@@ -36,6 +36,7 @@ public:
const QSet<Utils::FilePath> &sourceFiles() const { return m_sourceFiles; }
QString projectName() const { return m_projectName; }
Utils::FilePath projectFilePath() const { return m_projectFilePath; }
ProjectExplorer::Project *project() const;
Utils::FilePath projectRoot() const { return m_projectFilePath.parentDir(); }
Utils::FilePath buildRoot() const { return m_buildRoot; }
const CppCodeModelSettings &settings() const { return m_settings; }

View File

@@ -4,6 +4,7 @@
#include "projectpart.h"
#include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h>
#include <utils/algorithm.h>
@@ -44,6 +45,11 @@ bool ProjectPart::belongsToProject(const Utils::FilePath &project) const
return topLevelProject == project;
}
Project *ProjectPart::project() const
{
return ProjectManager::projectWithProjectFilePath(topLevelProject);
}
QByteArray ProjectPart::readProjectConfigFile(const QString &projectConfigFile)
{
QByteArray result;

View File

@@ -30,7 +30,6 @@ class CPPEDITOR_EXPORT ProjectPart
public:
using ConstPtr = QSharedPointer<const ProjectPart>;
public:
static ConstPtr create(const Utils::FilePath &topLevelProject,
const ProjectExplorer::RawProjectPart &rpp = {},
const QString &displayName = {},
@@ -49,10 +48,10 @@ public:
bool hasProject() const { return !topLevelProject.isEmpty(); }
bool belongsToProject(const ProjectExplorer::Project *project) const;
bool belongsToProject(const Utils::FilePath &project) const;
ProjectExplorer::Project *project() const;
static QByteArray readProjectConfigFile(const QString &projectConfigFile);
public:
const Utils::FilePath topLevelProject;
const QString displayName;
const QString projectFile;
@@ -117,4 +116,31 @@ public:
const CPlusPlus::LanguageFeatures languageFeatures;
};
class ProjectPartInfo {
public:
enum Hint {
NoHint = 0,
IsFallbackMatch = 1 << 0,
IsAmbiguousMatch = 1 << 1,
IsPreferredMatch = 1 << 2,
IsFromProjectMatch = 1 << 3,
IsFromDependenciesMatch = 1 << 4,
};
Q_DECLARE_FLAGS(Hints, Hint)
ProjectPartInfo() = default;
ProjectPartInfo(const ProjectPart::ConstPtr &projectPart,
const QList<ProjectPart::ConstPtr> &projectParts,
Hints hints)
: projectPart(projectPart)
, projectParts(projectParts)
, hints(hints)
{
}
ProjectPart::ConstPtr projectPart;
QList<ProjectPart::ConstPtr> projectParts; // The one above as first plus alternatives.
Hints hints = NoHint;
};
} // namespace CppEditor

View File

@@ -8,6 +8,7 @@
#include "../cppprojectfile.h"
#include "../cpprefactoringchanges.h"
#include "../indexitem.h"
#include "../insertionpointlocator.h"
#include "cppquickfix.h"
#include "cppquickfixhelpers.h"

View File

@@ -27,12 +27,13 @@
#include "qmt/project_controller/projectcontroller.h"
#include "qmt/project/project.h"
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/icore.h>
#include <cppeditor/cpplocatordata.h>
#include <cppeditor/indexitem.h>
#include <cppeditor/searchsymbols.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
#include <QMenu>