1*0a6a1f1dSLionel Sambuc //===- unittests/CodeGen/BufferSourceTest.cpp - MemoryBuffer source tests -===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc
10*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTConsumer.h"
11*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTContext.h"
12*0a6a1f1dSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
13*0a6a1f1dSLionel Sambuc #include "clang/Basic/TargetInfo.h"
14*0a6a1f1dSLionel Sambuc #include "clang/CodeGen/ModuleBuilder.h"
15*0a6a1f1dSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Lex/Preprocessor.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Parse/ParseAST.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Sema/Sema.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/ADT/Triple.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/IR/LLVMContext.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/Support/Host.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
23*0a6a1f1dSLionel Sambuc #include "gtest/gtest.h"
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc using namespace llvm;
26*0a6a1f1dSLionel Sambuc using namespace clang;
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc namespace {
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc // Emitting constructors for global objects involves looking
31*0a6a1f1dSLionel Sambuc // at the source file name. This makes sure that we don't crash
32*0a6a1f1dSLionel Sambuc // if the source file is a memory buffer.
33*0a6a1f1dSLionel Sambuc const char TestProgram[] =
34*0a6a1f1dSLionel Sambuc "class EmitCXXGlobalInitFunc "
35*0a6a1f1dSLionel Sambuc "{ "
36*0a6a1f1dSLionel Sambuc "public: "
37*0a6a1f1dSLionel Sambuc " EmitCXXGlobalInitFunc() {} "
38*0a6a1f1dSLionel Sambuc "}; "
39*0a6a1f1dSLionel Sambuc "EmitCXXGlobalInitFunc test; ";
40*0a6a1f1dSLionel Sambuc
TEST(BufferSourceTest,EmitCXXGlobalInitFunc)41*0a6a1f1dSLionel Sambuc TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
42*0a6a1f1dSLionel Sambuc CompilerInstance compiler;
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc compiler.createDiagnostics();
45*0a6a1f1dSLionel Sambuc compiler.getLangOpts().CPlusPlus = 1;
46*0a6a1f1dSLionel Sambuc compiler.getLangOpts().CPlusPlus11 = 1;
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambuc compiler.getTargetOpts().Triple = llvm::Triple::normalize(
49*0a6a1f1dSLionel Sambuc llvm::sys::getProcessTriple());
50*0a6a1f1dSLionel Sambuc compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
51*0a6a1f1dSLionel Sambuc compiler.getDiagnostics(),
52*0a6a1f1dSLionel Sambuc std::make_shared<clang::TargetOptions>(
53*0a6a1f1dSLionel Sambuc compiler.getTargetOpts())));
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc compiler.createFileManager();
56*0a6a1f1dSLionel Sambuc compiler.createSourceManager(compiler.getFileManager());
57*0a6a1f1dSLionel Sambuc compiler.createPreprocessor(clang::TU_Prefix);
58*0a6a1f1dSLionel Sambuc
59*0a6a1f1dSLionel Sambuc compiler.createASTContext();
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
62*0a6a1f1dSLionel Sambuc CreateLLVMCodeGen(
63*0a6a1f1dSLionel Sambuc compiler.getDiagnostics(),
64*0a6a1f1dSLionel Sambuc "EmitCXXGlobalInitFuncTest",
65*0a6a1f1dSLionel Sambuc compiler.getCodeGenOpts(),
66*0a6a1f1dSLionel Sambuc compiler.getTargetOpts(),
67*0a6a1f1dSLionel Sambuc llvm::getGlobalContext())));
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc compiler.createSema(clang::TU_Prefix,NULL);
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambuc clang::SourceManager &sm = compiler.getSourceManager();
72*0a6a1f1dSLionel Sambuc sm.setMainFileID(sm.createFileID(
73*0a6a1f1dSLionel Sambuc llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc clang::ParseAST(compiler.getSema(), false, false);
76*0a6a1f1dSLionel Sambuc }
77*0a6a1f1dSLionel Sambuc
78*0a6a1f1dSLionel Sambuc }
79