|
15 | 15 | #include "clang/Frontend/FrontendActions.h"
|
16 | 16 | #include "clang/Tooling/CompilationDatabase.h"
|
17 | 17 | #include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
|
| 18 | +#include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h" |
18 | 19 | #include "clang/Tooling/Tooling.h"
|
19 | 20 | #include "llvm/ADT/STLExtras.h"
|
20 | 21 | #include "llvm/CAS/CASProvidingFileSystem.h"
|
@@ -357,3 +358,85 @@ TEST(DependencyScanner, ScanDepsWithModuleLookup) {
|
357 | 358 | InterceptFS->StatPaths.end());
|
358 | 359 | EXPECT_EQ(InterceptFS->ReadFiles, std::vector<std::string>{"test.m"});
|
359 | 360 | }
|
| 361 | + |
| 362 | +TEST(DependencyScanner, ScanDepsWithDiagConsumer) { |
| 363 | + StringRef CWD = "/root"; |
| 364 | + |
| 365 | + auto VFS = new llvm::vfs::InMemoryFileSystem(); |
| 366 | + VFS->setCurrentWorkingDirectory(CWD); |
| 367 | + auto Sept = llvm::sys::path::get_separator(); |
| 368 | + std::string HeaderPath = |
| 369 | + std::string(llvm::formatv("{0}root{0}header.h", Sept)); |
| 370 | + std::string TestPath = std::string(llvm::formatv("{0}root{0}test.cpp", Sept)); |
| 371 | + std::string AsmPath = std::string(llvm::formatv("{0}root{0}test.s", Sept)); |
| 372 | + |
| 373 | + VFS->addFile(HeaderPath, 0, llvm::MemoryBuffer::getMemBuffer("\n")); |
| 374 | + VFS->addFile(TestPath, 0, |
| 375 | + llvm::MemoryBuffer::getMemBuffer("#include \"header.h\"\n")); |
| 376 | + VFS->addFile(AsmPath, 0, llvm::MemoryBuffer::getMemBuffer("")); |
| 377 | + |
| 378 | + DependencyScanningService Service(ScanningMode::DependencyDirectivesScan, |
| 379 | + ScanningOutputFormat::Make, CASOptions(), |
| 380 | + nullptr, nullptr, nullptr); |
| 381 | + DependencyScanningWorker Worker(Service, VFS); |
| 382 | + |
| 383 | + llvm::DenseSet<ModuleID> AlreadySeen; |
| 384 | + FullDependencyConsumer DC(AlreadySeen); |
| 385 | + CallbackActionController AC(nullptr); |
| 386 | + |
| 387 | + struct EnsureFinishedConsumer : public DiagnosticConsumer { |
| 388 | + bool Finished = false; |
| 389 | + void finish() override { Finished = true; } |
| 390 | + }; |
| 391 | + |
| 392 | + { |
| 393 | + // Check that a successful scan calls DiagConsumer.finish(). |
| 394 | + std::vector<std::string> Args = {"clang", |
| 395 | + "-target", |
| 396 | + "x86_64-apple-macosx10.7", |
| 397 | + "-c", |
| 398 | + "test.cpp", |
| 399 | + "-o" |
| 400 | + "test.cpp.o"}; |
| 401 | + |
| 402 | + EnsureFinishedConsumer DiagConsumer; |
| 403 | + bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 404 | + |
| 405 | + EXPECT_TRUE(Success); |
| 406 | + EXPECT_EQ(DiagConsumer.getNumErrors(), 0u); |
| 407 | + EXPECT_TRUE(DiagConsumer.Finished); |
| 408 | + } |
| 409 | + |
| 410 | + { |
| 411 | + // Check that an invalid command-line, which never enters the scanning |
| 412 | + // action calls DiagConsumer.finish(). |
| 413 | + std::vector<std::string> Args = {"clang", "-invalid-arg"}; |
| 414 | + EnsureFinishedConsumer DiagConsumer; |
| 415 | + bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 416 | + |
| 417 | + EXPECT_FALSE(Success); |
| 418 | + EXPECT_GE(DiagConsumer.getNumErrors(), 1u); |
| 419 | + EXPECT_TRUE(DiagConsumer.Finished); |
| 420 | + } |
| 421 | + |
| 422 | + { |
| 423 | + // Check that a valid command line that produces no scanning jobs calls |
| 424 | + // DiagConsumer.finish(). |
| 425 | + std::vector<std::string> Args = {"clang", |
| 426 | + "-target", |
| 427 | + "x86_64-apple-macosx10.7", |
| 428 | + "-c", |
| 429 | + "-x", |
| 430 | + "assembler", |
| 431 | + "test.s", |
| 432 | + "-o" |
| 433 | + "test.cpp.o"}; |
| 434 | + |
| 435 | + EnsureFinishedConsumer DiagConsumer; |
| 436 | + bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 437 | + |
| 438 | + EXPECT_FALSE(Success); |
| 439 | + EXPECT_EQ(DiagConsumer.getNumErrors(), 1u); |
| 440 | + EXPECT_TRUE(DiagConsumer.Finished); |
| 441 | + } |
| 442 | +} |
0 commit comments