1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
|
/* This file is part of KDevelop
Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "test_expressionparser.h"
#include "testhelper.h"
#include <QtTest/QtTest>
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/topducontext.h>
#include "typeutils.h"
#include "declarationbuilder.h"
#include "usebuilder.h"
#include <language/duchain/declaration.h>
#include <language/editor/documentrange.h>
#include <language/duchain/dumpchain.h>
#include "cppeditorintegrator.h"
#include "dumptypes.h"
#include "environmentmanager.h"
#include "templatedeclaration.h"
#include "cppducontext.h"
#include <rpp/chartools.h>
#include "tokens.h"
#include "parsesession.h"
#include "rpp/preprocessor.h"
#include "expressionvisitor.h"
#include "expressionparser.h"
#include <language/duchain/classfunctiondeclaration.h>
#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <typeinfo>
using namespace KTextEditor;
using namespace KDevelop;
using namespace TypeUtils;
using namespace Cpp;
class MyExpressionVisitor : public Cpp::ExpressionVisitor {
public:
MyExpressionVisitor( ParseSession* session ) : ExpressionVisitor(session) {
}
protected:
virtual void expressionType( AST* node, const AbstractType::Ptr& type, Instance /*instance */) {
Cpp::DumpChain d;
kDebug(9007) << "expression-result for";
DUChainReadLocker lock( DUChain::lock() );
d.dump( node, session() );
kDebug(9007) << "is:" << type->toString();
}
};
QTEST_MAIN(TestExpressionParser)
char* debugString( const QString& str ) {
char* ret = new char[str.length()+1];
QByteArray b = str.toAscii();
strcpy( ret, b.data() );
return ret;
}
namespace QTest {
template<>
char* toString(const Cursor& cursor)
{
QByteArray ba = "Cursor(";
ba += QByteArray::number(cursor.line()) + ", " + QByteArray::number(cursor.column());
ba += ')';
return qstrdup(ba.data());
}
template<>
char* toString(const QualifiedIdentifier& id)
{
QByteArray arr = id.toString().toLatin1();
return qstrdup(arr.data());
}
template<>
char* toString(const Identifier& id)
{
QByteArray arr = id.toString().toLatin1();
return qstrdup(arr.data());
}
/*template<>
char* toString(QualifiedIdentifier::MatchTypes t)
{
QString ret;
switch (t) {
case QualifiedIdentifier::NoMatch:
ret = "No Match";
break;
case QualifiedIdentifier::Contains:
ret = "Contains";
break;
case QualifiedIdentifier::ContainedBy:
ret = "Contained By";
break;
case QualifiedIdentifier::ExactMatch:
ret = "Exact Match";
break;
}
QByteArray arr = ret.toString().toLatin1();
return qstrdup(arr.data());
}*/
template<>
char* toString(const Declaration& def)
{
QString s = QString("Declaration %1 (%2): %3").arg(def.identifier().toString()).arg(def.qualifiedIdentifier().toString()).arg(reinterpret_cast<long>(&def));
return qstrdup(s.toLatin1().constData());
}
template<>
char* toString(const TypePtr<AbstractType>& type)
{
QString s = QString("Type: %1").arg(type ? type->toString() : QString("<null>"));
return qstrdup(s.toLatin1().constData());
}
}
#define TEST_FILE_PARSE_ONLY if (testFileParseOnly) QSKIP("Skip", SkipSingle);
TestExpressionParser::TestExpressionParser()
{
testFileParseOnly = false;
}
void TestExpressionParser::initTestCase()
{
AutoTestShell::init();
TestCore* core = new TestCore();
core->initialize(KDevelop::Core::NoUi);
EnvironmentManager::init();
DUChain::self()->disablePersistentStorage();
}
void TestExpressionParser::cleanupTestCase()
{
}
Declaration* TestExpressionParser::findDeclaration(DUContext* context, const Identifier& id, const CursorInRevision& position)
{
QList<Declaration*> ret = context->findDeclarations(id, position);
if (ret.count())
return ret.first();
return 0;
}
Declaration* TestExpressionParser::findDeclaration(DUContext* context, const QualifiedIdentifier& id, const CursorInRevision& position)
{
QList<Declaration*> ret = context->findDeclarations(id, position);
if (ret.count())
return ret.first();
return 0;
}
void TestExpressionParser::testIntegralType() {
DUContext* top = parse(" ", DumpNone);
Cpp::ExpressionParser parser(true,true);
{
Cpp::ExpressionEvaluationResult result = parser.evaluateType("const char*", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
AbstractType::Ptr aType(result.type.abstractType());
PointerType::Ptr ptrType = aType.cast<PointerType>();
QVERIFY(ptrType);
QVERIFY(!(ptrType->modifiers() & PointerType::ConstModifier));
AbstractType::Ptr aBaseType = ptrType->baseType();
QVERIFY(aBaseType);
IntegralType::Ptr iBaseType = aBaseType.cast<IntegralType>();
QVERIFY(iBaseType);
QCOMPARE(iBaseType->dataType(), (uint)IntegralType::TypeChar);
QVERIFY(iBaseType->modifiers() & IntegralType::ConstModifier);
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateType("'x'", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
AbstractType::Ptr aType(result.type.abstractType());
ConstantIntegralType::Ptr ciType = aType.cast<ConstantIntegralType>();
QVERIFY(ciType);
QCOMPARE(ciType->dataType(), (uint)ConstantIntegralType::TypeChar);
QCOMPARE(ciType->value<char>(), 'x');
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateType("5", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
AbstractType::Ptr aType(result.type.abstractType());
ConstantIntegralType::Ptr ciType = aType.cast<ConstantIntegralType>();
QVERIFY(ciType);
QCOMPARE(ciType->dataType(), (uint)ConstantIntegralType::TypeInt);
QCOMPARE(ciType->value<int>(), 5);
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateType("true", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
AbstractType::Ptr aType(result.type.abstractType());
ConstantIntegralType::Ptr ciType = aType.cast<ConstantIntegralType>();
QVERIFY(ciType);
QCOMPARE(ciType->dataType(), (uint)ConstantIntegralType::TypeBoolean);
QCOMPARE(ciType->value<bool>(), true);
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateType("'\\n'", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
AbstractType::Ptr aType(result.type.abstractType());
ConstantIntegralType::Ptr ciType = aType.cast<ConstantIntegralType>();
QVERIFY(ciType);
QCOMPARE(ciType->dataType(), (uint)ConstantIntegralType::TypeChar);
QCOMPARE(ciType->value<char>(), '\n');
}
}
void TestExpressionParser::testTemplatesSimple() {
QByteArray method("template<class T> T test(const T& t) {}; template<class T, class T2> class A { }; class B{}; class C{}; typedef A<B,C> B;");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock(DUChain::lock());
Declaration* defClassA = top->localDeclarations()[1];
QCOMPARE(defClassA->identifier(), Identifier("A"));
QVERIFY(defClassA->type<CppClassType>());
Declaration* defClassB = top->localDeclarations()[2];
QCOMPARE(defClassB->identifier(), Identifier("B"));
QVERIFY(defClassB->type<CppClassType>());
Declaration* defClassC = top->localDeclarations()[3];
QCOMPARE(defClassC->identifier(), Identifier("C"));
QVERIFY(defClassC->type<CppClassType>());
DUContext* classA = defClassA->logicalInternalContext(0);
QVERIFY(classA->parentContext());
QCOMPARE(classA->importedParentContexts().count(), 1); //The template-parameter context is imported
QCOMPARE(classA->localScopeIdentifier(), QualifiedIdentifier("A"));
DUContext* classB = defClassB->logicalInternalContext(0);
QVERIFY(classB->parentContext());
QCOMPARE(classB->importedParentContexts().count(), 0);
QCOMPARE(classB->localScopeIdentifier(), QualifiedIdentifier("B"));
DUContext* classC = defClassC->logicalInternalContext(0);
QVERIFY(classC->parentContext());
QCOMPARE(classC->importedParentContexts().count(), 0);
QCOMPARE(classC->localScopeIdentifier(), QualifiedIdentifier("C"));
release(top);
}
void TestExpressionParser::testTemplates() {
QByteArray method("class A{}; template<class T, int val> struct Container {typedef Container<T> SelfType; enum{ Value = val }; T member; T operator*() const {}; }; Container<A> c;");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock(DUChain::lock());
Declaration* defClassA = top->localDeclarations()[0];
QCOMPARE(defClassA->identifier(), Identifier("A"));
QVERIFY(defClassA->type<CppClassType>());
Declaration* container = top->localDeclarations()[1];
QCOMPARE(container->identifier(), Identifier("Container"));
QVERIFY(container->type<CppClassType>());
Cpp::TemplateDeclaration* templateDecl = dynamic_cast<Cpp::TemplateDeclaration*>(container);
QVERIFY(templateDecl);
Cpp::ExpressionParser parser;
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("c.member", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<CppClassType>());
QVERIFY(result.allDeclarationsSize());
AbstractType::Ptr t(result.type.abstractType());
QVERIFY(dynamic_cast<IdentifiedType*>(t.unsafeData()));
QCOMPARE(dynamic_cast<IdentifiedType*>(t.unsafeData())->declaration(0), defClassA);
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("*c", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<CppClassType>());
QVERIFY(result.allDeclarationsSize());
AbstractType::Ptr t(result.type.abstractType());
QVERIFY(dynamic_cast<IdentifiedType*>(t.unsafeData()));
QCOMPARE(dynamic_cast<IdentifiedType*>(t.unsafeData())->declaration(0), defClassA);
}
release(top);
}
void TestExpressionParser::testArray() {
QByteArray method("struct Bla { int val; } blaArray[5+3];");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock(DUChain::lock());
Cpp::ExpressionParser parser;
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("Bla", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<CppClassType>());
QCOMPARE(result.toString(), QString("Bla"));
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("blaArray", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
kDebug() << result.toString();
QVERIFY(result.type.type<ArrayType>());
QCOMPARE(result.type.type<ArrayType>()->dimension(), 8);
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("blaArray[5].val", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
AbstractType::Ptr t(result.type.abstractType());
QVERIFY(dynamic_cast<KDevelop::IntegralType*>(t.unsafeData()));
}
release(top);
}
void TestExpressionParser::testDynamicArray() {
QByteArray method("struct Bla { int val; } blaArray[] = { {5} };");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock(DUChain::lock());
Cpp::ExpressionParser parser;
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("Bla", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<CppClassType>());
QCOMPARE(result.toString(), QString("Bla"));
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("blaArray", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
kDebug() << result.toString();
QVERIFY(result.type.type<ArrayType>());
}
{
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("blaArray[5].val", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<KDevelop::IntegralType>());
}
release(top);
}
void TestExpressionParser::testTemplates2()
{
QByteArray method("class C { class A; template<class T> T test() {} }; class B { class A; void test() {C c; } }; ");
DUContext* top = parse(method, DumpAll);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(top->childContexts().count(), 2);
QCOMPARE(top->childContexts()[1]->childContexts().size(), 2);
{
Cpp::ExpressionParser parser(false, true);;
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("c.test<A>()", KDevelop::DUContextPointer(top->childContexts()[1]->childContexts()[1]));
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
kDebug() << result.toString();
QCOMPARE(result.type.abstractType()->toString(), QString("B::A"));
}
release(top);
}
void TestExpressionParser::testSmartPointer() {
QByteArray method("template<class T> struct SmartPointer { T* operator ->() const {}; template<class Target> SmartPointer<Target> cast() {}; T& operator*() {}; } ; class B{int i;}; class C{}; SmartPointer<B> bPointer;");
//QByteArray method("template<class T> struct SmartPointer { template<class Target> void cast() {}; } ; ");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock(DUChain::lock());
Cpp::ExpressionParser parser;
QVERIFY(top->localDeclarations()[0]->internalContext());
AbstractType::Ptr t(top->localDeclarations()[3]->abstractType());
IdentifiedType* idType = dynamic_cast<IdentifiedType*>(t.unsafeData());
QVERIFY(idType);
QCOMPARE(idType->declaration(0)->context(), KDevelop::DUContextPointer(top).data());
QVERIFY(idType->declaration(0)->internalContext() != top->localDeclarations()[0]->internalContext());
Declaration* baseDecl = top->localDeclarations()[0];
Declaration* specialDecl = idType->declaration(0);
TemplateDeclaration* baseTemplateDecl = dynamic_cast<TemplateDeclaration*>(baseDecl);
TemplateDeclaration* specialTemplateDecl = dynamic_cast<TemplateDeclaration*>(specialDecl);
QVERIFY(baseTemplateDecl);
QVERIFY(specialTemplateDecl);
QVERIFY(specialTemplateDecl->isInstantiatedFrom(baseTemplateDecl));
QVERIFY(specialDecl->internalContext() != baseDecl->internalContext());
QCOMPARE(specialDecl->internalContext()->importedParentContexts().count(), 1); //Only the template-contexts are imported
QCOMPARE(baseDecl->internalContext()->importedParentContexts().count(), 1);
DUContext* specialTemplateContext = specialDecl->internalContext()->importedParentContexts().first().context(0);
DUContext* baseTemplateContext = baseDecl->internalContext()->importedParentContexts().first().context(0);
QVERIFY(specialTemplateContext != baseTemplateContext);
QCOMPARE(specialTemplateContext->type(), DUContext::Template);
QCOMPARE(baseTemplateContext->type(), DUContext::Template);
kDebug(9007) << typeid(baseTemplateContext).name();
kDebug(9007) << typeid(specialTemplateContext).name();
/* CppDUContext<DUContext>* baseTemplateCtx = dynamic_cast<CppDUContext<DUContext>*>(baseTemplateContext);
CppDUContext<DUContext>* specialTemplateCtx = dynamic_cast<CppDUContext<DUContext>*>(specialTemplateContext);
QVERIFY(baseTemplateCtx);
QVERIFY(specialTemplateCtx);
QCOMPARE(specialTemplateCtx->instantiatedFrom(), baseTemplateContext);*/
QCOMPARE(specialTemplateContext->localDeclarations().count(), 1);
QCOMPARE(baseTemplateContext->localDeclarations().count(), 1);
QVERIFY(specialTemplateContext->localDeclarations()[0] != baseTemplateContext->localDeclarations()[0]);
kDebug(9007) << top->localDeclarations()[3]->abstractType()->toString();
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression( "*bPointer", KDevelop::DUContextPointer(top));
QVERIFY(result.isInstance);
QCOMPARE(result.type.abstractType()->toString(), QString("B&"));
result = parser.evaluateExpression( "bPointer->i", KDevelop::DUContextPointer(top));
QVERIFY(result.isInstance);
QCOMPARE(result.type.abstractType()->toString(), QString("int"));
/* result = parser.evaluateExpression( "bPointer->cast<B>()->i", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QCOMPARE(result.type.abstractType()->toString(), QString("int"));*/
release(top);
}
void TestExpressionParser::testAutoTemplate()
{
QByteArray code = "struct C { float m(); }; C c; template<class T> auto foo(T n) -> decltype(n.m()); template<class T> auto foo2(T n) -> decltype(n);";
LockedTopDUContext top = parse(code);
QVERIFY(top);
DUChainReadLocker lock;
QCOMPARE(top->localDeclarations().size(), 4);
FunctionType::Ptr fType = top->localDeclarations()[2]->abstractType().cast<FunctionType>();
QVERIFY(fType);
QVERIFY(fType->returnType());
DelayedType::Ptr ret = fType->returnType().cast<DelayedType>();
QVERIFY(ret);
QVERIFY(ret->kind() == DelayedType::Delayed);
Cpp::ExpressionParser parser;
ExpressionEvaluationResult res = parser.evaluateExpression("foo(c)", DUContextPointer(top));
QVERIFY(res.isValid());
QCOMPARE(res.type.abstractType()->toString(), QString("float"));
res = parser.evaluateExpression("foo2(c)", DUContextPointer(top));
QVERIFY(res.isValid());
QCOMPARE(res.type.abstractType()->toString(), QString("C"));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TestExpressionParser::testSimpleExpression() {
TEST_FILE_PARSE_ONLY
QByteArray test = "struct Cont { int& a; Cont* operator -> () {}; double operator*(); }; Cont c; Cont* d = &c; void test() { c.a = 5; d->a = 5; (*d).a = 5; c.a(5, 1, c); c.b<Fulli>(); }";
DUContext* c = parse( test, DumpNone /*DumpDUChain | DumpAST */);
DUChainWriteLocker lock(DUChain::lock());
DUContext* testContext = c->childContexts()[1];
QCOMPARE( testContext->type(), DUContext::Function );
//Make sure the declaration of "c" is found correctly
Declaration* d = findDeclaration( testContext, QualifiedIdentifier("c") );
QVERIFY(d);
AbstractType::Ptr t(d->abstractType());
QVERIFY( dynamic_cast<IdentifiedType*>( t.unsafeData() ) );
QVERIFY( dynamic_cast<IdentifiedType*>( t.unsafeData() )->qualifiedIdentifier().toString() == "Cont" );
kDebug(9007) << "test-Context:" << testContext;
lock.unlock();
Cpp::ExpressionParser parser;
Cpp::ExpressionEvaluationResult result;
result = parser.evaluateType( "decltype(1+2)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int"));
QVERIFY(!result.isInstance);
lock.unlock();
result = parser.evaluateType( "decltype(c)()", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont"));
QVERIFY(!result.isInstance);
lock.unlock();
result = parser.evaluateType( "const Cont", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("const Cont"));
QVERIFY(!result.isInstance);
lock.unlock();
result = parser.evaluateExpression( "Cont", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont"));
QVERIFY(!result.isInstance);
lock.unlock();
result = parser.evaluateExpression( "c.a", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
lock.unlock();
result = parser.evaluateExpression( "d", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QCOMPARE(result.type.abstractType()->toString(), QString("Cont*"));
QCOMPARE(result.type.type<PointerType>()->baseType()->toString(), QString("Cont"));
lock.unlock();
//Test pointer-referencing
result = parser.evaluateExpression( "&c.a", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int*"));
QVERIFY(result.isInstance);
lock.unlock();
//Test pointer-referencing and dereferencing
result = parser.evaluateExpression( "*(&c.a)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int"));
kDebug() << result.toString();
QVERIFY(result.isInstance);
lock.unlock();
//Test overloaded "operator*"
result = parser.evaluateExpression( "*c", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("double"));
QVERIFY(result.isInstance);
lock.unlock();
//Test overloaded "operator->"
result = parser.evaluateExpression( "c->a", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int&"));
QVERIFY(result.isInstance);
lock.unlock();
//Test normal pointer-access + assign expression
result = parser.evaluateExpression( "d->a = 5", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int&"));
QVERIFY(result.isInstance);
lock.unlock();
//Test double * (one real, one overloaded)
result = parser.evaluateExpression( "**d", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("double"));
QVERIFY(result.isInstance);
lock.unlock();
//Test double &
result = parser.evaluateExpression( "&d", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont**"));
QVERIFY(result.isInstance);
lock.unlock();
//Test type-expression
result = parser.evaluateExpression( "Cont", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(!result.isInstance);
QVERIFY(result.type.abstractType());
lock.unlock();
//Test conditional expression
result = parser.evaluateExpression( "d ? c.a : c.a", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int&"));
QVERIFY(result.isInstance);
lock.unlock();
result = parser.evaluateExpression( "\"hello\"", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString().trimmed(), QString("const char*").trimmed());
QVERIFY(result.isInstance);
lock.unlock();
result = parser.evaluateExpression( "sizeof(Cont)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int"));
QVERIFY(result.isInstance);
lock.unlock();
result = parser.evaluateExpression( "new Cont()", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont*"));
QVERIFY(result.isInstance);
lock.unlock();
result = parser.evaluateExpression( "5", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("5"));
QVERIFY(result.isInstance);
QVERIFY(result.type.type<ConstantIntegralType>());
lock.unlock();
QVERIFY(!TypeUtils::isNullType(result.type.abstractType()));
result = parser.evaluateExpression( "5.5", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("5.5"));
QVERIFY(result.isInstance);
QVERIFY(result.type.type<ConstantIntegralType>());
lock.unlock();
QVERIFY(!TypeUtils::isNullType(result.type.abstractType()));
result = parser.evaluateExpression( "5.5 + 1.5", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("7"));
QVERIFY(result.isInstance);
QVERIFY(result.type.type<ConstantIntegralType>());
lock.unlock();
QVERIFY(!TypeUtils::isNullType(result.type.abstractType()));
result = parser.evaluateExpression( "3 + 0.5", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("3.5"));
QVERIFY(result.isInstance);
QVERIFY(result.type.type<ConstantIntegralType>());
lock.unlock();
QVERIFY(!TypeUtils::isNullType(result.type.abstractType()));
result = parser.evaluateExpression( "3u + 0.5", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("3.5"));
QVERIFY(result.isInstance);
QVERIFY(result.type.type<ConstantIntegralType>());
lock.unlock();
QVERIFY(!TypeUtils::isNullType(result.type.abstractType()));
result = parser.evaluateExpression( "0", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("0"));
QVERIFY(result.isInstance);
QVERIFY(result.type.type<ConstantIntegralType>());
lock.unlock();
QVERIFY(TypeUtils::isNullType(result.type.abstractType()));
lock.lock();
release(c);
}
void TestExpressionParser::testThis() {
TEST_FILE_PARSE_ONLY
QByteArray text("class A{ void test() { } void test2() const { }; void extTest(); }; void A::extTest() {}");
DUContext* top = parse( text, DumpNone);//DumpNone /*DumpDUChain | DumpAST */);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(top->childContexts().count(), 3);
QCOMPARE(top->childContexts()[0]->type(), DUContext::Class);
QCOMPARE(top->childContexts()[0]->childContexts().count(), 5);
DUContext* testContext = top->childContexts()[0]->childContexts()[1];
QCOMPARE(testContext->type(), DUContext::Other);
QVERIFY(testContext->owner());
DUContext* test2Context = top->childContexts()[0]->childContexts()[3];
QCOMPARE(test2Context->type(), DUContext::Other);
QVERIFY(test2Context->owner());
Cpp::ExpressionParser parser;
Cpp::ExpressionEvaluationResult result1 = parser.evaluateExpression( "this", KDevelop::DUContextPointer(testContext));
QVERIFY(result1.isValid());
QVERIFY(result1.type);
QVERIFY(result1.isInstance);
QCOMPARE(result1.type.abstractType()->toString(), QString("A*"));
Cpp::ExpressionEvaluationResult result2 = parser.evaluateExpression( "this", KDevelop::DUContextPointer(test2Context));
QVERIFY(result2.isValid());
QVERIFY(result2.type);
QVERIFY(result2.isInstance);
QCOMPARE(result2.type.abstractType()->toString().trimmed(), QString("A* const"));
DUContext* extTestCtx = top->childContexts()[2];
QCOMPARE(extTestCtx->type(), DUContext::Other);
QVERIFY(extTestCtx->owner());
Cpp::ExpressionEvaluationResult result3 = parser.evaluateExpression( "this", KDevelop::DUContextPointer(extTestCtx));
QVERIFY(result3.isValid());
QVERIFY(result3.type);
QVERIFY(result3.isInstance);
QCOMPARE(result3.type.abstractType()->toString(), QString("A*"));
release(top);
}
void TestExpressionParser::testBaseClasses() {
TEST_FILE_PARSE_ONLY
}
void TestExpressionParser::testTypeConversion2() {
TEST_FILE_PARSE_ONLY
QByteArray test = "const int& i; int q; unsigned int qq; int* ii; int* const iii;";
DUContext* c = parse( test, DumpNone /*DumpDUChain | DumpAST */);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(c->localDeclarations().count(), 5);
TypeConversion tc(c->topContext());
QVERIFY(tc.implicitConversion(c->localDeclarations()[1]->indexedType(), c->localDeclarations()[0]->indexedType()));
QVERIFY(tc.implicitConversion(c->localDeclarations()[2]->indexedType(), c->localDeclarations()[0]->indexedType()));
QVERIFY(tc.implicitConversion(c->localDeclarations()[1]->indexedType(), c->localDeclarations()[0]->indexedType(), false));
QVERIFY(tc.implicitConversion(c->localDeclarations()[2]->indexedType(), c->localDeclarations()[0]->indexedType(), false));
//Only the pointed-to part must be const-compatible
QVERIFY(tc.implicitConversion(c->localDeclarations()[4]->indexedType(), c->localDeclarations()[3]->indexedType()));
//QVERIFY(0);
//lock.lock();
release(c);
}
void TestExpressionParser::testTypeConversionWithTypedefs() {
TEST_FILE_PARSE_ONLY
QByteArray test = "const int i; typedef int q; typedef unsigned int qq; int* ii; int* const iii;";
DUContext* c = parse( test, DumpNone /*DumpDUChain | DumpAST */);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(c->localDeclarations().count(), 5);
TypeConversion tc(c->topContext());
QVERIFY(tc.implicitConversion(c->localDeclarations()[1]->indexedType(), c->localDeclarations()[0]->indexedType()));
QVERIFY(tc.implicitConversion(c->localDeclarations()[2]->indexedType(), c->localDeclarations()[0]->indexedType()));
QVERIFY(tc.implicitConversion(c->localDeclarations()[1]->indexedType(), c->localDeclarations()[2]->indexedType()));
QVERIFY(tc.implicitConversion(c->localDeclarations()[2]->indexedType(), c->localDeclarations()[1]->indexedType()));
QVERIFY(tc.implicitConversion(c->localDeclarations()[1]->indexedType(), c->localDeclarations()[0]->indexedType(), false));
QVERIFY(tc.implicitConversion(c->localDeclarations()[2]->indexedType(), c->localDeclarations()[0]->indexedType(), false));
//Only the pointed-to part must be const-compatible
QVERIFY(tc.implicitConversion(c->localDeclarations()[4]->indexedType(), c->localDeclarations()[3]->indexedType()));
//QVERIFY(0);
//lock.lock();
release(c);
}
void TestExpressionParser::testTypeConversion() {
TEST_FILE_PARSE_ONLY
QByteArray test = "struct Cont { operator int() {}; }; void test( int c = 5 ) { this->test( Cont(), 1, 5.5, 6); }";
DUContext* c = parse( test, DumpNone /*DumpDUChain | DumpAST */);
DUChainWriteLocker lock(DUChain::lock());
DUContext* testContext = c->childContexts()[1];
QCOMPARE( testContext->type(), DUContext::Function );
DUContext* contContext = c->childContexts()[0];
Declaration* decl = contContext->localDeclarations()[0];
QCOMPARE(decl->identifier(), Identifier("operator{...cast...}"));
KDevelop::FunctionType::Ptr function = decl->type<KDevelop::FunctionType>();
QCOMPARE(function->returnType()->toString(), QString("int"));
Declaration* testDecl = c->localDeclarations()[1];
AbstractFunctionDeclaration* functionDecl = dynamic_cast<AbstractFunctionDeclaration*>(testDecl);
QVERIFY(functionDecl);
QVERIFY(functionDecl->defaultParametersSize() == 1);
QCOMPARE(functionDecl->defaultParameters()[0].str(), QString("5"));
//QVERIFY(0);
//lock.lock();
release(c);
}
void TestExpressionParser::testEnum() {
TEST_FILE_PARSE_ONLY
QByteArray test = "enum Honk { Hank, Hunk }; void test() {}";
DUContext* c = parse( test, DumpNone /*DumpDUChain | DumpAST */);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(c->childContexts().size(), 3);
lock.unlock();
Cpp::ExpressionParser parser;
//Reenable this once the type-parsing system etc. is fixed
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression( "Hank", KDevelop::DUContextPointer(c));
lock.lock();
kDebug() << typeid(*result.type.abstractType()).name();
QVERIFY(result.type.type<EnumeratorType>());
//QVERIFY(0);
//lock.lock();
release(c);
}
void TestExpressionParser::testCasts() {
TEST_FILE_PARSE_ONLY
QByteArray test = "struct Cont2 {}; struct Cont { int& a; Cont* operator -> () {}; double operator*(); }; Cont c; Cont* d = &c; void test() { c.a = 5; d->a = 5; (*d).a = 5; c.a(5, 1, c); c(); c.a = dynamic_cast<const Cont2*>(d); }";
DUContext* c = parse( test, DumpNone );
DUChainWriteLocker lock(DUChain::lock());
DUContext* testContext = c->childContexts()[2];
QCOMPARE( testContext->type(), DUContext::Function );
//Make sure the declaration of "c" is found correctly
Declaration* d = findDeclaration( testContext, QualifiedIdentifier("c") );
QVERIFY(d);
AbstractType::Ptr t(d->abstractType());
QVERIFY( dynamic_cast<IdentifiedType*>( t.unsafeData() ) );
QVERIFY( dynamic_cast<IdentifiedType*>( t.unsafeData() )->qualifiedIdentifier().toString() == "Cont" );
lock.unlock();
Cpp::ExpressionParser parser;
//Reenable this once the type-parsing system etc. is fixed
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression( "(Cont2*)(d)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2*"));
lock.unlock();
result = parser.evaluateExpression( "dynamic_cast<Cont2*>(d)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2*"));
lock.unlock();
result = parser.evaluateExpression( "static_cast<Cont2*>(d)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2*"));
lock.unlock();
result = parser.evaluateExpression( "reinterpret_cast<Cont2*>(d)", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2*"));
lock.unlock();
result = parser.evaluateExpression( "new Cont2*", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2**"));
lock.unlock();
result = parser.evaluateExpression( "new Cont2", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2*"));
lock.unlock();
result = parser.evaluateExpression( "new Cont2[5]", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2*"));
lock.unlock();
result = parser.evaluateExpression( "(int*)new Cont2[5]", KDevelop::DUContextPointer(testContext));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("int*"));
lock.unlock();
lock.lock();
release(c);
}
void TestExpressionParser::testOperators() {
TEST_FILE_PARSE_ONLY
QByteArray test = "struct Cont2 {int operator[] {}; operator()() {};}; struct Cont3{}; struct Cont { Cont3 operator[](int i) {} Cont3 operator()() {} Cont3 operator+(const Cont3& c3 ) {} }; Cont c; Cont2 operator+( const Cont& c, const Cont& c2){} Cont3 c3;";
DUContext* ctx = parse( test, DumpNone );
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(ctx->childContexts().count(), 5);
QCOMPARE(ctx->localDeclarations().count(), 6);
Declaration* cont2Dec = ctx->localDeclarations()[0];
Declaration* cont3Dec = ctx->localDeclarations()[1];
Declaration* contDec = ctx->localDeclarations()[2];
Declaration* cDec = ctx->localDeclarations()[3];
Declaration* plusDec = ctx->localDeclarations()[4];
QVERIFY(cont2Dec->internalContext());
QVERIFY(cont3Dec->internalContext());
QVERIFY(contDec->internalContext());
QVERIFY(!cDec->internalContext());
QVERIFY(plusDec->internalContext());
QVERIFY(findDeclaration(ctx, QualifiedIdentifier("Cont::operator()")));
QVERIFY(findDeclaration(ctx, QualifiedIdentifier("Cont::operator()"))->type<KDevelop::FunctionType>());
CppClassType::Ptr cont2 = cont2Dec->type<CppClassType>();
CppClassType::Ptr cont3 = cont3Dec->type<CppClassType>();
CppClassType::Ptr cont = contDec->type<CppClassType>();
QVERIFY(cont2);
QVERIFY(cont3);
QVERIFY(cont);
lock.unlock();
Cpp::ExpressionParser parser;
//Reenable this once the type-parsing system etc. is fixed
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression( "c+c", KDevelop::DUContextPointer(ctx));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont2"));
lock.unlock();
result = parser.evaluateExpression( "c+c3", KDevelop::DUContextPointer(ctx));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont3"));
lock.unlock();
result = parser.evaluateExpression( "c()", KDevelop::DUContextPointer(ctx));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont3"));
lock.unlock();
result = parser.evaluateExpression( "c[5]", KDevelop::DUContextPointer(ctx));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont3"));
lock.unlock();
//A simple test: Constructing a type should always result in the type, no matter whether there is a constructor.
result = parser.evaluateExpression( "Cont(5)", KDevelop::DUContextPointer(ctx));
lock.lock();
QVERIFY(result.isValid());
QVERIFY(result.isInstance);
QVERIFY(result.type.abstractType());
QCOMPARE(result.type.abstractType()->toString(), QString("Cont"));
lock.unlock();
lock.lock();
release(ctx);
}
void TestExpressionParser::testTemplateFunctions() {
{
QByteArray method("template<class T> T test(T t) {}");
DUContext* top = parse(method, DumpAll);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(top->localDeclarations().count(), 1);
QCOMPARE(top->usesCount(), 1);
Cpp::ExpressionParser parser(false, true);
Cpp::ExpressionEvaluationResult result;
result = parser.evaluateExpression( "test<int>()", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<IntegralType>());
result = parser.evaluateExpression( "test(5)", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type.type<IntegralType>());
release(top);
}
{
QByteArray method("template<class T> class A { template<class T> T a(const T& q) {}; }; class C{}; template<class T> T a(T& q) {}; template<class T> T b(A<T> q) {}; template<class T> T c(const A<T*&>& q) {}; A<int>* aClass;");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock(DUChain::lock());
QCOMPARE(top->localDeclarations().count(), 6);
Declaration* d = findDeclaration(top, QualifiedIdentifier("a<A>"));
QVERIFY(d);
KDevelop::FunctionType::Ptr cppFunction = d->type<KDevelop::FunctionType>();
QVERIFY(cppFunction);
QCOMPARE(cppFunction->arguments().count(), 1);
QCOMPARE(cppFunction->returnType()->indexed(), top->localDeclarations()[0]->indexedType());
QCOMPARE(cppFunction->arguments()[0]->toString(), QString("A&"));
Cpp::ExpressionParser parser(false, true);
Cpp::ExpressionEvaluationResult result;
result = parser.evaluateExpression( "a(C())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[1]->abstractType()->indexed());
result = parser.evaluateExpression( "b<C>(A<C>())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[1]->abstractType()->indexed());
result = parser.evaluateExpression( "b(A<C>())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
kDebug() << result.toString();
/* IdentifiedType* identified = dynamic_cast<IdentifiedType*>(result.type.abstractType().data());
Q_ASSERT(identified);*/
//kDebug() << "identified:" << identified->qualifiedIdentifier();
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[1]->abstractType()->indexed());
result = parser.evaluateExpression( "a<A>(A())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[0]->abstractType()->indexed());
result = parser.evaluateExpression( "A<C>()", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
result = parser.evaluateExpression( "aClass->a<A>(A())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[0]->abstractType()->indexed());
///@todo find out why this doesn't work
// result = parser.evaluateExpression( "aClass->a(A())", KDevelop::DUContextPointer(top));
// QVERIFY(result.isValid());
// QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[0]->abstractType()->indexed());
// kDebug() << result.type.abstractType()->toString();
result = parser.evaluateExpression( "a(A())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[0]->abstractType()->indexed());
//This test will succeed again once we have a working type repository!
result = parser.evaluateExpression( "c(A<C*&>())", KDevelop::DUContextPointer(top));
QVERIFY(result.isValid());
kDebug() << result.toString();
QCOMPARE(result.type.abstractType()->indexed(), top->localDeclarations()[1]->abstractType()->indexed());
release(top);
}
}
void TestExpressionParser::testTypeID()
{
QByteArray method("namespace std { struct type_info{ const char* name(); }; }\nvoid f() {float f;}");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock;
QCOMPARE(top->localDeclarations().count(), 2);
QCOMPARE(top->childContexts().size(), 3);
DUContextPointer fBody(top->childContexts().last());
Declaration* tiDec = top->findDeclarations(QualifiedIdentifier("::std::type_info")).first();
Cpp::ExpressionParser parser(false, true);
Cpp::ExpressionEvaluationResult result;
result = parser.evaluateExpression("typeid(int)", fBody);
QVERIFY(result.isValid());
QVERIFY(result.type.type<StructureType>());
QVERIFY(result.type.abstractType()->equals(tiDec->abstractType().constData()));
result = parser.evaluateExpression("typeid(5)", fBody);
QVERIFY(result.isValid());
QVERIFY(result.type.type<StructureType>());
QVERIFY(result.type.abstractType()->equals(tiDec->abstractType().constData()));
result = parser.evaluateExpression("typeid(f)", fBody);
QVERIFY(result.isValid());
QVERIFY(result.type.type<StructureType>());
QVERIFY(result.type.abstractType()->equals(tiDec->abstractType().constData()));
result = parser.evaluateExpression("typeid(f).name()", fBody);
QVERIFY(result.isValid());
QVERIFY(result.type.type<PointerType>());
QVERIFY(result.type.type<PointerType>()->baseType().cast<IntegralType>());
QCOMPARE(result.type.type<PointerType>()->baseType().cast<IntegralType>()->dataType(), (uint)IntegralType::TypeChar);
}
void TestExpressionParser::testConstness()
{
QByteArray method("struct A { int x; }; const A* a = new A;");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock;
QCOMPARE(top->localDeclarations().count(), 2);
QCOMPARE(top->childContexts().count(), 1);
// "a" is a pointer type, which itself is not const, but it points to const data
Declaration* a = top->localDeclarations().last();
QVERIFY(!TypeUtils::isConstant(a->abstractType()));
QVERIFY(a->abstractType().cast<PointerType>());
QVERIFY(TypeUtils::isConstant(a->abstractType().cast<PointerType>()->baseType()));
Cpp::ExpressionParser parser(false, true, true);
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("a->x", DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type);
QVERIFY(TypeUtils::isConstant(result.type.abstractType()));
}
void TestExpressionParser::testConstnessOverload()
{
QByteArray method("struct A { const A& foo() const; A foo(); };\n"
"const A constA; A nonConstA;");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock;
QCOMPARE(top->localDeclarations().count(), 3);
QCOMPARE(top->childContexts().count(), 1);
Declaration* constA = top->localDeclarations().at(1);
QVERIFY(TypeUtils::isConstant(constA->abstractType()));
Declaration* nonConstA = top->localDeclarations().at(2);
QVERIFY(!TypeUtils::isConstant(nonConstA->abstractType()));
Cpp::ExpressionParser parser(false, true, true);
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("constA.foo()", DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type);
// should be the const& version
AbstractType::Ptr type = result.type.abstractType();
QVERIFY(!TypeUtils::isConstant(type));
QVERIFY(type.cast<ReferenceType>());
QVERIFY(TypeUtils::isConstant(type.cast<ReferenceType>()->baseType()));
QVERIFY(type.cast<ReferenceType>()->baseType().cast<StructureType>());
result = parser.evaluateExpression("nonConstA.foo()", DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type);
// should be plain a
type = result.type.abstractType();
QVERIFY(!TypeUtils::isConstant(type));
QVERIFY(!TypeUtils::isReferenceType(type));
QVERIFY(type.cast<StructureType>());
}
void TestExpressionParser::testConstnessOverloadSubscript()
{
QByteArray method("struct A { const A& operator[](int i) const; A operator[](int i); };\n"
"const A constA; A nonConstA;");
DUContext* top = parse(method, DumpNone);
DUChainWriteLocker lock;
QCOMPARE(top->localDeclarations().count(), 3);
QCOMPARE(top->childContexts().count(), 1);
Declaration* constA = top->localDeclarations().at(1);
QVERIFY(TypeUtils::isConstant(constA->abstractType()));
Declaration* nonConstA = top->localDeclarations().at(2);
QVERIFY(!TypeUtils::isConstant(nonConstA->abstractType()));
Cpp::ExpressionParser parser(false, true, true);
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression("constA[0]", DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type);
// should be the const& version
AbstractType::Ptr type = result.type.abstractType();
QVERIFY(!TypeUtils::isConstant(type));
QVERIFY(type.cast<ReferenceType>());
QVERIFY(TypeUtils::isConstant(type.cast<ReferenceType>()->baseType()));
QVERIFY(type.cast<ReferenceType>()->baseType().cast<StructureType>());
result = parser.evaluateExpression("nonConstA[0]", DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type);
// should be plain a
type = result.type.abstractType();
QVERIFY(!TypeUtils::isConstant(type));
QVERIFY(!TypeUtils::isReferenceType(type));
QVERIFY(type.cast<StructureType>());
}
void TestExpressionParser::testReference()
{
QByteArray method("");
KDevelop::DUContextPointer testContext(parse(method, DumpNone));
DUChainWriteLocker lock;
Cpp::ExpressionParser parser(false, true, true);
Cpp::ExpressionEvaluationResult result;
result = parser.evaluateType( "int&", testContext);
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int&"));
QVERIFY(!result.isInstance);
QVERIFY(result.type.type<ReferenceType>());
QVERIFY(!result.type.type<ReferenceType>()->isRValue());
result = parser.evaluateType( "int&&", testContext);
QVERIFY(result.isValid());
QCOMPARE(result.type.abstractType()->toString(), QString("int&&"));
QVERIFY(!result.isInstance);
QVERIFY(result.type.type<ReferenceType>());
QVERIFY(result.type.type<ReferenceType>()->isRValue());
}
void TestExpressionParser::testCharacterTypes_data()
{
QTest::addColumn<QString>("code");
QTest::addColumn<bool>("isString");
QTest::addColumn<uint>("type");
// see also: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2249.html
QTest::newRow("char") << "'a'" << false << (uint) IntegralType::TypeChar;
QTest::newRow("wchar_t") << "L'a'" << false << (uint) IntegralType::TypeWchar_t;
QTest::newRow("char16_t") << "u'a'" << false << (uint) IntegralType::TypeChar16_t;
QTest::newRow("char32_t") << "U'a';" << false << (uint) IntegralType::TypeChar32_t;
QTest::newRow("char-str") << "\"a\";" << true << (uint) IntegralType::TypeChar;
QTest::newRow("wchar_t-str") << "L\"a\";" << true << (uint) IntegralType::TypeWchar_t;
QTest::newRow("char16_t-str") << "u\"a\";" << true << (uint) IntegralType::TypeChar16_t;
QTest::newRow("char32_t-str") << "U\"a\";" << true << (uint) IntegralType::TypeChar32_t;
}
void TestExpressionParser::testCharacterTypes()
{
QFETCH(QString, code);
QFETCH(bool, isString);
QFETCH(uint, type);
DUContext* top = parse("", DumpNone);
DUChainWriteLocker lock;
Cpp::ExpressionParser parser(false, true);
Cpp::ExpressionEvaluationResult result = parser.evaluateExpression(code.toUtf8(), DUContextPointer(top));
QVERIFY(result.isValid());
QVERIFY(result.type);
IntegralType::Ptr integralType;
if (isString) {
QVERIFY(result.type.type<PointerType>());
integralType = result.type.type<PointerType>()->baseType().cast<IntegralType>();
} else {
integralType = result.type.type<IntegralType>();
}
QVERIFY(integralType);
QEXPECT_FAIL("wchar_t-str", "not yet supported, see ExpressionVisitor::visitStringLiteral", Abort);
QEXPECT_FAIL("char16_t-str", "not yet supported, see ExpressionVisitor::visitStringLiteral", Abort);
QEXPECT_FAIL("char32_t-str", "not yet supported, see ExpressionVisitor::visitStringLiteral", Abort);
QCOMPARE(integralType->dataType(), type);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TestExpressionParser::release(DUContext* top)
{
//KDevelop::EditorIntegrator::releaseTopRange(top->textRangePtr());
if(dynamic_cast<TopDUContext*>(top))
DUChain::self()->removeDocumentChain(static_cast<TopDUContext*>(top));
//delete top;
}
TopDUContext* TestExpressionParser::parse(const QByteArray& unit, DumpAreas dump)
{
if (dump)
kDebug(9007) << "==== Beginning new test case...:" << endl << unit;
ParseSession* session = new ParseSession();
session->setContentsAndGenerateLocationTable(tokenizeFromByteArray(unit));
Parser parser(&control);
TranslationUnitAST* ast = parser.parse(session);
ast->session = session;
if (dump & DumpAST) {
kDebug(9007) << "===== AST:";
cppDumper.dump(ast, session);
}
static int testNumber = 0;
IndexedString url(QString("file:///internal/%1").arg(testNumber++));
DeclarationBuilder definitionBuilder(session);
Cpp::EnvironmentFilePointer file( new Cpp::EnvironmentFile( url, 0 ) );
TopDUContext* top = definitionBuilder.buildDeclarations(file, ast);
UseBuilder useBuilder(session);
useBuilder.buildUses(ast);
if (dump & DumpDUChain) {
kDebug(9007) << "===== DUChain:";
DUChainWriteLocker lock(DUChain::lock());
dumpDUContext(top);
}
if (dump)
kDebug(9007) << "===== Finished test case.";
delete session;
return top;
}
#include "test_expressionparser.moc"
|