Debugger: filepathify DiagnosticLocation

Change-Id: Ibbbf137231b313ec10e3d57c0230217b0c1e0a6c
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
David Schulz
2021-05-28 12:37:35 +02:00
parent 27f8e2dbce
commit 0cfe27a53d
19 changed files with 78 additions and 70 deletions

View File

@@ -1277,14 +1277,14 @@ FilePath FilePath::onDevice(const FilePath &deviceTemplate) const
return res;
}
FilePath FilePath::pathAppended(const QString &str) const
FilePath FilePath::pathAppended(const QString &path) const
{
FilePath fn = *this;
if (str.isEmpty())
if (path.isEmpty())
return fn;
if (!fn.m_data.isEmpty() && !fn.m_data.endsWith(QLatin1Char('/')))
fn.m_data.append('/');
fn.m_data.append(str);
fn.m_data.append(path);
return fn;
}

View File

@@ -209,7 +209,7 @@ public:
ApplyFixIts(const QVector<DiagnosticItem *> &diagnosticItems)
{
for (DiagnosticItem *diagnosticItem : diagnosticItems) {
const QString &filePath = diagnosticItem->diagnostic().location.filePath;
const Utils::FilePath &filePath = diagnosticItem->diagnostic().location.filePath;
QTC_ASSERT(!filePath.isEmpty(), continue);
// Get or create refactoring file
@@ -245,14 +245,14 @@ public:
const Debugger::DiagnosticLocation start = step.ranges.first();
const Debugger::DiagnosticLocation end = step.ranges.last();
const int startPos = file.position(start.filePath, start.line, start.column);
const int endPos = file.position(start.filePath, end.line, end.column);
const int startPos = file.position(start.filePath.toString(), start.line, start.column);
const int endPos = file.position(start.filePath.toString(), end.line, end.column);
auto op = new ReplacementOperation;
op->pos = startPos;
op->length = endPos - startPos;
op->text = step.message;
op->fileName = start.filePath;
op->fileName = start.filePath.toString();
op->apply = apply;
replacements += op;
@@ -322,7 +322,7 @@ public:
}
private:
QMap<QString, RefactoringFileInfo> m_refactoringFileInfos;
QMap<Utils::FilePath, RefactoringFileInfo> m_refactoringFileInfos;
};
static FileInfos sortedFileInfos(const QVector<CppTools::ProjectPart::Ptr> &projectParts)
@@ -1119,7 +1119,7 @@ QSet<Diagnostic> ClangTool::diagnostics() const
{
return Utils::filtered(m_diagnosticModel->diagnostics(), [](const Diagnostic &diagnostic) {
using CppTools::ProjectFile;
return ProjectFile::isSource(ProjectFile::classify(diagnostic.location.filePath));
return ProjectFile::isSource(ProjectFile::classify(diagnostic.location.filePath.toString()));
});
}

View File

@@ -47,7 +47,7 @@ static Q_LOGGING_CATEGORY(LOG, "qtc.clangtools.model", QtWarningMsg)
namespace ClangTools {
namespace Internal {
FilePathItem::FilePathItem(const QString &filePath)
FilePathItem::FilePathItem(const Utils::FilePath &filePath)
: m_filePath(filePath)
{}
@@ -56,11 +56,11 @@ QVariant FilePathItem::data(int column, int role) const
if (column == DiagnosticView::DiagnosticColumn) {
switch (role) {
case Qt::DisplayRole:
return m_filePath;
return m_filePath.toUserOutput();
case Qt::DecorationRole:
return Core::FileIconProvider::icon(QFileInfo(m_filePath));
return Core::FileIconProvider::icon(m_filePath.toFileInfo());
case Debugger::DetailedErrorView::FullTextRole:
return m_filePath;
return m_filePath.toUserOutput();
default:
return QVariant();
}
@@ -119,12 +119,12 @@ void ClangToolsDiagnosticModel::addDiagnostics(const Diagnostics &diagnostics, b
}
// Create file path item if necessary
const QString filePath = d.location.filePath;
const Utils::FilePath &filePath = d.location.filePath;
FilePathItem *&filePathItem = m_filePathToItem[filePath];
if (!filePathItem) {
filePathItem = new FilePathItem(filePath);
rootItem()->appendChild(filePathItem);
addWatchedPath(d.location.filePath);
addWatchedPath(filePath.toString());
}
// Add to file path item
@@ -184,7 +184,7 @@ void ClangToolsDiagnosticModel::clearAndSetupCache()
void ClangToolsDiagnosticModel::onFileChanged(const QString &path)
{
forItemsAtLevel<2>([&](DiagnosticItem *item){
if (item->diagnostic().location.filePath == path)
if (item->diagnostic().location.filePath == Utils::FilePath::fromString(path))
item->setFixItStatus(FixitStatus::Invalidated);
});
removeWatchedPath(path);
@@ -238,7 +238,7 @@ static QString createExplainingStepToolTipString(const ExplainingStep &step)
static QString createLocationString(const Debugger::DiagnosticLocation &location)
{
const QString filePath = location.filePath;
const QString filePath = location.filePath.toUserOutput();
const QString lineNumber = QString::number(location.line);
const QString fileAndLine = filePath + QLatin1Char(':') + lineNumber;
return QLatin1String("in ") + fileAndLine;
@@ -262,7 +262,7 @@ static QString createExplainingStepString(const ExplainingStep &explainingStep,
static QString fullText(const Diagnostic &diagnostic)
{
QString text = diagnostic.location.filePath + QLatin1Char(':');
QString text = diagnostic.location.filePath.toUserOutput() + QLatin1Char(':');
text += lineColumnString(diagnostic.location) + QLatin1String(": ");
if (!diagnostic.category.isEmpty())
text += diagnostic.category + QLatin1String(": ");
@@ -452,7 +452,9 @@ QVariant ExplainingStepItem::data(int column, int role) const
return QVariant::fromValue(m_step.location);
case Debugger::DetailedErrorView::FullTextRole: {
return QString("%1:%2: %3")
.arg(m_step.location.filePath, lineColumnString(m_step.location), m_step.message);
.arg(m_step.location.filePath.toUserOutput(),
lineColumnString(m_step.location),
m_step.message);
}
case ClangToolsDiagnosticModel::TextRole:
return m_step.message;
@@ -461,11 +463,12 @@ QVariant ExplainingStepItem::data(int column, int role) const
case ClangToolsDiagnosticModel::DocumentationUrlRole:
return parent()->data(column, role);
case Qt::DisplayRole: {
const QString mainFilePath = static_cast<DiagnosticItem *>(parent())->diagnostic().location.filePath;
const Utils::FilePath mainFilePath
= static_cast<DiagnosticItem *>(parent())->diagnostic().location.filePath;
const QString locationString
= m_step.location.filePath == mainFilePath
? lineColumnString(m_step.location)
: QString("%1:%2").arg(QFileInfo(m_step.location.filePath).fileName(),
: QString("%1:%2").arg(m_step.location.filePath.fileName(),
lineColumnString(m_step.location));
if (m_step.isFixIt) {
@@ -645,10 +648,9 @@ bool DiagnosticFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &s
foreach (const SuppressedDiagnostic &d, m_suppressedDiagnostics) {
if (d.description != diag.description)
continue;
QString filePath = d.filePath.toString();
QFileInfo fi(filePath);
if (fi.isRelative())
filePath = m_lastProjectDirectory.toString() + QLatin1Char('/') + filePath;
Utils::FilePath filePath = d.filePath;
if (d.filePath.toFileInfo().isRelative())
filePath = m_lastProjectDirectory.pathAppended(filePath.toString());
if (filePath == diag.location.filePath) {
diagnosticItem->setTextMarkVisible(false);
return false;

View File

@@ -54,11 +54,11 @@ class ClangToolsDiagnosticModel;
class FilePathItem : public Utils::TreeItem
{
public:
FilePathItem(const QString &filePath);
FilePathItem(const Utils::FilePath &filePath);
QVariant data(int column, int role) const override;
private:
const QString m_filePath;
const Utils::FilePath m_filePath;
};
class DiagnosticMark;
@@ -139,7 +139,7 @@ private:
void clearAndSetupCache();
private:
QHash<QString, FilePathItem *> m_filePathToItem;
QHash<Utils::FilePath, FilePathItem *> m_filePathToItem;
QSet<Diagnostic> m_diagnostics;
std::map<QVector<ExplainingStep>, QVector<DiagnosticItem *>> stepsToItemsCache;
std::unique_ptr<QFileSystemWatcher> m_filesWatcher;

View File

@@ -222,7 +222,7 @@ void DiagnosticView::suppressCurrentDiagnostic()
diags << diag;
continue;
}
Utils::FilePath filePath = Utils::FilePath::fromString(diag.location.filePath);
Utils::FilePath filePath = diag.location.filePath;
const Utils::FilePath relativeFilePath
= filePath.relativeChildPath(project->projectDirectory());
if (!relativeFilePath.isEmpty())
@@ -402,7 +402,7 @@ void DiagnosticView::openEditorForCurrentIndex()
const QVariant v = model()->data(currentIndex(), Debugger::DetailedErrorView::LocationRole);
const auto loc = v.value<Debugger::DiagnosticLocation>();
if (loc.isValid())
Core::EditorManager::openEditorAt(loc.filePath, loc.line, loc.column - 1);
Core::EditorManager::openEditorAt(Utils::Link(loc.filePath, loc.line, loc.column - 1));
}
} // namespace Internal

View File

@@ -155,16 +155,16 @@ public:
int extraOffset = 0)
: m_node(node)
, m_fileCache(fileCache)
, m_filePath(QDir::cleanPath(asString(node["FilePath"])))
, m_filePath(Utils::FilePath::fromUserInput(asString(node["FilePath"])))
, m_fileOffsetKey(fileOffsetKey)
, m_extraOffset(extraOffset)
{}
QString filePath() const { return m_filePath; }
Utils::FilePath filePath() const { return m_filePath; }
Debugger::DiagnosticLocation toDiagnosticLocation() const
{
FileCache::Item &cacheItem = m_fileCache.item(m_filePath);
FileCache::Item &cacheItem = m_fileCache.item(m_filePath.toString());
const QByteArray fileContents = cacheItem.fileContents();
const char *data = fileContents.data();
@@ -205,7 +205,7 @@ public:
private:
const YAML::Node &m_node;
FileCache &m_fileCache;
QString m_filePath;
Utils::FilePath m_filePath;
const char *m_fileOffsetKey = nullptr;
int m_extraOffset = 0;
};
@@ -232,10 +232,8 @@ Diagnostics readExportedDiagnostics(const Utils::FilePath &logFilePath,
Location loc(node, fileCache);
if (loc.filePath().isEmpty())
continue;
if (acceptFromFilePath
&& !acceptFromFilePath(Utils::FilePath::fromString(loc.filePath()))) {
if (acceptFromFilePath && !acceptFromFilePath(loc.filePath()))
continue;
}
Diagnostic diag;
diag.location = loc.toDiagnosticLocation();

View File

@@ -232,7 +232,7 @@ ClangToolsProjectSettings::ClangToolsProjectSettingsPtr
}
SuppressedDiagnostic::SuppressedDiagnostic(const Diagnostic &diag)
: filePath(Utils::FilePath::fromString(diag.location.filePath))
: filePath(diag.location.filePath)
, description(diag.description)
, uniquifier(diag.explainingSteps.count())
{

View File

@@ -115,7 +115,7 @@ QString createDiagnosticToolTipString(
if (!steps.second.isEmpty())
steps.second += "<br>";
steps.second += QString("%1:%2: %3")
.arg(step.location.filePath,
.arg(step.location.filePath.toUserOutput(),
lineColumnString(step.location),
step.message);
}
@@ -147,7 +147,7 @@ QString createDiagnosticToolTipString(
QString createFullLocationString(const Debugger::DiagnosticLocation &location)
{
return location.filePath + QLatin1Char(':') + QString::number(location.line)
return location.filePath.toUserOutput() + QLatin1Char(':') + QString::number(location.line)
+ QLatin1Char(':') + QString::number(location.column);
}

View File

@@ -1162,8 +1162,8 @@ void disableChecks(const QList<Diagnostic> &diagnostics)
Utils::Id activeConfigId = settings->runSettings().diagnosticConfigId();
ClangToolsProjectSettings::ClangToolsProjectSettingsPtr projectSettings;
if (ProjectExplorer::Project *project = ProjectExplorer::SessionManager
::projectForFile(Utils::FilePath::fromString(diagnostics.first().location.filePath))) {
if (ProjectExplorer::Project *project = ProjectExplorer::SessionManager::projectForFile(
diagnostics.first().location.filePath)) {
projectSettings = ClangToolsProjectSettings::getSettings(project);
if (!projectSettings->useGlobalSettings())
activeConfigId = projectSettings->runSettings().diagnosticConfigId();

View File

@@ -39,7 +39,7 @@ namespace ClangTools {
namespace Internal {
DiagnosticMark::DiagnosticMark(const Diagnostic &diagnostic)
: TextEditor::TextMark(Utils::FilePath::fromString(diagnostic.location.filePath),
: TextEditor::TextMark(diagnostic.location.filePath,
diagnostic.location.line,
Utils::Id(Constants::DIAGNOSTIC_MARK_ID))
, m_diagnostic(diagnostic)

View File

@@ -264,7 +264,7 @@ void DocumentClangToolRunner::runNext()
static void updateLocation(Debugger::DiagnosticLocation &location)
{
location.filePath = vfso().originalFilePath(Utils::FilePath::fromString(location.filePath)).toString();
location.filePath = vfso().originalFilePath(location.filePath);
}
void DocumentClangToolRunner::onSuccess()
@@ -364,10 +364,9 @@ bool DocumentClangToolRunner::isSuppressed(const Diagnostic &diagnostic) const
auto equalsSuppressed = [this, &diagnostic](const SuppressedDiagnostic &suppressed) {
if (suppressed.description != diagnostic.description)
return false;
QString filePath = suppressed.filePath.toString();
QFileInfo fi(filePath);
if (fi.isRelative())
filePath = m_lastProjectDirectory.toString() + QLatin1Char('/') + filePath;
Utils::FilePath filePath = suppressed.filePath;
if (filePath.toFileInfo().isRelative())
filePath = m_lastProjectDirectory.pathAppended(filePath.toString());
return filePath == diagnostic.location.filePath;
};
return Utils::anyOf(m_suppressed, equalsSuppressed);

View File

@@ -68,9 +68,10 @@ void ClangToolQuickFixOperation::perform()
for (const ExplainingStep &step : m_diagnostic.explainingSteps) {
if (!step.isFixIt)
continue;
TextEditor::RefactoringFilePtr &refactoringFile = refactoringFiles[step.location.filePath];
TextEditor::RefactoringFilePtr &refactoringFile
= refactoringFiles[step.location.filePath.toString()];
if (refactoringFile.isNull())
refactoringFile = changes.file(Utils::FilePath::fromString(step.location.filePath));
refactoringFile = changes.file(step.location.filePath);
Utils::ChangeSet changeSet = refactoringFile->changeSet();
Range range = toRange(refactoringFile->document(), {step.ranges.first(), step.ranges.last()});
changeSet.replace(range, step.message);

View File

@@ -79,7 +79,7 @@ QVariant DiagnosticItem::data(int column, int role) const
if (column == DiagnosticsModel::DiagnosticColumn) {
switch (role) {
case DetailedErrorView::LocationRole: {
const auto location = DiagnosticLocation(m_diagnostic.fileName.toString(),
const auto location = DiagnosticLocation(m_diagnostic.fileName,
m_diagnostic.lineNumber,
0);
return QVariant::fromValue(location);
@@ -92,7 +92,7 @@ QVariant DiagnosticItem::data(int column, int role) const
return getIcon(m_diagnostic.severity);
case Debugger::DetailedErrorView::FullTextRole:
return QString("%1:%2: %3")
.arg(m_diagnostic.fileName.toString())
.arg(m_diagnostic.fileName.toUserOutput())
.arg(m_diagnostic.lineNumber)
.arg(m_diagnostic.message);
default:

View File

@@ -103,7 +103,7 @@ void DiagnosticView::openEditorForCurrentIndex()
const QVariant v = model()->data(currentIndex(), Debugger::DetailedErrorView::LocationRole);
const auto loc = v.value<Debugger::DiagnosticLocation>();
if (loc.isValid())
Core::EditorManager::openEditorAt(loc.filePath, loc.line, loc.column - 1);
Core::EditorManager::openEditorAt(Utils::Link(loc.filePath, loc.line, loc.column - 1));
}
void DiagnosticView::mouseDoubleClickEvent(QMouseEvent *event)

View File

@@ -66,7 +66,7 @@ DetailedErrorView::DetailedErrorView(QWidget *parent) :
const auto loc = index.model()->data(index, DetailedErrorView::LocationRole)
.value<DiagnosticLocation>();
if (loc.isValid())
Core::EditorManager::openEditorAt(loc.filePath, loc.line, loc.column - 1);
Core::EditorManager::openEditorAt(Utils::Link(loc.filePath, loc.line, loc.column - 1));
}
});
@@ -123,12 +123,12 @@ QVariant DetailedErrorView::locationData(int role, const DiagnosticLocation &loc
return QVariant::fromValue(location);
case Qt::DisplayRole:
return location.isValid() ? QString::fromLatin1("%1:%2:%3")
.arg(QFileInfo(location.filePath).fileName())
.arg(location.filePath.fileName())
.arg(location.line)
.arg(location.column)
: QString();
case Qt::ToolTipRole:
return location.filePath.isEmpty() ? QVariant() : QVariant(location.filePath);
return location.filePath.isEmpty() ? QVariant() : QVariant(location.filePath.toUserOutput());
case Qt::FontRole: {
QFont font = QApplication::font();
font.setUnderline(true);

View File

@@ -29,7 +29,7 @@ namespace Debugger {
DiagnosticLocation::DiagnosticLocation() = default;
DiagnosticLocation::DiagnosticLocation(const QString &filePath, int line, int column)
DiagnosticLocation::DiagnosticLocation(const Utils::FilePath &filePath, int line, int column)
: filePath(filePath), line(line), column(column)
{
}

View File

@@ -27,6 +27,8 @@
#include <debugger/debugger_global.h>
#include <utils/fileutils.h>
#include <QDebug>
#include <QMetaType>
#include <QString>
@@ -37,11 +39,11 @@ class DEBUGGER_EXPORT DiagnosticLocation
{
public:
DiagnosticLocation();
DiagnosticLocation(const QString &filePath, int line, int column);
DiagnosticLocation(const Utils::FilePath &filePath, int line, int column);
bool isValid() const;
QString filePath;
Utils::FilePath filePath;
// Both values start at 1.
int line = 0;

View File

@@ -175,7 +175,9 @@ ErrorItem::ErrorItem(const ErrorListModel *model, const Error &error)
static QVariant locationData(int role, const Frame &frame)
{
const Debugger::DiagnosticLocation location(frame.filePath(), frame.line(), 0);
const Debugger::DiagnosticLocation location(Utils::FilePath::fromString(frame.filePath()),
frame.line(),
0);
return Debugger::DetailedErrorView::locationData(role, location);
}

View File

@@ -112,16 +112,18 @@ static QString appendYamlSuffix(const char *filePathFragment)
TEST_F(ReadExportedDiagnostics, Tidy)
{
const QString sourceFile = TESTDATA "tidy.modernize-use-nullptr.cpp";
const Utils::FilePath sourceFile = Utils::FilePath::fromString(
TESTDATA "tidy.modernize-use-nullptr.cpp");
const QString exportedFile = createFile(appendYamlSuffix(TESTDATA "tidy.modernize-use-nullptr"),
sourceFile);
sourceFile.toString());
Diagnostic expectedDiag;
expectedDiag.name = "modernize-use-nullptr";
expectedDiag.location = {sourceFile, 2, 25};
expectedDiag.description = "use nullptr [modernize-use-nullptr]";
expectedDiag.type = "warning";
expectedDiag.hasFixits = true;
expectedDiag.explainingSteps = {ExplainingStep{"nullptr",
expectedDiag.explainingSteps = {
ExplainingStep{"nullptr",
expectedDiag.location,
{expectedDiag.location, {sourceFile, 2, 26}},
true}};
@@ -151,9 +153,10 @@ TEST_F(ReadExportedDiagnostics, AcceptDiagsFromFilePaths_None)
// Diagnostics from clang (static) analyzer passed through via clang-tidy
TEST_F(ReadExportedDiagnostics, Tidy_ClangAnalyzer)
{
const QString sourceFile = TESTDATA "clang-analyzer.dividezero.cpp";
const Utils::FilePath sourceFile = Utils::FilePath::fromString(TESTDATA
"clang-analyzer.dividezero.cpp");
const QString exportedFile = createFile(appendYamlSuffix(TESTDATA "clang-analyzer.dividezero"),
sourceFile);
sourceFile.toString());
Diagnostic expectedDiag;
expectedDiag.name = "clang-analyzer-core.DivideZero";
expectedDiag.location = {sourceFile, 4, 15};
@@ -188,8 +191,9 @@ TEST_F(ReadExportedDiagnostics, Tidy_ClangAnalyzer)
TEST_F(ReadExportedDiagnostics, Clazy)
{
const QString sourceFile = TESTDATA "clazy.qgetenv.cpp";
const QString exportedFile = createFile(appendYamlSuffix(TESTDATA "clazy.qgetenv"), sourceFile);
const Utils::FilePath sourceFile = Utils::FilePath::fromString(TESTDATA "clazy.qgetenv.cpp");
const QString exportedFile = createFile(appendYamlSuffix(TESTDATA "clazy.qgetenv"),
sourceFile.toString());
Diagnostic expectedDiag;
expectedDiag.name = "clazy-qgetenv";
expectedDiag.location = {sourceFile, 7, 5};