|
9 | 9 | #include "clang/AST/ASTConsumer.h"
|
10 | 10 | #include "clang/AST/DeclCXX.h"
|
11 | 11 | #include "clang/AST/DeclGroup.h"
|
| 12 | +#include "clang/Driver/Compilation.h" |
| 13 | +#include "clang/Driver/Driver.h" |
12 | 14 | #include "clang/Frontend/ASTUnit.h"
|
13 | 15 | #include "clang/Frontend/CompilerInstance.h"
|
14 | 16 | #include "clang/Frontend/FrontendAction.h"
|
|
18 | 20 | #include "clang/Tooling/Tooling.h"
|
19 | 21 | #include "llvm/ADT/STLExtras.h"
|
20 | 22 | #include "llvm/ADT/StringRef.h"
|
| 23 | +#include "llvm/Support/Host.h" |
21 | 24 | #include "llvm/Support/Path.h"
|
22 | 25 | #include "llvm/Support/TargetRegistry.h"
|
23 | 26 | #include "llvm/Support/TargetSelect.h"
|
@@ -258,6 +261,105 @@ TEST(ToolInvocation, DiagConsumerExpectingSourceManager) {
|
258 | 261 | EXPECT_TRUE(Consumer.SawSourceManager);
|
259 | 262 | }
|
260 | 263 |
|
| 264 | +namespace { |
| 265 | +/// Overlays the real filesystem with the given VFS and returns the result. |
| 266 | +llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> |
| 267 | +overlayRealFS(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
| 268 | + auto RFS = llvm::vfs::getRealFileSystem(); |
| 269 | + auto OverlayFS = llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(RFS); |
| 270 | + OverlayFS->pushOverlay(VFS); |
| 271 | + return OverlayFS; |
| 272 | +} |
| 273 | + |
| 274 | +struct CommandLineExtractorTest : public ::testing::Test { |
| 275 | + llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFS; |
| 276 | + llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
| 277 | + driver::Driver Driver; |
| 278 | + |
| 279 | +public: |
| 280 | + CommandLineExtractorTest() |
| 281 | + : InMemoryFS(new llvm::vfs::InMemoryFileSystem), |
| 282 | + Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions)), |
| 283 | + Driver("clang", llvm::sys::getDefaultTargetTriple(), *Diags, |
| 284 | + "clang LLVM compiler", overlayRealFS(InMemoryFS)) {} |
| 285 | + |
| 286 | + void addFile(StringRef Name, StringRef Content) { |
| 287 | + InMemoryFS->addFile(Name, 0, llvm::MemoryBuffer::getMemBuffer(Content)); |
| 288 | + } |
| 289 | + |
| 290 | + const llvm::opt::ArgStringList * |
| 291 | + extractCC1Arguments(llvm::ArrayRef<const char *> Argv) { |
| 292 | + const std::unique_ptr<driver::Compilation> Compilation( |
| 293 | + Driver.BuildCompilation(llvm::makeArrayRef(Argv))); |
| 294 | + |
| 295 | + return getCC1Arguments(Diags.get(), Compilation.get()); |
| 296 | + } |
| 297 | +}; |
| 298 | +} // namespace |
| 299 | + |
| 300 | +TEST_F(CommandLineExtractorTest, AcceptOffloading) { |
| 301 | + addFile("test.c", "int main() {}\n"); |
| 302 | + const char *Args[] = {"clang", "-target", "arm64-apple-macosx11.0.0", |
| 303 | + "-x", "hip", "test.c", |
| 304 | + "-nogpulib", "-nogpuinc"}; |
| 305 | + EXPECT_NE(extractCC1Arguments(Args), nullptr); |
| 306 | +} |
| 307 | + |
| 308 | +TEST_F(CommandLineExtractorTest, AcceptOffloadingCompile) { |
| 309 | + addFile("test.c", "int main() {}\n"); |
| 310 | + const char *Args[] = {"clang", "-target", "arm64-apple-macosx11.0.0", |
| 311 | + "-c", "-x", "hip", |
| 312 | + "test.c", "-nogpulib", "-nogpuinc"}; |
| 313 | + EXPECT_NE(extractCC1Arguments(Args), nullptr); |
| 314 | +} |
| 315 | + |
| 316 | +TEST_F(CommandLineExtractorTest, AcceptOffloadingSyntaxOnly) { |
| 317 | + addFile("test.c", "int main() {}\n"); |
| 318 | + const char *Args[] = { |
| 319 | + "clang", "-target", "arm64-apple-macosx11.0.0", |
| 320 | + "-fsyntax-only", "-x", "hip", |
| 321 | + "test.c", "-nogpulib", "-nogpuinc"}; |
| 322 | + EXPECT_NE(extractCC1Arguments(Args), nullptr); |
| 323 | +} |
| 324 | + |
| 325 | +TEST_F(CommandLineExtractorTest, AcceptExternalAssembler) { |
| 326 | + addFile("test.c", "int main() {}\n"); |
| 327 | + const char *Args[] = { |
| 328 | + "clang", "-target", "arm64-apple-macosx11.0.0", "-fno-integrated-as", |
| 329 | + "-c", "test.c"}; |
| 330 | + EXPECT_NE(extractCC1Arguments(Args), nullptr); |
| 331 | +} |
| 332 | + |
| 333 | +TEST_F(CommandLineExtractorTest, AcceptEmbedBitcode) { |
| 334 | + addFile("test.c", "int main() {}\n"); |
| 335 | + const char *Args[] = {"clang", "-target", "arm64-apple-macosx11.0.0", |
| 336 | + "-c", "-fembed-bitcode", "test.c"}; |
| 337 | + EXPECT_NE(extractCC1Arguments(Args), nullptr); |
| 338 | +} |
| 339 | + |
| 340 | +TEST_F(CommandLineExtractorTest, AcceptSaveTemps) { |
| 341 | + addFile("test.c", "int main() {}\n"); |
| 342 | + const char *Args[] = {"clang", "-target", "arm64-apple-macosx11.0.0", |
| 343 | + "-c", "-save-temps", "test.c"}; |
| 344 | + EXPECT_NE(extractCC1Arguments(Args), nullptr); |
| 345 | +} |
| 346 | + |
| 347 | +TEST_F(CommandLineExtractorTest, RejectMultipleArchitectures) { |
| 348 | + addFile("test.c", "int main() {}\n"); |
| 349 | + const char *Args[] = {"clang", "-target", "arm64-apple-macosx11.0.0", |
| 350 | + "-arch", "x86_64", "-arch", |
| 351 | + "arm64", "-c", "test.c"}; |
| 352 | + EXPECT_EQ(extractCC1Arguments(Args), nullptr); |
| 353 | +} |
| 354 | + |
| 355 | +TEST_F(CommandLineExtractorTest, RejectMultipleInputFiles) { |
| 356 | + addFile("one.c", "void one() {}\n"); |
| 357 | + addFile("two.c", "void two() {}\n"); |
| 358 | + const char *Args[] = {"clang", "-target", "arm64-apple-macosx11.0.0", |
| 359 | + "-c", "one.c", "two.c"}; |
| 360 | + EXPECT_EQ(extractCC1Arguments(Args), nullptr); |
| 361 | +} |
| 362 | + |
261 | 363 | struct VerifyEndCallback : public SourceFileCallbacks {
|
262 | 364 | VerifyEndCallback() : BeginCalled(0), EndCalled(0), Matched(false) {}
|
263 | 365 | bool handleBeginSource(CompilerInstance &CI) override {
|
|
0 commit comments