File indexing completed on 2024-04-28 05:38:40

0001 // clang+++ test.cpp -I /usr/include/qt/ -fPIC -lQt5Core -c
0002 #include <vector>
0003 #include <QtCore/QList>
0004 #include <QtCore/QStringList>
0005 #include <QtCore/QMap>
0006 #include <QtGui/QRegion>
0007 #include <QtCore/QVector>
0008 #include <QtCore/QSequentialIterable>
0009 
0010 
0011 
0012 void test_detachment()
0013 {
0014     // Test #1: Detaching the foreach container
0015     QList<int> list;
0016     foreach (int i, list) {
0017         list.first();
0018     }
0019 }
0020 
0021 struct Trivial
0022 {
0023     int a;
0024 };
0025 Q_DECLARE_TYPEINFO(Trivial, Q_PRIMITIVE_TYPE);
0026 
0027 struct BigTrivial
0028 {
0029     int a, b, c, d, e;
0030     void constFoo() const {}
0031     void nonConstFoo() {}
0032 };
0033 
0034 struct SmallNonTrivial
0035 {
0036     int a;
0037     ~SmallNonTrivial() {}
0038 };
0039 
0040 extern void nop();
0041 extern void nop2(BigTrivial &); // non-const-ref
0042 extern void nop3(const BigTrivial &); // const-ref
0043 extern void nop4(BigTrivial *); // pointer
0044 
0045 void test_missing_ref()
0046 {
0047     QList<Trivial> trivials;
0048     QList<BigTrivial> bigTrivials;
0049     QList<SmallNonTrivial> smallNonTrivials;
0050 
0051     // Test #2: No warning
0052     foreach (Trivial t, trivials) {
0053         nop();
0054     }
0055 
0056     // Test #3: Warning
0057     foreach (BigTrivial t, bigTrivials) {
0058         nop();
0059     }
0060 
0061     // Test #4: Warning
0062     foreach (SmallNonTrivial t, smallNonTrivials) {
0063         nop();
0064     }
0065 
0066     // Test #5: Warning
0067     foreach (const BigTrivial t, bigTrivials) {
0068         t.constFoo();
0069     }
0070 
0071     // Test #6: No warning
0072     foreach (BigTrivial t, bigTrivials) {
0073         t.nonConstFoo();
0074     }
0075 
0076     // Test #7: No warning
0077     foreach (BigTrivial t, bigTrivials) {
0078         t = BigTrivial();
0079 
0080 
0081     }
0082 
0083     // Test #8: No warning
0084     foreach (BigTrivial t, bigTrivials) {
0085         nop2(t);
0086     }
0087 
0088     // Test #9: Warning
0089     foreach (BigTrivial t, bigTrivials) {
0090         nop3(t);
0091     }
0092 
0093     // Test #9: No warning
0094     foreach (BigTrivial t, bigTrivials) {
0095         nop4(&t);
0096     }
0097 }
0098 
0099 void testSTLForeach()
0100 {
0101     std::vector<int> v = {1, 2, 3, 4};
0102     foreach (int i, v) { // Warning
0103     }
0104 }
0105 
0106 void testQStringList()
0107 {
0108     QStringList sl;
0109     sl << QChar('A') << QChar('B');
0110     foreach (const QString &s, sl) { // no warning
0111     }
0112 }
0113 
0114 
0115 void testQMultiMapDetach()
0116 {
0117     QMultiMap<int,int> m;
0118 
0119 
0120 
0121     foreach (int i, m) {
0122         m.first();
0123     }
0124 }
0125 
0126 void testQRegionRects()
0127 {
0128     QRegion r;
0129     foreach (const QRect &rect, r.rects()) {}
0130 }
0131 
0132 using Foo = QVarLengthArray<QString>;
0133 
0134 void varLengthArray()
0135 {
0136     QVarLengthArray<int, 1> varray;
0137     foreach (auto i, varray) {}
0138 
0139     {
0140         Foo foo;
0141         Q_FOREACH(auto &&s, foo) {}
0142     }
0143     {
0144         const Foo foo;
0145         Q_FOREACH(auto &&s, foo) {}
0146     }
0147 }
0148 
0149 void testQSequentialIterable()
0150 {
0151     QVariant vlist;
0152     QSequentialIterable iterable = vlist.value<QSequentialIterable>();
0153     foreach (const QVariant &v, iterable) {}
0154 }
0155 
0156 void testMemberQList()
0157 {
0158     struct { QStringList list; } data;
0159     foreach (const QString &s, data.list) {};
0160 }