Skip to content

Commit

Permalink
using better means of hushing static analyzer warnings (asserts that …
Browse files Browse the repository at this point in the history
…only have a cost in debug builds!)

hushing many more warnings from static analyzer
  • Loading branch information
eteran committed Jan 22, 2018
1 parent c5371f0 commit 0e6dfbb
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 86 deletions.
3 changes: 3 additions & 0 deletions Regex/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cstdint>
#include <cstring>

#include <QtGlobal>

extern uint8_t Compute_Size;

template <class T>
Expand All @@ -24,6 +26,7 @@ T *OPERAND(T *p) {

template <class T>
uint8_t GET_OP_CODE(T *p) {
Q_ASSERT(p);
return *reinterpret_cast<uint8_t *>(p);
}

Expand Down
5 changes: 2 additions & 3 deletions Util/include/Util/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ template <class Ch, class Tr = std::char_traits<Ch>>
constexpr view::basic_string_view<Ch, Tr> substr(const Ch *first, const Ch *last) {

const Ch *data = first;
typename view::basic_string_view<Ch, Tr>::size_type size = std::distance(first, last);

return view::basic_string_view<Ch, Tr>(data, size);
auto size = std::distance(first, last);
return view::basic_string_view<Ch, Tr>(data, static_cast<size_t>(size));
}

template <class Ch, class Tr = std::char_traits<Ch>>
Expand Down
14 changes: 8 additions & 6 deletions source/DocumentWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4747,7 +4747,7 @@ void DocumentWidget::processFinished(int exitCode, QProcess::ExitStatus exitStat
area->TextSetCursorPos(buf->BufCursorPosHint());

if (reselectStart != -1) {
buf->BufSelect(reselectStart, reselectStart + output_string.size());
buf->BufSelect(reselectStart, reselectStart + static_cast<int64_t>(output_string.size()));
}
} else {
safeBufReplace(buf, &cmdData->leftPos, &cmdData->rightPos, output_string);
Expand Down Expand Up @@ -6090,15 +6090,17 @@ std::unique_ptr<WindowHighlightData> DocumentWidget::createHighlightDataEx(Patte
const bool noPass1 = (nPass1Patterns == 0);
const bool noPass2 = (nPass2Patterns == 0);

if (noPass2 && pass1Pats) {
if (noPass2) {
Q_ASSERT(pass1Pats);
pass1Pats[0].style = PLAIN_STYLE;
} else if (noPass1 && pass2Pats) {
} else if (noPass1) {
Q_ASSERT(pass2Pats);
pass2Pats[0].style = PLAIN_STYLE;
} else if(pass1Pats && pass2Pats) {
} else {
Q_ASSERT(pass1Pats);
Q_ASSERT(pass2Pats);
pass1Pats[0].style = UNFINISHED_STYLE;
pass2Pats[0].style = PLAIN_STYLE;
} else {
qCritical("NEdit: Internal error: could not create highlight patterns.");
}

for (size_t i = 1; i < nPass1Patterns; i++) {
Expand Down
11 changes: 6 additions & 5 deletions source/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6230,7 +6230,7 @@ bool MainWindow::ReplaceAndSearchEx(DocumentWidget *document, TextArea *area, co
defaultRegexFlags(searchType));

document->buffer_->BufReplaceEx(startPos, endPos, replaceResult);
replaceLen = replaceResult.size();
replaceLen = static_cast<int64_t>(replaceResult.size());
} else {
document->buffer_->BufReplaceEx(startPos, endPos, replaceString.toStdString());
replaceLen = replaceString.size();
Expand Down Expand Up @@ -6364,7 +6364,7 @@ bool MainWindow::SearchAndReplaceEx(DocumentWidget *document, TextArea *area, co
defaultRegexFlags(searchType));

document->buffer_->BufReplaceEx(startPos, endPos, replaceResult);
replaceLen = replaceResult.size();
replaceLen = static_cast<int64_t>(replaceResult.size());
} else {
document->buffer_->BufReplaceEx(startPos, endPos, replaceString.toStdString());
replaceLen = replaceString.size();
Expand Down Expand Up @@ -6657,7 +6657,7 @@ void MainWindow::ReplaceInSelectionEx(DocumentWidget *document, TextArea *area,
}

tempBuf.BufReplaceEx(startPos + realOffset, endPos + realOffset, replaceResult);
replaceLen = replaceResult.size();
replaceLen = static_cast<int64_t>(replaceResult.size());
} else {
// at this point plain substitutions (should) always work
tempBuf.BufReplaceEx(startPos + realOffset, endPos + realOffset, replaceString.toStdString());
Expand Down Expand Up @@ -6839,7 +6839,7 @@ bool MainWindow::ReplaceAllEx(DocumentWidget *document, TextArea *area, const QS
document->buffer_->BufReplaceEx(copyStart, copyEnd, newFileString);

// Move the cursor to the end of the last replacement
area->TextSetCursorPos(copyStart + newFileString.size());
area->TextSetCursorPos(copyStart + static_cast<int64_t>(newFileString.size()));

return true;
}
Expand Down Expand Up @@ -7039,7 +7039,8 @@ bool MainWindow::searchMatchesSelectionEx(DocumentWidget *document, const QStrin

// return the start and end of the selection
if (isRect) {
document->buffer_->GetSimpleSelection(left, right);
bool ret = document->buffer_->GetSimpleSelection(left, right);
Q_ASSERT(ret);
} else {
*left = selStart;
*right = selEnd;
Expand Down
10 changes: 5 additions & 5 deletions source/Rangeset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,17 +1028,17 @@ int64_t Rangeset::RangesetRemove(TextBuffer *buffer, Rangeset *minusSet) {
Range *minusRanges = minusSet->ranges_;
int64_t nMinusRanges = minusSet->n_ranges_;

if (nOrigRanges == 0 || nMinusRanges == 0 || !origRanges || !minusRanges) {
if (nOrigRanges == 0 || nMinusRanges == 0) {
// no ranges in origSet or minusSet - nothing to do
return 0;
}

Q_ASSERT(origRanges);
Q_ASSERT(minusRanges);

/* we must provide more space: each range in minusSet might split a range in origSet */
Range *newRanges = RangesNew(n_ranges_ + minusSet->n_ranges_);
if(!newRanges) {
// This really shouldn't happen...
return 0;
}
Q_ASSERT(newRanges);

Range *oldRanges = origRanges;
ranges_ = newRanges;
Expand Down
2 changes: 1 addition & 1 deletion source/RangesetTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int RangesetTable::RangesetCreate() {
return std::find(list.begin(), list.end(), ch) == list.end();
});

size_t firstAvailableIndex = it - std::begin(rangeset_labels);
auto firstAvailableIndex = static_cast<size_t>(it - std::begin(rangeset_labels));

if(firstAvailableIndex >= sizeof(rangeset_labels)) {
return 0;
Expand Down
51 changes: 26 additions & 25 deletions source/TextBuffer.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ auto BasicTextBuffer<Ch, Tr>::BufAsStringEx() noexcept -> view_type {
template <class Ch, class Tr>
void BasicTextBuffer<Ch, Tr>::BufSetAllEx(view_type text) {

const int64_t length = text.size();
const auto length = static_cast<int64_t>(text.size());

callPreDeleteCBs(0, buffer_.size());

Expand All @@ -47,10 +47,10 @@ void BasicTextBuffer<Ch, Tr>::BufSetAllEx(view_type text) {
buffer_.assign(text);

// Zero all of the existing selections
updateSelections(0, deletedText.size(), 0);
updateSelections(0, static_cast<int64_t>(deletedText.size()), 0);

// Call the saved display routine(s) to update the screen
callModifyCBs(0, deletedText.size(), length, 0, deletedText);
callModifyCBs(0, static_cast<int64_t>(deletedText.size()), length, 0, deletedText);
}

/*
Expand Down Expand Up @@ -145,7 +145,7 @@ void BasicTextBuffer<Ch, Tr>::BufReplaceEx(int64_t start, int64_t end, view_type

// TODO(eteran): 2.0, do same type of parameter normalization as BufRemove does?

const int64_t nInserted = static_cast<int64_t>(text.size());
const auto nInserted = static_cast<int64_t>(text.size());

callPreDeleteCBs(start, end - start);
const string_type deletedText = BufGetRangeEx(start, end);
Expand Down Expand Up @@ -312,7 +312,7 @@ void BasicTextBuffer<Ch, Tr>::overlayRectEx(int64_t startPos, int64_t rectStart,

*nInserted = static_cast<int64_t>(outStr.size());
*nDeleted = end - start;
*endPos = start + outStr.size() - len + endOffset;
*endPos = start + static_cast<int64_t>(outStr.size()) - len + endOffset;
}

/*
Expand Down Expand Up @@ -390,9 +390,9 @@ void BasicTextBuffer<Ch, Tr>::BufReplaceRectEx(int64_t start, int64_t end, int64

if (nInsertedLines < nDeletedLines) {

insText.reserve(static_cast<size_t>(text.size()) + nDeletedLines - nInsertedLines);
insText.reserve(text.size() + static_cast<size_t>(nDeletedLines) - static_cast<size_t>(nInsertedLines));
insText.assign(text.begin(), text.end());
insText.append(nDeletedLines - nInsertedLines, Ch('\n'));
insText.append(static_cast<size_t>(nDeletedLines - nInsertedLines), Ch('\n'));

// NOTE(etreran): use insText instead of the passed in buffer
text = insText;
Expand Down Expand Up @@ -797,7 +797,7 @@ int64_t BasicTextBuffer<Ch, Tr>::BufExpandCharacter(Ch ch, int64_t indent, Ch ou
#if defined(VISUAL_CTRL_CHARS)
// Convert ASCII control codes to readable character sequences
if ((static_cast<size_t>(ch)) < 32) {
return snprintf(outStr, MAX_EXP_CHAR_LEN, "<%s>", controlCharacter(ch));
return snprintf(outStr, MAX_EXP_CHAR_LEN, "<%s>", controlCharacter(static_cast<size_t>(ch)));
}

if (ch == 127) {
Expand All @@ -822,7 +822,8 @@ int64_t BasicTextBuffer<Ch, Tr>::BufCharWidth(Ch ch, int64_t indent, int tabDist

#if defined(VISUAL_CTRL_CHARS)
if (static_cast<size_t>(ch) < 32) {
return Tr::length(controlCharacter(ch)) + 2;
const Ch *const s = controlCharacter(static_cast<size_t>(ch));
return static_cast<int64_t>(Tr::length(s) + 2);
}

if (ch == 127) {
Expand Down Expand Up @@ -1010,7 +1011,7 @@ boost::optional<int64_t> BasicTextBuffer<Ch, Tr>::BufSearchBackwardEx(int64_t st
*/
template <class Ch, class Tr>
int BasicTextBuffer<Ch, Tr>::BufCmpEx(int64_t pos, Ch *cmpText, int64_t size) const noexcept {
return buffer_.compare(pos, view_type(cmpText, size));
return buffer_.compare(pos, view_type(cmpText, static_cast<size_t>(size)));
}

template <class Ch, class Tr>
Expand All @@ -1032,7 +1033,7 @@ int BasicTextBuffer<Ch, Tr>::BufCmpEx(int64_t pos, Ch ch) const noexcept {
*/
template <class Ch, class Tr>
int64_t BasicTextBuffer<Ch, Tr>::insertEx(int64_t pos, view_type text) noexcept {
const int64_t length = text.size();
const auto length = static_cast<int64_t>(text.size());

buffer_.insert(pos, text);

Expand Down Expand Up @@ -1180,7 +1181,7 @@ void BasicTextBuffer<Ch, Tr>::deleteRect(int64_t start, int64_t end, int64_t rec

const string_type text = BufGetRangeEx(start, end);
const string_type expText = expandTabsEx(text, 0, tabDist_);
int64_t len = expText.size();
auto len = static_cast<int64_t>(expText.size());

string_type outStr;
outStr.reserve(expText.size() + static_cast<size_t>(nLines * MAX_EXP_CHAR_LEN * 2));
Expand All @@ -1197,7 +1198,7 @@ void BasicTextBuffer<Ch, Tr>::deleteRect(int64_t start, int64_t end, int64_t rec
// TODO(eteran): 2.0, remove the need for this temp
string_type temp;
deleteRectFromLine(line, rectStart, rectEnd, tabDist_, useTabs_, &temp, &endOffset);
len = temp.size();
len = static_cast<int64_t>(temp.size());

std::copy_n(temp.begin(), len, outPtr);

Expand All @@ -1214,8 +1215,8 @@ void BasicTextBuffer<Ch, Tr>::deleteRect(int64_t start, int64_t end, int64_t rec
deleteRange(start, end);
insertEx(start, outStr);

*replaceLen = outStr.size();
*endPos = start + outStr.size() - len + endOffset;
*replaceLen = static_cast<int64_t>(outStr.size());
*endPos = start + static_cast<int64_t>(outStr.size()) - len + endOffset;
}

/*
Expand Down Expand Up @@ -1335,7 +1336,7 @@ void BasicTextBuffer<Ch, Tr>::insertColEx(int64_t column, int64_t startPos, view
// TODO(eteran): 2.0, remove the need for this temp
string_type temp;
insertColInLineEx(line, insLine, column, insWidth, tabDist_, useTabs_, &temp, &endOffset);
len = temp.size();
len = static_cast<int64_t>(temp.size());

#if 0
/* Earlier comments claimed that trailing whitespace could multiply on \
Expand Down Expand Up @@ -1367,9 +1368,9 @@ void BasicTextBuffer<Ch, Tr>::insertColEx(int64_t column, int64_t startPos, view
deleteRange(start, end);
insertEx(start, outStr);

*nInserted = outStr.size();
*nInserted = static_cast<int64_t>(outStr.size());
*nDeleted = end - start;
*endPos = start + outStr.size() - len + endOffset;
*endPos = start + static_cast<int64_t>(outStr.size()) - len + endOffset;
}

/*
Expand Down Expand Up @@ -1568,10 +1569,10 @@ auto BasicTextBuffer<Ch, Tr>::unexpandTabsEx(view_type text, int64_t startIndent
Ch expandedChar[MAX_EXP_CHAR_LEN];
const int len = BufExpandTab(indent, expandedChar, tabDist);

const auto cmp = text.compare(pos, len, expandedChar, len);
const auto cmp = text.compare(pos, static_cast<size_t>(len), expandedChar, static_cast<size_t>(len));

if (len >= 3 && !cmp) {
pos += len;
pos += static_cast<size_t>(len);
*outPtr++ = Ch('\t');
indent += len;
} else {
Expand Down Expand Up @@ -1605,7 +1606,7 @@ auto BasicTextBuffer<Ch, Tr>::expandTabsEx(view_type text, int64_t startIndent,
for (Ch ch : text) {
if (ch == Ch('\t')) {
const int64_t len = BufCharWidth(ch, indent, tabDist);
outLen += len;
outLen += static_cast<size_t>(len);
indent += len;
} else if (ch == Ch('\n')) {
indent = startIndent;
Expand Down Expand Up @@ -1949,7 +1950,7 @@ void BasicTextBuffer<Ch, Tr>::overlayRectInLineEx(view_type line, view_type insL

/* If there's no text after rectStart and no text to insert, that's all */
if (insLine.empty() && linePtr == line.end()) {
*endOffset = outStr->size();
*endOffset = static_cast<int64_t>(outStr->size());
return;
}

Expand All @@ -1964,7 +1965,7 @@ void BasicTextBuffer<Ch, Tr>::overlayRectInLineEx(view_type line, view_type insL
the inserted string began at column 0 to its new column destination */
if (!insLine.empty()) {
string_type retabbedStr = realignTabsEx(insLine, 0, rectStart, tabDist, useTabs);
len = retabbedStr.size();
len = static_cast<int64_t>(retabbedStr.size());

for (Ch c : retabbedStr) {
*outPtr++ = c;
Expand All @@ -1975,7 +1976,7 @@ void BasicTextBuffer<Ch, Tr>::overlayRectInLineEx(view_type line, view_type insL

/* If the original line did not extend past "rectStart", that's all */
if (linePtr == line.end()) {
*endOffset = outStr->size();
*endOffset = static_cast<int64_t>(outStr->size());
return;
}

Expand All @@ -1984,7 +1985,7 @@ void BasicTextBuffer<Ch, Tr>::overlayRectInLineEx(view_type line, view_type insL
addPaddingEx(outPtr, outIndent, postRectIndent, tabDist, useTabs);
outIndent = postRectIndent;

*endOffset = outStr->size();
*endOffset = static_cast<int64_t>(outStr->size());

/* copy the text beyond "rectEnd" */
std::copy(linePtr, line.end(), outPtr);
Expand Down
8 changes: 4 additions & 4 deletions source/gap_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ void gap_buffer<Ch, Tr>::assign(view_type str) {
gap_start_ = length / 2;
gap_end_ = gap_start_ + PreferredGapSize;

Tr::copy(&buf_[0], &str[0], static_cast<size_t>(gap_start_));
Tr::copy(&buf_[gap_end_], &str[gap_start_], static_cast<size_t>(length - gap_start_));
Tr::copy(&buf_[0], &str[0], static_cast<size_t>(gap_start_));
Tr::copy(&buf_[gap_end_], &str[static_cast<size_t>(gap_start_)], static_cast<size_t>(length - gap_start_));

buf_[length + PreferredGapSize] = Ch();

Expand Down Expand Up @@ -506,8 +506,8 @@ void gap_buffer<Ch, Tr>::reallocate_buffer(int64_t new_gap_start, int64_t new_ga
Tr::copy(&newBuf[new_gap_end], &buf_[new_gap_start], static_cast<size_t>(gap_start_ - new_gap_start));
Tr::copy(&newBuf[new_gap_end + gap_start_ - new_gap_start], &buf_[gap_end_], static_cast<size_t>(size() - gap_start_));
} else { // newGapStart > gapStart_
Tr::copy(newBuf, &buf_[0], static_cast<size_t>(gap_start_));
Tr::copy(&newBuf[gap_start_], &buf_[gap_end_], static_cast<size_t>(new_gap_start - gap_start_));
Tr::copy(newBuf, &buf_[0], static_cast<size_t>(gap_start_));
Tr::copy(&newBuf[gap_start_], &buf_[gap_end_], static_cast<size_t>(new_gap_start - gap_start_));
Tr::copy(&newBuf[new_gap_end], &buf_[gap_end_ + new_gap_start - gap_start_], static_cast<size_t>(size() - new_gap_start));
}

Expand Down
7 changes: 2 additions & 5 deletions source/highlight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,8 @@ bool parseString(HighlightData *pattern, const char **string, char **styleString
return false;
}

if(!subPat) {
qCritical("NEdit: Internal error, failed to match in parseString [2]");
return false;
}

Q_ASSERT(subPat);

// the sub-pattern is a simple match, just color it
if (!subPat->subPatternRE) {
fillStyleString(&stringPtr, &stylePtr, pattern->subPatternRE->endp[0], /* subPat->startRE->endp[0],*/ subPat->style, prevChar);
Expand Down
Loading

0 comments on commit 0e6dfbb

Please sign in to comment.