| 1 |
//===- DependencyScanningWorkerTest.cpp -----------------------------------===// |
| 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/DependencyScanning/DependencyScanningWorker.h" |
| 10 |
#include "clang/DependencyScanning/DependencyScanningUtils.h" |
| 11 |
#include "llvm/Support/FormatVariadic.h" |
| 12 |
#include "gtest/gtest.h" |
| 13 |
#include <string> |
| 14 |
|
| 15 |
using namespace clang; |
| 16 |
using namespace dependencies; |
| 17 |
|
| 18 |
TEST(DependencyScanner, ScanDepsWithDiagConsumer) { |
| 19 |
StringRef CWD = "/root"; |
| 20 |
|
| 21 |
auto VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); |
| 22 |
VFS->setCurrentWorkingDirectory(CWD); |
| 23 |
auto Sept = llvm::sys::path::get_separator(); |
| 24 |
std::string HeaderPath = |
| 25 |
std::string(llvm::formatv("{0}root{0}header.h", Sept)); |
| 26 |
std::string TestPath = std::string(llvm::formatv("{0}root{0}test.cpp", Sept)); |
| 27 |
std::string AsmPath = std::string(llvm::formatv("{0}root{0}test.s", Sept)); |
| 28 |
|
| 29 |
VFS->addFile(HeaderPath, 0, llvm::MemoryBuffer::getMemBuffer("\n")); |
| 30 |
VFS->addFile(TestPath, 0, |
| 31 |
llvm::MemoryBuffer::getMemBuffer("#include \"header.h\"\n")); |
| 32 |
VFS->addFile(AsmPath, 0, llvm::MemoryBuffer::getMemBuffer("")); |
| 33 |
|
| 34 |
DependencyScanningService Service(ScanningMode::DependencyDirectivesScan, |
| 35 |
ScanningOutputFormat::Make); |
| 36 |
DependencyScanningWorker Worker(Service, VFS); |
| 37 |
|
| 38 |
llvm::DenseSet<ModuleID> AlreadySeen; |
| 39 |
FullDependencyConsumer DC(AlreadySeen); |
| 40 |
CallbackActionController AC(nullptr); |
| 41 |
|
| 42 |
struct EnsureFinishedConsumer : public DiagnosticConsumer { |
| 43 |
bool Finished = false; |
| 44 |
void finish() override { Finished = true; } |
| 45 |
}; |
| 46 |
|
| 47 |
{ |
| 48 |
// Check that a successful scan calls DiagConsumer.finish(). |
| 49 |
std::vector<std::string> Args = {"clang", |
| 50 |
"-cc1", |
| 51 |
"-triple", |
| 52 |
"x86_64-apple-macosx10.7", |
| 53 |
"-emit-obj", |
| 54 |
"test.cpp", |
| 55 |
"-o" |
| 56 |
"test.cpp.o"}; |
| 57 |
|
| 58 |
EnsureFinishedConsumer DiagConsumer; |
| 59 |
bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 60 |
|
| 61 |
EXPECT_TRUE(Success); |
| 62 |
EXPECT_EQ(DiagConsumer.getNumErrors(), 0u); |
| 63 |
EXPECT_TRUE(DiagConsumer.Finished); |
| 64 |
} |
| 65 |
|
| 66 |
{ |
| 67 |
// Check that an invalid command-line, which never enters the scanning |
| 68 |
// action calls DiagConsumer.finish(). |
| 69 |
std::vector<std::string> Args = {"clang", "-cc1", "-invalid-arg"}; |
| 70 |
EnsureFinishedConsumer DiagConsumer; |
| 71 |
bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 72 |
|
| 73 |
EXPECT_FALSE(Success); |
| 74 |
EXPECT_GE(DiagConsumer.getNumErrors(), 1u); |
| 75 |
EXPECT_TRUE(DiagConsumer.Finished); |
| 76 |
} |
| 77 |
|
| 78 |
{ |
| 79 |
// Check that a valid command line that produces no scanning jobs calls |
| 80 |
// DiagConsumer.finish(). |
| 81 |
std::vector<std::string> Args = {"clang", |
| 82 |
"-cc1", |
| 83 |
"-triple", |
| 84 |
"x86_64-apple-macosx10.7", |
| 85 |
"-emit-obj", |
| 86 |
"-x", |
| 87 |
"assembler", |
| 88 |
"test.s", |
| 89 |
"-o" |
| 90 |
"test.cpp.o"}; |
| 91 |
|
| 92 |
EnsureFinishedConsumer DiagConsumer; |
| 93 |
bool Success = Worker.computeDependencies(CWD, Args, DC, AC, DiagConsumer); |
| 94 |
|
| 95 |
EXPECT_FALSE(Success); |
| 96 |
EXPECT_EQ(DiagConsumer.getNumErrors(), 1u); |
| 97 |
EXPECT_TRUE(DiagConsumer.Finished); |
| 98 |
} |
| 99 |
} |