clangtest.cpp 22.2 KB
Newer Older
ussrhero's avatar
ussrhero committed
1
#include <stdafx.h>
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
2 3 4 5 6 7 8

#include "procfixture.h"

#include "kdlib/typeinfo.h"

using namespace kdlib;

9
class ClangTest : public ProcessFixture
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
{
public:

    ClangTest() : ProcessFixture( L"typetest" ) {}

};

static const wchar_t test_code1[] = L"  \
    struct TestStruct {          \
        bool   a0;               \
        char   a1;               \
        unsigned char  a2;       \
        short  a3;               \
        unsigned short  a4;      \
        int    a5;               \
        unsigned  a6;            \
        long  a7;                \
        unsigned long  a8;       \
        long long      a9;       \
        unsigned long long  a10; \
        long long  a11;          \
        float  a12;              \
        double  a13;             \
        long double  a14;        \
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
34 35
        wchar_t  a15;            \
        unsigned wchar_t  a16;   \
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
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
        void*   b0;              \
        char*   b1;              \
        unsigned char   b2[10];  \
        short&  b3;              \
        const unsigned short  b4;\
        static short  b5;        \
        volatile int  b6;        \
    };";


TEST_F(ClangTest, CompileStruct)
{
    TypeInfoPtr  compiledStruct;

    ASSERT_NO_THROW( compiledStruct = compileType(test_code1, L"TestStruct") );

    EXPECT_TRUE( compiledStruct->isUserDefined() );
    EXPECT_TRUE( compiledStruct->getElement(L"b2")->isArray() );
    EXPECT_TRUE( compiledStruct->getElement(L"b0")->isPointer() );
    EXPECT_TRUE( compiledStruct->getElement(L"b0")->deref()->isVoid() );
    EXPECT_TRUE( compiledStruct->getElement(L"a3")->isBase() );

    EXPECT_EQ( L"TestStruct", compiledStruct->getName() );

    EXPECT_LT( 0, compiledStruct->getSize() );

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
62
    EXPECT_EQ( 24, compiledStruct->getElementCount() );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
63 64 65 66 67 68 69 70 71 72

    EXPECT_EQ( L"Int4B", compiledStruct->getElement(L"a5")->getName() );
    EXPECT_EQ( L"Float", compiledStruct->getElement(L"a12")->getName() );
    EXPECT_EQ( L"a3", compiledStruct->getElementName(3) );
    EXPECT_EQ( L"a8", compiledStruct->getElementName(8) );

    EXPECT_EQ( 9, compiledStruct->getElementIndex( L"a9" ) );

    EXPECT_TRUE( compiledStruct->isStaticMember(L"b5") );
    EXPECT_FALSE( compiledStruct->isStaticMember(L"b6") );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
73 74
    EXPECT_TRUE( compiledStruct->isStaticMember(22) );
    EXPECT_FALSE( compiledStruct->isStaticMember(23) );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
75 76 77

   
    EXPECT_THROW( compiledStruct->getElement(L"aa")->getName(), TypeException );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
78
    EXPECT_THROW( compiledStruct->getElement(25), IndexException );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
79 80 81 82 83 84 85 86

    std::wstring  desc;
    EXPECT_NO_THROW( desc = compiledStruct->str() );
}


TEST_F(ClangTest, CompileOption)
{
87 88 89
    const size_t  defaultPtrSize = sizeof(void*);

    TypeInfoPtr  compiledStruct1, compiledStruct2, compiledStruct3;
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
90 91 92

    ASSERT_NO_THROW( compiledStruct1 = compileType(test_code1, L"TestStruct", L"--target=i686-pc-windows-msvc -w") );
    ASSERT_NO_THROW( compiledStruct2 = compileType(test_code1, L"TestStruct", L"--target=x86_64-pc-windows-msvc -w") );
93
    ASSERT_NO_THROW( compiledStruct3 = compileType(test_code1, L"TestStruct") );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
94 95 96 97

    EXPECT_TRUE( compiledStruct1->getSize() < compiledStruct2->getSize() );
    EXPECT_EQ( 4, compiledStruct1->getElement(L"b1")->getSize());
    EXPECT_EQ( 8, compiledStruct2->getElement(L"b1")->getSize());
98
    EXPECT_EQ( defaultPtrSize, compiledStruct3->getElement(L"b1")->getSize());
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
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
}

static const wchar_t  test_code2[] = L"\
    struct TestStruct;                 \
    struct TestStruct {                \
        struct TestStruct*  next;      \
        int  value;                    \
    };                                 \
    ";

TEST_F(ClangTest, ForwardDecl)
{
    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW( testStruct = compileType(test_code2, L"TestStruct") );
    EXPECT_TRUE( testStruct->getElement(L"next")->isPointer() );
    EXPECT_EQ( is64bitSystem() ? 8 : 4, testStruct->getElement(L"next")->getSize() );

    ASSERT_NO_THROW( testStruct = testStruct->getElement(L"next")->deref() );
    EXPECT_EQ( L"TestStruct", testStruct->getName() );

    std::wstring  desc;
    EXPECT_NO_THROW( desc = testStruct->getElement(L"next")->str() );
}

static const wchar_t test_code3[] = L" \
    struct Test1 {                     \
        int  a1;                       \
        int  a2;                       \
    };                                 \
                                       \
    struct Test2  {                    \
        int  b1;                       \
        struct Test1 b2;               \
        struct {                       \
            int  c1;                   \
            int  c2;                   \
        } b3;                          \
        struct {                       \
            int   c3;                  \
            int   c4;                  \
        };                             \
140 141 142 143 144 145 146 147 148
        union {                        \
            long  c5;                  \
            float  c6;                 \
        };                             \
        union {                        \
            char  c7;                  \
            short  c8;                 \
        } b4;                          \
        int  b5;                       \
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
149 150 151 152 153 154 155 156 157 158 159 160
        float;                         \
    };                                 \
    ";

TEST_F(ClangTest, Nested)
{
    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW( testStruct = compileType(test_code3, L"Test2") );

    EXPECT_EQ( L"a2", testStruct->getElement(L"b2")->getElementName(1) );
    EXPECT_EQ( L"c2", testStruct->getElement(L"b3")->getElementName(1) );
    EXPECT_EQ( L"c4", testStruct->getElementName(4) );
161 162
    EXPECT_EQ( L"c6", testStruct->getElementName(6) );
    EXPECT_EQ( L"c8", testStruct->getElement(L"b4")->getElementName(1) );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
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

    std::wstring  desc;
    EXPECT_NO_THROW( desc = testStruct->str() );
}


static const wchar_t test_code4[] = L"\
    union Test {                      \
        int  a1;                      \
        unsigned long long  a2;       \
        float  a3;                    \
    };                                \
    ";


TEST_F(ClangTest, Union)
{
    TypeInfoPtr  testUnion;
    ASSERT_NO_THROW( testUnion = compileType(test_code4, L"Test") );

    std::wstring  desc;
    EXPECT_NO_THROW( desc = testUnion->str() );
}


static const wchar_t test_code5[] = L"\
    struct Test {                     \
        int  a1 : 11;                 \
        int  a2 : 8;                  \
        int  a3 : 31;                 \
    };                                \
    ";

TEST_F(ClangTest, BitField)
{
    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW( testStruct = compileType(test_code5, L"Test") );

    EXPECT_TRUE( testStruct->getElement(L"a2")->isBitField() );
    EXPECT_EQ( 11, testStruct->getElement(L"a2")->getBitOffset() );
    EXPECT_EQ( 8, testStruct->getElement(L"a2")->getBitWidth() );

    std::wstring  desc;
    EXPECT_NO_THROW( desc = testStruct->str() );
}

209 210 211 212 213 214 215
static const wchar_t test_code6[] = L"\
    namespace ns1 {                   \
    namespace ns2 {                   \
       struct Test {                  \
           int  f1;                   \
       };                             \
    } }                               \
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
216 217
    ";

218 219 220 221 222 223 224 225
TEST_F(ClangTest, Namespace)
{
    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW( testStruct = compileType(test_code6, L"ns1::ns2::Test") );

    std::wstring  desc;
    EXPECT_NO_THROW( desc = testStruct->str() );
}
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
static const wchar_t test_code7[] = L"\
    class Test {                      \
        long long mem1;               \
     };                               \
    ";

TEST_F(ClangTest, SimpleClass)
{
    TypeInfoPtr  testClass;
    ASSERT_NO_THROW( testClass = compileType(test_code7, L"Test") );

    std::wstring  desc;
    EXPECT_NO_THROW( desc = testClass->str() );
}

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
TEST_F(ClangTest, InvalidDef)
{
    EXPECT_THROW( compileType( L"struct Test { charintfloatdouble  mem1; };", L"Test")->str(), TypeException);
//    EXPECT_THROW( compileType( L"struct Test { char mem1;", L"Test")->str(), TypeException);
    EXPECT_THROW( compileType( L"aaaaaa func(bbbb p1);", L"func")->str(), TypeException);
}

TEST_F(ClangTest, StructNoDef)
{
    TypeInfoPtr  structNoDef;
    ASSERT_NO_THROW( structNoDef = compileType(L"struct StructNoDef;", L"StructNoDef") );

    EXPECT_EQ(L"StructNoDef", structNoDef->getName());

    std::wstring desc;
    EXPECT_NO_THROW(desc = structNoDef->str() );

    EXPECT_THROW(structNoDef->getElementCount(), TypeException);
    EXPECT_THROW(structNoDef->getElement(0), TypeException);
}

263 264 265 266 267 268 269
TEST_F(ClangTest, DISABLED_Function)
{
    const std::wstring  src = L"#include \"../../../kdlib/include/test/testfunc.h\"";

    EXPECT_EQ( L"Void(__cdecl)(Int4B, Float)", compileType(src, L"CdeclFunc")->getName());
    EXPECT_EQ( L"Void(__stdcall)(Int4B, Float)", compileType(src, L"StdcallFunc")->getName());
}
270

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
271 272
TEST_F(ClangTest, Include)
{
273 274 275

    const std::wstring  src = L"#include \"../../../kdlib/include/test/testvars.h\"";

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
276 277
    TypeInfoProviderPtr  typeProvider;

278
    ASSERT_NO_THROW( typeProvider = getTypeInfoProviderFromSource(src) );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
279

280 281
    for ( auto typeName : {L"structTest", L"structWithNested", L"structWithArray", L"unionTest", L"structWithBits", L"structWithSignBits",
        /*L"testspace::testClass1",*/ L"structWithEnum", L"enumType" } )
282 283 284 285 286 287 288 289 290 291
    {
        TypeInfoPtr  structFromSrc, structFromPdb;
        std::wstring  str1, str2;

        ASSERT_NO_THROW( structFromSrc = typeProvider->getTypeByName(typeName) );
        ASSERT_NO_THROW( structFromPdb = loadType(typeName) );
        EXPECT_NO_THROW( str1 = structFromSrc->str() );
        EXPECT_NO_THROW( str2 = structFromPdb->str() );
        EXPECT_TRUE( str1 == str2 );
    }
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
292 293 294 295 296
}


TEST_F(ClangTest, WindowsH)
{
297
    std::wstring  src = L"#include <windows.h>\r\n";
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
298
    std::wstring  opt = L"-I\"C:/Program Files (x86)/Windows Kits/8.1/Include/um\" \
299
        -I\"C:/Program Files (x86)/Windows Kits/8.1/Include/shared\" -w";
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
300 301 302 303 304

    TypeInfoProviderPtr  typeProvider;

    ASSERT_NO_THROW( typeProvider = getTypeInfoProviderFromSource(src, opt) );

305
    for ( auto typeName :  {L"tagPOINT", L"tagWNDCLASSA", L"CreateFileA"} )
306 307 308
    {
        TypeInfoPtr  type1;
        ASSERT_NO_THROW( type1 = typeProvider->getTypeByName(typeName) );
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
309

310 311 312
        std::wstring  desc;
        EXPECT_NO_THROW( desc = type1->str() );
    }
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
313 314
}

SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
315 316 317 318

TEST_F(ClangTest, NtddkH)
{
    std::wstring  src = L"#include <ntddk.h>\r\n";
ussrhero's avatar
ussrhero committed
319
    std::wstring  opt = L"-I\"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/km\" \
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
320 321 322 323 324 325 326
        -I\"C:/Program Files (x86)/Windows Kits/8.1/Include/shared\" \
        -D_AMD64_ --target=x86_64-pc-windows-msvc -w";

    TypeInfoProviderPtr  typeProvider;

    ASSERT_NO_THROW( typeProvider = getTypeInfoProviderFromSource(src, opt) );

327 328
    for ( auto typeName :  {L"_UNICODE_STRING", L"_IRP", L"_MDL", L"_FILE_OBJECT", L"_DEVICE_OBJECT", L"_PS_CREATE_NOTIFY_INFO",
     L"ZwCreateFile"} )
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
329 330 331 332 333 334 335 336 337
    {
        TypeInfoPtr  type1;
        ASSERT_NO_THROW( type1 = typeProvider->getTypeByName(typeName) );

        std::wstring  desc;
        EXPECT_NO_THROW( desc = type1->str() );
    }
}

ussrhero's avatar
ussrhero committed
338 339 340 341 342
static const wchar_t test_typedef_src[] = L"\
                                            \
    typedef  int   int_def;                 \
                                            \
    typedef struct _Test {                  \
ussrhero's avatar
ussrhero committed
343
        int      field1;                    \
ussrhero's avatar
ussrhero committed
344 345
        int_def  field2;                    \
     } TestDef;                             \
ussrhero's avatar
ussrhero committed
346 347
                                            \
     typedef TestDef   TEST;                \
ussrhero's avatar
ussrhero committed
348 349
     typedef TestDef*  PTEST;               \
     typedef TestDef   **PPTEST;            \
ussrhero's avatar
ussrhero committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    ";

TEST_F(ClangTest, Typedef)
{
    TypeInfoProviderPtr  typeProvider;

    ASSERT_NO_THROW( typeProvider = getTypeInfoProviderFromSource(test_typedef_src) );

    TypeInfoPtr  intType;
    ASSERT_NO_THROW( intType = typeProvider->getTypeByName(L"int_def") );
    ASSERT_EQ(L"Int4B", intType->getName() );

    TypeInfoPtr  structType;
    ASSERT_NO_THROW( structType = typeProvider->getTypeByName(L"TestDef") );

    ASSERT_EQ(L"_Test", structType->getName() );
    ASSERT_EQ(L"Int4B", structType->getElement(L"field1")->getName() );
    ASSERT_EQ(L"Int4B", structType->getElement(L"field2")->getName() );

ussrhero's avatar
ussrhero committed
369
    ASSERT_EQ(L"_Test", typeProvider->getTypeByName(L"TEST")->getName() );
ussrhero's avatar
ussrhero committed
370 371
    ASSERT_EQ(L"_Test*", typeProvider->getTypeByName(L"PTEST")->getName());
    ASSERT_EQ(L"_Test**", typeProvider->getTypeByName(L"PPTEST")->getName());
ussrhero's avatar
ussrhero committed
372
}
SND\ussrhero_cp's avatar
SND\ussrhero_cp committed
373

ussrhero's avatar
ussrhero committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
TEST_F(ClangTest, TypeProviderEnum)
{
   const std::wstring  src = L"#include \"../../../kdlib/include/test/testvars.h\"";

    TypeInfoProviderPtr  typeProvider;

    ASSERT_NO_THROW( typeProvider = getTypeInfoProviderFromSource(src) );

    TypeInfoEnumeratorPtr  typeEnum;
    size_t  count;

    ASSERT_NO_THROW( typeEnum = typeProvider->getTypeEnumerator() );
    for ( count = 0; 0 != typeEnum->Next(); ++count);
    EXPECT_LT(1000, count);

    ASSERT_NO_THROW( typeEnum = typeProvider->getTypeEnumerator(L"struct*") );
    for ( count = 0; 0 != typeEnum->Next(); ++count);
ussrhero's avatar
ussrhero committed
391
    EXPECT_EQ(14, count);
ussrhero's avatar
ussrhero committed
392 393 394
}


395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

TEST_F(ClangTest, TemplateStruct)
{
    static const wchar_t srcCode[] =  L"\
    template<typename T1, typename T2>      \
    struct TestStruct {                     \
        T1  field1;                         \
        T2  field2;                         \
    };                                      \
    TestStruct<int,float>  var1;            \
    TestStruct<char, unsigned long>  var2;  \
    ";

    TypeInfoProviderPtr  typeProvider;
    ASSERT_NO_THROW(typeProvider = getTypeInfoProviderFromSource(srcCode));

ussrhero's avatar
ussrhero committed
411 412
    EXPECT_EQ(L"Int4B", typeProvider->getTypeByName(L"TestStruct<int,float>")->getElement(0)->getName());
    EXPECT_EQ(L"UInt4B", typeProvider->getTypeByName(L"TestStruct<char,unsigned long>")->getElement(1)->getName());
ussrhero's avatar
ussrhero committed
413
    EXPECT_TRUE(typeProvider->getTypeByName(L"TestStruct<char,unsigned long>")->isTemplate());
414 415 416 417
    EXPECT_THROW(typeProvider->getTypeByName(L"TestStruct"), TypeException);
    EXPECT_THROW(typeProvider->getTypeByName(L"TestStruct<float>"), TypeException);
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
TEST_F(ClangTest, TemplateNestedStruct)
{
    static const wchar_t srcCode[] = L"\
    template<typename T>                    \
    struct TestStruct {                     \
        T   field;                          \
    };                                      \
    TestStruct<TestStruct<int>>  var1;      \
    TestStruct<TestStruct<TestStruct<int>>>  var2;  \
    ";

    TypeInfoProviderPtr  typeProvider;
    ASSERT_NO_THROW(typeProvider = getTypeInfoProviderFromSource(srcCode));

    EXPECT_NO_THROW(typeProvider->getTypeByName(L"TestStruct<TestStruct<int> >"));
    EXPECT_NO_THROW(typeProvider->getTypeByName(L"TestStruct<TestStruct<TestStruct<int> > >"));
}
435 436 437 438 439 440 441 442 443 444 445 446 447 448

TEST_F(ClangTest, TemplateValueStruct)
{
    static const wchar_t srcCode[] = L"\
    template<int v1, long v2>          \
    struct TestStruct {                \
        int  field1;                   \
    };                                 \
    TestStruct<2,-3>  var1;            \
    ";

    TypeInfoProviderPtr  typeProvider;
    ASSERT_NO_THROW(typeProvider = getTypeInfoProviderFromSource(srcCode));

ussrhero's avatar
ussrhero committed
449 450
    EXPECT_NO_THROW(typeProvider->getTypeByName(L"TestStruct<2,-3>"));
    EXPECT_THROW(typeProvider->getTypeByName(L"TestStruct<2,3>"), TypeException);
451
}
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

TEST_F(ClangTest, MultipleInheritance)
{
    static const wchar_t srcCode[] = L"\
    class Base1 {                      \
        int  field1;                   \
    };                                 \
    class Base2 {                      \
        int  field2;                   \
    };                                 \
    class TestClass : public Base1, private Base2 {  \
        char  field3;                  \
    };                                 \
    ";

    TypeInfoProviderPtr  typeProvider;
    ASSERT_NO_THROW(typeProvider = getTypeInfoProviderFromSource(srcCode));

    TypeInfoPtr  typeInfo;
    ASSERT_NO_THROW(typeInfo = typeProvider->getTypeByName(L"TestClass"));
    EXPECT_EQ(3, typeInfo->getElementCount());
    EXPECT_EQ(2, typeInfo->getBaseClassesCount());
474 475 476 477 478 479
    EXPECT_EQ(L"field1", typeInfo->getBaseClass(0)->getElementName(0));
    EXPECT_EQ(L"field2", typeInfo->getBaseClass(L"Base2")->getElementName(0));
    EXPECT_FALSE(typeInfo->getBaseClassOffset(0) == typeInfo->getBaseClassOffset(L"Base2"));

    std::wstring  str;
    EXPECT_NO_THROW(str = typeInfo->str());
480
}
481 482 483 484 485 486 487 488

TEST_F(ClangTest, NestedEnum)
{
    static const wchar_t srcCode[] = L"\
    struct Test {                      \
        enum {                         \
           VAL = 1                     \
        };                             \
ussrhero's avatar
ussrhero committed
489 490 491
        enum NestedEnum {              \
          VAL1 = 2                     \
        };                             \
492 493 494 495 496 497 498
    };                                 \
    ";

    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW(testStruct = compileType(srcCode, L"Test"));

    EXPECT_EQ(1, *testStruct->getElement(L"VAL"));
ussrhero's avatar
ussrhero committed
499
    EXPECT_EQ(2, *testStruct->getElement(L"VAL1"));
ussrhero's avatar
ussrhero committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517


}

TEST_F(ClangTest, PtrToIncompleteArray)
{
    static const wchar_t srcCode[] = L" \
    struct Test {                       \
        int(*a)[];                      \
    };                                  \
    ";

    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW(testStruct = compileType(srcCode, L"Test"));
    EXPECT_NO_THROW(testStruct->getElement(L"a")->getSize());
    EXPECT_NO_THROW(testStruct->getElement(L"a")->deref());
    EXPECT_TRUE(testStruct->getElement(L"a")->deref()->isIncomplete());
    EXPECT_THROW(testStruct->getElement(L"a")->deref()->getSize(), TypeException);
518
}
ussrhero's avatar
ussrhero committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

TEST_F(ClangTest, PtrToIncompleteStruct)
{
    static const wchar_t srcCode[] = L" \
    struct Test1;                       \
    struct Test2 {                      \
        Test1*  t;                      \
    };                                  \
    ";

    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW(testStruct = compileType(srcCode, L"Test2"));
    EXPECT_NO_THROW(testStruct->getElement(L"t")->getSize());
    EXPECT_NO_THROW(testStruct->getElement(L"t")->deref());
    EXPECT_TRUE(testStruct->getElement(L"t")->deref()->isIncomplete());
    EXPECT_THROW(testStruct->getElement(L"t")->deref()->getSize(), TypeException);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
}

TEST_F(ClangTest, isInheritedMember)
{
    static const wchar_t srcCode[] = L" \
    struct Base {                       \
       int mbase;                       \
    };                                  \
    struct Test : Base {                \
       int mchild;                      \
    };                                  \
    ";

    TypeInfoPtr  testStruct;
    ASSERT_NO_THROW(testStruct = compileType(srcCode, L"Test"));
    EXPECT_TRUE(testStruct->isInheritedMember(L"mbase"));
    EXPECT_FALSE(testStruct->isInheritedMember(L"mchild"));
    EXPECT_TRUE(testStruct->isInheritedMember(0));
    EXPECT_FALSE(testStruct->isInheritedMember(1));
554 555 556 557 558 559 560 561 562 563 564 565 566
 }

TEST_F(ClangTest, EnumFuncNames)
{
    static const wchar_t srcCode[] = L"      \
    void func(int, char);                    \
    inline void func1() {}                   \
    template <int> int func2() { return 0; } \
    template<> int func2<2>() { return 1; }  \
    namespace testns {                       \
        char func3(void);                    \
    }                                        \
    class testcls {                          \
ussrhero's avatar
ussrhero committed
567
      void method(int);                      \
568 569 570
    };                                       \
    ";

ussrhero's avatar
ussrhero committed
571
    auto  symEnum = getSymbolProviderFromSource(srcCode)->getSymbolEnumerator();
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

    std::wstring symbol;
    std::vector<std::wstring> symbols;
    while ( !(symbol = symEnum->Next()).empty() )
    {
        symbols.push_back(symbol);
    }

    EXPECT_EQ( std::vector<std::wstring>({ 
        L"func",
        L"func1",
        L"func2<2>", 
        L"testns::func3",
        L"testcls::method"
        }), symbols);
ussrhero's avatar
ussrhero committed
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
}

TEST_F(ClangTest, Func)
{
    static const wchar_t srcCode[] = L"      \
    void func(int, char);                    \
    inline void func1() {}                   \
    template <int> int func2() { return 0; } \
    template<> int func2<2>() { return 1; }  \
    namespace testns {                       \
        char func3(void);                    \
    }                                        \
    class testcls {                          \
      void method(int);                      \
    };                                       \
    template<typename T>                     \
    class testcls1 {                         \
       void method();                        \
    };                                       \
    template<>                               \
    class testcls1<int> {                    \
        void method();                       \
    };                                       \
    ";

    EXPECT_EQ(L"Void(__cdecl)(Int4B, Char)", compileType(srcCode, L"func")->getName());
    EXPECT_EQ(L"Void(__cdecl)()", compileType(srcCode, L"func1")->getName());
    EXPECT_EQ(L"Int4B(__cdecl)()", compileType(srcCode, L"func2<2>")->getName());
    EXPECT_EQ(L"Char(__cdecl)()", compileType(srcCode, L"testns::func3")->getName());
ussrhero's avatar
ussrhero committed
616 617 618 619 620 621 622 623 624 625
    if (kdlib::is64bitSystem())
    {
        EXPECT_EQ(L"Void(__cdecl testcls::)(Int4B)", compileType(srcCode, L"testcls::method")->getName());
        EXPECT_EQ(L"Void(__cdecl testcls1<int>::)()", compileType(srcCode, L"testcls1<int>::method")->getName());
    }
    else
    {
        EXPECT_EQ(L"Void(__thiscall testcls::)(Int4B)", compileType(srcCode, L"testcls::method")->getName());
        EXPECT_EQ(L"Void(__thiscall testcls1<int>::)()", compileType(srcCode, L"testcls1<int>::method")->getName());
    }
ussrhero's avatar
ussrhero committed
626 627 628 629 630 631 632

    EXPECT_THROW(compileType(srcCode, L"func2"), TypeException);
    EXPECT_THROW(compileType(srcCode, L"func3"), TypeException);
    EXPECT_THROW(compileType(srcCode, L"method"), TypeException);
    EXPECT_THROW(compileType(srcCode, L"testcls1::method"), TypeException);
    EXPECT_THROW(compileType(srcCode, L"testcls1<char>::method"), TypeException);
}