blob: 550979777f52992e2973486fdc6e050316f8e1b1 [file] [log] [blame]
Daniel Dunbar636404a2009-11-13 03:51:44 +00001//===--- 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"
Daniel Dunbar546a6762009-11-13 04:12:06 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Basic/SourceManager.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000014#include "clang/Basic/TargetInfo.h"
Daniel Dunbaraaa148f2009-11-13 05:52:11 +000015#include "clang/Lex/HeaderSearch.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/PTHManager.h"
18#include "clang/Frontend/Utils.h"
Daniel Dunbar636404a2009-11-13 03:51:44 +000019#include "llvm/LLVMContext.h"
20using namespace clang;
21
22CompilerInstance::CompilerInstance(llvm::LLVMContext *_LLVMContext,
23 bool _OwnsLLVMContext)
24 : LLVMContext(_LLVMContext),
25 OwnsLLVMContext(_OwnsLLVMContext) {
26}
27
28CompilerInstance::~CompilerInstance() {
29 if (OwnsLLVMContext)
30 delete LLVMContext;
31}
Daniel Dunbar546a6762009-11-13 04:12:06 +000032
33void CompilerInstance::createFileManager() {
34 FileMgr.reset(new FileManager());
35}
36
37void CompilerInstance::createSourceManager() {
38 SourceMgr.reset(new SourceManager());
39}
Daniel Dunbaraaa148f2009-11-13 05:52:11 +000040
41void CompilerInstance::createPreprocessor() {
42 PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(),
43 getPreprocessorOpts(), getHeaderSearchOpts(),
44 getDependencyOutputOpts(), getTarget(),
45 getSourceManager(), getFileManager()));
46}
47
48Preprocessor *
49CompilerInstance::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}