CompilerInstance.cpp revision 2bc750700487163e31e4f67cd0e0886868209e3c
12a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//===--- CompilerInstance.cpp ---------------------------------------------===//
22a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//
32a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//                     The LLVM Compiler Infrastructure
42a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//
52a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// This file is distributed under the University of Illinois Open Source
62a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar// License. See LICENSE.TXT for details.
72a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//
82a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar//===----------------------------------------------------------------------===//
92a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar
102a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar#include "clang/Frontend/CompilerInstance.h"
11f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor#include "clang/Sema/Sema.h"
1212ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbar#include "clang/AST/ASTConsumer.h"
135eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar#include "clang/AST/ASTContext.h"
142a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar#include "clang/Basic/Diagnostic.h"
1516b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar#include "clang/Basic/FileManager.h"
1616b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar#include "clang/Basic/SourceManager.h"
172a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar#include "clang/Basic/TargetInfo.h"
180397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar#include "clang/Basic/Version.h"
1922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Lex/HeaderSearch.h"
2022dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Lex/Preprocessor.h"
2122dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Lex/PTHManager.h"
224e85b8ae413fa00fa42aa3e625c2db3b1932f8d3David Blaikie#include "clang/Frontend/ChainedDiagnosticConsumer.h"
230397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar#include "clang/Frontend/FrontendAction.h"
2421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor#include "clang/Frontend/FrontendActions.h"
25c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar#include "clang/Frontend/FrontendDiagnostic.h"
269df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar#include "clang/Frontend/LogDiagnosticPrinter.h"
270fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar#include "clang/Frontend/TextDiagnosticPrinter.h"
28621bc69624599da62abd9bc9e5edd8a63ac99fe6David Blaikie#include "clang/Frontend/VerifyDiagnosticConsumer.h"
2922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar#include "clang/Frontend/Utils.h"
306ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redl#include "clang/Serialization/ASTReader.h"
31c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar#include "clang/Sema/CodeCompleteConsumer.h"
3232bef4edba854303800b3b01cb49a282e5da4f69Michael J. Spencer#include "llvm/Support/FileSystem.h"
33ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar#include "llvm/Support/MemoryBuffer.h"
340fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar#include "llvm/Support/raw_ostream.h"
3595dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor#include "llvm/ADT/Statistic.h"
36f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam#include "llvm/Support/Timer.h"
3703013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Host.h"
3803013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
3903013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Program.h"
4003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Signals.h"
413a321e23f66128dbb986343927456ff6702af617Michael J. Spencer#include "llvm/Support/system_error.h"
420ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor#include "llvm/Support/CrashRecoveryContext.h"
431626601b00a1feccd6b7a3f54e2991f56b659192Dylan Noblesmith#include "llvm/Config/config.h"
442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor// Support for FileLockManager
462bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <fstream>
472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <sys/types.h>
482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <sys/stat.h>
492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
502bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#if LLVM_ON_UNIX
512bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#include <unistd.h>
522bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif
532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
542a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbarusing namespace clang;
552a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar
5642e9f8e4983d50f896ec716207817b9d96e7e79cDaniel DunbarCompilerInstance::CompilerInstance()
57f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  : Invocation(new CompilerInvocation()), ModuleManager(0) {
586228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbar}
592a79e162a3fde25c1941151a67966830d873f2dbDaniel Dunbar
602a79e162a3fde25c1941151a67966830d873f2dbDaniel DunbarCompilerInstance::~CompilerInstance() {
6142e9f8e4983d50f896ec716207817b9d96e7e79cDaniel Dunbar}
6242e9f8e4983d50f896ec716207817b9d96e7e79cDaniel Dunbar
636228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbarvoid CompilerInstance::setInvocation(CompilerInvocation *Value) {
644f32786ac45210143654390177105eb749b614e9Ted Kremenek  Invocation = Value;
656228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbar}
666228ca00121669ec06a19df4fad87d5049c097cfDaniel Dunbar
67d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid CompilerInstance::setDiagnostics(DiagnosticsEngine *Value) {
6828019772db70d4547be05a042eb950bc910f134fDouglas Gregor  Diagnostics = Value;
698a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar}
708a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
718a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbarvoid CompilerInstance::setTarget(TargetInfo *Value) {
724f32786ac45210143654390177105eb749b614e9Ted Kremenek  Target = Value;
738a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar}
748a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
758a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbarvoid CompilerInstance::setFileManager(FileManager *Value) {
764f32786ac45210143654390177105eb749b614e9Ted Kremenek  FileMgr = Value;
778a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar}
788a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
794f32786ac45210143654390177105eb749b614e9Ted Kremenekvoid CompilerInstance::setSourceManager(SourceManager *Value) {
804f32786ac45210143654390177105eb749b614e9Ted Kremenek  SourceMgr = Value;
818a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar}
828a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
834f32786ac45210143654390177105eb749b614e9Ted Kremenekvoid CompilerInstance::setPreprocessor(Preprocessor *Value) { PP = Value; }
848a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
854f32786ac45210143654390177105eb749b614e9Ted Kremenekvoid CompilerInstance::setASTContext(ASTContext *Value) { Context = Value; }
8612ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbar
87f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregorvoid CompilerInstance::setSema(Sema *S) {
88f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor  TheSema.reset(S);
89f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor}
90f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor
9112ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbarvoid CompilerInstance::setASTConsumer(ASTConsumer *Value) {
9212ce6943aae499225708ecf364c5a8b0a3269c87Daniel Dunbar  Consumer.reset(Value);
938a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar}
948a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
958a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbarvoid CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) {
968a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar  CompletionConsumer.reset(Value);
978a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar}
988a9f569262860b8d03203327afd6047be2a9b5a6Daniel Dunbar
990fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// Diagnostics
1000fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbarstatic void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts,
1017d0c4ccd65b4549283c55e4923602e234f3811c5Axel Naumann                              unsigned argc, const char* const *argv,
102d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                              DiagnosticsEngine &Diags) {
1030fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  std::string ErrorInfo;
1045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  llvm::OwningPtr<raw_ostream> OS(
10569247139f5ce3edf46c48344129792b3a05d090aKovarththanan Rajaratnam    new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo));
1060fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  if (!ErrorInfo.empty()) {
1073d67b1e25847319a5a271f9d5a8d607ef18d804aKovarththanan Rajaratnam    Diags.Report(diag::err_fe_unable_to_open_logfile)
1083d67b1e25847319a5a271f9d5a8d607ef18d804aKovarththanan Rajaratnam                 << DiagOpts.DumpBuildInformation << ErrorInfo;
1090fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar    return;
1100fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  }
1110fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
112dd63b28107f21692b5065588f0e90b4534946f93Daniel Dunbar  (*OS) << "clang -cc1 command line arguments: ";
1130fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  for (unsigned i = 0; i != argc; ++i)
1140fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar    (*OS) << argv[i] << ' ';
1150fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  (*OS) << '\n';
1160fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
1170fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  // Chain in a diagnostic client which will log the diagnostics.
11878ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  DiagnosticConsumer *Logger =
11969247139f5ce3edf46c48344129792b3a05d090aKovarththanan Rajaratnam    new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true);
1204e85b8ae413fa00fa42aa3e625c2db3b1932f8d3David Blaikie  Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
1210fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar}
1220fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
1239df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbarstatic void SetUpDiagnosticLog(const DiagnosticOptions &DiagOpts,
124b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar                               const CodeGenOptions *CodeGenOpts,
125d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                               DiagnosticsEngine &Diags) {
1269df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  std::string ErrorInfo;
1279df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  bool OwnsStream = false;
1285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  raw_ostream *OS = &llvm::errs();
1299df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  if (DiagOpts.DiagnosticLogFile != "-") {
1309df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar    // Create the output stream.
1319df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar    llvm::raw_fd_ostream *FileOS(
1329df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar      new llvm::raw_fd_ostream(DiagOpts.DiagnosticLogFile.c_str(),
133e01eceb54c9235a41675bfd9f32c50bd005fb8d0Daniel Dunbar                               ErrorInfo, llvm::raw_fd_ostream::F_Append));
1349df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar    if (!ErrorInfo.empty()) {
1359df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar      Diags.Report(diag::warn_fe_cc_log_diagnostics_failure)
1369df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar        << DiagOpts.DumpBuildInformation << ErrorInfo;
1379df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar    } else {
1389df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar      FileOS->SetUnbuffered();
1399df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar      FileOS->SetUseAtomicWrites(true);
1409df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar      OS = FileOS;
1419df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar      OwnsStream = true;
1429df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar    }
1439df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  }
1449df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar
1459df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  // Chain in the diagnostic client which will log the diagnostics.
146b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar  LogDiagnosticPrinter *Logger = new LogDiagnosticPrinter(*OS, DiagOpts,
147b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar                                                          OwnsStream);
148b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar  if (CodeGenOpts)
149b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar    Logger->setDwarfDebugFlags(CodeGenOpts->DwarfDebugFlags);
1504e85b8ae413fa00fa42aa3e625c2db3b1932f8d3David Blaikie  Diags.setClient(new ChainedDiagnosticConsumer(Diags.takeClient(), Logger));
1519df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar}
1529df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar
153e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregorvoid CompilerInstance::createDiagnostics(int Argc, const char* const *Argv,
15478ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie                                         DiagnosticConsumer *Client,
155aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                                         bool ShouldOwnClient,
156aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                                         bool ShouldCloneClient) {
157b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar  Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client,
158aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                                  ShouldOwnClient, ShouldCloneClient,
159aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                                  &getCodeGenOpts());
1600fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar}
1610fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
162d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikiellvm::IntrusiveRefCntPtr<DiagnosticsEngine>
16328019772db70d4547be05a042eb950bc910f134fDouglas GregorCompilerInstance::createDiagnostics(const DiagnosticOptions &Opts,
164e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor                                    int Argc, const char* const *Argv,
16578ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie                                    DiagnosticConsumer *Client,
16678243658c533168d51fd076fba328437932ba6f1Douglas Gregor                                    bool ShouldOwnClient,
167aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                                    bool ShouldCloneClient,
168b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar                                    const CodeGenOptions *CodeGenOpts) {
16933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
170d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
171d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie      Diags(new DiagnosticsEngine(DiagID));
172221c7211c507482a91e97ede1bf6cf65a456ff67Daniel Dunbar
1730fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  // Create the diagnostic client for reporting errors or for
1740fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  // implementing -verify.
175aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  if (Client) {
176aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor    if (ShouldCloneClient)
177aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor      Diags->setClient(Client->clone(*Diags), ShouldOwnClient);
178aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor    else
179aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor      Diags->setClient(Client, ShouldOwnClient);
180aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor  } else
181e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor    Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
182f79dced038c63572965c016b969cfa870670d16eDaniel Dunbar
183f79dced038c63572965c016b969cfa870670d16eDaniel Dunbar  // Chain in -verify checker, if requested.
18478243658c533168d51fd076fba328437932ba6f1Douglas Gregor  if (Opts.VerifyDiagnostics)
185621bc69624599da62abd9bc9e5edd8a63ac99fe6David Blaikie    Diags->setClient(new VerifyDiagnosticConsumer(*Diags));
1860fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
1879df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  // Chain in -diagnostic-log-file dumper, if requested.
1889df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar  if (!Opts.DiagnosticLogFile.empty())
189b6534bbee90bf73f364072051d10b60352d43c3eDaniel Dunbar    SetUpDiagnosticLog(Opts, CodeGenOpts, *Diags);
1909df23493f5b8a223dfbc491e4b7de3850797c2e7Daniel Dunbar
1910fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  if (!Opts.DumpBuildInformation.empty())
1923d67b1e25847319a5a271f9d5a8d607ef18d804aKovarththanan Rajaratnam    SetUpBuildDumpLog(Opts, Argc, Argv, *Diags);
1930fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
1940fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar  // Configure our handling of diagnostics.
1955bf932bd0d5db98273938675dbf81cbb2f5ffff7Kovarththanan Rajaratnam  ProcessWarningOptions(*Diags, Opts);
1960fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
19728019772db70d4547be05a042eb950bc910f134fDouglas Gregor  return Diags;
1980fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar}
1990fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
2000fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// File Manager
2010fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
20216b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbarvoid CompilerInstance::createFileManager() {
2034f32786ac45210143654390177105eb749b614e9Ted Kremenek  FileMgr = new FileManager(getFileSystemOpts());
20416b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar}
20516b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar
2060fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// Source Manager
2070fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
20839b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattnervoid CompilerInstance::createSourceManager(FileManager &FileMgr) {
2094f32786ac45210143654390177105eb749b614e9Ted Kremenek  SourceMgr = new SourceManager(getDiagnostics(), FileMgr);
21016b7449d86b843d0926b04f87104cf3fff7149feDaniel Dunbar}
21122dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar
2120fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar// Preprocessor
2130fbb3d9a9cdd2201848be9eb017c54cd78538122Daniel Dunbar
21422dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbarvoid CompilerInstance::createPreprocessor() {
2156aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  const PreprocessorOptions &PPOpts = getPreprocessorOpts();
2166aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
21722dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  // Create a PTH manager if we are using some form of a token cache.
21822dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  PTHManager *PTHMgr = 0;
219049d3a06ea9f8fc03582488a2b7f24512565a335Daniel Dunbar  if (!PPOpts.TokenCache.empty())
2206aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    PTHMgr = PTHManager::Create(PPOpts.TokenCache, getDiagnostics());
2216aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
22222dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  // Create the Preprocessor.
2236aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  HeaderSearch *HeaderInfo = new HeaderSearch(getFileManager());
224998b3d3e8528ebd9d2c5d78d3a82edd90a8953a4Douglas Gregor  PP = new Preprocessor(getDiagnostics(), getLangOpts(), &getTarget(),
2256aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                        getSourceManager(), *HeaderInfo, *this, PTHMgr,
2266aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                        /*OwnsHeaderSearch=*/true);
2276aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
22822dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  // Note that this is different then passing PTHMgr to Preprocessor's ctor.
22922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  // That argument is used as the IdentifierInfoLookup argument to
23022dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  // IdentifierTable's ctor.
23122dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  if (PTHMgr) {
2326aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    PTHMgr->setPreprocessor(&*PP);
23322dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar    PP->setPTHManager(PTHMgr);
23422dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  }
2356aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
23694dc8f640ebea52241412512ed48601626edbc58Douglas Gregor  if (PPOpts.DetailedRecord)
237dca8ee8b7bc86076916a3a80f553f7a4e98c14afDouglas Gregor    PP->createPreprocessingRecord(
2386aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                  PPOpts.DetailedRecordIncludesNestedMacroExpansions);
2396aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
2406aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
24194dc8f640ebea52241412512ed48601626edbc58Douglas Gregor
2426e975c4517958bcc11c834336d340797356058dbDouglas Gregor  // Set up the module path, including the hash for the
2436e975c4517958bcc11c834336d340797356058dbDouglas Gregor  // module-creation options.
2446e975c4517958bcc11c834336d340797356058dbDouglas Gregor  llvm::SmallString<256> SpecificModuleCache(
2456e975c4517958bcc11c834336d340797356058dbDouglas Gregor                           getHeaderSearchOpts().ModuleCachePath);
2466e975c4517958bcc11c834336d340797356058dbDouglas Gregor  if (!getHeaderSearchOpts().DisableModuleHash)
2476e975c4517958bcc11c834336d340797356058dbDouglas Gregor    llvm::sys::path::append(SpecificModuleCache,
2486e975c4517958bcc11c834336d340797356058dbDouglas Gregor                            getInvocation().getModuleHash());
249fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor  PP->getHeaderSearchInfo().configureModules(SpecificModuleCache,
250fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor    getPreprocessorOpts().ModuleBuildPath.empty()
251fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor      ? std::string()
252fba18aa8f2cd1994dc65e8cb9f4be201c560dc0bDouglas Gregor      : getPreprocessorOpts().ModuleBuildPath.back());
2536e975c4517958bcc11c834336d340797356058dbDouglas Gregor
25422dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  // Handle generating dependencies, if requested.
2556aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  const DependencyOutputOptions &DepOpts = getDependencyOutputOpts();
25622dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar  if (!DepOpts.OutputFile.empty())
25722dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar    AttachDependencyFileGen(*PP, DepOpts);
2586aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
259eef63e0997e0f6d6436736ea919b851cfe34955aDaniel Dunbar  // Handle generating header include information, if requested.
260eef63e0997e0f6d6436736ea919b851cfe34955aDaniel Dunbar  if (DepOpts.ShowHeaderIncludes)
261eef63e0997e0f6d6436736ea919b851cfe34955aDaniel Dunbar    AttachHeaderIncludeGen(*PP);
262b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar  if (!DepOpts.HeaderIncludeOutputFile.empty()) {
2635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    StringRef OutputPath = DepOpts.HeaderIncludeOutputFile;
264b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar    if (OutputPath == "-")
265b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar      OutputPath = "";
266da60885cb4ee85d167cf704061f10b758067cf5aDaniel Dunbar    AttachHeaderIncludeGen(*PP, /*ShowAllHeaders=*/true, OutputPath,
267da60885cb4ee85d167cf704061f10b758067cf5aDaniel Dunbar                           /*ShowDepth=*/false);
268b34d69b9292534c1c574f168f0ac10aea652adcaDaniel Dunbar  }
26922dacfacacf5559028550ba6ddfbaa4ea6cb3944Daniel Dunbar}
2705eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar
2715eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar// ASTContext
2725eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar
2735eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbarvoid CompilerInstance::createASTContext() {
2745eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar  Preprocessor &PP = getPreprocessor();
2754f32786ac45210143654390177105eb749b614e9Ted Kremenek  Context = new ASTContext(getLangOpts(), PP.getSourceManager(),
276bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                           &getTarget(), PP.getIdentifierTable(),
2774f32786ac45210143654390177105eb749b614e9Ted Kremenek                           PP.getSelectorTable(), PP.getBuiltinInfo(),
2784f32786ac45210143654390177105eb749b614e9Ted Kremenek                           /*size_reserve=*/ 0);
2795eb810024dc8a1d12d5f066c02c978f07c4fcb00Daniel Dunbar}
2800f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
2810f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar// ExternalASTSource
2820f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
2835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid CompilerInstance::createPCHExternalASTSource(StringRef Path,
284ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl                                                  bool DisablePCHValidation,
2858ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor                                                  bool DisableStatCache,
286ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl                                                 void *DeserializationListener){
2870f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar  llvm::OwningPtr<ExternalASTSource> Source;
2881d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl  bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
2890f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar  Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot,
2908ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor                                          DisablePCHValidation,
2918ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor                                          DisableStatCache,
292ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl                                          getPreprocessor(), getASTContext(),
2931d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl                                          DeserializationListener,
2941d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl                                          Preamble));
295f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  ModuleManager = static_cast<ASTReader*>(Source.get());
2960f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar  getASTContext().setExternalSource(Source);
2970f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar}
2980f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
2990f800391ffbfe3820e1c60246a09a97e5f065179Daniel DunbarExternalASTSource *
3005f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerCompilerInstance::createPCHExternalASTSource(StringRef Path,
3010f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar                                             const std::string &Sysroot,
302fae3b2f4743dad616623c4df2fdb0f5128bd36d9Douglas Gregor                                             bool DisablePCHValidation,
3038ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor                                             bool DisableStatCache,
3040f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar                                             Preprocessor &PP,
305ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl                                             ASTContext &Context,
3061d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl                                             void *DeserializationListener,
3071d9f1fe7173e3084325f43c78af812a36d8a2a7cSebastian Redl                                             bool Preamble) {
308c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  llvm::OwningPtr<ASTReader> Reader;
309f8a1e51c48761ee1d7803c3fa35ac94f42ebb55eDouglas Gregor  Reader.reset(new ASTReader(PP, Context,
310832d620b4ae0fc5fe28561b885b4cfc65cf5c9abDouglas Gregor                             Sysroot.empty() ? "" : Sysroot.c_str(),
3118ef6c8cb6c5627240e2339fd7062c9873f821d7eDouglas Gregor                             DisablePCHValidation, DisableStatCache));
3120f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
313ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  Reader->setDeserializationListener(
314571db7f0cb31789737be92fce1c1b738e6dbe795Sebastian Redl            static_cast<ASTDeserializationListener *>(DeserializationListener));
31572a9ae18553bf8b6bdad84d2c54f73741a47e275Douglas Gregor  switch (Reader->ReadAST(Path,
31672a9ae18553bf8b6bdad84d2c54f73741a47e275Douglas Gregor                          Preamble ? serialization::MK_Preamble
31772a9ae18553bf8b6bdad84d2c54f73741a47e275Douglas Gregor                                   : serialization::MK_PCH)) {
318c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  case ASTReader::Success:
3190f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    // Set the predefines buffer as suggested by the PCH reader. Typically, the
3200f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    // predefines buffer will be empty.
3210f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    PP.setPredefines(Reader->getSuggestedPredefines());
3220f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    return Reader.take();
3230f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
324c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  case ASTReader::Failure:
3250f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    // Unrecoverable failure: don't even try to process the input file.
3260f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    break;
3270f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
328c43b54cbc10654ed59de797898042e1a05265246Sebastian Redl  case ASTReader::IgnorePCH:
3290f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    // No suitable PCH file could be found. Return an error.
3300f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar    break;
3310f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar  }
3320f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar
3330f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar  return 0;
3340f800391ffbfe3820e1c60246a09a97e5f065179Daniel Dunbar}
335c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar
336c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar// Code Completion
337c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar
3381abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregorstatic bool EnableCodeCompletion(Preprocessor &PP,
3391abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                 const std::string &Filename,
3401abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                 unsigned Line,
3411abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                 unsigned Column) {
3421abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  // Tell the source manager to chop off the given file at a specific
3431abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  // line and column.
34439b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner  const FileEntry *Entry = PP.getFileManager().getFile(Filename);
3451abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  if (!Entry) {
3461abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor    PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file)
3471abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor      << Filename;
3481abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor    return true;
3491abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  }
3501abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
3511abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  // Truncate the named file at the given line/column.
3521abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  PP.SetCodeCompletionPoint(Entry, Line, Column);
3531abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  return false;
3541abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor}
3551abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor
356c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbarvoid CompilerInstance::createCodeCompletionConsumer() {
357c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar  const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt;
3581abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  if (!CompletionConsumer) {
3591abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor    CompletionConsumer.reset(
3601abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor      createCodeCompletionConsumer(getPreprocessor(),
3611abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                   Loc.FileName, Loc.Line, Loc.Column,
3621abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                   getFrontendOpts().ShowMacrosInCodeCompletion,
363d8e8a58ee35ab334ab9d0c2154dca029c1822e8aDouglas Gregor                             getFrontendOpts().ShowCodePatternsInCodeCompletion,
3648071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor                           getFrontendOpts().ShowGlobalSymbolsInCodeCompletion,
3651abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                   llvm::outs()));
3661abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor    if (!CompletionConsumer)
3671abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor      return;
3681abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName,
3691abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor                                  Loc.Line, Loc.Column)) {
3701abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor    CompletionConsumer.reset();
371c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    return;
3721abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  }
3732b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor
3742b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor  if (CompletionConsumer->isOutputBinary() &&
3752b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor      llvm::sys::Program::ChangeStdoutToBinary()) {
3762b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor    getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary);
3772b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor    CompletionConsumer.reset();
3782b4074f1d3919e77cb33ca49c960521dea27afabDouglas Gregor  }
379c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar}
380c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar
381f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnamvoid CompilerInstance::createFrontendTimer() {
382f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam  FrontendTimer.reset(new llvm::Timer("Clang front-end timer"));
383f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam}
384f79bafa608a5d7c49ec40ad199af5e32f3038b47Kovarththanan Rajaratnam
385c2f484f1f05216a9a427ac84b5773789a4661111Daniel DunbarCodeCompleteConsumer *
386c2f484f1f05216a9a427ac84b5773789a4661111Daniel DunbarCompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
387c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar                                               const std::string &Filename,
388c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar                                               unsigned Line,
389c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar                                               unsigned Column,
390c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar                                               bool ShowMacros,
391d8e8a58ee35ab334ab9d0c2154dca029c1822e8aDouglas Gregor                                               bool ShowCodePatterns,
3928071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor                                               bool ShowGlobals,
3935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                               raw_ostream &OS) {
3941abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor  if (EnableCodeCompletion(PP, Filename, Line, Column))
395c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar    return 0;
396c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar
397c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar  // Set up the creation routine for code-completion.
398a9f4f620daf073805b89e893afcdc5eb7a9bdc50Douglas Gregor  return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns,
3998071e4212ae08f8014e0c3ae6d18b7388003a5ccDouglas Gregor                                          ShowGlobals, OS);
400c2f484f1f05216a9a427ac84b5773789a4661111Daniel Dunbar}
401a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar
402467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregorvoid CompilerInstance::createSema(TranslationUnitKind TUKind,
403f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor                                  CodeCompleteConsumer *CompletionConsumer) {
404f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor  TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(),
405467dc88512b4ba4bb16e274ea3771dc1415d31daDouglas Gregor                         TUKind, CompletionConsumer));
406f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor}
407f18d0d8b39e891460d50f8a8b85029885b264986Douglas Gregor
408a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar// Output Files
409a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar
410dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidisvoid CompilerInstance::addOutputFile(const OutputFile &OutFile) {
411dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  assert(OutFile.OS && "Attempt to add empty stream to output list!");
412dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  OutputFiles.push_back(OutFile);
413a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar}
414a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar
415e51dd7be67808d52c80c09b832d875e9655ce6e0Kovarththanan Rajaratnamvoid CompilerInstance::clearOutputFiles(bool EraseFiles) {
416dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  for (std::list<OutputFile>::iterator
417a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar         it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) {
418dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    delete it->OS;
419dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    if (!it->TempFilename.empty()) {
420af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson      if (EraseFiles) {
421af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson        bool existed;
422af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson        llvm::sys::fs::remove(it->TempFilename, existed);
423af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson      } else {
424af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson        llvm::SmallString<128> NewOutFile(it->Filename);
425af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson
426389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis        // If '-working-directory' was passed, the output filename should be
427389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis        // relative to that.
4282e2468e2d2ccbb9a38fe3b6b754009af7e5d39a9Anders Carlsson        FileMgr->FixupRelativePath(NewOutFile);
429af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson        if (llvm::error_code ec = llvm::sys::fs::rename(it->TempFilename,
430af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson                                                        NewOutFile.str())) {
431dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis          getDiagnostics().Report(diag::err_fe_unable_to_rename_temp)
432af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson            << it->TempFilename << it->Filename << ec.message();
433af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson
434af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson          bool existed;
435af036a6c25f540a9daf51e721485ae6d0e645bd8Anders Carlsson          llvm::sys::fs::remove(it->TempFilename, existed);
436dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis        }
437dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis      }
438dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    } else if (!it->Filename.empty() && EraseFiles)
439dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis      llvm::sys::Path(it->Filename).eraseFromDisk();
440dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis
441a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar  }
442a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar  OutputFiles.clear();
443a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar}
444a9204831639e31474b927681b97c46781b758a1aDaniel Dunbar
445f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbarllvm::raw_fd_ostream *
446f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel DunbarCompilerInstance::createDefaultOutputFile(bool Binary,
4475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef InFile,
4485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef Extension) {
449f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  return createOutputFile(getFrontendOpts().OutputFile, Binary,
450ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar                          /*RemoveFileOnSignal=*/true, InFile, Extension);
451f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar}
452f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar
453f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbarllvm::raw_fd_ostream *
4545f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerCompilerInstance::createOutputFile(StringRef OutputPath,
455ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar                                   bool Binary, bool RemoveFileOnSignal,
4565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef InFile,
4577e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis                                   StringRef Extension,
4587e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis                                   bool UseTemporary) {
459dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  std::string Error, OutputPathName, TempPathName;
460f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary,
461ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar                                              RemoveFileOnSignal,
462f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar                                              InFile, Extension,
4637e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis                                              UseTemporary,
464dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis                                              &OutputPathName,
465dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis                                              &TempPathName);
466f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  if (!OS) {
467360435908c9b90429cfe192fab22854af1d4497cDaniel Dunbar    getDiagnostics().Report(diag::err_fe_unable_to_open_output)
468360435908c9b90429cfe192fab22854af1d4497cDaniel Dunbar      << OutputPath << Error;
469360435908c9b90429cfe192fab22854af1d4497cDaniel Dunbar    return 0;
470f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  }
471f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar
472f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  // Add the output file -- but don't try to remove "-", since this means we are
473f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  // using stdin.
474dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "",
475dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis                TempPathName, OS));
476f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar
477f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  return OS;
478f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar}
479f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar
480f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbarllvm::raw_fd_ostream *
4815f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerCompilerInstance::createOutputFile(StringRef OutputPath,
482f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar                                   std::string &Error,
483f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar                                   bool Binary,
484ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar                                   bool RemoveFileOnSignal,
4855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef InFile,
4865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   StringRef Extension,
4877e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis                                   bool UseTemporary,
488dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis                                   std::string *ResultPathName,
489dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis                                   std::string *TempPathName) {
490dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  std::string OutFile, TempFile;
491f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  if (!OutputPath.empty()) {
492f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    OutFile = OutputPath;
493f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  } else if (InFile == "-") {
494f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    OutFile = "-";
495f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  } else if (!Extension.empty()) {
496f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    llvm::sys::Path Path(InFile);
497f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    Path.eraseSuffix();
498f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    Path.appendSuffix(Extension);
499f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    OutFile = Path.str();
500f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  } else {
501f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    OutFile = "-";
502f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  }
5037e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis
5047e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis  llvm::OwningPtr<llvm::raw_fd_ostream> OS;
5057e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis  std::string OSFile;
5067e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis
5077e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis  if (UseTemporary && OutFile != "-") {
508dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    llvm::sys::Path OutPath(OutFile);
509dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    // Only create the temporary if we can actually write to OutPath, otherwise
510dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    // we want to fail early.
51132bef4edba854303800b3b01cb49a282e5da4f69Michael J. Spencer    bool Exists;
51232bef4edba854303800b3b01cb49a282e5da4f69Michael J. Spencer    if ((llvm::sys::fs::exists(OutPath.str(), Exists) || !Exists) ||
513dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis        (OutPath.isRegularFile() && OutPath.canWrite())) {
514dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis      // Create a temporary file.
5157e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      llvm::SmallString<128> TempPath;
5167e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      TempPath = OutFile;
5177e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      TempPath += "-%%%%%%%%";
5187e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      int fd;
5197e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath,
5207e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis                               /*makeAbsolute=*/false) == llvm::errc::success) {
5217e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis        OS.reset(new llvm::raw_fd_ostream(fd, /*shouldClose=*/true));
5227e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis        OSFile = TempFile = TempPath.str();
5237e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      }
524dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    }
525dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  }
526dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis
5277e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis  if (!OS) {
5287e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis    OSFile = OutFile;
5297e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis    OS.reset(
5307e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      new llvm::raw_fd_ostream(OSFile.c_str(), Error,
5317e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis                               (Binary ? llvm::raw_fd_ostream::F_Binary : 0)));
5327e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis    if (!Error.empty())
5337e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis      return 0;
5347e90985df09855dc309ed888a5b16a0ae684f8e3Argyrios Kyrtzidis  }
535f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar
536dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  // Make sure the out stream file gets removed if we crash.
537ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar  if (RemoveFileOnSignal)
538ff9cd968cd5b623e3ec7e5f862b598cd22f7ec79Daniel Dunbar    llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile));
539dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis
540f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar  if (ResultPathName)
541f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar    *ResultPathName = OutFile;
542dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis  if (TempPathName)
543dc24572a44575e07a5d8bb6de52641a69f1bab27Argyrios Kyrtzidis    *TempPathName = TempFile;
544f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar
545fc97102a80bfe0afaa25883a2aa6b5e1d7307d0aDaniel Dunbar  return OS.take();
546f482d59386dbc70716f7a5f65adb37ff86b501e6Daniel Dunbar}
547ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar
548ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar// Initialization Utilities
549ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar
5505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool CompilerInstance::InitializeSourceManager(StringRef InputFile) {
551ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar  return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
552ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar                                 getSourceManager(), getFrontendOpts());
553ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar}
554ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar
5555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerbool CompilerInstance::InitializeSourceManager(StringRef InputFile,
556d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                                               DiagnosticsEngine &Diags,
557ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar                                               FileManager &FileMgr,
558ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar                                               SourceManager &SourceMgr,
559ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar                                               const FrontendOptions &Opts) {
560507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  // Figure out where to get and map in the main file.
561507097ec40105ed927cb5a744fad98f5875aacacArgyrios Kyrtzidis  if (InputFile != "-") {
56239b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner    const FileEntry *File = FileMgr.getFile(InputFile);
563694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman    if (!File) {
564ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar      Diags.Report(diag::err_fe_error_reading) << InputFile;
565ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar      return false;
566ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar    }
567694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman    SourceMgr.createMainFileID(File);
568ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar  } else {
5694eeebc464e1f968d9968a4786c82558f18ac2ed8Michael J. Spencer    llvm::OwningPtr<llvm::MemoryBuffer> SB;
5704eeebc464e1f968d9968a4786c82558f18ac2ed8Michael J. Spencer    if (llvm::MemoryBuffer::getSTDIN(SB)) {
5713a321e23f66128dbb986343927456ff6702af617Michael J. Spencer      // FIXME: Give ec.message() in this diag.
572ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar      Diags.Report(diag::err_fe_error_reading_stdin);
573ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar      return false;
574ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar    }
57590d9081cacb4b0163f2c7527f666d6515257067cDan Gohman    const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
57639b49bcaaddb1049234fca9500c0ac02c088e23dChris Lattner                                                   SB->getBufferSize(), 0);
57790d9081cacb4b0163f2c7527f666d6515257067cDan Gohman    SourceMgr.createMainFileID(File);
5784eeebc464e1f968d9968a4786c82558f18ac2ed8Michael J. Spencer    SourceMgr.overrideFileContents(File, SB.take());
579ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar  }
580ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar
581694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman  assert(!SourceMgr.getMainFileID().isInvalid() &&
582694137c54c79a33c9ac6c07e68327750dcd5adf7Dan Gohman         "Couldn't establish MainFileID!");
583ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar  return true;
584ccb6cb6fd9e48697564d536b07397b95dfc28d5bDaniel Dunbar}
5850397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
5860397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar// High-Level Operations
5870397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
5880397af277e3bba16da1fd125ddba07415686b429Daniel Dunbarbool CompilerInstance::ExecuteAction(FrontendAction &Act) {
5890397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
5900397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
5910397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
5920397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
5930397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // FIXME: Take this as an argument, once all the APIs we used have moved to
5940397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // taking it as an input instead of hard-coding llvm::errs.
5955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  raw_ostream &OS = llvm::errs();
5960397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
5970397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // Create the target instance.
5980397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts()));
5990397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  if (!hasTarget())
6000397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    return false;
6010397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
6020397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // Inform the target of the language options.
6030397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  //
6040397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // FIXME: We shouldn't need to do this, the target should be immutable once
6050397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // created. This complexity should be lifted elsewhere.
6060397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  getTarget().setForcedLangOptions(getLangOpts());
6070397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
6080397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  // Validate/process some options.
6090397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  if (getHeaderSearchOpts().Verbose)
6100397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    OS << "clang -cc1 version " CLANG_VERSION_STRING
6110397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar       << " based upon " << PACKAGE_STRING
6120397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar       << " hosted on " << llvm::sys::getHostTriple() << "\n";
6130397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
6140397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  if (getFrontendOpts().ShowTimers)
6150397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    createFrontendTimer();
6160397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
61795dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor  if (getFrontendOpts().ShowStats)
61895dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor    llvm::EnableStatistics();
61995dd5583e3900443a1d7970d557d8c54aa320601Douglas Gregor
6200397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) {
6210397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    const std::string &InFile = getFrontendOpts().Inputs[i].second;
6220397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
6232056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar    // Reset the ID tables if we are reusing the SourceManager.
6242056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar    if (hasSourceManager())
6252056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar      getSourceManager().clearIDTables();
6260397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
627d3598a65716e120aef45aa2841d730e03f7101feDaniel Dunbar    if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) {
6280397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar      Act.Execute();
6290397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar      Act.EndSourceFile();
6300397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    }
6310397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  }
6320397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
63353eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  if (getDiagnosticOpts().ShowCarets) {
634f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    // We can have multiple diagnostics sharing one diagnostic client.
635f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    // Get the total number of warnings/errors from the client.
636f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
637f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
63853eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner
63953eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner    if (NumWarnings)
64053eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner      OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
64153eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner    if (NumWarnings && NumErrors)
64253eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner      OS << " and ";
64353eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner    if (NumErrors)
64453eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner      OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s");
64553eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner    if (NumWarnings || NumErrors)
64653eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner      OS << " generated.\n";
64753eee7ba970d21ff15bbd4334164037a3b4cc4b8Chris Lattner  }
6480397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
6492056048f0f619adadc9a5416a2c4cdf95c58eef7Daniel Dunbar  if (getFrontendOpts().ShowStats && hasFileManager()) {
6500397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    getFileManager().PrintStats();
6510397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar    OS << "\n";
6520397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar  }
6530397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
654ab41b97c3cddf1bcdd8bf82ab09ed3ceafcd05b1Argyrios Kyrtzidis  return !getDiagnostics().getClient()->getNumErrors();
6550397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar}
6560397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
65721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// \brief Determine the appropriate source input kind based on language
65821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// options.
65921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregorstatic InputKind getSourceInputKindFromOptions(const LangOptions &LangOpts) {
66021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  if (LangOpts.OpenCL)
66121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    return IK_OpenCL;
66221cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  if (LangOpts.CUDA)
66321cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    return IK_CUDA;
66421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  if (LangOpts.ObjC1)
66521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    return LangOpts.CPlusPlus? IK_ObjCXX : IK_ObjC;
66621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  return LangOpts.CPlusPlus? IK_CXX : IK_C;
66721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor}
66821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
6690ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregornamespace {
6700ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  struct CompileModuleData {
6710ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor    CompilerInstance &Instance;
6720ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor    GeneratePCHAction &CreateModuleAction;
6730ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  };
6740ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor}
6750ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor
6760ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor/// \brief Helper function that executes the module-generating action under
6770ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor/// a crash recovery context.
6780ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregorstatic void doCompileModule(void *UserData) {
6790ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  CompileModuleData &Data = *reinterpret_cast<CompileModuleData *>(UserData);
6800ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  Data.Instance.ExecuteAction(Data.CreateModuleAction);
6810ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor}
6820ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor
6832bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregornamespace {
6842bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// \brief Class that manages the creation of a lock file to aid
6852bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// implicit coordination between different processes.
6862bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  ///
6872bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// The implicit coordination works by creating a ".lock" file alongside
6882bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// the file that we're coordinating for, using the atomicity of the file
6892bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// system to ensure that only a single process can create that ".lock" file.
6902bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// When the lock file is removed, the owning process has finished the
6912bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  /// operation.
6922bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  class LockFileManager {
6932bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  public:
6942bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    /// \brief Describes the state of a lock file.
6952bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    enum LockFileState {
6962bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      /// \brief The lock file has been created and is owned by this instance
6972bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      /// of the object.
6982bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      LFS_Owned,
6992bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      /// \brief The lock file already exists and is owned by some other
7002bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      /// instance.
7012bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      LFS_Shared,
7022bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      /// \brief An error occurred while trying to create or find the lock
7032bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      /// file.
7042bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      LFS_Error
7052bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    };
7062bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7072bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  private:
7082bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    llvm::SmallString<128> LockFileName;
7092bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    llvm::SmallString<128> UniqueLockFileName;
7102bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7112bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    llvm::Optional<std::pair<std::string, int> > Owner;
7122bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    llvm::Optional<llvm::error_code> Error;
7132bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7142bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    LockFileManager(const LockFileManager &);
7152bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    LockFileManager &operator=(const LockFileManager &);
7162bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7172bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    static llvm::Optional<std::pair<std::string, int> >
7182bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    readLockFile(StringRef LockFileName);
7192bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7202bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    static bool processStillExecuting(StringRef Hostname, int PID);
7212bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7222bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  public:
7232bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7242bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    LockFileManager(StringRef FileName);
7252bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    ~LockFileManager();
7262bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7272bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    /// \brief Determine the state of the lock file.
7282bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    LockFileState getState() const;
7292bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7302bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    operator LockFileState() const { return getState(); }
7312bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7322bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    /// \brief For a shared lock, wait until the owner releases the lock.
7332bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    void waitForUnlock();
7342bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  };
7352bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
7362bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7372bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// \brief Attempt to read the lock file with the given name, if it exists.
7382bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor///
7392bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// \param LockFileName The name of the lock file to read.
7402bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor///
7412bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor/// \returns The process ID of the process that owns this lock file
7422bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregorllvm::Optional<std::pair<std::string, int> >
7432bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::readLockFile(StringRef LockFileName) {
7442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Check whether the lock file exists. If not, clearly there's nothing
7452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // to read, so we just return.
7462bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  bool Exists = false;
7472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (llvm::sys::fs::exists(LockFileName, Exists) || !Exists)
7482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return llvm::Optional<std::pair<std::string, int> >();
7492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7502bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Read the owning host and PID out of the lock file. If it appears that the
7512bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // owning process is dead, the lock file is invalid.
7522bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  int PID = 0;
7532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  std::string Hostname;
7542bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  std::ifstream Input(LockFileName.str().c_str());
7552bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (Input >> Hostname >> PID && PID > 0 &&
7562bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      processStillExecuting(Hostname, PID))
7572bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return std::make_pair(Hostname, PID);
7582bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7592bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Delete the lock file. It's invalid anyway.
7602bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  bool Existed;
7612bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  llvm::sys::fs::remove(LockFileName, Existed);
7622bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  return llvm::Optional<std::pair<std::string, int> >();
7632bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
7642bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7652bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregorbool LockFileManager::processStillExecuting(StringRef Hostname, int PID) {
7662bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#if LLVM_ON_UNIX
7672bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  char MyHostname[256];
7682bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  MyHostname[255] = 0;
7692bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  MyHostname[0] = 0;
7702bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  gethostname(MyHostname, 255);
7712bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Check whether the process is dead. If so, we're done.
7722bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (MyHostname == Hostname && getsid(PID) == -1 && errno == ESRCH)
7732bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return false;
7742bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif
7752bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7762bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  return true;
7772bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
7782bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7792bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::LockFileManager(StringRef FileName)
7802bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor{
7812bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  LockFileName = FileName;
7822bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  LockFileName += ".lock";
7832bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7842bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // If the lock file already exists, don't bother to try to create our own
7852bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // lock file; it won't work anyway. Just figure out who owns this lock file.
7862bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if ((Owner = readLockFile(LockFileName)))
7872bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
7882bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
7892bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Create a lock file that is unique to this instance.
7902bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  UniqueLockFileName = LockFileName;
7912bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  UniqueLockFileName += "-%%%%%%%%";
7922bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  int UniqueLockFileID;
7932bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (llvm::error_code EC
7942bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor        = llvm::sys::fs::unique_file(UniqueLockFileName.str(),
7952bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor                                     UniqueLockFileID,
7962bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor                                     UniqueLockFileName,
7972bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor                                     /*makeAbsolute=*/false)) {
7982bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Error = EC;
7992bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
8002bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  }
8012bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8022bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Write our process ID to our unique lock file.
8032bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  {
8042bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    llvm::raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
8052bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8062bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#if LLVM_ON_UNIX
8072bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // FIXME: move getpid() call into LLVM
8082bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    char hostname[256];
8092bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    hostname[255] = 0;
8102bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    hostname[0] = 0;
8112bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    gethostname(hostname, 255);
8122bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Out << hostname << ' ' << getpid();
8132bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#else
8142bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Out << "localhost 1";
8152bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif
8162bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Out.close();
8172bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8182bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    if (Out.has_error()) {
8192bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      // We failed to write out PID, so make up an excuse, remove the
8202bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      // unique lock file, and fail.
8212bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      Error = llvm::make_error_code(llvm::errc::no_space_on_device);
8222bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      bool Existed;
8232bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      llvm::sys::fs::remove(UniqueLockFileName.c_str(), Existed);
8242bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      return;
8252bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    }
8262bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  }
8272bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8282bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Create a hard link from the lock file name. If this succeeds, we're done.
8292bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  llvm::error_code EC
8302bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    = llvm::sys::fs::create_hard_link(UniqueLockFileName.str(),
8312bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor                                      LockFileName.str());
8322bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (EC == llvm::errc::success)
8332bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
8342bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8352bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Creating the hard link failed.
8362bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8372bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#ifdef LLVM_ON_UNIX
8382bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // The creation of the hard link may appear to fail, but if stat'ing the
8392bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // unique file returns a link count of 2, then we can still declare success.
8402bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  struct stat StatBuf;
8412bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (stat(UniqueLockFileName.c_str(), &StatBuf) == 0 &&
8422bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      StatBuf.st_nlink == 2)
8432bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
8442bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor#endif
8452bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8462bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Someone else managed to create the lock file first. Wipe out our unique
8472bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // lock file (it's useless now) and read the process ID from the lock file.
8482bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  bool Existed;
8492bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  llvm::sys::fs::remove(UniqueLockFileName.str(), Existed);
8502bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if ((Owner = readLockFile(LockFileName)))
8512bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
8522bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8532bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // There is a lock file that nobody owns; try to clean it up and report
8542bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // an error.
8552bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  llvm::sys::fs::remove(LockFileName.str(), Existed);
8562bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  Error = EC;
8572bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
8582bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8592bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::LockFileState LockFileManager::getState() const {
8602bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (Owner)
8612bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return LFS_Shared;
8622bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8632bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (Error)
8642bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return LFS_Error;
8652bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8662bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  return LFS_Owned;
8672bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
8682bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8692bc750700487163e31e4f67cd0e0886868209e3cDouglas GregorLockFileManager::~LockFileManager() {
8702bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (getState() != LFS_Owned)
8712bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
8722bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8732bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Since we own the lock, remove the lock file and our own unique lock file.
8742bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  bool Existed;
8752bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  llvm::sys::fs::remove(LockFileName.str(), Existed);
8762bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  llvm::sys::fs::remove(UniqueLockFileName.str(), Existed);
8772bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
8782bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8792bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregorvoid LockFileManager::waitForUnlock() {
8802bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  if (getState() != LFS_Shared)
8812bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
8822bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8832bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  struct timespec Interval;
8842bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  Interval.tv_sec = 0;
8852bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  Interval.tv_nsec = 1000000;
8862bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8872bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Don't wait more than an hour for the file to appear.
8882bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  const unsigned MaxSeconds = 3600;
8892bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  do {
8902bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // Sleep for the designated interval, to allow the owning process time to
8912bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // finish up and
8922bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // FIXME: Should we hook in to system APIs to get a notification when the
8932bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // lock file is deleted?
8942bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    nanosleep(&Interval, NULL);
8952bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
8962bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // If the file no longer exists, we're done.
8972bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    bool Exists = false;
8982bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    if (!llvm::sys::fs::exists(LockFileName.str(), Exists) && !Exists)
8992bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      return;
9002bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
9012bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    if (!processStillExecuting((*Owner).first, (*Owner).second))
9022bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      return;
9032bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
9042bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // Exponentially increase the time we wait for the lock to be removed.
9052bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Interval.tv_sec *= 2;
9062bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Interval.tv_nsec *= 2;
9072bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    if (Interval.tv_nsec >= 1000000000) {
9082bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      ++Interval.tv_sec;
9092bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor      Interval.tv_nsec -= 1000000000;
9102bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    }
9112bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  } while (Interval.tv_sec < MaxSeconds);
9122bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
9132bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  // Give up.
9142bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor}
9152bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
91621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// \brief Compile a module file for the given module name with the given
91721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// umbrella header, using the options provided by the importing compiler
91821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor/// instance.
91921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregorstatic void compileModule(CompilerInstance &ImportingInstance,
92021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor                          StringRef ModuleName,
9216e975c4517958bcc11c834336d340797356058dbDouglas Gregor                          StringRef ModuleFileName,
92221cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor                          StringRef UmbrellaHeader) {
9232bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  LockFileManager Locked(ModuleFileName);
9242bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  switch (Locked) {
9252bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  case LockFileManager::LFS_Error:
9262bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    return;
9272bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
9282bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  case LockFileManager::LFS_Owned:
9292bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // We're responsible for building the module ourselves. Do so below.
9302bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    break;
9312bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
9322bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  case LockFileManager::LFS_Shared:
9332bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // Someone else is responsible for building the module. Wait for them to
9342bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    // finish.
9352bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    Locked.waitForUnlock();
9362bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor    break;
9372bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor  }
9382bc750700487163e31e4f67cd0e0886868209e3cDouglas Gregor
93921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  // Construct a compiler invocation for creating this module.
94021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  llvm::IntrusiveRefCntPtr<CompilerInvocation> Invocation
94121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    (new CompilerInvocation(ImportingInstance.getInvocation()));
942b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor
943b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor  // For any options that aren't intended to affect how a module is built,
944b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor  // reset them to their default values.
9451c7e0472f5683a8ade62285f366637050cf113e5Douglas Gregor  Invocation->getLangOpts().resetNonModularOptions();
9461c7e0472f5683a8ade62285f366637050cf113e5Douglas Gregor  Invocation->getPreprocessorOpts().resetNonModularOptions();
947b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor
948b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor  // Note that this module is part of the module build path, so that we
949b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor  // can detect cycles in the module graph.
9504ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor  Invocation->getPreprocessorOpts().ModuleBuildPath.push_back(ModuleName);
9511c7e0472f5683a8ade62285f366637050cf113e5Douglas Gregor
952b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor  // Set up the inputs/outputs so that we build the module from its umbrella
953b2d39c217b2cfe87cbe69f25df83869940d8adcdDouglas Gregor  // header.
95421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  FrontendOptions &FrontendOpts = Invocation->getFrontendOpts();
9556e975c4517958bcc11c834336d340797356058dbDouglas Gregor  FrontendOpts.OutputFile = ModuleFileName.str();
95621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  FrontendOpts.DisableFree = false;
95721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  FrontendOpts.Inputs.clear();
95821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  FrontendOpts.Inputs.push_back(
95921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    std::make_pair(getSourceInputKindFromOptions(Invocation->getLangOpts()),
96021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor                                                 UmbrellaHeader));
96178243658c533168d51fd076fba328437932ba6f1Douglas Gregor
96278243658c533168d51fd076fba328437932ba6f1Douglas Gregor  Invocation->getDiagnosticOpts().VerifyDiagnostics = 0;
96378243658c533168d51fd076fba328437932ba6f1Douglas Gregor
9644ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor
96576d991ec6c5cd247ee18fe65c35d43c2d47cf094Douglas Gregor  assert(ImportingInstance.getInvocation().getModuleHash() ==
96676d991ec6c5cd247ee18fe65c35d43c2d47cf094Douglas Gregor           Invocation->getModuleHash() && "Module hash mismatch!");
96721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
96821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  // Construct a compiler instance that will be used to actually create the
96921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  // module.
97021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  CompilerInstance Instance;
97121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  Instance.setInvocation(&*Invocation);
97278243658c533168d51fd076fba328437932ba6f1Douglas Gregor  Instance.createDiagnostics(/*argc=*/0, /*argv=*/0,
97378243658c533168d51fd076fba328437932ba6f1Douglas Gregor                             &ImportingInstance.getDiagnosticClient(),
974aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                             /*ShouldOwnClient=*/true,
975aee526e77657afd1600276450e9c346953ad51d7Douglas Gregor                             /*ShouldCloneClient=*/true);
97621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
97721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  // Construct a module-generating action.
97821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  GeneratePCHAction CreateModuleAction(true);
97921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
9800ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  // Execute the action to actually build the module in-place. Use a separate
9810ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  // thread so that we get a stack large enough.
9820ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  const unsigned ThreadStackSize = 8 << 20;
9830ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  llvm::CrashRecoveryContext CRC;
9840ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  CompileModuleData Data = { Instance, CreateModuleAction };
9850ced799878d1beb8f0fa1cc31fa6d2e4229c217cDouglas Gregor  CRC.RunSafelyOnThread(&doCompileModule, &Data, ThreadStackSize);
98678243658c533168d51fd076fba328437932ba6f1Douglas Gregor}
98721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
9886aa52ec6b969faabf3764baf79d89810b8249a7eDouglas GregorModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc,
9896aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                       IdentifierInfo &ModuleName,
9906aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                       SourceLocation ModuleNameLoc) {
9916aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  // Determine what file we're searching from.
9926aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  SourceManager &SourceMgr = getSourceManager();
9936aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
9946aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  const FileEntry *CurFile
9956aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    = SourceMgr.getFileEntryForID(SourceMgr.getFileID(ExpandedImportLoc));
9966aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (!CurFile)
9976aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    CurFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
9986aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
9996aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  // Search for a module with the given name.
100021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  std::string UmbrellaHeader;
10016e975c4517958bcc11c834336d340797356058dbDouglas Gregor  std::string ModuleFileName;
10026aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  const FileEntry *ModuleFile
100321cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName(),
10046e975c4517958bcc11c834336d340797356058dbDouglas Gregor                                             &ModuleFileName,
100521cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor                                             &UmbrellaHeader);
100621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
100721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  bool BuildingModule = false;
100821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  if (!ModuleFile && !UmbrellaHeader.empty()) {
100921cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    // We didn't find the module, but there is an umbrella header that
101021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    // can be used to create the module file. Create a separate compilation
101121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    // module to do so.
10124ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor
10134ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor    // Check whether there is a cycle in the module graph.
10144ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor    SmallVectorImpl<std::string> &ModuleBuildPath
10154ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      = getPreprocessorOpts().ModuleBuildPath;
10164ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor    SmallVectorImpl<std::string>::iterator Pos
10174ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      = std::find(ModuleBuildPath.begin(), ModuleBuildPath.end(),
10184ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor                  ModuleName.getName());
10194ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor    if (Pos != ModuleBuildPath.end()) {
10204ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      llvm::SmallString<256> CyclePath;
10214ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      for (; Pos != ModuleBuildPath.end(); ++Pos) {
10224ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor        CyclePath += *Pos;
10234ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor        CyclePath += " -> ";
10244ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      }
10254ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      CyclePath += ModuleName.getName();
10264ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor
10274ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      getDiagnostics().Report(ModuleNameLoc, diag::err_module_cycle)
10284ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor        << ModuleName.getName() << CyclePath;
10294ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor      return 0;
10304ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor    }
10314ebd45f4279d84416568ada6adf56044bdf391b7Douglas Gregor
103208d6acf6e5efa6df57345922d412476127f856fcDouglas Gregor    getDiagnostics().Report(ModuleNameLoc, diag::warn_module_build)
103308d6acf6e5efa6df57345922d412476127f856fcDouglas Gregor      << ModuleName.getName();
103421cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    BuildingModule = true;
10356e975c4517958bcc11c834336d340797356058dbDouglas Gregor    compileModule(*this, ModuleName.getName(), ModuleFileName, UmbrellaHeader);
103621cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    ModuleFile = PP->getHeaderSearchInfo().lookupModule(ModuleName.getName());
103721cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor  }
103821cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor
10396aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (!ModuleFile) {
104021cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor    getDiagnostics().Report(ModuleNameLoc,
104121cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor                            BuildingModule? diag::err_module_not_built
104221cae2059a06f7d89eee169409c9266def1b1acaDouglas Gregor                                          : diag::err_module_not_found)
10436aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor      << ModuleName.getName()
10446aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor      << SourceRange(ImportLoc, ModuleNameLoc);
10456aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return 0;
10466aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
10476aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
10486aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  // If we don't already have an ASTReader, create one now.
10496aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  if (!ModuleManager) {
1050de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor    if (!hasASTContext())
1051de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor      createASTContext();
1052de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor
10536aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    std::string Sysroot = getHeaderSearchOpts().Sysroot;
10546aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    const PreprocessorOptions &PPOpts = getPreprocessorOpts();
1055f8a1e51c48761ee1d7803c3fa35ac94f42ebb55eDouglas Gregor    ModuleManager = new ASTReader(getPreprocessor(), *Context,
10566aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                  Sysroot.empty() ? "" : Sysroot.c_str(),
10576aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                  PPOpts.DisablePCHValidation,
10586aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                  PPOpts.DisableStatCache);
1059de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor    if (hasASTConsumer()) {
1060de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor      ModuleManager->setDeserializationListener(
1061de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor        getASTConsumer().GetASTDeserializationListener());
1062de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor      getASTContext().setASTMutationListener(
1063de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor        getASTConsumer().GetASTMutationListener());
1064de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor    }
10656aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    llvm::OwningPtr<ExternalASTSource> Source;
10666aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    Source.reset(ModuleManager);
10676aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    getASTContext().setExternalSource(Source);
1068de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor    if (hasSema())
1069de8a9050d79d66325a18168a0994fed125a7790dDouglas Gregor      ModuleManager->InitializeSema(getSema());
10701a995ddaa53a20dcd063ea47eb1f533ecb0d243aDouglas Gregor    if (hasASTConsumer())
10711a995ddaa53a20dcd063ea47eb1f533ecb0d243aDouglas Gregor      ModuleManager->StartTranslationUnit(&getASTConsumer());
10726aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
10736aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
10746aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  // Try to load the module we found.
10756aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  switch (ModuleManager->ReadAST(ModuleFile->getName(),
10766aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor                                 serialization::MK_Module)) {
10776aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  case ASTReader::Success:
10786aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    break;
10796aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
10806aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  case ASTReader::IgnorePCH:
10816aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // FIXME: The ASTReader will already have complained, but can we showhorn
10826aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // that diagnostic information into a more useful form?
10836aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return 0;
10846aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
10856aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  case ASTReader::Failure:
10866aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    // Already complained.
10876aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor    return 0;
10886aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  }
10896aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor
10906aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  // FIXME: The module file's FileEntry makes a poor key indeed!
10916aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor  return (ModuleKey)ModuleFile;
10926aa52ec6b969faabf3764baf79d89810b8249a7eDouglas Gregor}
10930397af277e3bba16da1fd125ddba07415686b429Daniel Dunbar
1094