blob: 64128f158f2e8aa5db8e1a0364a62ede259dd32d [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001//===- unittests/libclang/LibclangTest.cpp --- libclang tests -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang-c/Index.h"
11#include "gtest/gtest.h"
12
13TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) {
14 EXPECT_EQ(CXError_InvalidArguments,
15 clang_parseTranslationUnit2(0, 0, 0, 0, 0, 0, 0, 0));
16}
17
18TEST(libclang, clang_createTranslationUnit_InvalidArgs) {
19 EXPECT_EQ(0, clang_createTranslationUnit(0, 0));
20}
21
22TEST(libclang, clang_createTranslationUnit2_InvalidArgs) {
23 EXPECT_EQ(CXError_InvalidArguments,
24 clang_createTranslationUnit2(0, 0, 0));
25
26 CXTranslationUnit TU = reinterpret_cast<CXTranslationUnit>(1);
27 EXPECT_EQ(CXError_InvalidArguments,
28 clang_createTranslationUnit2(0, 0, &TU));
29 EXPECT_EQ(0, TU);
30}
31
32namespace {
33struct TestVFO {
34 const char *Contents;
35 CXVirtualFileOverlay VFO;
36
37 TestVFO(const char *Contents) : Contents(Contents) {
38 VFO = clang_VirtualFileOverlay_create(0);
39 }
40
41 void map(const char *VPath, const char *RPath) {
42 CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath);
43 EXPECT_EQ(Err, CXError_Success);
44 }
45
46 void mapError(const char *VPath, const char *RPath, CXErrorCode ExpErr) {
47 CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath);
48 EXPECT_EQ(Err, ExpErr);
49 }
50
51 ~TestVFO() {
52 if (!Contents)
53 return;
54 char *BufPtr;
55 unsigned BufSize;
56 clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &BufPtr, &BufSize);
57 std::string BufStr(BufPtr, BufSize);
58 EXPECT_STREQ(Contents, BufStr.c_str());
59 free(BufPtr);
60 clang_VirtualFileOverlay_dispose(VFO);
61 }
62};
63}
64
65TEST(libclang, VirtualFileOverlay) {
66 {
67 const char *contents =
68 "{\n"
69 " 'version': 0,\n"
70 " 'roots': [\n"
71 " {\n"
72 " 'type': 'directory',\n"
73 " 'name': \"/path/virtual\",\n"
74 " 'contents': [\n"
75 " {\n"
76 " 'type': 'file',\n"
77 " 'name': \"foo.h\",\n"
78 " 'external-contents': \"/real/foo.h\"\n"
79 " }\n"
80 " ]\n"
81 " }\n"
82 " ]\n"
83 "}\n";
84 TestVFO T(contents);
85 T.map("/path/virtual/foo.h", "/real/foo.h");
86 }
87 {
88 TestVFO T(NULL);
89 T.mapError("/path/./virtual/../foo.h", "/real/foo.h",
90 CXError_InvalidArguments);
91 }
92 {
93 const char *contents =
94 "{\n"
95 " 'version': 0,\n"
96 " 'roots': [\n"
97 " {\n"
98 " 'type': 'directory',\n"
99 " 'name': \"/another/dir\",\n"
100 " 'contents': [\n"
101 " {\n"
102 " 'type': 'file',\n"
103 " 'name': \"foo2.h\",\n"
104 " 'external-contents': \"/real/foo2.h\"\n"
105 " }\n"
106 " ]\n"
107 " },\n"
108 " {\n"
109 " 'type': 'directory',\n"
110 " 'name': \"/path/virtual/dir\",\n"
111 " 'contents': [\n"
112 " {\n"
113 " 'type': 'file',\n"
114 " 'name': \"foo1.h\",\n"
115 " 'external-contents': \"/real/foo1.h\"\n"
116 " },\n"
117 " {\n"
118 " 'type': 'file',\n"
119 " 'name': \"foo3.h\",\n"
120 " 'external-contents': \"/real/foo3.h\"\n"
121 " },\n"
122 " {\n"
123 " 'type': 'directory',\n"
124 " 'name': \"in/subdir\",\n"
125 " 'contents': [\n"
126 " {\n"
127 " 'type': 'file',\n"
128 " 'name': \"foo4.h\",\n"
129 " 'external-contents': \"/real/foo4.h\"\n"
130 " }\n"
131 " ]\n"
132 " }\n"
133 " ]\n"
134 " }\n"
135 " ]\n"
136 "}\n";
137 TestVFO T(contents);
138 T.map("/path/virtual/dir/foo1.h", "/real/foo1.h");
139 T.map("/another/dir/foo2.h", "/real/foo2.h");
140 T.map("/path/virtual/dir/foo3.h", "/real/foo3.h");
141 T.map("/path/virtual/dir/in/subdir/foo4.h", "/real/foo4.h");
142 }
143 {
144 const char *contents =
145 "{\n"
146 " 'version': 0,\n"
147 " 'case-sensitive': 'false',\n"
148 " 'roots': [\n"
149 " {\n"
150 " 'type': 'directory',\n"
151 " 'name': \"/path/virtual\",\n"
152 " 'contents': [\n"
153 " {\n"
154 " 'type': 'file',\n"
155 " 'name': \"foo.h\",\n"
156 " 'external-contents': \"/real/foo.h\"\n"
157 " }\n"
158 " ]\n"
159 " }\n"
160 " ]\n"
161 "}\n";
162 TestVFO T(contents);
163 T.map("/path/virtual/foo.h", "/real/foo.h");
164 clang_VirtualFileOverlay_setCaseSensitivity(T.VFO, false);
165 }
166}
167
168TEST(libclang, ModuleMapDescriptor) {
169 const char *Contents =
170 "framework module TestFrame {\n"
171 " umbrella header \"TestFrame.h\"\n"
172 "\n"
173 " export *\n"
174 " module * { export * }\n"
175 "}\n";
176
177 CXModuleMapDescriptor MMD = clang_ModuleMapDescriptor_create(0);
178
179 clang_ModuleMapDescriptor_setFrameworkModuleName(MMD, "TestFrame");
180 clang_ModuleMapDescriptor_setUmbrellaHeader(MMD, "TestFrame.h");
181
182 char *BufPtr;
183 unsigned BufSize;
184 clang_ModuleMapDescriptor_writeToBuffer(MMD, 0, &BufPtr, &BufSize);
185 std::string BufStr(BufPtr, BufSize);
186 EXPECT_STREQ(Contents, BufStr.c_str());
187 free(BufPtr);
188 clang_ModuleMapDescriptor_dispose(MMD);
189}