1 module parse.raw;
2 
3 import test.infra;
4 import clang.c.index;
5 
6 
7 @("C++ file with one simple struct")
8 @system unittest {
9     with(NewCppFile("foo.cpp",
10                     q{ struct { int int_; double double_; }; }))
11     {
12         import std.string: toStringz;
13 
14         auto index = clang_createIndex(0, 0);
15         const(char)*[] commandLineArgs;
16         CXUnsavedFile[] unsavedFiles;
17 
18         auto translUnit = clang_parseTranslationUnit(
19             index,
20             fileName.toStringz,
21             commandLineArgs.ptr,
22             cast(int)commandLineArgs.length,
23             unsavedFiles.ptr,
24             cast(uint)unsavedFiles.length,
25             CXTranslationUnit_None,
26         );
27         auto cursor = clang_getTranslationUnitCursor(translUnit);
28 
29         void* clientData = null;
30         clang_visitChildren(cursor, &fooCppVisitor, clientData);
31     }
32 }
33 
34 private extern(C) CXChildVisitResult fooCppVisitor(CXCursor cursor, CXCursor parent, void* clientData) {
35 
36     static int cursorIndex;
37 
38     switch(cursorIndex) {
39 
40     default:
41         assert(false);
42 
43     case 0:
44         clang_getCursorKind(cursor).shouldEqual(CXCursor_StructDecl);
45         clang_getCursorKind(parent).shouldEqual(CXCursor_TranslationUnit);
46         break;
47 
48     case 1:
49         clang_getCursorKind(cursor).shouldEqual(CXCursor_FieldDecl);
50         clang_getCursorKind(parent).shouldEqual(CXCursor_StructDecl);
51         break;
52 
53     case 2:
54         clang_getCursorKind(cursor).shouldEqual(CXCursor_FieldDecl);
55         clang_getCursorKind(parent).shouldEqual(CXCursor_StructDecl);
56         break;
57     }
58 
59     ++cursorIndex;
60     return CXChildVisit_Recurse;
61 }
62 
63 @("C++ file with one simple struct and throwing visitor")
64 @system unittest {
65     with(NewCppFile("foo.cpp",
66                     q{ struct { int int_; double double_; }; }))
67     {
68         import std.string: toStringz;
69         import std.algorithm: map;
70         import std.array: array;
71 
72         auto index = clang_createIndex(0, 0);
73         const(char)*[] commandLineArgs;
74         CXUnsavedFile[] unsavedFiles;
75 
76         auto translUnit = clang_parseTranslationUnit(
77             index,
78             fileName.toStringz,
79             commandLineArgs.ptr,
80             cast(int)commandLineArgs.length,
81             unsavedFiles.ptr,
82             cast(uint)unsavedFiles.length,
83             CXTranslationUnit_None,
84         );
85         auto cursor = clang_getTranslationUnitCursor(translUnit);
86 
87         void* clientData = null;
88         clang_visitChildren(cursor, &throwingVisitor, clientData).shouldThrow;
89     }
90 }
91 
92 private extern(C) CXChildVisitResult throwingVisitor(CXCursor cursor, CXCursor parent, void* clientData) {
93     throw new Exception("oops");
94 }