From a4e8dca0c3665a26ff5e7dffada97aeab86dd1a5 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Thu, 1 Aug 2013 12:05:47 +0200 Subject: [PATCH] avoid false positives when looking for magic comments expect meta data only at the beginning of statements. this required syntax fixes in some unrelated autotests. Task-number: QTBUG-11866 Change-Id: Ic0c0c150ac8648df6953a510b8eb0c199c8d3ae2 Reviewed-by: hjk Reviewed-by: Oswald Buddenhagen --- src/linguist/lupdate/cpp.cpp | 20 +++++++++++++++++++- .../testdata/good/multiple_locations/finddialog.cpp | 4 ++-- .../testdata/good/multiple_locations/main.cpp | 2 +- .../linguist/lupdate/testdata/good/parsecpp/main.cpp | 15 +++++++++++---- .../lupdate/testdata/good/parsecpp/project.ts.result | 5 +++++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/linguist/lupdate/cpp.cpp b/src/linguist/lupdate/cpp.cpp index bbac669..eb19c0d 100644 --- a/src/linguist/lupdate/cpp.cpp +++ b/src/linguist/lupdate/cpp.cpp @@ -329,6 +329,7 @@ private: // Parser state uint yyTok; + bool metaExpected; QString context; QString text; QString comment; @@ -829,6 +830,7 @@ uint CppParser::getToken() yyBraceDepth = yyMinBraceDepth; yyMinBraceDepth = 0; inDefine = false; + metaExpected = true; yyCh = getChar(); return Tok_Cancel; // Break out of any multi-token constructs } @@ -1627,6 +1629,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac #endif int line; bool yyTokColonSeen = false; // Start of c'tor's initializer list + metaExpected = true; yyWord.reserve(yyInStr.size()); // Rather insane. That's because we do no length checking. yyInPtr = (const ushort *)yyInStr.unicode(); @@ -1745,6 +1748,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac prospectiveContext.clear(); pendingContext.clear(); + metaExpected = true; yyTok = getToken(); } break; @@ -1763,6 +1767,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac functionContextUnresolved.clear(); prospectiveContext.clear(); pendingContext.clear(); + metaExpected = true; yyTok = getToken(); } else if (yyTok == Tok_Equals) { // e.g. namespace Is = OuterSpace::InnerSpace; @@ -1786,6 +1791,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac } else if (yyTok == Tok_LeftBrace) { // Anonymous namespace namespaceDepths.push(namespaces.count()); + metaExpected = true; yyTok = getToken(); } break; @@ -1935,6 +1941,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac extracomment.clear(); msgid.clear(); extra.clear(); + metaExpected = false; yyTok = getToken(); break; case Tok_translateUtf8: @@ -1991,6 +1998,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac extracomment.clear(); msgid.clear(); extra.clear(); + metaExpected = false; yyTok = getToken(); break; case Tok_trid: @@ -2009,6 +2017,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac extracomment.clear(); msgid.clear(); extra.clear(); + metaExpected = false; break; case Tok_Q_DECLARE_TR_FUNCTIONS: if (getMacroArgs()) { @@ -2032,6 +2041,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac if (yyTok == Tok_Ident && !yyParenDepth) prospectiveContext.clear(); } + metaExpected = false; break; case Tok_Arrow: yyTok = getToken(); @@ -2075,6 +2085,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac msgid.clear(); extra.clear(); } + metaExpected = true; yyTok = getToken(); break; case Tok_Access: @@ -2082,6 +2093,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac do { yyTok = getToken(); } while (yyTok == Tok_Access); // Multiple specifiers are possible, e.g. "public slots" + metaExpected = true; if (yyTok == Tok_Colon) goto case_default; break; @@ -2095,6 +2107,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac if (yyTok == Tok_Colon) yyTokColonSeen = true; } + metaExpected = true; yyTok = getToken(); break; case Tok_LeftBrace: @@ -2105,8 +2118,13 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac } yyTokColonSeen = false; // fallthrough + case Tok_Comma: case Tok_LeftParen: + metaExpected = true; + yyTok = getToken(); + break; case Tok_RightParen: + metaExpected = false; yyTok = getToken(); break; default: @@ -2137,7 +2155,7 @@ void CppParser::parseInternal(ConversionData &cd, const QStringList &includeStac void CppParser::processComment() { - if (!tor) + if (!tor || !metaExpected) return; const QChar *ptr = yyWord.unicode(); diff --git a/tests/auto/linguist/lupdate/testdata/good/multiple_locations/finddialog.cpp b/tests/auto/linguist/lupdate/testdata/good/multiple_locations/finddialog.cpp index 15f3ad4..cd9a64a 100644 --- a/tests/auto/linguist/lupdate/testdata/good/multiple_locations/finddialog.cpp +++ b/tests/auto/linguist/lupdate/testdata/good/multiple_locations/finddialog.cpp @@ -39,9 +39,9 @@ ** ****************************************************************************/ -QT_TRANSLATE_NOOP("context", "just a message") +QT_TRANSLATE_NOOP("context", "just a message"); //: This is one comment -QT_TRANSLATE_NOOP("context", "just a message") +QT_TRANSLATE_NOOP("context", "just a message"); diff --git a/tests/auto/linguist/lupdate/testdata/good/multiple_locations/main.cpp b/tests/auto/linguist/lupdate/testdata/good/multiple_locations/main.cpp index b501ff2..d06e8b2 100644 --- a/tests/auto/linguist/lupdate/testdata/good/multiple_locations/main.cpp +++ b/tests/auto/linguist/lupdate/testdata/good/multiple_locations/main.cpp @@ -43,7 +43,7 @@ //: This is a comment, too. -QT_TRANSLATE_NOOP("context", "just a message") +QT_TRANSLATE_NOOP("context", "just a message"); diff --git a/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp b/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp index 04e3b14..bbd29e3 100644 --- a/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp +++ b/tests/auto/linguist/lupdate/testdata/good/parsecpp/main.cpp @@ -191,12 +191,12 @@ class Testing : QObject { //: extra comment for NOOP //: which spans multiple lines -QT_TRANSLATE_NOOP("scope", "string") // 4.4 says the line of this is at the next statement +QT_TRANSLATE_NOOP("scope", "string") /*: complain & ignore */; // 4.4 says the line of this is at the next statement //: extra comment for NOOP3 -QT_TRANSLATE_NOOP3_UTF8("scope", "string", "comment") // 4.4 doesn't see this +QT_TRANSLATE_NOOP3_UTF8("scope", "string", "comment"); // 4.4 doesn't see this QT_TRANSLATE_NOOP("scope", "string " // this is an interleaved comment - "continuation on next line") + "continuation on next line"); class TestingTake17 : QObject { @@ -223,7 +223,7 @@ class TestingTake17 : QObject { //: again an extra comment, this time for id-based NOOP //% "This is supposed\tto be quoted \" newline\n" //% "backslashed \\ stuff." -QT_TRID_NOOP("this_a_id") +QT_TRID_NOOP("this_a_id"); //~ some thing //% "This needs to be here. Really." @@ -387,3 +387,10 @@ Class42::Class42() : { tr("and does that?"); } + + + +// QTBUG-11866: magic comment parsing is too greedy +Class42::hello(int something /*= 17 */, QString str = Class42::tr("eyo")) +{ +} 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 bea8445..83eb95e 100644 --- a/tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result +++ b/tests/auto/linguist/lupdate/testdata/good/parsecpp/project.ts.result @@ -46,6 +46,11 @@ backslashed \ stuff. and does that? + + + eyo + + Dialog2 -- 2.7.4