| 2599 | } |
| 2600 | |
| 2601 | void TestDUChain::testBitWidth() |
| 2602 | { |
| 2603 | #if CINDEX_VERSION_MINOR >= 16 |
| 2604 | TestFile file(QStringLiteral(R"( |
| 2605 | #include <climits> |
| 2606 | |
| 2607 | struct foo { |
| 2608 | int a; |
| 2609 | unsigned int b:1; |
| 2610 | unsigned char c:7; |
| 2611 | int d:32; |
| 2612 | int e[2]; |
| 2613 | unsigned length : (sizeof(int)*CHAR_BIT - 1); |
| 2614 | |
| 2615 | // error case |
| 2616 | int f:0; |
| 2617 | int g:-1; |
| 2618 | int h:33; |
| 2619 | };)"), QStringLiteral("cpp")); |
| 2620 | |
| 2621 | QVERIFY(file.parseAndWait()); |
| 2622 | { |
| 2623 | DUChainReadLocker lock; |
| 2624 | QVERIFY(file.topContext()); |
| 2625 | QCOMPARE(file.topContext()->localDeclarations().size(), 1); |
| 2626 | QCOMPARE(file.topContext()->childContexts().size(), 1); |
| 2627 | |
| 2628 | auto fooContext = file.topContext()->childContexts().first(); |
| 2629 | QVERIFY(fooContext); |
| 2630 | QCOMPARE(fooContext->type(), DUContext::Class); |
| 2631 | QCOMPARE(fooContext->localDeclarations().size(), 9); |
| 2632 | QCOMPARE(fooContext->childContexts().size(), 0); |
| 2633 | |
| 2634 | auto varA = dynamic_cast<ClassMemberDeclaration *>(fooContext->localDeclarations().at(0)); |
| 2635 | QVERIFY(varA); |
| 2636 | QCOMPARE(varA->bitWidth(), ClassMemberDeclaration::NotABitField); |
| 2637 | auto varB = dynamic_cast<ClassMemberDeclaration *>(fooContext->localDeclarations().at(1)); |
| 2638 | QVERIFY(varB); |
| 2639 | QCOMPARE(varB->bitWidth(), 1); |
| 2640 | auto varC = dynamic_cast<ClassMemberDeclaration *>(fooContext->localDeclarations().at(2)); |
| 2641 | QVERIFY(varC); |
| 2642 | QCOMPARE(varC->bitWidth(), 7); |
| 2643 | auto varD = dynamic_cast<ClassMemberDeclaration *>(fooContext->localDeclarations().at(3)); |
| 2644 | QVERIFY(varD); |
| 2645 | QCOMPARE(varD->bitWidth(), 32); |
| 2646 | auto varE = dynamic_cast<ClassMemberDeclaration *>(fooContext->localDeclarations().at(4)); |
| 2647 | QVERIFY(varE); |
| 2648 | QCOMPARE(varE->bitWidth(), ClassMemberDeclaration::NotABitField); |
| 2649 | const auto varLength = dynamic_cast<const ClassMemberDeclaration*>(fooContext->localDeclarations().at(5)); |
| 2650 | QVERIFY(varLength); |
| 2651 | QCOMPARE(varLength->bitWidth(), sizeof(int) * CHAR_BIT - 1); |
| 2652 | |
| 2653 | // error case |
| 2654 | auto top = file.topContext(); |
| 2655 | QVERIFY(top); |
| 2656 | QCOMPARE(top->problems().count(), 3); |
| 2657 | |
| 2658 | // error: named bit-field 'f' has zero width |
nothing calls this directly
no test coverage detected