Moc: use const (and const APIs) more

For CoW types, prefer const methods to avoid needless detach()ing.

Change-Id: Iefc33552d826aa30320e52acd2d421c9bdae127e
Reviewed-by: Edward Welbourne <edward.welbourne@theqtcompany.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2016-04-14 09:23:41 +03:00
parent 5361a8dec5
commit 777d46b403
4 changed files with 25 additions and 25 deletions

View File

@ -79,7 +79,7 @@ Generator::Generator(ClassDef *classDef, const QList<QByteArray> &metaTypes, con
, knownGadgets(knownGadgets) , knownGadgets(knownGadgets)
{ {
if (cdef->superclassList.size()) if (cdef->superclassList.size())
purestSuperClass = cdef->superclassList.first().first; purestSuperClass = cdef->superclassList.constFirst().first;
} }
static inline int lengthOfEscapeSequence(const QByteArray &s, int i) static inline int lengthOfEscapeSequence(const QByteArray &s, int i)

View File

@ -360,7 +360,7 @@ int runMoc(int argc, char **argv)
int spos = filename.lastIndexOf(QDir::separator()); int spos = filename.lastIndexOf(QDir::separator());
int ppos = filename.lastIndexOf(QLatin1Char('.')); int ppos = filename.lastIndexOf(QLatin1Char('.'));
// spos >= -1 && ppos > spos => ppos >= 0 // spos >= -1 && ppos > spos => ppos >= 0
moc.noInclude = (ppos > spos && filename[ppos + 1].toLower() != QLatin1Char('h')); moc.noInclude = (ppos > spos && filename.at(ppos + 1).toLower() != QLatin1Char('h'));
} }
if (defaultInclude) { if (defaultInclude) {
if (moc.includePath.isEmpty()) { if (moc.includePath.isEmpty()) {

View File

@ -136,7 +136,7 @@ bool Moc::parseClassHead(ClassDef *def)
} while (test(COMMA)); } while (test(COMMA));
if (!def->superclassList.isEmpty() if (!def->superclassList.isEmpty()
&& knownGadgets.contains(def->superclassList.first().first)) { && knownGadgets.contains(def->superclassList.constFirst().first)) {
// Q_GADGET subclasses are treated as Q_GADGETs // Q_GADGET subclasses are treated as Q_GADGETs
knownGadgets.insert(def->classname, def->qualified); knownGadgets.insert(def->classname, def->qualified);
knownGadgets.insert(def->qualified, def->qualified); knownGadgets.insert(def->qualified, def->qualified);
@ -312,7 +312,7 @@ void Moc::parseFunctionArguments(FunctionDef *def)
} }
if (!def->arguments.isEmpty() if (!def->arguments.isEmpty()
&& def->arguments.last().normalizedType == "QPrivateSignal") { && def->arguments.constLast().normalizedType == "QPrivateSignal") {
def->arguments.removeLast(); def->arguments.removeLast();
def->isPrivateSignal = true; def->isPrivateSignal = true;
} }
@ -730,7 +730,7 @@ void Moc::parse()
if (funcDef.isConstructor) { if (funcDef.isConstructor) {
if ((access == FunctionDef::Public) && funcDef.isInvokable) { if ((access == FunctionDef::Public) && funcDef.isInvokable) {
def.constructorList += funcDef; def.constructorList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def.constructorList += funcDef; def.constructorList += funcDef;
@ -743,7 +743,7 @@ void Moc::parse()
def.publicList += funcDef; def.publicList += funcDef;
if (funcDef.isSlot) { if (funcDef.isSlot) {
def.slotList += funcDef; def.slotList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def.slotList += funcDef; def.slotList += funcDef;
@ -752,7 +752,7 @@ void Moc::parse()
++def.revisionedMethods; ++def.revisionedMethods;
} else if (funcDef.isSignal) { } else if (funcDef.isSignal) {
def.signalList += funcDef; def.signalList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def.signalList += funcDef; def.signalList += funcDef;
@ -761,7 +761,7 @@ void Moc::parse()
++def.revisionedMethods; ++def.revisionedMethods;
} else if (funcDef.isInvokable) { } else if (funcDef.isInvokable) {
def.methodList += funcDef; def.methodList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def.methodList += funcDef; def.methodList += funcDef;
@ -863,7 +863,7 @@ void Moc::generate(FILE *out)
{ {
QByteArray fn = filename; QByteArray fn = filename;
int i = filename.length()-1; int i = filename.length()-1;
while (i>0 && filename[i-1] != '/' && filename[i-1] != '\\') while (i > 0 && filename.at(i - 1) != '/' && filename.at(i - 1) != '\\')
--i; // skip path --i; // skip path
if (i >= 0) if (i >= 0)
fn = filename.mid(i); fn = filename.mid(i);
@ -879,7 +879,7 @@ void Moc::generate(FILE *out)
includePath += '/'; includePath += '/';
for (int i = 0; i < includeFiles.size(); ++i) { for (int i = 0; i < includeFiles.size(); ++i) {
QByteArray inc = includeFiles.at(i); QByteArray inc = includeFiles.at(i);
if (inc[0] != '<' && inc[0] != '"') { if (inc.at(0) != '<' && inc.at(0) != '"') {
if (includePath.size() && includePath != "./") if (includePath.size() && includePath != "./")
inc.prepend(includePath); inc.prepend(includePath);
inc = '\"' + inc + '\"'; inc = '\"' + inc + '\"';
@ -887,7 +887,7 @@ void Moc::generate(FILE *out)
fprintf(out, "#include %s\n", inc.constData()); fprintf(out, "#include %s\n", inc.constData());
} }
} }
if (classList.size() && classList.first().classname == "Qt") if (classList.size() && classList.constFirst().classname == "Qt")
fprintf(out, "#include <QtCore/qobject.h>\n"); fprintf(out, "#include <QtCore/qobject.h>\n");
fprintf(out, "#include <QtCore/qbytearray.h>\n"); // For QByteArrayData fprintf(out, "#include <QtCore/qbytearray.h>\n"); // For QByteArrayData
@ -965,7 +965,7 @@ void Moc::parseSlots(ClassDef *def, FunctionDef::Access access)
++def->revisionedMethods; ++def->revisionedMethods;
} }
def->slotList += funcDef; def->slotList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def->slotList += funcDef; def->slotList += funcDef;
@ -1021,7 +1021,7 @@ void Moc::parseSignals(ClassDef *def)
++def->revisionedMethods; ++def->revisionedMethods;
} }
def->signalList += funcDef; def->signalList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def->signalList += funcDef; def->signalList += funcDef;
@ -1059,7 +1059,7 @@ void Moc::createPropertyDef(PropertyDef &propDef)
next(); next();
propDef.name = lexem(); propDef.name = lexem();
while (test(IDENTIFIER)) { while (test(IDENTIFIER)) {
QByteArray l = lexem(); const QByteArray l = lexem();
if (l[0] == 'C' && l == "CONSTANT") { if (l[0] == 'C' && l == "CONSTANT") {
propDef.constant = true; propDef.constant = true;
continue; continue;
@ -1395,7 +1395,7 @@ void Moc::parseSlotInPrivate(ClassDef *def, FunctionDef::Access access)
funcDef.access = access; funcDef.access = access;
parseFunction(&funcDef, true); parseFunction(&funcDef, true);
def->slotList += funcDef; def->slotList += funcDef;
while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) { while (funcDef.arguments.size() > 0 && funcDef.arguments.constLast().isDefault) {
funcDef.wasCloned = true; funcDef.wasCloned = true;
funcDef.arguments.removeLast(); funcDef.arguments.removeLast();
def->slotList += funcDef; def->slotList += funcDef;
@ -1539,7 +1539,7 @@ void Moc::checkSuperClasses(ClassDef *def)
if (interface2IdMap.contains(superClass)) { if (interface2IdMap.contains(superClass)) {
bool registeredInterface = false; bool registeredInterface = false;
for (int i = 0; i < def->interfaceList.count(); ++i) for (int i = 0; i < def->interfaceList.count(); ++i)
if (def->interfaceList.at(i).first().className == superClass) { if (def->interfaceList.at(i).constFirst().className == superClass) {
registeredInterface = true; registeredInterface = true;
break; break;
} }

View File

@ -205,13 +205,13 @@ Symbols Preprocessor::tokenize(const QByteArray& input, int lineNum, Preprocesso
// STRING_LITERAL handling in moc // STRING_LITERAL handling in moc
if (!Preprocessor::preprocessOnly if (!Preprocessor::preprocessOnly
&& !symbols.isEmpty() && !symbols.isEmpty()
&& symbols.last().token == STRING_LITERAL) { && symbols.constLast().token == STRING_LITERAL) {
QByteArray newString = symbols.last().unquotedLexem(); QByteArray newString = symbols.constLast().unquotedLexem();
newString += input.mid(lexem - begin + 1, data - lexem - 2); newString += input.mid(lexem - begin + 1, data - lexem - 2);
newString.prepend('\"'); newString.prepend('\"');
newString.append('\"'); newString.append('\"');
symbols.last() = Symbol(symbols.last().lineNum, symbols.last() = Symbol(symbols.constLast().lineNum,
STRING_LITERAL, STRING_LITERAL,
newString); newString);
continue; continue;
@ -679,7 +679,7 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym
if (s.token == WHITESPACE) if (s.token == WHITESPACE)
continue; continue;
while (expansion.size() && expansion.last().token == PP_WHITESPACE) while (expansion.size() && expansion.constLast().token == PP_WHITESPACE)
expansion.pop_back(); expansion.pop_back();
Symbol next = s; Symbol next = s;
@ -692,8 +692,8 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym
next = arg.at(0); next = arg.at(0);
} }
if (!expansion.isEmpty() && expansion.last().token == s.token) { if (!expansion.isEmpty() && expansion.constLast().token == s.token) {
Symbol last = expansion.last(); Symbol last = expansion.constLast();
expansion.pop_back(); expansion.pop_back();
if (last.token == STRING_LITERAL || s.token == STRING_LITERAL) if (last.token == STRING_LITERAL || s.token == STRING_LITERAL)
@ -1127,12 +1127,12 @@ void Preprocessor::preprocess(const QByteArray &filename, Symbols &preprocessed)
} }
// remove trailing whitespace // remove trailing whitespace
while (!macro.symbols.isEmpty() && while (!macro.symbols.isEmpty() &&
(macro.symbols.last().token == PP_WHITESPACE || macro.symbols.last().token == WHITESPACE)) (macro.symbols.constLast().token == PP_WHITESPACE || macro.symbols.constLast().token == WHITESPACE))
macro.symbols.pop_back(); macro.symbols.pop_back();
if (!macro.symbols.isEmpty()) { if (!macro.symbols.isEmpty()) {
if (macro.symbols.first().token == PP_HASHHASH || if (macro.symbols.constFirst().token == PP_HASHHASH ||
macro.symbols.last().token == PP_HASHHASH) { macro.symbols.constLast().token == PP_HASHHASH) {
error("'##' cannot appear at either end of a macro expansion"); error("'##' cannot appear at either end of a macro expansion");
} }
} }