Dmitri Gribenko | f85b929 | 2014-02-18 15:29:17 +0000 | [diff] [blame] | 1 | //===- 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 | |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 13 | TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) { |
Dmitri Gribenko | f85b929 | 2014-02-18 15:29:17 +0000 | [diff] [blame] | 14 | EXPECT_EQ(CXError_InvalidArguments, |
| 15 | clang_parseTranslationUnit2(0, 0, 0, 0, 0, 0, 0, 0)); |
| 16 | } |
| 17 | |
Dmitri Gribenko | 8850cda | 2014-02-19 10:24:00 +0000 | [diff] [blame] | 18 | TEST(libclang, clang_createTranslationUnit_InvalidArgs) { |
| 19 | EXPECT_EQ(0, clang_createTranslationUnit(0, 0)); |
| 20 | } |
| 21 | |
| 22 | TEST(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 | } |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | struct 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() { |
Nico Weber | 655af09 | 2014-04-24 19:04:10 +0000 | [diff] [blame^] | 52 | if (Contents) { |
| 53 | char *BufPtr; |
| 54 | unsigned BufSize; |
| 55 | clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &BufPtr, &BufSize); |
| 56 | std::string BufStr(BufPtr, BufSize); |
| 57 | EXPECT_STREQ(Contents, BufStr.c_str()); |
| 58 | free(BufPtr); |
| 59 | } |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 60 | clang_VirtualFileOverlay_dispose(VFO); |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | TEST(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 | { |
Ben Langmuir | a56071c5 | 2014-04-17 03:31:02 +0000 | [diff] [blame] | 88 | const char *contents = |
| 89 | "{\n" |
| 90 | " 'version': 0,\n" |
| 91 | " 'roots': [\n" |
| 92 | " {\n" |
| 93 | " 'type': 'directory',\n" |
| 94 | " 'name': \"/path/virtual\",\n" |
| 95 | " 'contents': [\n" |
| 96 | " {\n" |
| 97 | " 'type': 'file',\n" |
| 98 | " 'name': \"\\u2602.h\",\n" |
| 99 | " 'external-contents': \"/real/\\u2602.h\"\n" |
| 100 | " }\n" |
| 101 | " ]\n" |
| 102 | " }\n" |
| 103 | " ]\n" |
| 104 | "}\n"; |
| 105 | TestVFO T(contents); |
| 106 | T.map("/path/virtual/☂.h", "/real/☂.h"); |
| 107 | } |
| 108 | { |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 109 | TestVFO T(NULL); |
| 110 | T.mapError("/path/./virtual/../foo.h", "/real/foo.h", |
| 111 | CXError_InvalidArguments); |
| 112 | } |
| 113 | { |
| 114 | const char *contents = |
| 115 | "{\n" |
| 116 | " 'version': 0,\n" |
| 117 | " 'roots': [\n" |
| 118 | " {\n" |
| 119 | " 'type': 'directory',\n" |
| 120 | " 'name': \"/another/dir\",\n" |
| 121 | " 'contents': [\n" |
| 122 | " {\n" |
| 123 | " 'type': 'file',\n" |
| 124 | " 'name': \"foo2.h\",\n" |
| 125 | " 'external-contents': \"/real/foo2.h\"\n" |
| 126 | " }\n" |
| 127 | " ]\n" |
| 128 | " },\n" |
| 129 | " {\n" |
| 130 | " 'type': 'directory',\n" |
| 131 | " 'name': \"/path/virtual/dir\",\n" |
| 132 | " 'contents': [\n" |
| 133 | " {\n" |
| 134 | " 'type': 'file',\n" |
| 135 | " 'name': \"foo1.h\",\n" |
| 136 | " 'external-contents': \"/real/foo1.h\"\n" |
| 137 | " },\n" |
| 138 | " {\n" |
| 139 | " 'type': 'file',\n" |
| 140 | " 'name': \"foo3.h\",\n" |
| 141 | " 'external-contents': \"/real/foo3.h\"\n" |
| 142 | " },\n" |
| 143 | " {\n" |
| 144 | " 'type': 'directory',\n" |
| 145 | " 'name': \"in/subdir\",\n" |
| 146 | " 'contents': [\n" |
| 147 | " {\n" |
| 148 | " 'type': 'file',\n" |
| 149 | " 'name': \"foo4.h\",\n" |
| 150 | " 'external-contents': \"/real/foo4.h\"\n" |
| 151 | " }\n" |
| 152 | " ]\n" |
| 153 | " }\n" |
| 154 | " ]\n" |
| 155 | " }\n" |
| 156 | " ]\n" |
| 157 | "}\n"; |
| 158 | TestVFO T(contents); |
| 159 | T.map("/path/virtual/dir/foo1.h", "/real/foo1.h"); |
| 160 | T.map("/another/dir/foo2.h", "/real/foo2.h"); |
| 161 | T.map("/path/virtual/dir/foo3.h", "/real/foo3.h"); |
| 162 | T.map("/path/virtual/dir/in/subdir/foo4.h", "/real/foo4.h"); |
| 163 | } |
Argyrios Kyrtzidis | a9ab4d4 | 2014-03-20 04:51:48 +0000 | [diff] [blame] | 164 | { |
| 165 | const char *contents = |
| 166 | "{\n" |
| 167 | " 'version': 0,\n" |
| 168 | " 'case-sensitive': 'false',\n" |
| 169 | " 'roots': [\n" |
| 170 | " {\n" |
| 171 | " 'type': 'directory',\n" |
| 172 | " 'name': \"/path/virtual\",\n" |
| 173 | " 'contents': [\n" |
| 174 | " {\n" |
| 175 | " 'type': 'file',\n" |
| 176 | " 'name': \"foo.h\",\n" |
| 177 | " 'external-contents': \"/real/foo.h\"\n" |
| 178 | " }\n" |
| 179 | " ]\n" |
| 180 | " }\n" |
| 181 | " ]\n" |
| 182 | "}\n"; |
| 183 | TestVFO T(contents); |
| 184 | T.map("/path/virtual/foo.h", "/real/foo.h"); |
| 185 | clang_VirtualFileOverlay_setCaseSensitivity(T.VFO, false); |
| 186 | } |
Argyrios Kyrtzidis | 0b9682e | 2014-02-25 03:59:23 +0000 | [diff] [blame] | 187 | } |
Argyrios Kyrtzidis | d502a10 | 2014-03-03 07:41:45 +0000 | [diff] [blame] | 188 | |
| 189 | TEST(libclang, ModuleMapDescriptor) { |
| 190 | const char *Contents = |
| 191 | "framework module TestFrame {\n" |
| 192 | " umbrella header \"TestFrame.h\"\n" |
| 193 | "\n" |
| 194 | " export *\n" |
| 195 | " module * { export * }\n" |
| 196 | "}\n"; |
| 197 | |
| 198 | CXModuleMapDescriptor MMD = clang_ModuleMapDescriptor_create(0); |
| 199 | |
| 200 | clang_ModuleMapDescriptor_setFrameworkModuleName(MMD, "TestFrame"); |
| 201 | clang_ModuleMapDescriptor_setUmbrellaHeader(MMD, "TestFrame.h"); |
| 202 | |
| 203 | char *BufPtr; |
| 204 | unsigned BufSize; |
| 205 | clang_ModuleMapDescriptor_writeToBuffer(MMD, 0, &BufPtr, &BufSize); |
| 206 | std::string BufStr(BufPtr, BufSize); |
| 207 | EXPECT_STREQ(Contents, BufStr.c_str()); |
| 208 | free(BufPtr); |
| 209 | clang_ModuleMapDescriptor_dispose(MMD); |
| 210 | } |