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" |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 13 | #include "clang/Basic/TargetInfo.h" |
| 14 | #include "clang/CodeGen/ModuleBuilder.h" |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/CompilerInstance.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 17 | #include "clang/Parse/ParseAST.h" |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Sema.h" |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LLVMContext.h" |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Host.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 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) { |
Mehdi Amini | 59fb3ac | 2016-04-14 05:34:32 +0000 | [diff] [blame] | 42 | LLVMContext Context; |
Mehdi Amini | 3d7224c | 2016-04-14 05:37:41 +0000 | [diff] [blame] | 43 | CompilerInstance compiler; |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 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", |
Adrian Prantl | a106faa | 2015-06-30 02:34:37 +0000 | [diff] [blame] | 66 | compiler.getHeaderSearchOpts(), |
| 67 | compiler.getPreprocessorOpts(), |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 68 | compiler.getCodeGenOpts(), |
Mehdi Amini | 59fb3ac | 2016-04-14 05:34:32 +0000 | [diff] [blame] | 69 | Context))); |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 70 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 71 | compiler.createSema(clang::TU_Prefix, nullptr); |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 72 | |
| 73 | clang::SourceManager &sm = compiler.getSourceManager(); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 74 | sm.setMainFileID(sm.createFileID( |
| 75 | llvm::MemoryBuffer::getMemBuffer(TestProgram), clang::SrcMgr::C_User)); |
Keno Fischer | 84778b2 | 2014-08-26 22:10:15 +0000 | [diff] [blame] | 76 | |
| 77 | clang::ParseAST(compiler.getSema(), false, false); |
| 78 | } |
| 79 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 80 | } // end anonymous namespace |