blob: 1934e661383b256639ab26dd1abd644e6517af35 [file] [log] [blame]
Keno Fischer84778b22014-08-26 22:10:15 +00001//===- unittests/CodeGen/BufferSourceTest.cpp - MemoryBuffer source 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 "clang/AST/ASTConsumer.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/RecursiveASTVisitor.h"
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000013#include "clang/Basic/TargetInfo.h"
14#include "clang/CodeGen/ModuleBuilder.h"
Keno Fischer84778b22014-08-26 22:10:15 +000015#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Lex/Preprocessor.h"
Keno Fischer84778b22014-08-26 22:10:15 +000017#include "clang/Parse/ParseAST.h"
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000018#include "clang/Sema/Sema.h"
Keno Fischer84778b22014-08-26 22:10:15 +000019#include "llvm/ADT/Triple.h"
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000020#include "llvm/IR/LLVMContext.h"
Keno Fischer84778b22014-08-26 22:10:15 +000021#include "llvm/Support/Host.h"
22#include "llvm/Support/MemoryBuffer.h"
Keno Fischer84778b22014-08-26 22:10:15 +000023#include "gtest/gtest.h"
24
25using namespace llvm;
26using namespace clang;
27
28namespace {
29
30// Emitting constructors for global objects involves looking
31// at the source file name. This makes sure that we don't crash
32// if the source file is a memory buffer.
33const char TestProgram[] =
34 "class EmitCXXGlobalInitFunc "
35 "{ "
36 "public: "
37 " EmitCXXGlobalInitFunc() {} "
38 "}; "
39 "EmitCXXGlobalInitFunc test; ";
40
41TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
Mehdi Amini59fb3ac2016-04-14 05:34:32 +000042 LLVMContext Context;
Mehdi Amini3d7224c2016-04-14 05:37:41 +000043 CompilerInstance compiler;
Keno Fischer84778b22014-08-26 22:10:15 +000044
45 compiler.createDiagnostics();
46 compiler.getLangOpts().CPlusPlus = 1;
47 compiler.getLangOpts().CPlusPlus11 = 1;
48
49 compiler.getTargetOpts().Triple = llvm::Triple::normalize(
50 llvm::sys::getProcessTriple());
51 compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
52 compiler.getDiagnostics(),
53 std::make_shared<clang::TargetOptions>(
54 compiler.getTargetOpts())));
55
56 compiler.createFileManager();
57 compiler.createSourceManager(compiler.getFileManager());
58 compiler.createPreprocessor(clang::TU_Prefix);
59
60 compiler.createASTContext();
61
62 compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
63 CreateLLVMCodeGen(
64 compiler.getDiagnostics(),
65 "EmitCXXGlobalInitFuncTest",
Adrian Prantla106faa2015-06-30 02:34:37 +000066 compiler.getHeaderSearchOpts(),
67 compiler.getPreprocessorOpts(),
Keno Fischer84778b22014-08-26 22:10:15 +000068 compiler.getCodeGenOpts(),
Mehdi Amini59fb3ac2016-04-14 05:34:32 +000069 Context)));
Keno Fischer84778b22014-08-26 22:10:15 +000070
Hans Wennborgdcfba332015-10-06 23:40:43 +000071 compiler.createSema(clang::TU_Prefix, nullptr);
Keno Fischer84778b22014-08-26 22:10:15 +000072
73 clang::SourceManager &sm = compiler.getSourceManager();
David Blaikie50a5f972014-08-29 07:59:55 +000074 sm.setMainFileID(sm.createFileID(
75 llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
Keno Fischer84778b22014-08-26 22:10:15 +000076
77 clang::ParseAST(compiler.getSema(), false, false);
78}
79
Hans Wennborgdcfba332015-10-06 23:40:43 +000080} // end anonymous namespace