Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 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/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Lex/Preprocessor.h" |
| 15 | #include "clang/CodeGen/ModuleBuilder.h" |
| 16 | #include "clang/Sema/Sema.h" |
| 17 | #include "clang/Parse/ParseAST.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "llvm/ADT/Triple.h" |
| 20 | #include "llvm/Support/Host.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/IR/LLVMContext.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 | |
| 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(); |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 72 | sm.setMainFileID( |
| 73 | sm.createFileID(llvm::MemoryBuffer::getMemBuffer(TestProgram).release(), |
| 74 | clang::SrcMgr::C_User)); |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 75 | |
| 76 | clang::ParseAST(compiler.getSema(), false, false); |
| 77 | } |
| 78 | |
| 79 | } |