summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2009-06-25 19:01:24 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2009-07-02 11:48:20 (GMT)
commit6f673e2cf55755a97aeb8971994ab9fc62fb794e (patch)
treef075a45e9fd6a18cfde96fd28069b719be743532
parentfb51a10ee0451274a430227566ae26efb2ac4474 (diff)
downloadQt-6f673e2cf55755a97aeb8971994ab9fc62fb794e.zip
Qt-6f673e2cf55755a97aeb8971994ab9fc62fb794e.tar.gz
Qt-6f673e2cf55755a97aeb8971994ab9fc62fb794e.tar.bz2
Experimental: allow QSharedPointer to be used with forward declarations that are declared in this file.
The one-definition rule allows the forward declaration appearing below to apply to code that was earlier. Therefore, if the compiler finds out how to delete the object, we can allow a QSharedPointer of a forward- declared-type. This means the actual problem is just a warning with g++. To catch the error, we need a separate .cpp file and I'd rather run this as an external test.
-rw-r--r--src/corelib/tools/qsharedpointer_impl.h2
-rw-r--r--tests/auto/qsharedpointer/externaltests.cpp20
-rw-r--r--tests/auto/qsharedpointer/externaltests.h4
-rw-r--r--tests/auto/qsharedpointer/externaltests.pri1
-rw-r--r--tests/auto/qsharedpointer/forwarddeclaration.cpp52
-rw-r--r--tests/auto/qsharedpointer/forwarddeclared.cpp53
-rw-r--r--tests/auto/qsharedpointer/forwarddeclared.h54
-rw-r--r--tests/auto/qsharedpointer/qsharedpointer.pro9
-rw-r--r--tests/auto/qsharedpointer/tst_qsharedpointer.cpp42
9 files changed, 224 insertions, 13 deletions
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h
index cb78f29..2fa9eb2 100644
--- a/src/corelib/tools/qsharedpointer_impl.h
+++ b/src/corelib/tools/qsharedpointer_impl.h
@@ -129,7 +129,7 @@ namespace QtSharedPointer {
inline T *operator->() const { return data(); }
protected:
- inline Basic() : value(0 * sizeof(T)) { }
+ inline Basic() : value(0) { }
// ~Basic();
inline void verifyReconstruction(const T *ptr)
diff --git a/tests/auto/qsharedpointer/externaltests.cpp b/tests/auto/qsharedpointer/externaltests.cpp
index 75ac5f1..8077b84 100644
--- a/tests/auto/qsharedpointer/externaltests.cpp
+++ b/tests/auto/qsharedpointer/externaltests.cpp
@@ -59,7 +59,7 @@ static QString makespec()
{
static const char default_makespec[] = DEFAULT_MAKESPEC;
const char *p;
- for (p = default_makespec + sizeof(default_makespec); p >= default_makespec; --p)
+ for (p = default_makespec + sizeof(default_makespec) - 1; p >= default_makespec; --p)
if (*p == '/' || *p == '\\')
break;
@@ -122,6 +122,7 @@ namespace QTest {
enum Target { Compile, Link, Run };
QList<QByteArray> qmakeLines;
+ QStringList extraProgramSources;
QByteArray programHeader;
QExternalTest::QtModules qtModules;
QExternalTest::ApplicationType appType;
@@ -199,6 +200,16 @@ namespace QTest {
d->appType = type;
}
+ QStringList QExternalTest::extraProgramSources() const
+ {
+ return d->extraProgramSources;
+ }
+
+ void QExternalTest::setExtraProgramSources(const QStringList &extra)
+ {
+ d->extraProgramSources = extra;
+ }
+
QByteArray QExternalTest::programHeader() const
{
return d->programHeader;
@@ -483,6 +494,13 @@ namespace QTest {
else
projectFile.write("\nCONFIG += release\n");
+ QByteArray extraSources = QFile::encodeName(extraProgramSources.join(" "));
+ if (!extraSources.isEmpty()) {
+ projectFile.write("SOURCES += ");
+ projectFile.write(extraSources);
+ projectFile.putChar('\n');
+ }
+
// Add Qt modules
if (qtModules & QExternalTest::QtCore)
projectFile.write("QT += core\n");
diff --git a/tests/auto/qsharedpointer/externaltests.h b/tests/auto/qsharedpointer/externaltests.h
index 24a3236..ecc107e 100644
--- a/tests/auto/qsharedpointer/externaltests.h
+++ b/tests/auto/qsharedpointer/externaltests.h
@@ -45,6 +45,7 @@
#include <QList>
#include <QByteArray>
+#include <QStringList>
QT_BEGIN_NAMESPACE
namespace QTest {
@@ -102,6 +103,9 @@ namespace QTest {
ApplicationType applicationType() const;
void setApplicationType(ApplicationType type);
+ QStringList extraProgramSources() const;
+ void setExtraProgramSources(const QStringList &list);
+
QByteArray programHeader() const;
void setProgramHeader(const QByteArray &header);
diff --git a/tests/auto/qsharedpointer/externaltests.pri b/tests/auto/qsharedpointer/externaltests.pri
index 717acac..1fdcf65 100644
--- a/tests/auto/qsharedpointer/externaltests.pri
+++ b/tests/auto/qsharedpointer/externaltests.pri
@@ -1,4 +1,5 @@
SOURCES += $$PWD/externaltests.cpp
+HEADERS += $$PWD/externaltests.h
cleanedQMAKESPEC = $$replace(QMAKESPEC, \\\\, /)
DEFINES += DEFAULT_MAKESPEC=\\\"$$cleanedQMAKESPEC\\\"
diff --git a/tests/auto/qsharedpointer/forwarddeclaration.cpp b/tests/auto/qsharedpointer/forwarddeclaration.cpp
new file mode 100644
index 0000000..1dbbeec
--- /dev/null
+++ b/tests/auto/qsharedpointer/forwarddeclaration.cpp
@@ -0,0 +1,52 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://www.qtsoftware.com/contact.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#define QT_SHAREDPOINTER_TRACK_POINTERS
+#include "qsharedpointer.h"
+
+class ForwardDeclared;
+ForwardDeclared *forwardPointer();
+
+void externalForwardDeclaration()
+{
+ struct Wrapper { QSharedPointer<ForwardDeclared> pointer; };
+}
+
diff --git a/tests/auto/qsharedpointer/forwarddeclared.cpp b/tests/auto/qsharedpointer/forwarddeclared.cpp
new file mode 100644
index 0000000..4ab467a
--- /dev/null
+++ b/tests/auto/qsharedpointer/forwarddeclared.cpp
@@ -0,0 +1,53 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://www.qtsoftware.com/contact.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "forwarddeclared.h"
+
+ForwardDeclared *forwardPointer()
+{
+ return new ForwardDeclared;
+}
+
+int forwardDeclaredDestructorRunCount;
+ForwardDeclared::~ForwardDeclared()
+{
+ ++forwardDeclaredDestructorRunCount;
+}
diff --git a/tests/auto/qsharedpointer/forwarddeclared.h b/tests/auto/qsharedpointer/forwarddeclared.h
new file mode 100644
index 0000000..a4cc2b4
--- /dev/null
+++ b/tests/auto/qsharedpointer/forwarddeclared.h
@@ -0,0 +1,54 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at http://www.qtsoftware.com/contact.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef FORWARDDECLARED_H
+#define FORWARDDECLARED_H
+
+extern int forwardDeclaredDestructorRunCount;
+class ForwardDeclared
+{
+public:
+ ~ForwardDeclared();
+};
+
+ForwardDeclared *forwardPointer();
+
+#endif // FORWARDDECLARED_H
diff --git a/tests/auto/qsharedpointer/qsharedpointer.pro b/tests/auto/qsharedpointer/qsharedpointer.pro
index e329803..30c81cb 100644
--- a/tests/auto/qsharedpointer/qsharedpointer.pro
+++ b/tests/auto/qsharedpointer/qsharedpointer.pro
@@ -1,7 +1,8 @@
load(qttest_p4)
-
-SOURCES += tst_qsharedpointer.cpp
+SOURCES += tst_qsharedpointer.cpp \
+ forwarddeclaration.cpp \
+ forwarddeclared.cpp
QT = core
-DEFINES += SRCDIR=\\\"$$PWD\\\"
-
+DEFINES += SRCDIR=\\\"$$PWD/\\\"
include(externaltests.pri)
+HEADERS += forwarddeclared.h
diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
index d6321c7..dd53e3c 100644
--- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
+++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
@@ -225,16 +225,37 @@ void tst_QSharedPointer::basics()
}
class ForwardDeclared;
+ForwardDeclared *forwardPointer();
+void externalForwardDeclaration();
+extern int forwardDeclaredDestructorRunCount;
+
void tst_QSharedPointer::forwardDeclaration1()
{
- class Wrapper { QSharedPointer<ForwardDeclared> pointer; };
+ externalForwardDeclaration();
+
+ struct Wrapper { QSharedPointer<ForwardDeclared> pointer; };
+
+ forwardDeclaredDestructorRunCount = 0;
+ {
+ Wrapper w;
+ w.pointer = QSharedPointer<ForwardDeclared>(forwardPointer());
+ QVERIFY(!w.pointer.isNull());
+ }
+ QCOMPARE(forwardDeclaredDestructorRunCount, 1);
}
-class ForwardDeclared { };
+#include "forwarddeclared.h"
+
void tst_QSharedPointer::forwardDeclaration2()
{
- class Wrapper { QSharedPointer<ForwardDeclared> pointer; };
- Wrapper w;
+ forwardDeclaredDestructorRunCount = 0;
+ {
+ struct Wrapper { QSharedPointer<ForwardDeclared> pointer; };
+ Wrapper w1, w2;
+ w1.pointer = QSharedPointer<ForwardDeclared>(forwardPointer());
+ QVERIFY(!w1.pointer.isNull());
+ }
+ QCOMPARE(forwardDeclaredDestructorRunCount, 1);
}
void tst_QSharedPointer::memoryManagement()
@@ -1108,8 +1129,10 @@ void tst_QSharedPointer::invalidConstructs_data()
// use of forward-declared class
QTest::newRow("forward-declaration")
- << &QTest::QExternalTest::tryCompileFail
- << "QSharedPointer<ForwardDeclared> ptr;";
+ << &QTest::QExternalTest::tryRun
+ << "forwardDeclaredDestructorRunCount = 0;\n"
+ "{ QSharedPointer<ForwardDeclared> ptr = QSharedPointer<ForwardDeclared>(forwardPointer()); }\n"
+ "exit(forwardDeclaredDestructorRunCount);";
QTest::newRow("creating-forward-declaration")
<< &QTest::QExternalTest::tryCompileFail
<< "QSharedPointer<ForwardDeclared>::create();";
@@ -1216,6 +1239,7 @@ void tst_QSharedPointer::invalidConstructs()
QTest::QExternalTest test;
test.setDebugMode(true);
test.setQtModules(QTest::QExternalTest::QtCore);
+ test.setExtraProgramSources(QStringList() << SRCDIR "forwarddeclared.cpp");
test.setProgramHeader(
"#define QT_SHAREDPOINTER_TRACK_POINTERS\n"
"#include <QtCore/qsharedpointer.h>\n"
@@ -1223,7 +1247,11 @@ void tst_QSharedPointer::invalidConstructs()
"\n"
"struct Data { int i; };\n"
"struct DerivedData: public Data { int j; };\n"
- "struct ForwardDeclared;");
+ "\n"
+ "extern int forwardDeclaredDestructorRunCount;\n"
+ "struct ForwardDeclared;\n"
+ "ForwardDeclared *forwardPointer();\n"
+ );
QFETCH(QString, code);
static bool sane = true;