• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     LLVMContext Context;
43     CompilerInstance compiler;
44 
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",
66             compiler.getHeaderSearchOpts(),
67             compiler.getPreprocessorOpts(),
68             compiler.getCodeGenOpts(),
69             Context)));
70 
71     compiler.createSema(clang::TU_Prefix, nullptr);
72 
73     clang::SourceManager &sm = compiler.getSourceManager();
74     sm.setMainFileID(sm.createFileID(
75         llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User));
76 
77     clang::ParseAST(compiler.getSema(), false, false);
78 }
79 
80 } // end anonymous namespace
81