Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 1 | //===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit tests -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <fstream> |
| 11 | |
| 12 | #include "clang/Frontend/ASTUnit.h" |
| 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Frontend/CompilerInvocation.h" |
| 15 | #include "clang/Frontend/PCHContainerOperations.h" |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/Path.h" |
| 18 | #include "llvm/Support/ToolOutputFile.h" |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
Ivan Donchevskii | 2ebe3a0 | 2018-06-06 07:17:26 +0000 | [diff] [blame] | 26 | class ASTUnitTest : public ::testing::Test { |
| 27 | protected: |
| 28 | int FD; |
| 29 | llvm::SmallString<256> InputFileName; |
| 30 | std::unique_ptr<ToolOutputFile> input_file; |
| 31 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags; |
| 32 | std::shared_ptr<CompilerInvocation> CInvok; |
| 33 | std::shared_ptr<PCHContainerOperations> PCHContainerOps; |
| 34 | |
| 35 | std::unique_ptr<ASTUnit> createASTUnit(bool isVolatile) { |
| 36 | EXPECT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD, |
| 37 | InputFileName)); |
Ivan Donchevskii | d16cccf | 2018-06-06 08:25:54 +0000 | [diff] [blame^] | 38 | input_file = llvm::make_unique<ToolOutputFile>(InputFileName, FD); |
Ivan Donchevskii | 2ebe3a0 | 2018-06-06 07:17:26 +0000 | [diff] [blame] | 39 | input_file->os() << ""; |
| 40 | |
| 41 | const char *Args[] = {"clang", "-xc++", InputFileName.c_str()}; |
| 42 | |
| 43 | Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
| 44 | |
| 45 | CInvok = createInvocationFromCommandLine(Args, Diags); |
| 46 | |
| 47 | if (!CInvok) |
| 48 | return nullptr; |
| 49 | |
| 50 | FileManager *FileMgr = |
| 51 | new FileManager(FileSystemOptions(), vfs::getRealFileSystem()); |
| 52 | PCHContainerOps = std::make_shared<PCHContainerOperations>(); |
| 53 | |
| 54 | return ASTUnit::LoadFromCompilerInvocation( |
| 55 | CInvok, PCHContainerOps, Diags, FileMgr, false, false, 0, TU_Complete, |
| 56 | false, false, isVolatile); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) { |
Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 61 | // Check that the printing policy is restored with the correct language |
| 62 | // options when loading an ASTUnit from a file. To this end, an ASTUnit |
| 63 | // for a C++ translation unit is set up and written to a temporary file. |
| 64 | |
| 65 | // By default `UseVoidForZeroParams` is true for non-C++ language options, |
| 66 | // thus we can check this field after loading the ASTUnit to deduce whether |
| 67 | // the correct (C++) language options were used when setting up the printing |
| 68 | // policy. |
| 69 | |
| 70 | { |
| 71 | PrintingPolicy PolicyWithDefaultLangOpt(LangOptions{}); |
| 72 | EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams); |
| 73 | } |
| 74 | |
Ivan Donchevskii | 2ebe3a0 | 2018-06-06 07:17:26 +0000 | [diff] [blame] | 75 | std::unique_ptr<ASTUnit> AST = createASTUnit(false); |
Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 76 | |
| 77 | if (!AST) |
| 78 | FAIL() << "failed to create ASTUnit"; |
| 79 | |
| 80 | EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams); |
| 81 | |
| 82 | llvm::SmallString<256> ASTFileName; |
Ivan Donchevskii | 2ebe3a0 | 2018-06-06 07:17:26 +0000 | [diff] [blame] | 83 | ASSERT_FALSE( |
| 84 | llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName)); |
Reid Kleckner | 2590edf | 2017-09-23 01:04:42 +0000 | [diff] [blame] | 85 | ToolOutputFile ast_file(ASTFileName, FD); |
Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 86 | AST->Save(ASTFileName.str()); |
| 87 | |
| 88 | EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName)); |
| 89 | |
| 90 | std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile( |
Ivan Donchevskii | 2ebe3a0 | 2018-06-06 07:17:26 +0000 | [diff] [blame] | 91 | ASTFileName.str(), PCHContainerOps->getRawReader(), |
| 92 | ASTUnit::LoadEverything, Diags, FileSystemOptions(), |
| 93 | /*UseDebugInfo=*/false); |
Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 94 | |
| 95 | if (!AU) |
| 96 | FAIL() << "failed to load ASTUnit"; |
| 97 | |
| 98 | EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams); |
| 99 | } |
| 100 | |
Ivan Donchevskii | 2ebe3a0 | 2018-06-06 07:17:26 +0000 | [diff] [blame] | 101 | TEST_F(ASTUnitTest, GetBufferForFileMemoryMapping) { |
| 102 | std::unique_ptr<ASTUnit> AST = createASTUnit(true); |
| 103 | |
| 104 | if (!AST) |
| 105 | FAIL() << "failed to create ASTUnit"; |
| 106 | |
| 107 | std::unique_ptr<llvm::MemoryBuffer> memoryBuffer = |
| 108 | AST->getBufferForFile(InputFileName); |
| 109 | |
| 110 | EXPECT_NE(memoryBuffer->getBufferKind(), |
| 111 | llvm::MemoryBuffer::MemoryBuffer_MMap); |
| 112 | } |
| 113 | |
Vedant Kumar | fd9fad9 | 2017-08-25 18:07:03 +0000 | [diff] [blame] | 114 | } // anonymous namespace |