| 1 |
//===- unittests/libclang/LibclangTest.cpp --- libclang tests -------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "clang-c/Index.h" |
| 10 |
#include "clang-c/Rewrite.h" |
| 11 |
#include "llvm/ADT/StringRef.h" |
| 12 |
#include "llvm/Support/Debug.h" |
| 13 |
#include "llvm/Support/FileSystem.h" |
| 14 |
#include "llvm/Support/Path.h" |
| 15 |
#include "llvm/Support/raw_ostream.h" |
| 16 |
#include "gtest/gtest.h" |
| 17 |
#include "TestUtils.h" |
| 18 |
#include <fstream> |
| 19 |
#include <functional> |
| 20 |
#include <map> |
| 21 |
#include <memory> |
| 22 |
#include <optional> |
| 23 |
#include <set> |
| 24 |
#define DEBUG_TYPE "libclang-test" |
| 25 |
|
| 26 |
TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) { |
| 27 |
EXPECT_EQ(CXError_InvalidArguments, |
| 28 |
clang_parseTranslationUnit2(nullptr, nullptr, nullptr, 0, nullptr, |
| 29 |
0, 0, nullptr)); |
| 30 |
} |
| 31 |
|
| 32 |
TEST(libclang, clang_createTranslationUnit_InvalidArgs) { |
| 33 |
EXPECT_EQ(nullptr, clang_createTranslationUnit(nullptr, nullptr)); |
| 34 |
} |
| 35 |
|
| 36 |
TEST(libclang, clang_createTranslationUnit2_InvalidArgs) { |
| 37 |
EXPECT_EQ(CXError_InvalidArguments, |
| 38 |
clang_createTranslationUnit2(nullptr, nullptr, nullptr)); |
| 39 |
|
| 40 |
CXTranslationUnit TU = reinterpret_cast<CXTranslationUnit>(1); |
| 41 |
EXPECT_EQ(CXError_InvalidArguments, |
| 42 |
clang_createTranslationUnit2(nullptr, nullptr, &TU)); |
| 43 |
EXPECT_EQ(nullptr, TU); |
| 44 |
} |
| 45 |
|
| 46 |
namespace { |
| 47 |
struct TestVFO { |
| 48 |
const char *Contents; |
| 49 |
CXVirtualFileOverlay VFO; |
| 50 |
|
| 51 |
TestVFO(const char *Contents) : Contents(Contents) { |
| 52 |
VFO = clang_VirtualFileOverlay_create(0); |
| 53 |
} |
| 54 |
|
| 55 |
void map(const char *VPath, const char *RPath) { |
| 56 |
CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath); |
| 57 |
EXPECT_EQ(Err, CXError_Success); |
| 58 |
} |
| 59 |
|
| 60 |
void mapError(const char *VPath, const char *RPath, CXErrorCode ExpErr) { |
| 61 |
CXErrorCode Err = clang_VirtualFileOverlay_addFileMapping(VFO, VPath, RPath); |
| 62 |
EXPECT_EQ(Err, ExpErr); |
| 63 |
} |
| 64 |
|
| 65 |
~TestVFO() { |
| 66 |
if (Contents) { |
| 67 |
char *BufPtr; |
| 68 |
unsigned BufSize; |
| 69 |
clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &BufPtr, &BufSize); |
| 70 |
std::string BufStr(BufPtr, BufSize); |
| 71 |
EXPECT_STREQ(Contents, BufStr.c_str()); |
| 72 |
clang_free(BufPtr); |
| 73 |
} |
| 74 |
clang_VirtualFileOverlay_dispose(VFO); |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
TEST(libclang, VirtualFileOverlay_Basic) { |
| 80 |
const char *contents = |
| 81 |
"{\n" |
| 82 |
" 'version': 0,\n" |
| 83 |
" 'roots': [\n" |
| 84 |
" {\n" |
| 85 |
" 'type': 'directory',\n" |
| 86 |
" 'name': \"/path/virtual\",\n" |
| 87 |
" 'contents': [\n" |
| 88 |
" {\n" |
| 89 |
" 'type': 'file',\n" |
| 90 |
" 'name': \"foo.h\",\n" |
| 91 |
" 'external-contents': \"/real/foo.h\"\n" |
| 92 |
" }\n" |
| 93 |
" ]\n" |
| 94 |
" }\n" |
| 95 |
" ]\n" |
| 96 |
"}\n"; |
| 97 |
TestVFO T(contents); |
| 98 |
T.map("/path/virtual/foo.h", "/real/foo.h"); |
| 99 |
} |
| 100 |
|
| 101 |
TEST(libclang, VirtualFileOverlay_Unicode) { |
| 102 |
const char *contents = |
| 103 |
"{\n" |
| 104 |
" 'version': 0,\n" |
| 105 |
" 'roots': [\n" |
| 106 |
" {\n" |
| 107 |
" 'type': 'directory',\n" |
| 108 |
" 'name': \"/path/\\u266B\",\n" |
| 109 |
" 'contents': [\n" |
| 110 |
" {\n" |
| 111 |
" 'type': 'file',\n" |
| 112 |
" 'name': \"\\u2602.h\",\n" |
| 113 |
" 'external-contents': \"/real/\\u2602.h\"\n" |
| 114 |
" }\n" |
| 115 |
" ]\n" |
| 116 |
" }\n" |
| 117 |
" ]\n" |
| 118 |
"}\n"; |
| 119 |
TestVFO T(contents); |
| 120 |
T.map("/path/♫/☂.h", "/real/☂.h"); |
| 121 |
} |
| 122 |
|
| 123 |
TEST(libclang, VirtualFileOverlay_InvalidArgs) { |
| 124 |
TestVFO T(nullptr); |
| 125 |
T.mapError("/path/./virtual/../foo.h", "/real/foo.h", |
| 126 |
CXError_InvalidArguments); |
| 127 |
} |
| 128 |
|
| 129 |
TEST(libclang, VirtualFileOverlay_RemapDirectories) { |
| 130 |
const char *contents = |
| 131 |
"{\n" |
| 132 |
" 'version': 0,\n" |
| 133 |
" 'roots': [\n" |
| 134 |
" {\n" |
| 135 |
" 'type': 'directory',\n" |
| 136 |
" 'name': \"/another/dir\",\n" |
| 137 |
" 'contents': [\n" |
| 138 |
" {\n" |
| 139 |
" 'type': 'file',\n" |
| 140 |
" 'name': \"foo2.h\",\n" |
| 141 |
" 'external-contents': \"/real/foo2.h\"\n" |
| 142 |
" }\n" |
| 143 |
" ]\n" |
| 144 |
" },\n" |
| 145 |
" {\n" |
| 146 |
" 'type': 'directory',\n" |
| 147 |
" 'name': \"/path/virtual/dir\",\n" |
| 148 |
" 'contents': [\n" |
| 149 |
" {\n" |
| 150 |
" 'type': 'file',\n" |
| 151 |
" 'name': \"foo1.h\",\n" |
| 152 |
" 'external-contents': \"/real/foo1.h\"\n" |
| 153 |
" },\n" |
| 154 |
" {\n" |
| 155 |
" 'type': 'file',\n" |
| 156 |
" 'name': \"foo3.h\",\n" |
| 157 |
" 'external-contents': \"/real/foo3.h\"\n" |
| 158 |
" },\n" |
| 159 |
" {\n" |
| 160 |
" 'type': 'directory',\n" |
| 161 |
" 'name': \"in/subdir\",\n" |
| 162 |
" 'contents': [\n" |
| 163 |
" {\n" |
| 164 |
" 'type': 'file',\n" |
| 165 |
" 'name': \"foo4.h\",\n" |
| 166 |
" 'external-contents': \"/real/foo4.h\"\n" |
| 167 |
" }\n" |
| 168 |
" ]\n" |
| 169 |
" }\n" |
| 170 |
" ]\n" |
| 171 |
" }\n" |
| 172 |
" ]\n" |
| 173 |
"}\n"; |
| 174 |
TestVFO T(contents); |
| 175 |
T.map("/path/virtual/dir/foo1.h", "/real/foo1.h"); |
| 176 |
T.map("/another/dir/foo2.h", "/real/foo2.h"); |
| 177 |
T.map("/path/virtual/dir/foo3.h", "/real/foo3.h"); |
| 178 |
T.map("/path/virtual/dir/in/subdir/foo4.h", "/real/foo4.h"); |
| 179 |
} |
| 180 |
|
| 181 |
TEST(libclang, VirtualFileOverlay_CaseInsensitive) { |
| 182 |
const char *contents = |
| 183 |
"{\n" |
| 184 |
" 'version': 0,\n" |
| 185 |
" 'case-sensitive': 'false',\n" |
| 186 |
" 'roots': [\n" |
| 187 |
" {\n" |
| 188 |
" 'type': 'directory',\n" |
| 189 |
" 'name': \"/path/virtual\",\n" |
| 190 |
" 'contents': [\n" |
| 191 |
" {\n" |
| 192 |
" 'type': 'file',\n" |
| 193 |
" 'name': \"foo.h\",\n" |
| 194 |
" 'external-contents': \"/real/foo.h\"\n" |
| 195 |
" }\n" |
| 196 |
" ]\n" |
| 197 |
" }\n" |
| 198 |
" ]\n" |
| 199 |
"}\n"; |
| 200 |
TestVFO T(contents); |
| 201 |
T.map("/path/virtual/foo.h", "/real/foo.h"); |
| 202 |
clang_VirtualFileOverlay_setCaseSensitivity(T.VFO, false); |
| 203 |
} |
| 204 |
|
| 205 |
TEST(libclang, VirtualFileOverlay_SharedPrefix) { |
| 206 |
const char *contents = |
| 207 |
"{\n" |
| 208 |
" 'version': 0,\n" |
| 209 |
" 'roots': [\n" |
| 210 |
" {\n" |
| 211 |
" 'type': 'directory',\n" |
| 212 |
" 'name': \"/path/foo\",\n" |
| 213 |
" 'contents': [\n" |
| 214 |
" {\n" |
| 215 |
" 'type': 'file',\n" |
| 216 |
" 'name': \"bar\",\n" |
| 217 |
" 'external-contents': \"/real/bar\"\n" |
| 218 |
" },\n" |
| 219 |
" {\n" |
| 220 |
" 'type': 'file',\n" |
| 221 |
" 'name': \"bar.h\",\n" |
| 222 |
" 'external-contents': \"/real/bar.h\"\n" |
| 223 |
" }\n" |
| 224 |
" ]\n" |
| 225 |
" },\n" |
| 226 |
" {\n" |
| 227 |
" 'type': 'directory',\n" |
| 228 |
" 'name': \"/path/foobar\",\n" |
| 229 |
" 'contents': [\n" |
| 230 |
" {\n" |
| 231 |
" 'type': 'file',\n" |
| 232 |
" 'name': \"baz.h\",\n" |
| 233 |
" 'external-contents': \"/real/baz.h\"\n" |
| 234 |
" }\n" |
| 235 |
" ]\n" |
| 236 |
" },\n" |
| 237 |
" {\n" |
| 238 |
" 'type': 'directory',\n" |
| 239 |
" 'name': \"/path\",\n" |
| 240 |
" 'contents': [\n" |
| 241 |
" {\n" |
| 242 |
" 'type': 'file',\n" |
| 243 |
" 'name': \"foobarbaz.h\",\n" |
| 244 |
" 'external-contents': \"/real/foobarbaz.h\"\n" |
| 245 |
" }\n" |
| 246 |
" ]\n" |
| 247 |
" }\n" |
| 248 |
" ]\n" |
| 249 |
"}\n"; |
| 250 |
TestVFO T(contents); |
| 251 |
T.map("/path/foo/bar.h", "/real/bar.h"); |
| 252 |
T.map("/path/foo/bar", "/real/bar"); |
| 253 |
T.map("/path/foobar/baz.h", "/real/baz.h"); |
| 254 |
T.map("/path/foobarbaz.h", "/real/foobarbaz.h"); |
| 255 |
} |
| 256 |
|
| 257 |
TEST(libclang, VirtualFileOverlay_AdjacentDirectory) { |
| 258 |
const char *contents = |
| 259 |
"{\n" |
| 260 |
" 'version': 0,\n" |
| 261 |
" 'roots': [\n" |
| 262 |
" {\n" |
| 263 |
" 'type': 'directory',\n" |
| 264 |
" 'name': \"/path/dir1\",\n" |
| 265 |
" 'contents': [\n" |
| 266 |
" {\n" |
| 267 |
" 'type': 'file',\n" |
| 268 |
" 'name': \"foo.h\",\n" |
| 269 |
" 'external-contents': \"/real/foo.h\"\n" |
| 270 |
" },\n" |
| 271 |
" {\n" |
| 272 |
" 'type': 'directory',\n" |
| 273 |
" 'name': \"subdir\",\n" |
| 274 |
" 'contents': [\n" |
| 275 |
" {\n" |
| 276 |
" 'type': 'file',\n" |
| 277 |
" 'name': \"bar.h\",\n" |
| 278 |
" 'external-contents': \"/real/bar.h\"\n" |
| 279 |
" }\n" |
| 280 |
" ]\n" |
| 281 |
" }\n" |
| 282 |
" ]\n" |
| 283 |
" },\n" |
| 284 |
" {\n" |
| 285 |
" 'type': 'directory',\n" |
| 286 |
" 'name': \"/path/dir2\",\n" |
| 287 |
" 'contents': [\n" |
| 288 |
" {\n" |
| 289 |
" 'type': 'file',\n" |
| 290 |
" 'name': \"baz.h\",\n" |
| 291 |
" 'external-contents': \"/real/baz.h\"\n" |
| 292 |
" }\n" |
| 293 |
" ]\n" |
| 294 |
" }\n" |
| 295 |
" ]\n" |
| 296 |
"}\n"; |
| 297 |
TestVFO T(contents); |
| 298 |
T.map("/path/dir1/foo.h", "/real/foo.h"); |
| 299 |
T.map("/path/dir1/subdir/bar.h", "/real/bar.h"); |
| 300 |
T.map("/path/dir2/baz.h", "/real/baz.h"); |
| 301 |
} |
| 302 |
|
| 303 |
TEST(libclang, VirtualFileOverlay_TopLevel) { |
| 304 |
const char *contents = |
| 305 |
"{\n" |
| 306 |
" 'version': 0,\n" |
| 307 |
" 'roots': [\n" |
| 308 |
" {\n" |
| 309 |
" 'type': 'directory',\n" |
| 310 |
" 'name': \"/\",\n" |
| 311 |
" 'contents': [\n" |
| 312 |
" {\n" |
| 313 |
" 'type': 'file',\n" |
| 314 |
" 'name': \"foo.h\",\n" |
| 315 |
" 'external-contents': \"/real/foo.h\"\n" |
| 316 |
" }\n" |
| 317 |
" ]\n" |
| 318 |
" }\n" |
| 319 |
" ]\n" |
| 320 |
"}\n"; |
| 321 |
TestVFO T(contents); |
| 322 |
T.map("/foo.h", "/real/foo.h"); |
| 323 |
} |
| 324 |
|
| 325 |
TEST(libclang, VirtualFileOverlay_Empty) { |
| 326 |
const char *contents = |
| 327 |
"{\n" |
| 328 |
" 'version': 0,\n" |
| 329 |
" 'roots': [\n" |
| 330 |
" ]\n" |
| 331 |
"}\n"; |
| 332 |
TestVFO T(contents); |
| 333 |
} |
| 334 |
|
| 335 |
TEST(libclang, ModuleMapDescriptor) { |
| 336 |
const char *Contents = |
| 337 |
"framework module TestFrame {\n" |
| 338 |
" umbrella header \"TestFrame.h\"\n" |
| 339 |
"\n" |
| 340 |
" export *\n" |
| 341 |
" module * { export * }\n" |
| 342 |
"}\n"; |
| 343 |
|
| 344 |
CXModuleMapDescriptor MMD = clang_ModuleMapDescriptor_create(0); |
| 345 |
|
| 346 |
clang_ModuleMapDescriptor_setFrameworkModuleName(MMD, "TestFrame"); |
| 347 |
clang_ModuleMapDescriptor_setUmbrellaHeader(MMD, "TestFrame.h"); |
| 348 |
|
| 349 |
char *BufPtr; |
| 350 |
unsigned BufSize; |
| 351 |
clang_ModuleMapDescriptor_writeToBuffer(MMD, 0, &BufPtr, &BufSize); |
| 352 |
std::string BufStr(BufPtr, BufSize); |
| 353 |
EXPECT_STREQ(Contents, BufStr.c_str()); |
| 354 |
clang_free(BufPtr); |
| 355 |
clang_ModuleMapDescriptor_dispose(MMD); |
| 356 |
} |
| 357 |
|
| 358 |
TEST_F(LibclangParseTest, AllSkippedRanges) { |
| 359 |
std::string Header = "header.h", Main = "main.cpp"; |
| 360 |
WriteFile(Header, |
| 361 |
"#ifdef MANGOS\n" |
| 362 |
"printf(\"mmm\");\n" |
| 363 |
"#endif"); |
| 364 |
WriteFile(Main, |
| 365 |
"#include \"header.h\"\n" |
| 366 |
"#ifdef KIWIS\n" |
| 367 |
"printf(\"mmm!!\");\n" |
| 368 |
"#endif"); |
| 369 |
|
| 370 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, |
| 371 |
nullptr, 0, TUFlags); |
| 372 |
|
| 373 |
CXSourceRangeList *Ranges = clang_getAllSkippedRanges(ClangTU); |
| 374 |
EXPECT_EQ(2U, Ranges->count); |
| 375 |
|
| 376 |
CXSourceLocation cxl; |
| 377 |
unsigned line; |
| 378 |
cxl = clang_getRangeStart(Ranges->ranges[0]); |
| 379 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 380 |
EXPECT_EQ(1U, line); |
| 381 |
cxl = clang_getRangeEnd(Ranges->ranges[0]); |
| 382 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 383 |
EXPECT_EQ(3U, line); |
| 384 |
|
| 385 |
cxl = clang_getRangeStart(Ranges->ranges[1]); |
| 386 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 387 |
EXPECT_EQ(2U, line); |
| 388 |
cxl = clang_getRangeEnd(Ranges->ranges[1]); |
| 389 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 390 |
EXPECT_EQ(4U, line); |
| 391 |
|
| 392 |
clang_disposeSourceRangeList(Ranges); |
| 393 |
} |
| 394 |
|
| 395 |
TEST_F(LibclangParseTest, EvaluateChildExpression) { |
| 396 |
std::string Main = "main.m"; |
| 397 |
WriteFile(Main, "#define kFOO @\"foo\"\n" |
| 398 |
"void foobar(void) {\n" |
| 399 |
" {kFOO;}\n" |
| 400 |
"}\n"); |
| 401 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr, |
| 402 |
0, TUFlags); |
| 403 |
|
| 404 |
CXCursor C = clang_getTranslationUnitCursor(ClangTU); |
| 405 |
clang_visitChildren( |
| 406 |
C, |
| 407 |
[](CXCursor cursor, CXCursor parent, |
| 408 |
CXClientData client_data) -> CXChildVisitResult { |
| 409 |
if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) { |
| 410 |
int numberedStmt = 0; |
| 411 |
clang_visitChildren( |
| 412 |
cursor, |
| 413 |
[](CXCursor cursor, CXCursor parent, |
| 414 |
CXClientData client_data) -> CXChildVisitResult { |
| 415 |
int &numberedStmt = *((int *)client_data); |
| 416 |
if (clang_getCursorKind(cursor) == CXCursor_CompoundStmt) { |
| 417 |
if (numberedStmt) { |
| 418 |
CXEvalResult RE = clang_Cursor_Evaluate(cursor); |
| 419 |
EXPECT_NE(RE, nullptr); |
| 420 |
EXPECT_EQ(clang_EvalResult_getKind(RE), |
| 421 |
CXEval_ObjCStrLiteral); |
| 422 |
clang_EvalResult_dispose(RE); |
| 423 |
return CXChildVisit_Break; |
| 424 |
} |
| 425 |
numberedStmt++; |
| 426 |
} |
| 427 |
return CXChildVisit_Recurse; |
| 428 |
}, |
| 429 |
&numberedStmt); |
| 430 |
EXPECT_EQ(numberedStmt, 1); |
| 431 |
} |
| 432 |
return CXChildVisit_Continue; |
| 433 |
}, |
| 434 |
nullptr); |
| 435 |
} |
| 436 |
|
| 437 |
class LibclangReparseTest : public LibclangParseTest { |
| 438 |
public: |
| 439 |
void DisplayDiagnostics() { |
| 440 |
unsigned NumDiagnostics = clang_getNumDiagnostics(ClangTU); |
| 441 |
for (unsigned i = 0; i < NumDiagnostics; ++i) { |
| 442 |
auto Diag = clang_getDiagnostic(ClangTU, i); |
| 443 |
LLVM_DEBUG(llvm::dbgs() |
| 444 |
<< clang_getCString(clang_formatDiagnostic( |
| 445 |
Diag, clang_defaultDiagnosticDisplayOptions())) |
| 446 |
<< "\n"); |
| 447 |
clang_disposeDiagnostic(Diag); |
| 448 |
} |
| 449 |
} |
| 450 |
bool ReparseTU(unsigned num_unsaved_files, CXUnsavedFile* unsaved_files) { |
| 451 |
if (clang_reparseTranslationUnit(ClangTU, num_unsaved_files, unsaved_files, |
| 452 |
clang_defaultReparseOptions(ClangTU))) { |
| 453 |
LLVM_DEBUG(llvm::dbgs() << "Reparse failed\n"); |
| 454 |
return false; |
| 455 |
} |
| 456 |
DisplayDiagnostics(); |
| 457 |
return true; |
| 458 |
} |
| 459 |
}; |
| 460 |
|
| 461 |
TEST_F(LibclangReparseTest, FileName) { |
| 462 |
std::string CppName = "main.cpp"; |
| 463 |
WriteFile(CppName, "int main() {}"); |
| 464 |
ClangTU = clang_parseTranslationUnit(Index, CppName.c_str(), nullptr, 0, |
| 465 |
nullptr, 0, TUFlags); |
| 466 |
CXFile cxf = clang_getFile(ClangTU, CppName.c_str()); |
| 467 |
|
| 468 |
CXString cxname = clang_getFileName(cxf); |
| 469 |
ASSERT_STREQ(clang_getCString(cxname), CppName.c_str()); |
| 470 |
clang_disposeString(cxname); |
| 471 |
|
| 472 |
cxname = clang_File_tryGetRealPathName(cxf); |
| 473 |
ASSERT_TRUE(llvm::StringRef(clang_getCString(cxname)).endswith("main.cpp")); |
| 474 |
clang_disposeString(cxname); |
| 475 |
} |
| 476 |
|
| 477 |
TEST_F(LibclangReparseTest, Reparse) { |
| 478 |
const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;"; |
| 479 |
const char *HeaderBottom = "\n};\n#endif\n"; |
| 480 |
const char *CppFile = "#include \"HeaderFile.h\"\nint main() {" |
| 481 |
" Foo foo; foo.bar = 7; foo.baz = 8; }\n"; |
| 482 |
std::string HeaderName = "HeaderFile.h"; |
| 483 |
std::string CppName = "CppFile.cpp"; |
| 484 |
WriteFile(CppName, CppFile); |
| 485 |
WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom); |
| 486 |
|
| 487 |
ClangTU = clang_parseTranslationUnit(Index, CppName.c_str(), nullptr, 0, |
| 488 |
nullptr, 0, TUFlags); |
| 489 |
EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); |
| 490 |
DisplayDiagnostics(); |
| 491 |
|
| 492 |
// Immedaitely reparse. |
| 493 |
ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); |
| 494 |
EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); |
| 495 |
|
| 496 |
std::string NewHeaderContents = |
| 497 |
std::string(HeaderTop) + "int baz;" + HeaderBottom; |
| 498 |
WriteFile(HeaderName, NewHeaderContents); |
| 499 |
|
| 500 |
// Reparse after fix. |
| 501 |
ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); |
| 502 |
EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); |
| 503 |
} |
| 504 |
|
| 505 |
TEST_F(LibclangReparseTest, ReparseWithModule) { |
| 506 |
const char *HeaderTop = "#ifndef H\n#define H\nstruct Foo { int bar;"; |
| 507 |
const char *HeaderBottom = "\n};\n#endif\n"; |
| 508 |
const char *MFile = "#include \"HeaderFile.h\"\nint main() {" |
| 509 |
" struct Foo foo; foo.bar = 7; foo.baz = 8; }\n"; |
| 510 |
const char *ModFile = "module A { header \"HeaderFile.h\" }\n"; |
| 511 |
std::string HeaderName = "HeaderFile.h"; |
| 512 |
std::string MName = "MFile.m"; |
| 513 |
std::string ModName = "module.modulemap"; |
| 514 |
WriteFile(MName, MFile); |
| 515 |
WriteFile(HeaderName, std::string(HeaderTop) + HeaderBottom); |
| 516 |
WriteFile(ModName, ModFile); |
| 517 |
|
| 518 |
std::string ModulesCache = std::string("-fmodules-cache-path=") + TestDir; |
| 519 |
const char *Args[] = { "-fmodules", ModulesCache.c_str(), |
| 520 |
"-I", TestDir.c_str() }; |
| 521 |
int NumArgs = std::size(Args); |
| 522 |
ClangTU = clang_parseTranslationUnit(Index, MName.c_str(), Args, NumArgs, |
| 523 |
nullptr, 0, TUFlags); |
| 524 |
EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); |
| 525 |
DisplayDiagnostics(); |
| 526 |
|
| 527 |
// Immedaitely reparse. |
| 528 |
ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); |
| 529 |
EXPECT_EQ(1U, clang_getNumDiagnostics(ClangTU)); |
| 530 |
|
| 531 |
std::string NewHeaderContents = |
| 532 |
std::string(HeaderTop) + "int baz;" + HeaderBottom; |
| 533 |
WriteFile(HeaderName, NewHeaderContents); |
| 534 |
|
| 535 |
// Reparse after fix. |
| 536 |
ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); |
| 537 |
EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); |
| 538 |
} |
| 539 |
|
| 540 |
TEST_F(LibclangReparseTest, clang_parseTranslationUnit2FullArgv) { |
| 541 |
// Provide a fake GCC 99.9.9 standard library that always overrides any local |
| 542 |
// GCC installation. |
| 543 |
std::string EmptyFiles[] = {"lib/gcc/arm-linux-gnueabi/99.9.9/crtbegin.o", |
| 544 |
"include/arm-linux-gnueabi/.keep", |
| 545 |
"include/c++/99.9.9/vector"}; |
| 546 |
|
| 547 |
for (auto &Name : EmptyFiles) |
| 548 |
WriteFile(Name, "\n"); |
| 549 |
|
| 550 |
std::string Filename = "test.cc"; |
| 551 |
WriteFile(Filename, "#include <vector>\n"); |
| 552 |
|
| 553 |
std::string Clang = "bin/clang"; |
| 554 |
WriteFile(Clang, ""); |
| 555 |
|
| 556 |
const char *Argv[] = {Clang.c_str(), "-target", "arm-linux-gnueabi", |
| 557 |
"-stdlib=libstdc++", "--gcc-toolchain="}; |
| 558 |
|
| 559 |
EXPECT_EQ(CXError_Success, |
| 560 |
clang_parseTranslationUnit2FullArgv(Index, Filename.c_str(), Argv, |
| 561 |
std::size(Argv), |
| 562 |
nullptr, 0, TUFlags, &ClangTU)); |
| 563 |
EXPECT_EQ(0U, clang_getNumDiagnostics(ClangTU)); |
| 564 |
DisplayDiagnostics(); |
| 565 |
} |
| 566 |
|
| 567 |
class LibclangPrintingPolicyTest : public LibclangParseTest { |
| 568 |
public: |
| 569 |
CXPrintingPolicy Policy = nullptr; |
| 570 |
|
| 571 |
void SetUp() override { |
| 572 |
LibclangParseTest::SetUp(); |
| 573 |
std::string File = "file.cpp"; |
| 574 |
WriteFile(File, "int i;\n"); |
| 575 |
ClangTU = clang_parseTranslationUnit(Index, File.c_str(), nullptr, 0, |
| 576 |
nullptr, 0, TUFlags); |
| 577 |
CXCursor TUCursor = clang_getTranslationUnitCursor(ClangTU); |
| 578 |
Policy = clang_getCursorPrintingPolicy(TUCursor); |
| 579 |
} |
| 580 |
void TearDown() override { |
| 581 |
clang_PrintingPolicy_dispose(Policy); |
| 582 |
LibclangParseTest::TearDown(); |
| 583 |
} |
| 584 |
}; |
| 585 |
|
| 586 |
TEST_F(LibclangPrintingPolicyTest, SetAndGetProperties) { |
| 587 |
for (unsigned Value = 0; Value < 2; ++Value) { |
| 588 |
for (int I = 0; I < CXPrintingPolicy_LastProperty; ++I) { |
| 589 |
auto Property = static_cast<enum CXPrintingPolicyProperty>(I); |
| 590 |
|
| 591 |
clang_PrintingPolicy_setProperty(Policy, Property, Value); |
| 592 |
EXPECT_EQ(Value, clang_PrintingPolicy_getProperty(Policy, Property)); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
TEST_F(LibclangReparseTest, PreprocessorSkippedRanges) { |
| 598 |
std::string Header = "header.h", Main = "main.cpp"; |
| 599 |
WriteFile(Header, |
| 600 |
"#ifdef MANGOS\n" |
| 601 |
"printf(\"mmm\");\n" |
| 602 |
"#endif"); |
| 603 |
WriteFile(Main, |
| 604 |
"#include \"header.h\"\n" |
| 605 |
"#ifdef GUAVA\n" |
| 606 |
"#endif\n" |
| 607 |
"#ifdef KIWIS\n" |
| 608 |
"printf(\"mmm!!\");\n" |
| 609 |
"#endif"); |
| 610 |
|
| 611 |
for (int i = 0; i != 3; ++i) { |
| 612 |
unsigned flags = TUFlags | CXTranslationUnit_PrecompiledPreamble; |
| 613 |
if (i == 2) |
| 614 |
flags |= CXTranslationUnit_CreatePreambleOnFirstParse; |
| 615 |
|
| 616 |
if (i != 0) |
| 617 |
clang_disposeTranslationUnit(ClangTU); // dispose from previous iter |
| 618 |
|
| 619 |
// parse once |
| 620 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, |
| 621 |
nullptr, 0, flags); |
| 622 |
if (i != 0) { |
| 623 |
// reparse |
| 624 |
ASSERT_TRUE(ReparseTU(0, nullptr /* No unsaved files. */)); |
| 625 |
} |
| 626 |
|
| 627 |
// Check all ranges are there |
| 628 |
CXSourceRangeList *Ranges = clang_getAllSkippedRanges(ClangTU); |
| 629 |
EXPECT_EQ(3U, Ranges->count); |
| 630 |
|
| 631 |
CXSourceLocation cxl; |
| 632 |
unsigned line; |
| 633 |
cxl = clang_getRangeStart(Ranges->ranges[0]); |
| 634 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 635 |
EXPECT_EQ(1U, line); |
| 636 |
cxl = clang_getRangeEnd(Ranges->ranges[0]); |
| 637 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 638 |
EXPECT_EQ(3U, line); |
| 639 |
|
| 640 |
cxl = clang_getRangeStart(Ranges->ranges[1]); |
| 641 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 642 |
EXPECT_EQ(2U, line); |
| 643 |
cxl = clang_getRangeEnd(Ranges->ranges[1]); |
| 644 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 645 |
EXPECT_EQ(3U, line); |
| 646 |
|
| 647 |
cxl = clang_getRangeStart(Ranges->ranges[2]); |
| 648 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 649 |
EXPECT_EQ(4U, line); |
| 650 |
cxl = clang_getRangeEnd(Ranges->ranges[2]); |
| 651 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 652 |
EXPECT_EQ(6U, line); |
| 653 |
|
| 654 |
clang_disposeSourceRangeList(Ranges); |
| 655 |
|
| 656 |
// Check obtaining ranges by each file works |
| 657 |
CXFile cxf = clang_getFile(ClangTU, Header.c_str()); |
| 658 |
Ranges = clang_getSkippedRanges(ClangTU, cxf); |
| 659 |
EXPECT_EQ(1U, Ranges->count); |
| 660 |
cxl = clang_getRangeStart(Ranges->ranges[0]); |
| 661 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 662 |
EXPECT_EQ(1U, line); |
| 663 |
clang_disposeSourceRangeList(Ranges); |
| 664 |
|
| 665 |
cxf = clang_getFile(ClangTU, Main.c_str()); |
| 666 |
Ranges = clang_getSkippedRanges(ClangTU, cxf); |
| 667 |
EXPECT_EQ(2U, Ranges->count); |
| 668 |
cxl = clang_getRangeStart(Ranges->ranges[0]); |
| 669 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 670 |
EXPECT_EQ(2U, line); |
| 671 |
cxl = clang_getRangeStart(Ranges->ranges[1]); |
| 672 |
clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr); |
| 673 |
EXPECT_EQ(4U, line); |
| 674 |
clang_disposeSourceRangeList(Ranges); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
class LibclangSerializationTest : public LibclangParseTest { |
| 679 |
public: |
| 680 |
bool SaveAndLoadTU(const std::string &Filename) { |
| 681 |
unsigned options = clang_defaultSaveOptions(ClangTU); |
| 682 |
if (clang_saveTranslationUnit(ClangTU, Filename.c_str(), options) != |
| 683 |
CXSaveError_None) { |
| 684 |
LLVM_DEBUG(llvm::dbgs() << "Saving failed\n"); |
| 685 |
return false; |
| 686 |
} |
| 687 |
|
| 688 |
clang_disposeTranslationUnit(ClangTU); |
| 689 |
|
| 690 |
ClangTU = clang_createTranslationUnit(Index, Filename.c_str()); |
| 691 |
|
| 692 |
if (!ClangTU) { |
| 693 |
LLVM_DEBUG(llvm::dbgs() << "Loading failed\n"); |
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
return true; |
| 698 |
} |
| 699 |
}; |
| 700 |
|
| 701 |
TEST_F(LibclangSerializationTest, TokenKindsAreCorrectAfterLoading) { |
| 702 |
// Ensure that "class" is recognized as a keyword token after serializing |
| 703 |
// and reloading the AST, as it is not a keyword for the default LangOptions. |
| 704 |
std::string HeaderName = "test.h"; |
| 705 |
WriteFile(HeaderName, "enum class Something {};"); |
| 706 |
|
| 707 |
const char *Argv[] = {"-xc++-header", "-std=c++11"}; |
| 708 |
|
| 709 |
ClangTU = clang_parseTranslationUnit(Index, HeaderName.c_str(), Argv, |
| 710 |
std::size(Argv), nullptr, |
| 711 |
0, TUFlags); |
| 712 |
|
| 713 |
auto CheckTokenKinds = [=]() { |
| 714 |
CXSourceRange Range = |
| 715 |
clang_getCursorExtent(clang_getTranslationUnitCursor(ClangTU)); |
| 716 |
|
| 717 |
CXToken *Tokens; |
| 718 |
unsigned int NumTokens; |
| 719 |
clang_tokenize(ClangTU, Range, &Tokens, &NumTokens); |
| 720 |
|
| 721 |
ASSERT_EQ(6u, NumTokens); |
| 722 |
EXPECT_EQ(CXToken_Keyword, clang_getTokenKind(Tokens[0])); |
| 723 |
EXPECT_EQ(CXToken_Keyword, clang_getTokenKind(Tokens[1])); |
| 724 |
EXPECT_EQ(CXToken_Identifier, clang_getTokenKind(Tokens[2])); |
| 725 |
EXPECT_EQ(CXToken_Punctuation, clang_getTokenKind(Tokens[3])); |
| 726 |
EXPECT_EQ(CXToken_Punctuation, clang_getTokenKind(Tokens[4])); |
| 727 |
EXPECT_EQ(CXToken_Punctuation, clang_getTokenKind(Tokens[5])); |
| 728 |
|
| 729 |
clang_disposeTokens(ClangTU, Tokens, NumTokens); |
| 730 |
}; |
| 731 |
|
| 732 |
CheckTokenKinds(); |
| 733 |
|
| 734 |
std::string ASTName = "test.ast"; |
| 735 |
WriteFile(ASTName, ""); |
| 736 |
|
| 737 |
ASSERT_TRUE(SaveAndLoadTU(ASTName)); |
| 738 |
|
| 739 |
CheckTokenKinds(); |
| 740 |
} |
| 741 |
|
| 742 |
TEST_F(LibclangParseTest, clang_getVarDeclInitializer) { |
| 743 |
std::string Main = "main.cpp"; |
| 744 |
WriteFile(Main, "int foo() { return 5; }; const int a = foo();"); |
| 745 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr, |
| 746 |
0, TUFlags); |
| 747 |
|
| 748 |
CXCursor C = clang_getTranslationUnitCursor(ClangTU); |
| 749 |
clang_visitChildren( |
| 750 |
C, |
| 751 |
[](CXCursor cursor, CXCursor parent, |
| 752 |
CXClientData client_data) -> CXChildVisitResult { |
| 753 |
if (clang_getCursorKind(cursor) == CXCursor_VarDecl) { |
| 754 |
const CXCursor Initializer = clang_Cursor_getVarDeclInitializer(cursor); |
| 755 |
EXPECT_FALSE(clang_Cursor_isNull(Initializer)); |
| 756 |
CXString Spelling = clang_getCursorSpelling(Initializer); |
| 757 |
const char* const SpellingCSstr = clang_getCString(Spelling); |
| 758 |
EXPECT_TRUE(SpellingCSstr); |
| 759 |
EXPECT_EQ(std::string(SpellingCSstr), std::string("foo")); |
| 760 |
clang_disposeString(Spelling); |
| 761 |
return CXChildVisit_Break; |
| 762 |
} |
| 763 |
return CXChildVisit_Continue; |
| 764 |
}, |
| 765 |
nullptr); |
| 766 |
} |
| 767 |
|
| 768 |
TEST_F(LibclangParseTest, clang_hasVarDeclGlobalStorageFalse) { |
| 769 |
std::string Main = "main.cpp"; |
| 770 |
WriteFile(Main, "void foo() { int a; }"); |
| 771 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr, |
| 772 |
0, TUFlags); |
| 773 |
|
| 774 |
CXCursor C = clang_getTranslationUnitCursor(ClangTU); |
| 775 |
clang_visitChildren( |
| 776 |
C, |
| 777 |
[](CXCursor cursor, CXCursor parent, |
| 778 |
CXClientData client_data) -> CXChildVisitResult { |
| 779 |
if (clang_getCursorKind(cursor) == CXCursor_VarDecl) { |
| 780 |
EXPECT_FALSE(clang_Cursor_hasVarDeclGlobalStorage(cursor)); |
| 781 |
return CXChildVisit_Break; |
| 782 |
} |
| 783 |
return CXChildVisit_Continue; |
| 784 |
}, |
| 785 |
nullptr); |
| 786 |
} |
| 787 |
|
| 788 |
TEST_F(LibclangParseTest, clang_Cursor_hasVarDeclGlobalStorageTrue) { |
| 789 |
std::string Main = "main.cpp"; |
| 790 |
WriteFile(Main, "int a;"); |
| 791 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr, |
| 792 |
0, TUFlags); |
| 793 |
|
| 794 |
CXCursor C = clang_getTranslationUnitCursor(ClangTU); |
| 795 |
clang_visitChildren( |
| 796 |
C, |
| 797 |
[](CXCursor cursor, CXCursor parent, |
| 798 |
CXClientData client_data) -> CXChildVisitResult { |
| 799 |
if (clang_getCursorKind(cursor) == CXCursor_VarDecl) { |
| 800 |
EXPECT_TRUE(clang_Cursor_hasVarDeclGlobalStorage(cursor)); |
| 801 |
return CXChildVisit_Break; |
| 802 |
} |
| 803 |
return CXChildVisit_Continue; |
| 804 |
}, |
| 805 |
nullptr); |
| 806 |
} |
| 807 |
|
| 808 |
TEST_F(LibclangParseTest, clang_Cursor_hasVarDeclExternalStorageFalse) { |
| 809 |
std::string Main = "main.cpp"; |
| 810 |
WriteFile(Main, "int a;"); |
| 811 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr, |
| 812 |
0, TUFlags); |
| 813 |
|
| 814 |
CXCursor C = clang_getTranslationUnitCursor(ClangTU); |
| 815 |
clang_visitChildren( |
| 816 |
C, |
| 817 |
[](CXCursor cursor, CXCursor parent, |
| 818 |
CXClientData client_data) -> CXChildVisitResult { |
| 819 |
if (clang_getCursorKind(cursor) == CXCursor_VarDecl) { |
| 820 |
EXPECT_FALSE(clang_Cursor_hasVarDeclExternalStorage(cursor)); |
| 821 |
return CXChildVisit_Break; |
| 822 |
} |
| 823 |
return CXChildVisit_Continue; |
| 824 |
}, |
| 825 |
nullptr); |
| 826 |
} |
| 827 |
|
| 828 |
TEST_F(LibclangParseTest, clang_Cursor_hasVarDeclExternalStorageTrue) { |
| 829 |
std::string Main = "main.cpp"; |
| 830 |
WriteFile(Main, "extern int a;"); |
| 831 |
ClangTU = clang_parseTranslationUnit(Index, Main.c_str(), nullptr, 0, nullptr, |
| 832 |
0, TUFlags); |
| 833 |
|
| 834 |
CXCursor C = clang_getTranslationUnitCursor(ClangTU); |
| 835 |
clang_visitChildren( |
| 836 |
C, |
| 837 |
[](CXCursor cursor, CXCursor parent, |
| 838 |
CXClientData client_data) -> CXChildVisitResult { |
| 839 |
if (clang_getCursorKind(cursor) == CXCursor_VarDecl) { |
| 840 |
EXPECT_TRUE(clang_Cursor_hasVarDeclExternalStorage(cursor)); |
| 841 |
return CXChildVisit_Break; |
| 842 |
} |
| 843 |
return CXChildVisit_Continue; |
| 844 |
}, |
| 845 |
nullptr); |
| 846 |
} |
| 847 |
|
| 848 |
TEST_F(LibclangParseTest, clang_getUnqualifiedTypeRemovesQualifiers) { |
| 849 |
std::string Header = "header.h"; |
| 850 |
WriteFile(Header, "void foo1(const int);\n" |
| 851 |
"void foo2(volatile int);\n" |
| 852 |
"void foo3(const volatile int);\n" |
| 853 |
"void foo4(int* const);\n" |
| 854 |
"void foo5(int* volatile);\n" |
| 855 |
"void foo6(int* restrict);\n" |
| 856 |
"void foo7(int* const volatile);\n" |
| 857 |
"void foo8(int* volatile restrict);\n" |
| 858 |
"void foo9(int* const restrict);\n" |
| 859 |
"void foo10(int* const volatile restrict);\n"); |
| 860 |
|
| 861 |
auto is_qualified = [](CXType type) -> bool { |
| 862 |
return clang_isConstQualifiedType(type) || |
| 863 |
clang_isVolatileQualifiedType(type) || |
| 864 |
clang_isRestrictQualifiedType(type); |
| 865 |
}; |
| 866 |
|
| 867 |
ClangTU = clang_parseTranslationUnit(Index, Header.c_str(), nullptr, 0, |
| 868 |
nullptr, 0, TUFlags); |
| 869 |
|
| 870 |
Traverse([&is_qualified](CXCursor cursor, CXCursor) { |
| 871 |
if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) { |
| 872 |
CXType arg_type = clang_getArgType(clang_getCursorType(cursor), 0); |
| 873 |
EXPECT_TRUE(is_qualified(arg_type)) |
| 874 |
<< "Input data '" << fromCXString(clang_getCursorSpelling(cursor)) |
| 875 |
<< "' first argument does not have a qualified type."; |
| 876 |
|
| 877 |
CXType unqualified_arg_type = clang_getUnqualifiedType(arg_type); |
| 878 |
EXPECT_FALSE(is_qualified(unqualified_arg_type)) |
| 879 |
<< "The type '" << fromCXString(clang_getTypeSpelling(arg_type)) |
| 880 |
<< "' was not unqualified after a call to clang_getUnqualifiedType."; |
| 881 |
} |
| 882 |
|
| 883 |
return CXChildVisit_Continue; |
| 884 |
}); |
| 885 |
} |
| 886 |
|
| 887 |
TEST_F(LibclangParseTest, clang_getNonReferenceTypeRemovesRefQualifiers) { |
| 888 |
std::string Header = "header.h"; |
| 889 |
WriteFile(Header, "void foo1(int&);\n" |
| 890 |
"void foo2(int&&);\n"); |
| 891 |
|
| 892 |
auto is_ref_qualified = [](CXType type) -> bool { |
| 893 |
return (type.kind == CXType_LValueReference) || |
| 894 |
(type.kind == CXType_RValueReference); |
| 895 |
}; |
| 896 |
|
| 897 |
const char *Args[] = {"-xc++"}; |
| 898 |
ClangTU = clang_parseTranslationUnit(Index, Header.c_str(), Args, 1, nullptr, |
| 899 |
0, TUFlags); |
| 900 |
|
| 901 |
Traverse([&is_ref_qualified](CXCursor cursor, CXCursor) { |
| 902 |
if (clang_getCursorKind(cursor) == CXCursor_FunctionDecl) { |
| 903 |
CXType arg_type = clang_getArgType(clang_getCursorType(cursor), 0); |
| 904 |
EXPECT_TRUE(is_ref_qualified(arg_type)) |
| 905 |
<< "Input data '" << fromCXString(clang_getCursorSpelling(cursor)) |
| 906 |
<< "' first argument does not have a ref-qualified type."; |
| 907 |
|
| 908 |
CXType non_reference_arg_type = clang_getNonReferenceType(arg_type); |
| 909 |
EXPECT_FALSE(is_ref_qualified(non_reference_arg_type)) |
| 910 |
<< "The type '" << fromCXString(clang_getTypeSpelling(arg_type)) |
| 911 |
<< "' ref-qualifier was not removed after a call to " |
| 912 |
"clang_getNonReferenceType."; |
| 913 |
} |
| 914 |
|
| 915 |
return CXChildVisit_Continue; |
| 916 |
}); |
| 917 |
} |
| 918 |
|
| 919 |
TEST_F(LibclangParseTest, VisitUsingTypeLoc) { |
| 920 |
const char testSource[] = R"cpp( |
| 921 |
namespace ns1 { |
| 922 |
class Class1 |
| 923 |
{ |
| 924 |
void fun(); |
| 925 |
}; |
| 926 |
} |
| 927 |
|
| 928 |
using ns1::Class1; |
| 929 |
|
| 930 |
void Class1::fun() {} |
| 931 |
)cpp"; |
| 932 |
std::string fileName = "main.cpp"; |
| 933 |
WriteFile(fileName, testSource); |
| 934 |
const char *Args[] = {"-xc++"}; |
| 935 |
ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1, |
| 936 |
nullptr, 0, TUFlags); |
| 937 |
|
| 938 |
std::optional<CXCursor> typeRefCsr; |
| 939 |
Traverse([&](CXCursor cursor, CXCursor parent) -> CXChildVisitResult { |
| 940 |
if (cursor.kind == CXCursor_TypeRef) { |
| 941 |
typeRefCsr.emplace(cursor); |
| 942 |
} |
| 943 |
return CXChildVisit_Recurse; |
| 944 |
}); |
| 945 |
ASSERT_TRUE(typeRefCsr.has_value()); |
| 946 |
EXPECT_EQ(fromCXString(clang_getCursorSpelling(*typeRefCsr)), |
| 947 |
"class ns1::Class1"); |
| 948 |
} |
| 949 |
|
| 950 |
class LibclangRewriteTest : public LibclangParseTest { |
| 951 |
public: |
| 952 |
CXRewriter Rew = nullptr; |
| 953 |
std::string Filename; |
| 954 |
CXFile File = nullptr; |
| 955 |
|
| 956 |
void SetUp() override { |
| 957 |
LibclangParseTest::SetUp(); |
| 958 |
Filename = "file.cpp"; |
| 959 |
WriteFile(Filename, "int main() { return 0; }"); |
| 960 |
ClangTU = clang_parseTranslationUnit(Index, Filename.c_str(), nullptr, 0, |
| 961 |
nullptr, 0, TUFlags); |
| 962 |
Rew = clang_CXRewriter_create(ClangTU); |
| 963 |
File = clang_getFile(ClangTU, Filename.c_str()); |
| 964 |
} |
| 965 |
void TearDown() override { |
| 966 |
clang_CXRewriter_dispose(Rew); |
| 967 |
LibclangParseTest::TearDown(); |
| 968 |
} |
| 969 |
}; |
| 970 |
|
| 971 |
static std::string getFileContent(const std::string& Filename) { |
| 972 |
std::ifstream RewrittenFile(Filename); |
| 973 |
std::string RewrittenFileContent; |
| 974 |
std::string Line; |
| 975 |
while (std::getline(RewrittenFile, Line)) { |
| 976 |
if (RewrittenFileContent.empty()) |
| 977 |
RewrittenFileContent = Line; |
| 978 |
else { |
| 979 |
RewrittenFileContent += "\n" + Line; |
| 980 |
} |
| 981 |
} |
| 982 |
return RewrittenFileContent; |
| 983 |
} |
| 984 |
|
| 985 |
TEST_F(LibclangRewriteTest, RewriteReplace) { |
| 986 |
CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 987 |
CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 988 |
CXSourceRange Rng = clang_getRange(B, E); |
| 989 |
|
| 990 |
clang_CXRewriter_replaceText(Rew, Rng, "MAIN"); |
| 991 |
|
| 992 |
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 993 |
EXPECT_EQ(getFileContent(Filename), "int MAIN() { return 0; }"); |
| 994 |
} |
| 995 |
|
| 996 |
TEST_F(LibclangRewriteTest, RewriteReplaceShorter) { |
| 997 |
CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 998 |
CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 999 |
CXSourceRange Rng = clang_getRange(B, E); |
| 1000 |
|
| 1001 |
clang_CXRewriter_replaceText(Rew, Rng, "foo"); |
| 1002 |
|
| 1003 |
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 1004 |
EXPECT_EQ(getFileContent(Filename), "int foo() { return 0; }"); |
| 1005 |
} |
| 1006 |
|
| 1007 |
TEST_F(LibclangRewriteTest, RewriteReplaceLonger) { |
| 1008 |
CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 1009 |
CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 1010 |
CXSourceRange Rng = clang_getRange(B, E); |
| 1011 |
|
| 1012 |
clang_CXRewriter_replaceText(Rew, Rng, "patatino"); |
| 1013 |
|
| 1014 |
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 1015 |
EXPECT_EQ(getFileContent(Filename), "int patatino() { return 0; }"); |
| 1016 |
} |
| 1017 |
|
| 1018 |
TEST_F(LibclangRewriteTest, RewriteInsert) { |
| 1019 |
CXSourceLocation Loc = clang_getLocation(ClangTU, File, 1, 5); |
| 1020 |
|
| 1021 |
clang_CXRewriter_insertTextBefore(Rew, Loc, "ro"); |
| 1022 |
|
| 1023 |
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 1024 |
EXPECT_EQ(getFileContent(Filename), "int romain() { return 0; }"); |
| 1025 |
} |
| 1026 |
|
| 1027 |
TEST_F(LibclangRewriteTest, RewriteRemove) { |
| 1028 |
CXSourceLocation B = clang_getLocation(ClangTU, File, 1, 5); |
| 1029 |
CXSourceLocation E = clang_getLocation(ClangTU, File, 1, 9); |
| 1030 |
CXSourceRange Rng = clang_getRange(B, E); |
| 1031 |
|
| 1032 |
clang_CXRewriter_removeText(Rew, Rng); |
| 1033 |
|
| 1034 |
ASSERT_EQ(clang_CXRewriter_overwriteChangedFiles(Rew), 0); |
| 1035 |
EXPECT_EQ(getFileContent(Filename), "int () { return 0; }"); |
| 1036 |
} |