|
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/MC/TargetRegistry.h"
|
@@ -302,3 +303,84 @@ TEST(DependencyScanner, ScanDepsWithModuleLookup) {
|
302 | 303 | InterceptFS->StatPaths.end());
|
303 | 304 | EXPECT_EQ(InterceptFS->ReadFiles, std::vector<std::string>{"test.m"});
|
304 | 305 | }
|
| 306 | + |
| 307 | +TEST(DependencyScanner, ScanDepsWithDiagConsumer) { |
| 308 | + StringRef CWD = "/root"; |
| 309 | + |
| 310 | + auto VFS = new llvm::vfs::InMemoryFileSystem(); |
| 311 | + VFS->setCurrentWorkingDirectory(CWD); |
| 312 | + auto Sept = llvm::sys::path::get_separator(); |
| 313 | + std::string HeaderPath = |
| 314 | + std::string(llvm::formatv("{0}root{0}header.h", Sept)); |
| 315 | + std::string TestPath = std::string(llvm::formatv("{0}root{0}test.cpp", Sept)); |
| 316 | + std::string AsmPath = std::string(llvm::formatv("{0}root{0}test.s", Sept)); |
| 317 | + |
| 318 | + VFS->addFile(HeaderPath, 0, llvm::MemoryBuffer::getMemBuffer("\n")); |
| 319 | + VFS->addFile(TestPath, 0, |
| 320 | + llvm::MemoryBuffer::getMemBuffer("#include \"header.h\"\n")); |
| 321 | + VFS->addFile(AsmPath, 0, llvm::MemoryBuffer::getMemBuffer("")); |
| 322 | + |
| 323 | + DependencyScanningService Service(ScanningMode::DependencyDirectivesScan, |
| 324 | + ScanningOutputFormat::Make); |
| 325 | + DependencyScanningWorker Worker(Service, VFS); |
| 326 | + |
| 327 | + llvm::DenseSet<ModuleID> AlreadySeen; |
| 328 | + FullDependencyConsumer DC(AlreadySeen); |
| 329 | + CallbackActionController AC(nullptr); |
| 330 | + |
| 331 | + struct EnsureFinishedConsumer : public DiagnosticConsumer { |
| 332 | + bool Finished = false; |
| 333 | + void finish() override { Finished = true; } |
| 334 | + }; |
| 335 | + |
| 336 | + { |
| 337 | + // Check that a successful scan calls DiagConsumer.finish(). |
| 338 | + std::vector<std::string> Args = {"clang", |
| 339 | + "-target", |
| 340 | + "x86_64-apple-macosx10.7", |
| 341 | + "-c", |
| 342 | + "test.cpp", |
| 343 | + "-o" |
| 344 | + "test.cpp.o"}; |
| 345 | + |
| 346 | + EnsureFinishedConsumer DiagConsumer; |
| 347 | + bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 348 | + |
| 349 | + EXPECT_TRUE(Success); |
| 350 | + EXPECT_EQ(DiagConsumer.getNumErrors(), 0u); |
| 351 | + EXPECT_TRUE(DiagConsumer.Finished); |
| 352 | + } |
| 353 | + |
| 354 | + { |
| 355 | + // Check that an invalid command-line, which never enters the scanning |
| 356 | + // action calls DiagConsumer.finish(). |
| 357 | + std::vector<std::string> Args = {"clang", "-invalid-arg"}; |
| 358 | + EnsureFinishedConsumer DiagConsumer; |
| 359 | + bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 360 | + |
| 361 | + EXPECT_FALSE(Success); |
| 362 | + EXPECT_GE(DiagConsumer.getNumErrors(), 1u); |
| 363 | + EXPECT_TRUE(DiagConsumer.Finished); |
| 364 | + } |
| 365 | + |
| 366 | + { |
| 367 | + // Check that a valid command line that produces no scanning jobs calls |
| 368 | + // DiagConsumer.finish(). |
| 369 | + std::vector<std::string> Args = {"clang", |
| 370 | + "-target", |
| 371 | + "x86_64-apple-macosx10.7", |
| 372 | + "-c", |
| 373 | + "-x", |
| 374 | + "assembler", |
| 375 | + "test.s", |
| 376 | + "-o" |
| 377 | + "test.cpp.o"}; |
| 378 | + |
| 379 | + EnsureFinishedConsumer DiagConsumer; |
| 380 | + bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 381 | + |
| 382 | + EXPECT_FALSE(Success); |
| 383 | + EXPECT_EQ(DiagConsumer.getNumErrors(), 1u); |
| 384 | + EXPECT_TRUE(DiagConsumer.Finished); |
| 385 | + } |
| 386 | +} |
0 commit comments