1 //===--- CompilerInstance.cpp ---------------------------------------------===// 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/Frontend/CompilerInstance.h" 11 #include "clang/Basic/Diagnostic.h" 12 #include "clang/Basic/FileManager.h" 13 #include "clang/Basic/SourceManager.h" 14 #include "clang/Basic/TargetInfo.h" 15 #include "clang/Lex/HeaderSearch.h" 16 #include "clang/Lex/Preprocessor.h" 17 #include "clang/Lex/PTHManager.h" 18 #include "clang/Frontend/Utils.h" 19 #include "llvm/LLVMContext.h" 20 using namespace clang; 21 22 CompilerInstance::CompilerInstance(llvm::LLVMContext *_LLVMContext, 23 bool _OwnsLLVMContext) 24 : LLVMContext(_LLVMContext), 25 OwnsLLVMContext(_OwnsLLVMContext) { 26 } 27 28 CompilerInstance::~CompilerInstance() { 29 if (OwnsLLVMContext) 30 delete LLVMContext; 31 } 32 33 void CompilerInstance::createFileManager() { 34 FileMgr.reset(new FileManager()); 35 } 36 37 void CompilerInstance::createSourceManager() { 38 SourceMgr.reset(new SourceManager()); 39 } 40 41 void CompilerInstance::createPreprocessor() { 42 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(), 43 getPreprocessorOpts(), getHeaderSearchOpts(), 44 getDependencyOutputOpts(), getTarget(), 45 getSourceManager(), getFileManager())); 46 } 47 48 Preprocessor * 49 CompilerInstance::createPreprocessor(Diagnostic &Diags, 50 const LangOptions &LangInfo, 51 const PreprocessorOptions &PPOpts, 52 const HeaderSearchOptions &HSOpts, 53 const DependencyOutputOptions &DepOpts, 54 const TargetInfo &Target, 55 SourceManager &SourceMgr, 56 FileManager &FileMgr) { 57 // Create a PTH manager if we are using some form of a token cache. 58 PTHManager *PTHMgr = 0; 59 if (!PPOpts.getTokenCache().empty()) 60 PTHMgr = PTHManager::Create(PPOpts.getTokenCache(), Diags); 61 62 // FIXME: Don't fail like this. 63 if (Diags.hasErrorOccurred()) 64 exit(1); 65 66 // Create the Preprocessor. 67 HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr); 68 Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, 69 SourceMgr, *HeaderInfo, PTHMgr, 70 /*OwnsHeaderSearch=*/true); 71 72 // Note that this is different then passing PTHMgr to Preprocessor's ctor. 73 // That argument is used as the IdentifierInfoLookup argument to 74 // IdentifierTable's ctor. 75 if (PTHMgr) { 76 PTHMgr->setPreprocessor(PP); 77 PP->setPTHManager(PTHMgr); 78 } 79 80 InitializePreprocessor(*PP, PPOpts, HSOpts); 81 82 // Handle generating dependencies, if requested. 83 if (!DepOpts.OutputFile.empty()) 84 AttachDependencyFileGen(*PP, DepOpts); 85 86 return PP; 87 } 88