Skip to content

Commit 7cb65b7

Browse files
committed
GUI: Speedup code editor when selecting another warning in the same file
1 parent 46d997c commit 7cb65b7

6 files changed

Lines changed: 67 additions & 15 deletions

File tree

gui/codeeditor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,19 @@ void CodeEditor::setError(const QString &code, int errorLine, const QStringList
257257
highlightErrorLine();
258258
}
259259

260+
void CodeEditor::setError(int errorLine, const QStringList &symbols)
261+
{
262+
mHighlighter->setSymbols(symbols);
263+
264+
mErrorPosition = getPos(toPlainText(), errorLine);
265+
QTextCursor tc = textCursor();
266+
tc.setPosition(mErrorPosition);
267+
setTextCursor(tc);
268+
centerCursor();
269+
270+
highlightErrorLine();
271+
}
272+
260273
int CodeEditor::lineNumberAreaWidth()
261274
{
262275
int digits = 1;

gui/codeeditor.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,29 @@ class CodeEditor : public QPlainTextEdit {
7777
*/
7878
void setError(const QString &code, int errorLine, const QStringList &symbols);
7979

80+
/**
81+
* Goto another error in existing source file
82+
* \param errorLine line number
83+
* \param symbols the related symbols, these are marked
84+
*/
85+
void setError(int errorLine, const QStringList &symbols);
86+
87+
void setFileName(const QString &fileName)
88+
{
89+
mFileName = fileName;
90+
}
91+
92+
QString getFileName() const
93+
{
94+
return mFileName;
95+
}
96+
97+
void clear()
98+
{
99+
mFileName.clear();
100+
setPlainText(QString());
101+
}
102+
80103
protected:
81104
void resizeEvent(QResizeEvent *event) override;
82105

@@ -93,6 +116,7 @@ private slots:
93116
Highlighter *mHighlighter;
94117
CodeEditorStyle *mWidgetStyle;
95118
int mErrorPosition;
119+
QString mFileName;
96120
};
97121

98122

gui/resultstree.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static const char LINE[] = "line";
6363
static const char MESSAGE[] = "message";
6464
static const char SEVERITY[] = "severity";
6565
static const char SINCEDATE[] = "sinceDate";
66+
static const char SYMBOLNAMES[] = "symbolNames";
6667
static const char SUMMARY[] = "summary";
6768
static const char TAGS[] = "tags";
6869

@@ -223,6 +224,7 @@ bool ResultsTree::addErrorItem(const ErrorItem &item)
223224
data[FILE0] = stripPath(item.file0, true);
224225
data[FUNCTION] = item.function;
225226
data[SINCEDATE] = item.sinceDate;
227+
data[SYMBOLNAMES] = item.symbolNames;
226228
data[TAGS] = line.tags;
227229
data[HIDE] = hide;
228230
stditem->setData(QVariant(data));
@@ -256,6 +258,7 @@ bool ResultsTree::addErrorItem(const ErrorItem &item)
256258
child_data[CWE] = line.cwe;
257259
child_data[CPPCHECKID] = line.cppcheckId;
258260
child_data[INCONCLUSIVE] = line.inconclusive;
261+
child_data[SYMBOLNAMES] = item.symbolNames;
259262
child_item->setData(QVariant(child_data));
260263
}
261264
}

gui/resultsview.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ void ResultsView::updateDetails(const QModelIndex &index)
385385
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(mUI.mTree->model());
386386
QStandardItem *item = model->itemFromIndex(index);
387387

388-
mUI.mCode->setPlainText(QString());
389-
390388
if (!item) {
389+
mUI.mCode->clear();
391390
mUI.mDetails->setText(QString());
392391
return;
393392
}
@@ -400,6 +399,7 @@ void ResultsView::updateDetails(const QModelIndex &index)
400399

401400
// If there is no severity data then it is a parent item without summary and message
402401
if (!data.contains("severity")) {
402+
mUI.mCode->clear();
403403
mUI.mDetails->setText(QString());
404404
return;
405405
}
@@ -425,19 +425,25 @@ void ResultsView::updateDetails(const QModelIndex &index)
425425
if (!QFileInfo(filepath).exists() && QFileInfo(mUI.mTree->getCheckDirectory() + '/' + filepath).exists())
426426
filepath = mUI.mTree->getCheckDirectory() + '/' + filepath;
427427

428-
QFile file(filepath);
429-
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
430-
QStringList symbols;
431-
QRegularExpression re(".*: ([A-Za-z_][A-Za-z0-9_]*)$");
432-
const QString errorMessage = data["message"].toString();
433-
QRegularExpressionMatch match = re.match(errorMessage);
434-
if (match.hasMatch()) {
435-
symbols << match.captured(1);
436-
}
428+
QStringList symbols;
429+
if (data.contains("symbolNames"))
430+
symbols = data["symbolNames"].toString().split("\n");
437431

438-
QTextStream in(&file);
439-
mUI.mCode->setError(in.readAll(), lineNumber, symbols);
432+
if (filepath == mUI.mCode->getFileName())
433+
{
434+
mUI.mCode->setError(lineNumber, symbols);
435+
return;
440436
}
437+
438+
QFile file(filepath);
439+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
440+
mUI.mCode->clear();
441+
return;
442+
}
443+
444+
QTextStream in(&file);
445+
mUI.mCode->setError(in.readAll(), lineNumber, symbols);
446+
mUI.mCode->setFileName(filepath);
441447
}
442448

443449
void ResultsView::log(const QString &str)

gui/resultsview.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ private slots:
359359
void on_mListLog_customContextMenuRequested(const QPoint &pos);
360360
private:
361361
QSet<QString> mContracts;
362+
363+
/** Current file shown in the code editor */
364+
QString mCurrentFileName;
362365
};
363366
/// @}
364367
#endif // RESULTSVIEW_H

lib/bughuntingchecks.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,11 @@ static void uninit(const Token *tok, const ExprEngine::Value &value, ExprEngine:
352352
const std::string inconclusiveMessage(inconclusive ? ". It is inconclusive if there would be a problem in the function call." : "");
353353

354354
if (!uninitStructMember.empty()) {
355+
const std::string symbol = tok->expressionString() + "." + uninitStructMember;
355356
dataBase->reportError(tok,
356357
Severity::SeverityType::error,
357358
"bughuntingUninitStructMember",
358-
"Cannot determine that '" + tok->expressionString() + "." + uninitStructMember + "' is initialized" + inconclusiveMessage,
359+
"$symbol:" + symbol + "\nCannot determine that '$symbol' is initialized" + inconclusiveMessage,
359360
CWE_USE_OF_UNINITIALIZED_VARIABLE,
360361
inconclusive,
361362
value.type == ExprEngine::ValueType::BailoutValue);
@@ -366,10 +367,12 @@ static void uninit(const Token *tok, const ExprEngine::Value &value, ExprEngine:
366367
if (uninitData)
367368
uninitexpr += "[0]";
368369

370+
const std::string symbol = (tok->varId() > 0) ? ("$symbol:" + tok->str() + "\n") : std::string();
371+
369372
dataBase->reportError(tok,
370373
Severity::SeverityType::error,
371374
"bughuntingUninit",
372-
"Cannot determine that '" + uninitexpr + "' is initialized" + inconclusiveMessage,
375+
symbol + "Cannot determine that '" + uninitexpr + "' is initialized" + inconclusiveMessage,
373376
CWE_USE_OF_UNINITIALIZED_VARIABLE,
374377
inconclusive,
375378
value.type == ExprEngine::ValueType::BailoutValue);

0 commit comments

Comments
 (0)