summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOswald Buddenhagen <oswald.buddenhagen@nokia.com>2010-04-15 19:48:28 (GMT)
committerOswald Buddenhagen <oswald.buddenhagen@nokia.com>2010-04-15 20:16:33 (GMT)
commitd758d652e1b3950f9df6aa4c1bdef67ba9b88b15 (patch)
tree338c03b07a733a553e9dbd30ec103635f8a5dbd4
parent2c0f13ccfe750241eb7ddedf6412da380a146975 (diff)
downloadQt-d758d652e1b3950f9df6aa4c1bdef67ba9b88b15.zip
Qt-d758d652e1b3950f9df6aa4c1bdef67ba9b88b15.tar.gz
Qt-d758d652e1b3950f9df6aa4c1bdef67ba9b88b15.tar.bz2
make QT_TR_NOOP work in static initializers
do that by ignoring all equal signs and everything between and including brackets. this makes static initializers look effectively like function definitions, thus creating proper context. Task-number: QTBUG-9276
-rw-r--r--tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp23
-rw-r--r--tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result17
-rw-r--r--tools/linguist/lupdate/cpp.cpp53
3 files changed, 87 insertions, 6 deletions
diff --git a/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp b/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp
index 6e73d6d..0765bfc 100644
--- a/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp
+++ b/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp
@@ -252,3 +252,26 @@ class YetAnotherTest : QObject {
//: This is a message without a source string
QString test = qtTrId("yet_another_id");
+
+
+
+// QTBUG-9276: context in static initializers
+class Bogus : QObject {
+ Q_OBJECT
+
+ static const char * const s_strings[];
+};
+
+const char * const Bogus::s_strings[] = {
+ QT_TR_NOOP("this should be in Bogus")
+};
+
+const char * const Bogus::s_strings[SIZE] = {
+ QT_TR_NOOP("this should be in Bogus")
+};
+
+void bogosity()
+{
+ // no spaces here. test collateral damage from ignoring equal sign
+ Class::member=QObject::tr("just QObject");
+}
diff --git a/tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result b/tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result
index 6d50c21..208191d 100644
--- a/tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result
+++ b/tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result
@@ -26,6 +26,15 @@ backslashed \ stuff.</source>
</message>
</context>
<context>
+ <name>Bogus</name>
+ <message>
+ <location filename="main.cpp" line="266"/>
+ <location filename="main.cpp" line="270"/>
+ <source>this should be in Bogus</source>
+ <translation type="unfinished"></translation>
+ </message>
+</context>
+<context>
<name>Dialog2</name>
<message numerus="yes">
<location filename="main.cpp" line="70"/>
@@ -184,6 +193,14 @@ backslashed \ stuff.</source>
</message>
</context>
<context>
+ <name>QObject</name>
+ <message>
+ <location filename="main.cpp" line="276"/>
+ <source>just QObject</source>
+ <translation type="unfinished"></translation>
+ </message>
+</context>
+<context>
<name>QTranslator</name>
<message>
<location filename="main.cpp" line="93"/>
diff --git a/tools/linguist/lupdate/cpp.cpp b/tools/linguist/lupdate/cpp.cpp
index 3422212..4791450 100644
--- a/tools/linguist/lupdate/cpp.cpp
+++ b/tools/linguist/lupdate/cpp.cpp
@@ -212,13 +212,15 @@ public:
private:
struct IfdefState {
IfdefState() {}
- IfdefState(int _braceDepth, int _parenDepth) :
+ IfdefState(int _bracketDepth, int _braceDepth, int _parenDepth) :
+ bracketDepth(_bracketDepth),
braceDepth(_braceDepth),
parenDepth(_parenDepth),
elseLine(-1)
{}
SavedState state;
+ int bracketDepth, bracketDepth1st;
int braceDepth, braceDepth1st;
int parenDepth, parenDepth1st;
int elseLine;
@@ -280,7 +282,7 @@ private:
Tok_tr, Tok_trUtf8, Tok_translate, Tok_translateUtf8, Tok_trid,
Tok_Q_OBJECT, Tok_Q_DECLARE_TR_FUNCTIONS,
Tok_Ident, Tok_Comment, Tok_String, Tok_Arrow, Tok_Colon, Tok_ColonColon,
- Tok_Equals,
+ Tok_Equals, Tok_LeftBracket, Tok_RightBracket,
Tok_LeftBrace, Tok_RightBrace, Tok_LeftParen, Tok_RightParen, Tok_Comma, Tok_Semicolon,
Tok_Null, Tok_Integer,
Tok_QuotedInclude, Tok_AngledInclude,
@@ -296,10 +298,12 @@ private:
QString yyWord;
qlonglong yyInteger;
QStack<IfdefState> yyIfdefStack;
+ int yyBracketDepth;
int yyBraceDepth;
int yyParenDepth;
int yyLineNo;
int yyCurLineNo;
+ int yyBracketLineNo;
int yyBraceLineNo;
int yyParenLineNo;
@@ -336,9 +340,11 @@ CppParser::CppParser(ParseResults *_results)
results = new ParseResults;
directInclude = false;
}
+ yyBracketDepth = 0;
yyBraceDepth = 0;
yyParenDepth = 0;
yyCurLineNo = 1;
+ yyBracketLineNo = 1;
yyBraceLineNo = 1;
yyParenLineNo = 1;
yyAtNewline = true;
@@ -565,7 +571,7 @@ uint CppParser::getToken()
yyCh = getChar();
if (yyCh == 'f') {
// if, ifdef, ifndef
- yyIfdefStack.push(IfdefState(yyBraceDepth, yyParenDepth));
+ yyIfdefStack.push(IfdefState(yyBracketDepth, yyBraceDepth, yyParenDepth));
yyCh = getChar();
} else if (yyCh == 'n') {
// include
@@ -604,16 +610,20 @@ uint CppParser::getToken()
if (!yyIfdefStack.isEmpty()) {
IfdefState &is = yyIfdefStack.top();
if (is.elseLine != -1) {
- if (yyBraceDepth != is.braceDepth1st || yyParenDepth != is.parenDepth1st)
- qWarning("%s:%d: Parenthesis/brace mismatch between "
+ if (yyBracketDepth != is.bracketDepth1st
+ || yyBraceDepth != is.braceDepth1st
+ || yyParenDepth != is.parenDepth1st)
+ qWarning("%s:%d: Parenthesis/bracket/brace mismatch between "
"#if and #else branches; using #if branch\n",
qPrintable(yyFileName), is.elseLine);
} else {
+ is.bracketDepth1st = yyBracketDepth;
is.braceDepth1st = yyBraceDepth;
is.parenDepth1st = yyParenDepth;
saveState(&is.state);
}
is.elseLine = yyLineNo;
+ yyBracketDepth = is.bracketDepth;
yyBraceDepth = is.braceDepth;
yyParenDepth = is.parenDepth;
}
@@ -623,10 +633,13 @@ uint CppParser::getToken()
if (!yyIfdefStack.isEmpty()) {
IfdefState is = yyIfdefStack.pop();
if (is.elseLine != -1) {
- if (yyBraceDepth != is.braceDepth1st || yyParenDepth != is.parenDepth1st)
+ if (yyBracketDepth != is.bracketDepth1st
+ || yyBraceDepth != is.braceDepth1st
+ || yyParenDepth != is.parenDepth1st)
qWarning("%s:%d: Parenthesis/brace mismatch between "
"#if and #else branches; using #if branch\n",
qPrintable(yyFileName), is.elseLine);
+ yyBracketDepth = is.bracketDepth1st;
yyBraceDepth = is.braceDepth1st;
yyParenDepth = is.parenDepth1st;
loadState(&is.state);
@@ -899,6 +912,21 @@ uint CppParser::getToken()
yyParenDepth--;
yyCh = getChar();
return Tok_RightParen;
+ case '[':
+ if (yyBracketDepth == 0)
+ yyBracketLineNo = yyCurLineNo;
+ yyBracketDepth++;
+ yyCh = getChar();
+ return Tok_LeftBracket;
+ case ']':
+ if (yyBracketDepth == 0)
+ qWarning("%s:%d: Excess closing bracket in C++ code"
+ " (or abuse of the C++ preprocessor)\n",
+ qPrintable(yyFileName), yyCurLineNo);
+ else
+ yyBracketDepth--;
+ yyCh = getChar();
+ return Tok_RightBracket;
case ',':
yyCh = getChar();
return Tok_Comma;
@@ -1534,6 +1562,12 @@ void CppParser::parseInternal(ConversionData &cd, QSet<QString> &inclusions)
yyCh = getChar();
yyTok = getToken();
while (yyTok != Tok_Eof) {
+ // these are array indexing operations. we ignore them entirely
+ // so they don't confuse our scoping of static initializers.
+ // we enter the loop by either reading a left bracket or by an
+ // #else popping the state.
+ while (yyBracketDepth)
+ yyTok = getToken();
//qDebug() << "TOKEN: " << yyTok;
switch (yyTok) {
case Tok_QuotedInclude: {
@@ -2074,6 +2108,9 @@ void CppParser::parseInternal(ConversionData &cd, QSet<QString> &inclusions)
default:
if (!yyParenDepth)
prospectiveContext.clear();
+ // fallthrough
+ case Tok_Equals: // for static initializers; other cases make no difference
+ case Tok_RightBracket: // ignoring indexing; same reason
case_default:
yyTok = getToken();
break;
@@ -2088,6 +2125,10 @@ void CppParser::parseInternal(ConversionData &cd, QSet<QString> &inclusions)
qWarning("%s:%d: Unbalanced opening parenthesis in C++ code"
" (or abuse of the C++ preprocessor)\n",
qPrintable(yyFileName), yyParenLineNo);
+ else if (yyBracketDepth != 0)
+ qWarning("%s:%d: Unbalanced opening bracket in C++ code"
+ " (or abuse of the C++ preprocessor)\n",
+ qPrintable(yyFileName), yyBracketLineNo);
}
const ParseResults *CppParser::recordResults(bool isHeader)