Commit d9affc16 authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Adapt to new diagnostics presentation API in AnalyzerBase.



Task-number: QCE-34
Change-Id: Ia86fa082b3798ba42ec209b0e417e8a8ca0f8fa7
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent 501aad8d
......@@ -21,21 +21,6 @@
namespace ClangStaticAnalyzer {
namespace Internal {
Location::Location()
: line(0), column(0)
{
}
Location::Location(const QString &filePath, int line, int column)
: filePath(filePath), line(line), column(column)
{
}
bool Location::isValid() const
{
return !filePath.isEmpty() && line >= 0 && column >= 0;
}
ExplainingStep::ExplainingStep()
: depth(0)
{
......
......@@ -19,6 +19,8 @@
#ifndef CLANGSTATICANALZYERDIAGNOSTIC_H
#define CLANGSTATICANALZYERDIAGNOSTIC_H
#include <analyzerbase/diagnosticlocation.h>
#include <QList>
#include <QMetaType>
#include <QString>
......@@ -26,19 +28,6 @@
namespace ClangStaticAnalyzer {
namespace Internal {
class Location
{
public:
Location();
Location(const QString &filePath, int line, int column);
bool isValid() const;
QString filePath;
int line;
int column;
};
class ExplainingStep
{
public:
......@@ -48,8 +37,8 @@ public:
QString message;
QString extendedMessage;
Location location;
QList<Location> ranges;
Analyzer::DiagnosticLocation location;
QList<Analyzer::DiagnosticLocation> ranges;
int depth;
};
......@@ -63,7 +52,7 @@ public:
QString type;
QString issueContextKind;
QString issueContext;
Location location;
Analyzer::DiagnosticLocation location;
QList<ExplainingStep> explainingSteps;
};
......
......@@ -18,6 +18,7 @@
#include "clangstaticanalyzerdiagnosticmodel.h"
#include "clangstaticanalyzerdiagnosticview.h"
#include "clangstaticanalyzerprojectsettingsmanager.h"
#include "clangstaticanalyzerutils.h"
......@@ -28,32 +29,53 @@
#include <QCoreApplication>
#include <QFileInfo>
#include <cmath>
namespace ClangStaticAnalyzer {
namespace Internal {
ClangStaticAnalyzerDiagnosticModel::ClangStaticAnalyzerDiagnosticModel(QObject *parent)
: QAbstractListModel(parent)
class DiagnosticItem : public Utils::TreeItem
{
}
public:
DiagnosticItem(const Diagnostic &diag);
void ClangStaticAnalyzerDiagnosticModel::addDiagnostics(const QList<Diagnostic> &diagnostics)
Diagnostic diagnostic() const { return m_diagnostic; }
private:
QVariant data(int column, int role) const override;
const Diagnostic m_diagnostic;
};
class ExplainingStepItem : public Utils::TreeItem
{
beginInsertRows(QModelIndex(), m_diagnostics.size(),
m_diagnostics.size() + diagnostics.size() - 1 );
m_diagnostics += diagnostics;
endInsertRows();
public:
ExplainingStepItem(const ExplainingStep &step);
private:
QVariant data(int column, int role) const override;
const ExplainingStep m_step;
};
ClangStaticAnalyzerDiagnosticModel::ClangStaticAnalyzerDiagnosticModel(QObject *parent)
: Utils::TreeModel(parent)
{
setHeader(QStringList() << tr("Issue") << tr("Location"));
}
void ClangStaticAnalyzerDiagnosticModel::clear()
void ClangStaticAnalyzerDiagnosticModel::addDiagnostics(const QList<Diagnostic> &diagnostics)
{
beginResetModel();
m_diagnostics.clear();
endResetModel();
foreach (const Diagnostic &d, diagnostics)
rootItem()->appendChild(new DiagnosticItem(d));
}
int ClangStaticAnalyzerDiagnosticModel::rowCount(const QModelIndex &parent) const
QList<Diagnostic> ClangStaticAnalyzerDiagnosticModel::diagnostics() const
{
return parent.isValid() ? 0 : m_diagnostics.count();
QList<Diagnostic> diags;
foreach (const Utils::TreeItem * const item, rootItem()->children())
diags << static_cast<const DiagnosticItem *>(item)->diagnostic();
return diags;
}
static QString createDiagnosticToolTipString(const Diagnostic &diagnostic)
......@@ -100,28 +122,162 @@ static QString createDiagnosticToolTipString(const Diagnostic &diagnostic)
return html;
}
QVariant ClangStaticAnalyzerDiagnosticModel::data(const QModelIndex &index, int role) const
static QString createExplainingStepToolTipString(const ExplainingStep &step)
{
if (!index.isValid())
return QVariant();
if (step.message == step.extendedMessage)
return createFullLocationString(step.location);
typedef QPair<QString, QString> StringPair;
QList<StringPair> lines;
if (!step.message.isEmpty()) {
lines << qMakePair(
QCoreApplication::translate("ClangStaticAnalyzer::ExplainingStep", "Message:"),
step.message.toHtmlEscaped());
}
if (!step.extendedMessage.isEmpty()) {
lines << qMakePair(
QCoreApplication::translate("ClangStaticAnalyzer::ExplainingStep", "Extended Message:"),
step.extendedMessage.toHtmlEscaped());
}
lines << qMakePair(
QCoreApplication::translate("ClangStaticAnalyzer::ExplainingStep", "Location:"),
createFullLocationString(step.location));
QString html = QLatin1String("<html>"
"<head>"
"<style>dt { font-weight:bold; } dd { font-family: monospace; }</style>\n"
"<body><dl>");
foreach (const StringPair &pair, lines) {
html += QLatin1String("<dt>");
html += pair.first;
html += QLatin1String("</dt><dd>");
html += pair.second;
html += QLatin1String("</dd>\n");
}
html += QLatin1String("</dl></body></html>");
return html;
}
static QString createLocationString(const Analyzer::DiagnosticLocation &location)
{
const QString filePath = location.filePath;
const QString lineNumber = QString::number(location.line);
const QString fileAndLine = filePath + QLatin1Char(':') + lineNumber;
return QLatin1String("in ") + fileAndLine;
}
static QString createExplainingStepNumberString(int number)
{
const int fieldWidth = 2;
return QString::fromLatin1("%1:").arg(number, fieldWidth);
}
static QString createExplainingStepString(const ExplainingStep &explainingStep, int number)
{
return createExplainingStepNumberString(number)
+ QLatin1Char(' ')
+ explainingStep.extendedMessage
+ QLatin1Char(' ')
+ createLocationString(explainingStep.location);
}
static QString fullText(const Diagnostic &diagnostic)
{
// Summary.
QString text = diagnostic.category + QLatin1String(": ") + diagnostic.type;
if (diagnostic.type != diagnostic.description)
text += QLatin1String(": ") + diagnostic.description;
text += QLatin1Char('\n');
// Explaining steps.
int explainingStepNumber = 1;
foreach (const ExplainingStep &explainingStep, diagnostic.explainingSteps) {
text += createExplainingStepString(explainingStep, explainingStepNumber++)
+ QLatin1Char('\n');
}
text.chop(1); // Trailing newline.
return text;
}
if (index.parent().isValid())
DiagnosticItem::DiagnosticItem(const Diagnostic &diag) : m_diagnostic(diag)
{
// Don't show explaining steps if they add no information.
if (diag.explainingSteps.count() == 1) {
const ExplainingStep &step = diag.explainingSteps.first();
if (step.message == diag.description && step.location == diag.location)
return;
}
foreach (const ExplainingStep &s, diag.explainingSteps)
appendChild(new ExplainingStepItem(s));
}
QVariant locationData(int role, const Analyzer::DiagnosticLocation &location)
{
switch (role) {
case Analyzer::DetailedErrorView::LocationRole:
return QVariant::fromValue(location);
case Qt::ToolTipRole:
return location.filePath.isEmpty() ? QVariant() : QVariant(location.filePath);
default:
return QVariant();
}
}
QVariant DiagnosticItem::data(int column, int role) const
{
if (column == Analyzer::DetailedErrorView::LocationColumn)
return locationData(role, m_diagnostic.location);
const int row = index.row();
if (row < 0 || row >= m_diagnostics.size())
// DiagnosticColumn
switch (role) {
case Analyzer::DetailedErrorView::FullTextRole:
return fullText(m_diagnostic);
case ClangStaticAnalyzerDiagnosticModel::DiagnosticRole:
return QVariant::fromValue(m_diagnostic);
case Qt::DisplayRole:
return m_diagnostic.description;
case Qt::ToolTipRole:
return createDiagnosticToolTipString(m_diagnostic);
default:
return QVariant();
}
}
const Diagnostic diagnostic = m_diagnostics.at(row);
ExplainingStepItem::ExplainingStepItem(const ExplainingStep &step) : m_step(step)
{
}
if (role == Qt::DisplayRole)
return QString(QLatin1String("Some specific diagnostic")); // TODO: Remove?
else if (role == Qt::ToolTipRole)
return createDiagnosticToolTipString(diagnostic);
else if (role == Qt::UserRole)
return QVariant::fromValue<Diagnostic>(diagnostic);
QVariant ExplainingStepItem::data(int column, int role) const
{
if (column == Analyzer::DetailedErrorView::LocationColumn)
return locationData(role, m_step.location);
return QVariant();
// DiagnosticColumn
switch (role) {
case Analyzer::DetailedErrorView::FullTextRole:
return fullText(static_cast<DiagnosticItem *>(parent())->diagnostic());
case ClangStaticAnalyzerDiagnosticModel::DiagnosticRole:
return QVariant::fromValue(static_cast<DiagnosticItem *>(parent())->diagnostic());
case Qt::DisplayRole: {
const int row = parent()->children().indexOf(const_cast<ExplainingStepItem *>(this)) + 1;
const int padding = static_cast<int>(std::log10(parent()->rowCount()))
- static_cast<int>(std::log10(row));
return QString::fromLatin1("%1%2: %3")
.arg(QString(padding, QLatin1Char(' ')))
.arg(row)
.arg(m_step.message);
}
case Qt::ToolTipRole:
return createExplainingStepToolTipString(m_step);
default:
return QVariant();
}
}
......@@ -165,7 +321,8 @@ void ClangStaticAnalyzerDiagnosticFilterModel::addSuppressedDiagnostic(
bool ClangStaticAnalyzerDiagnosticFilterModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
{
Q_UNUSED(sourceParent);
if (sourceParent.isValid())
return true;
const Diagnostic diag = static_cast<ClangStaticAnalyzerDiagnosticModel *>(sourceModel())
->diagnostics().at(sourceRow);
foreach (const SuppressedDiagnostic &d, m_suppressedDiagnostics) {
......
......@@ -19,12 +19,13 @@
#ifndef CLANGSTATICANALYZERDIAGNOSTICMODEL_H
#define CLANGSTATICANALYZERDIAGNOSTICMODEL_H
#include "clangstaticanalyzerlogfilereader.h"
#include "clangstaticanalyzerdiagnostic.h"
#include "clangstaticanalyzerprojectsettings.h"
#include <analyzerbase/detailederrorview.h>
#include <utils/fileutils.h>
#include <utils/treemodel.h>
#include <QAbstractListModel>
#include <QPointer>
#include <QSortFilterProxyModel>
......@@ -33,7 +34,7 @@ namespace ProjectExplorer { class Project; }
namespace ClangStaticAnalyzer {
namespace Internal {
class ClangStaticAnalyzerDiagnosticModel : public QAbstractListModel
class ClangStaticAnalyzerDiagnosticModel : public Utils::TreeModel
{
Q_OBJECT
......@@ -41,15 +42,11 @@ public:
ClangStaticAnalyzerDiagnosticModel(QObject *parent = 0);
void addDiagnostics(const QList<Diagnostic> &diagnostics);
QList<Diagnostic> diagnostics() const { return m_diagnostics; }
void clear();
QList<Diagnostic> diagnostics() const;
// QAbstractListModel interface
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
private:
QList<Diagnostic> m_diagnostics;
enum ItemRole {
DiagnosticRole = Analyzer::DetailedErrorView::FullTextRole + 1
};
};
class ClangStaticAnalyzerDiagnosticFilterModel : public QSortFilterProxyModel
......
......@@ -18,7 +18,6 @@
#include "clangstaticanalyzerdiagnosticview.h"
#include "clangstaticanalyzerlogfilereader.h"
#include "clangstaticanalyzerdiagnosticmodel.h"
#include "clangstaticanalyzerprojectsettings.h"
#include "clangstaticanalyzerprojectsettingsmanager.h"
......@@ -28,266 +27,27 @@
#include <utils/qtcassert.h>
#include <QAction>
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QLabel>
#include <QVBoxLayout>
using namespace Analyzer;
namespace {
QLabel *createCommonLabel()
{
QLabel *label = new QLabel;
label->setWordWrap(true);
label->setContentsMargins(0, 0, 0, 0);
label->setMargin(0);
label->setIndent(10);
return label;
}
QString createSummaryText(const ClangStaticAnalyzer::Internal::Diagnostic &diagnostic,
const QPalette &palette)
{
const QColor color = palette.color(QPalette::Text);
const QString linkStyle = QString::fromLatin1("style=\"color:rgba(%1, %2, %3, %4);\"")
.arg(color.red())
.arg(color.green())
.arg(color.blue())
.arg(int(0.7 * 255));
const QString fileName = QFileInfo(diagnostic.location.filePath).fileName();
const QString location = fileName + QLatin1Char(' ')
+ QString::number(diagnostic.location.line);
return QString::fromLatin1("%1&nbsp;&nbsp;<span %3>%2</span>")
.arg(diagnostic.description.toHtmlEscaped(),
location,
linkStyle);
}
QLabel *createSummaryLabel(const ClangStaticAnalyzer::Internal::Diagnostic &diagnostic)
{
QLabel *label = createCommonLabel();
QPalette palette = label->palette();
palette.setBrush(QPalette::Text, palette.highlightedText());
label->setPalette(palette);
label->setText(createSummaryText(diagnostic, palette));
return label;
}
QLabel *createExplainingStepLabel(const QFont &font, bool useAlternateRowPalette)
{
QLabel *label = createCommonLabel();
// Font
QFont fixedPitchFont = font;
fixedPitchFont.setFixedPitch(true);
label->setFont(fixedPitchFont);
// Background
label->setAutoFillBackground(true);
if (useAlternateRowPalette) {
QPalette p = label->palette();
p.setBrush(QPalette::Base, p.alternateBase());
label->setPalette(p);
}
return label;
}
QString createLocationString(const ClangStaticAnalyzer::Internal::Location &location,
bool withMarkup, bool withAbsolutePath)
{
const QString filePath = location.filePath;
const QString lineNumber = QString::number(location.line);
const QString columnNumber = QString::number(location.column - 1);
const QString fileAndLine = (withAbsolutePath ? filePath : QFileInfo(filePath).fileName())
+ QLatin1Char(':') + lineNumber;
if (withMarkup) {
return QLatin1String("in <a href=\"file://")
+ filePath + QLatin1Char(':') + lineNumber + QLatin1Char(':') + columnNumber
+ QLatin1String("\">")
+ fileAndLine
+ QLatin1String("</a>");
} else {
return QLatin1String("in ") + fileAndLine;
}
}
QString createExplainingStepNumberString(int number, bool withMarkup)
{
const int fieldWidth = 2;
const QString result = QString::fromLatin1("%1:").arg(number, fieldWidth);
return withMarkup
? QLatin1String("<code style='white-space:pre'>") + result + QLatin1String("</code>")
: result;
}
QString createExplainingStepToolTipString(const ClangStaticAnalyzer::Internal::ExplainingStep &step)
{
if (step.message == step.extendedMessage)
return createFullLocationString(step.location);
typedef QPair<QString, QString> StringPair;
QList<StringPair> lines;
if (!step.message.isEmpty()) {
lines << qMakePair(
QCoreApplication::translate("ClangStaticAnalyzer::ExplainingStep", "Message:"),
step.message.toHtmlEscaped());
}
if (!step.extendedMessage.isEmpty()) {
lines << qMakePair(
QCoreApplication::translate("ClangStaticAnalyzer::ExplainingStep", "Extended Message:"),
step.extendedMessage.toHtmlEscaped());
}
lines << qMakePair(
QCoreApplication::translate("ClangStaticAnalyzer::ExplainingStep", "Location:"),
createFullLocationString(step.location));
QString html = QLatin1String("<html>"
"<head>"
"<style>dt { font-weight:bold; } dd { font-family: monospace; }</style>\n"
"<body><dl>");
foreach (const StringPair &pair, lines) {
html += QLatin1String("<dt>");
html += pair.first;
html += QLatin1String("</dt><dd>");
html += pair.second;
html += QLatin1String("</dd>\n");
}
html += QLatin1String("</dl></body></html>");
return html;
}
QString createExplainingStepString(
const ClangStaticAnalyzer::Internal::ExplainingStep &explainingStep,
int number, bool withMarkup, bool withAbsolutePath)
{
return createExplainingStepNumberString(number, withMarkup)
+ QLatin1Char(' ')
+ (withMarkup
? explainingStep.extendedMessage.toHtmlEscaped()
: explainingStep.extendedMessage)
+ QLatin1Char(' ')
+ createLocationString(explainingStep.location, withMarkup, withAbsolutePath);
}
} // anonymous namespace
namespace ClangStaticAnalyzer {
namespace Internal {
ClangStaticAnalyzerDiagnosticDelegate::ClangStaticAnalyzerDiagnosticDelegate(QListView *parent)
: DetailedErrorDelegate(parent)
{
}
DetailedErrorDelegate::SummaryLineInfo ClangStaticAnalyzerDiagnosticDelegate::summaryInfo(
const QModelIndex &index) const
{
const Diagnostic diagnostic = index.data(Qt::UserRole).value<Diagnostic>();
QTC_ASSERT(diagnostic.isValid(), return SummaryLineInfo());
DetailedErrorDelegate::SummaryLineInfo info;
info.errorText = diagnostic.description;
info.errorLocation = createLocationString(diagnostic.location,
/*withMarkup=*/ false,
/*withAbsolutePath=*/ false);
return info;
}
Diagnostic ClangStaticAnalyzerDiagnosticDelegate::getDiagnostic(const QModelIndex &index) const
{
return index.data(Qt::UserRole).value<Diagnostic>();
}
QWidget *ClangStaticAnalyzerDiagnosticDelegate::createDetailsWidget(const QFont &font,
const QModelIndex &index,
QWidget *parent) const
{
QWidget *widget = new QWidget(parent);
const Diagnostic diagnostic = getDiagnostic(index);
if (!diagnostic.isValid())
return widget;
QVBoxLayout *layout = new QVBoxLayout;
// Add summary label
QLabel *summaryLineLabel = createSummaryLabel(diagnostic);