Commit b4eeba21 authored by Igor Kushnir's avatar Igor Kushnir Committed by Milian Wolff
Browse files

Remove skipFunctionArguments() and code only it used

This function was used only in KDevelop's old custom C++ parser
(oldcpp) until its removal in f31d32f4.

This function contains one of two uses of another function
findCommaOrEnd(). Remove this dead usage in order to modify
findCommaOrEnd() without worrying about it.

Also remove a simple helper class SafetyCounter and 3 helper functions
declared in stringhelpers.cpp, which were used only in
skipFunctionArguments().
parent 51a56fa6
......@@ -272,7 +272,6 @@ install(FILES
duchain/abstractfunctiondeclaration.h
duchain/functiondeclaration.h
duchain/stringhelpers.h
duchain/safetycounter.h
duchain/namespacealiasdeclaration.h
duchain/aliasdeclaration.h
duchain/dumpdotgraph.h
......
/*
SPDX-FileCopyrightText: 2006 David Nolden <david.nolden.kdevelop@art-master.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __SAFETYCOUNTER_H__
#define __SAFETYCOUNTER_H__
#include <QDebug>
struct SafetyCounter
{
int safetyCounter = 0;
const int maxSafetyCounter;
explicit SafetyCounter(int max = 40000) : maxSafetyCounter(max)
{
}
void init()
{
safetyCounter = 0;
}
SafetyCounter& operator ++()
{
safetyCounter++;
return *this;
}
///Returns whether the counter is ok, but without increasing it
bool ok() const
{
return safetyCounter < maxSafetyCounter;
}
operator bool() {
safetyCounter++;
bool ret = safetyCounter < maxSafetyCounter;
if (!ret) {
if (safetyCounter == maxSafetyCounter) {
#ifdef DEPTHBACKTRACE
qDebug() << "WARNING: Safety-counter reached count > " << maxSafetyCounter << ", operation stopped";
#endif
}
}
return ret;
}
};
#endif
......@@ -5,11 +5,9 @@
*/
#include "stringhelpers.h"
#include "safetycounter.h"
#include <debug.h>
#include <QString>
#include <QStringList>
#include <QVarLengthArray>
namespace {
......@@ -247,100 +245,6 @@ int findCommaOrEnd(const QString& str, int pos, QChar validEnd)
return str.length();
}
QString reverse(const QString& str)
{
QString ret;
int len = str.length();
ret.reserve(len);
for (int a = len - 1; a >= 0; --a) {
switch (str[a].unicode()) {
case '(':
ret += QLatin1Char(')');
continue;
case '[':
ret += QLatin1Char(']');
continue;
case '{':
ret += QLatin1Char('}');
continue;
case '<':
ret += QLatin1Char('>');
continue;
case ')':
ret += QLatin1Char('(');
continue;
case ']':
ret += QLatin1Char('[');
continue;
case '}':
ret += QLatin1Char('{');
continue;
case '>':
ret += QLatin1Char('<');
continue;
default:
ret += str[a];
continue;
}
}
return ret;
}
///@todo this hackery sucks
QString escapeForBracketMatching(QString str)
{
str.replace(QLatin1String("<<"), QLatin1String("$&"));
str.replace(QLatin1String(">>"), QLatin1String("$$"));
str.replace(QLatin1String("\\\""), QLatin1String("$!"));
str.replace(QLatin1String("->"), QLatin1String("$?"));
return str;
}
QString escapeFromBracketMatching(QString str)
{
str.replace(QLatin1String("$&"), QLatin1String("<<"));
str.replace(QLatin1String("$$"), QLatin1String(">>"));
str.replace(QLatin1String("$!"), QLatin1String("\\\""));
str.replace(QLatin1String("$?"), QLatin1String("->"));
return str;
}
void skipFunctionArguments(const QString& str_, QStringList& skippedArguments, int& argumentsStart)
{
QString withStrings = escapeForBracketMatching(str_);
QString str = escapeForBracketMatching(clearStrings(str_));
//Blank out everything that can confuse the bracket-matching algorithm
QString reversed = reverse(str.left(argumentsStart));
QString withStringsReversed = reverse(withStrings.left(argumentsStart));
//Now we should decrease argumentStart at the end by the count of steps we go right until we find the beginning of the function
SafetyCounter s(1000);
int pos = 0;
int len = reversed.length();
//we are searching for an opening-brace, but the reversion has also reversed the brace
while (pos < len && s) {
int lastPos = pos;
pos = KDevelop::findCommaOrEnd(reversed, pos);
if (pos > lastPos) {
QString arg = reverse(withStringsReversed.mid(lastPos, pos - lastPos)).trimmed();
if (!arg.isEmpty())
skippedArguments.push_front(escapeFromBracketMatching(arg)); //We are processing the reversed reverseding, so push to front
}
if (reversed[pos] == QLatin1Char(')') || reversed[pos] == QLatin1Char('>'))
break;
else
++pos;
}
if (!s) {
qCDebug(LANGUAGE) << "skipFunctionArguments: Safety-counter triggered";
}
argumentsStart -= pos;
}
QString reduceWhiteSpace(const QString& str_)
{
const QStringRef str = QStringRef(&str_).trimmed();
......
......@@ -14,7 +14,6 @@
class QByteArray;
class QString;
class QStringList;
namespace KDevelop {
class ParamIteratorPrivate;
......@@ -34,15 +33,6 @@ int KDEVPLATFORMLANGUAGE_EXPORT findClose(const QString& str, int pos);
*/
int KDEVPLATFORMLANGUAGE_EXPORT findCommaOrEnd(const QString& str, int pos, QChar validEnd = QLatin1Char( ' ' ));
/**
* Skips in the string backwards over function-arguments, and stops at the right side of a "("
* @param str string to skip
* @param skippedArguments Will contain all skipped arguments
* @param argumentsStart Should be set to the position where the seeking should start, will be changed to the right side of a "(" when found. Should be at the right side of a '(', and may be max. str.length()
*/
void KDEVPLATFORMLANGUAGE_EXPORT skipFunctionArguments(const QString& str, QStringList& skippedArguments,
int& argumentsStart);
/**
* Removes white space at the beginning and end, and replaces contiguous inner white-spaces with single white-spaces. Newlines are treated as whitespaces, the returned text will have no more newlines.
*/
......
......@@ -9,7 +9,6 @@
#include "debuglog.h"
#include <language/duchain/stringhelpers.h>
#include <language/duchain/safetycounter.h>
#include <QChar>
#include <QString>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment