1 //===- 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"
13 #include "clang/Basic/TargetInfo.h"
14 #include "clang/CodeGen/ModuleBuilder.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Parse/ParseAST.h"
18 #include "clang/Sema/Sema.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "gtest/gtest.h"
24 
25 using namespace llvm;
26 using namespace clang;
27 
28 namespace {
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.
33 const char TestProgram[] =
34     "class EmitCXXGlobalInitFunc    "
35     "{                              "
36     "public:                        "
37     "   EmitCXXGlobalInitFunc() {}  "
38     "};                             "
39     "EmitCXXGlobalInitFunc test;    ";
40 
TEST(BufferSourceTest,EmitCXXGlobalInitFunc)41 TEST(BufferSourceTest, EmitCXXGlobalInitFunc) {
42     CompilerInstance compiler;
43 
44     compiler.createDiagnostics();
45     compiler.getLangOpts().CPlusPlus = 1;
46     compiler.getLangOpts().CPlusPlus11 = 1;
47 
48     compiler.getTargetOpts().Triple = llvm::Triple::normalize(
49         llvm::sys::getProcessTriple());
50     compiler.setTarget(clang::TargetInfo::CreateTargetInfo(
51       compiler.getDiagnostics(),
52       std::make_shared<clang::TargetOptions>(
53         compiler.getTargetOpts())));
54 
55     compiler.createFileManager();
56     compiler.createSourceManager(compiler.getFileManager());
57     compiler.createPreprocessor(clang::TU_Prefix);
58 
59     compiler.createASTContext();
60 
61     compiler.setASTConsumer(std::unique_ptr<ASTConsumer>(
62         CreateLLVMCodeGen(
63             compiler.getDiagnostics(),
64             "EmitCXXGlobalInitFuncTest",
65             compiler.getCodeGenOpts(),
66             compiler.getTargetOpts(),
67             llvm::getGlobalContext())));
68 
69     compiler.createSema(clang::TU_Prefix,NULL);
70 
71     clang::SourceManager &sm = compiler.getSourceManager();
72     sm.setMainFileID(sm.createFileID(
73         llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
74 
75     clang::ParseAST(compiler.getSema(), false, false);
76 }
77 
78 }
79