summaryrefslogtreecommitdiff
path: root/unittests/Frontend
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2018-07-28 11:06:01 +0000
committerDimitry Andric <dim@FreeBSD.org>2018-07-28 11:06:01 +0000
commit486754660bb926339aefcf012a3f848592babb8b (patch)
treeecdbc446c9876f4f120f701c243373cd3cb43db3 /unittests/Frontend
parent55e6d896ad333f07bb3b1ba487df214fc268a4ab (diff)
Notes
Diffstat (limited to 'unittests/Frontend')
-rw-r--r--unittests/Frontend/ASTUnitTest.cpp81
-rw-r--r--unittests/Frontend/CMakeLists.txt2
-rw-r--r--unittests/Frontend/OutputStreamTest.cpp46
3 files changed, 102 insertions, 27 deletions
diff --git a/unittests/Frontend/ASTUnitTest.cpp b/unittests/Frontend/ASTUnitTest.cpp
index 4f529cf55de98..c60004e40bd72 100644
--- a/unittests/Frontend/ASTUnitTest.cpp
+++ b/unittests/Frontend/ASTUnitTest.cpp
@@ -23,7 +23,41 @@ using namespace clang;
namespace {
-TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
+class ASTUnitTest : public ::testing::Test {
+protected:
+ int FD;
+ llvm::SmallString<256> InputFileName;
+ std::unique_ptr<ToolOutputFile> input_file;
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags;
+ std::shared_ptr<CompilerInvocation> CInvok;
+ std::shared_ptr<PCHContainerOperations> PCHContainerOps;
+
+ std::unique_ptr<ASTUnit> createASTUnit(bool isVolatile) {
+ EXPECT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD,
+ InputFileName));
+ input_file = llvm::make_unique<ToolOutputFile>(InputFileName, FD);
+ input_file->os() << "";
+
+ const char *Args[] = {"clang", "-xc++", InputFileName.c_str()};
+
+ Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions());
+
+ CInvok = createInvocationFromCommandLine(Args, Diags);
+
+ if (!CInvok)
+ return nullptr;
+
+ FileManager *FileMgr =
+ new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
+ PCHContainerOps = std::make_shared<PCHContainerOperations>();
+
+ return ASTUnit::LoadFromCompilerInvocation(
+ CInvok, PCHContainerOps, Diags, FileMgr, false, false, 0, TU_Complete,
+ false, false, isVolatile);
+ }
+};
+
+TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) {
// Check that the printing policy is restored with the correct language
// options when loading an ASTUnit from a file. To this end, an ASTUnit
// for a C++ translation unit is set up and written to a temporary file.
@@ -38,29 +72,7 @@ TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams);
}
- int FD;
- llvm::SmallString<256> InputFileName;
- ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD, InputFileName));
- ToolOutputFile input_file(InputFileName, FD);
- input_file.os() << "";
-
- const char* Args[] = {"clang", "-xc++", InputFileName.c_str()};
-
- IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
- CompilerInstance::createDiagnostics(new DiagnosticOptions());
-
- std::shared_ptr<CompilerInvocation> CInvok =
- createInvocationFromCommandLine(Args, Diags);
-
- if (!CInvok)
- FAIL() << "could not create compiler invocation";
-
- FileManager *FileMgr =
- new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
- auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
-
- std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
- CInvok, PCHContainerOps, Diags, FileMgr);
+ std::unique_ptr<ASTUnit> AST = createASTUnit(false);
if (!AST)
FAIL() << "failed to create ASTUnit";
@@ -68,15 +80,17 @@ TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
llvm::SmallString<256> ASTFileName;
- ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName));
+ ASSERT_FALSE(
+ llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName));
ToolOutputFile ast_file(ASTFileName, FD);
AST->Save(ASTFileName.str());
EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName));
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
- ASTFileName.str(), PCHContainerOps->getRawReader(), ASTUnit::LoadEverything, Diags,
- FileSystemOptions(), /*UseDebugInfo=*/false);
+ ASTFileName.str(), PCHContainerOps->getRawReader(),
+ ASTUnit::LoadEverything, Diags, FileSystemOptions(),
+ /*UseDebugInfo=*/false);
if (!AU)
FAIL() << "failed to load ASTUnit";
@@ -84,4 +98,17 @@ TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
}
+TEST_F(ASTUnitTest, GetBufferForFileMemoryMapping) {
+ std::unique_ptr<ASTUnit> AST = createASTUnit(true);
+
+ if (!AST)
+ FAIL() << "failed to create ASTUnit";
+
+ std::unique_ptr<llvm::MemoryBuffer> memoryBuffer =
+ AST->getBufferForFile(InputFileName);
+
+ EXPECT_NE(memoryBuffer->getBufferKind(),
+ llvm::MemoryBuffer::MemoryBuffer_MMap);
+}
+
} // anonymous namespace
diff --git a/unittests/Frontend/CMakeLists.txt b/unittests/Frontend/CMakeLists.txt
index f3c4336ea22fa..c764fb9e2a0f2 100644
--- a/unittests/Frontend/CMakeLists.txt
+++ b/unittests/Frontend/CMakeLists.txt
@@ -9,6 +9,7 @@ add_clang_unittest(FrontendTests
CodeGenActionTest.cpp
ParsedSourceLocationTest.cpp
PCHPreambleTest.cpp
+ OutputStreamTest.cpp
)
target_link_libraries(FrontendTests
PRIVATE
@@ -18,4 +19,5 @@ target_link_libraries(FrontendTests
clangLex
clangSema
clangCodeGen
+ clangFrontendTool
)
diff --git a/unittests/Frontend/OutputStreamTest.cpp b/unittests/Frontend/OutputStreamTest.cpp
new file mode 100644
index 0000000000000..ff036500d8c8a
--- /dev/null
+++ b/unittests/Frontend/OutputStreamTest.cpp
@@ -0,0 +1,46 @@
+//===- unittests/Frontend/OutputStreamTest.cpp --- FrontendAction tests --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/CodeGen/BackendUtil.h"
+#include "clang/CodeGen/CodeGenAction.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/FrontendTool/Utils.h"
+#include "clang/Lex/PreprocessorOptions.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang;
+using namespace clang::frontend;
+
+namespace {
+
+TEST(FrontendOutputTests, TestOutputStream) {
+ auto Invocation = std::make_shared<CompilerInvocation>();
+ Invocation->getPreprocessorOpts().addRemappedFile(
+ "test.cc", MemoryBuffer::getMemBuffer("").release());
+ Invocation->getFrontendOpts().Inputs.push_back(
+ FrontendInputFile("test.cc", InputKind::CXX));
+ Invocation->getFrontendOpts().ProgramAction = EmitBC;
+ Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
+ CompilerInstance Compiler;
+
+ SmallVector<char, 256> IRBuffer;
+ std::unique_ptr<raw_pwrite_stream> IRStream(
+ new raw_svector_ostream(IRBuffer));
+
+ Compiler.setOutputStream(std::move(IRStream));
+ Compiler.setInvocation(std::move(Invocation));
+ Compiler.createDiagnostics();
+
+ bool Success = ExecuteCompilerInvocation(&Compiler);
+ EXPECT_TRUE(Success);
+ EXPECT_TRUE(!IRBuffer.empty());
+ EXPECT_TRUE(StringRef(IRBuffer.data()).startswith("BC"));
+}
+}