]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Frontend/ASTUnit.cpp
Update clang to r97873.
[FreeBSD/FreeBSD.git] / lib / Frontend / ASTUnit.cpp
1 //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // ASTUnit Implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/PCHReader.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTConsumer.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "clang/Driver/Compilation.h"
21 #include "clang/Driver/Driver.h"
22 #include "clang/Driver/Job.h"
23 #include "clang/Driver/Tool.h"
24 #include "clang/Frontend/CompilerInstance.h"
25 #include "clang/Frontend/FrontendActions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Frontend/FrontendOptions.h"
28 #include "clang/Lex/HeaderSearch.h"
29 #include "clang/Lex/Preprocessor.h"
30 #include "clang/Basic/TargetOptions.h"
31 #include "clang/Basic/TargetInfo.h"
32 #include "clang/Basic/Diagnostic.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/System/Host.h"
35 #include "llvm/System/Path.h"
36 using namespace clang;
37
38 ASTUnit::ASTUnit(bool _MainFileIsAST)
39   : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) {
40 }
41 ASTUnit::~ASTUnit() {
42   ConcurrencyCheckValue = CheckLocked;
43   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
44     TemporaryFiles[I].eraseFromDisk();
45 }
46
47 namespace {
48
49 /// \brief Gathers information from PCHReader that will be used to initialize
50 /// a Preprocessor.
51 class PCHInfoCollector : public PCHReaderListener {
52   LangOptions &LangOpt;
53   HeaderSearch &HSI;
54   std::string &TargetTriple;
55   std::string &Predefines;
56   unsigned &Counter;
57
58   unsigned NumHeaderInfos;
59
60 public:
61   PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI,
62                    std::string &TargetTriple, std::string &Predefines,
63                    unsigned &Counter)
64     : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple),
65       Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {}
66
67   virtual bool ReadLanguageOptions(const LangOptions &LangOpts) {
68     LangOpt = LangOpts;
69     return false;
70   }
71
72   virtual bool ReadTargetTriple(llvm::StringRef Triple) {
73     TargetTriple = Triple;
74     return false;
75   }
76
77   virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef,
78                                     FileID PCHBufferID,
79                                     llvm::StringRef OriginalFileName,
80                                     std::string &SuggestedPredefines) {
81     Predefines = PCHPredef;
82     return false;
83   }
84
85   virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI) {
86     HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
87   }
88
89   virtual void ReadCounter(unsigned Value) {
90     Counter = Value;
91   }
92 };
93
94 class StoredDiagnosticClient : public DiagnosticClient {
95   llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags;
96   
97 public:
98   explicit StoredDiagnosticClient(
99                           llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
100     : StoredDiags(StoredDiags) { }
101   
102   virtual void HandleDiagnostic(Diagnostic::Level Level,
103                                 const DiagnosticInfo &Info);
104 };
105
106 /// \brief RAII object that optionally captures diagnostics, if
107 /// there is no diagnostic client to capture them already.
108 class CaptureDroppedDiagnostics {
109   Diagnostic &Diags;
110   StoredDiagnosticClient Client;
111   DiagnosticClient *PreviousClient;
112
113 public:
114   CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, 
115                            llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags)
116     : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) 
117   {
118     if (RequestCapture || Diags.getClient() == 0)
119       Diags.setClient(&Client);
120   }
121
122   ~CaptureDroppedDiagnostics() {
123     Diags.setClient(PreviousClient);
124   }
125 };
126
127 } // anonymous namespace
128
129 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
130                                               const DiagnosticInfo &Info) {
131   StoredDiags.push_back(StoredDiagnostic(Level, Info));
132 }
133
134 const std::string &ASTUnit::getOriginalSourceFileName() {
135   return OriginalSourceFile;
136 }
137
138 const std::string &ASTUnit::getPCHFileName() {
139   assert(isMainFileAST() && "Not an ASTUnit from a PCH file!");
140   return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName();
141 }
142
143 ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename,
144                                   Diagnostic &Diags,
145                                   bool OnlyLocalDecls,
146                                   RemappedFile *RemappedFiles,
147                                   unsigned NumRemappedFiles,
148                                   bool CaptureDiagnostics) {
149   llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true));
150   AST->OnlyLocalDecls = OnlyLocalDecls;
151   AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager()));
152
153   // If requested, capture diagnostics in the ASTUnit.
154   CaptureDroppedDiagnostics Capture(CaptureDiagnostics, Diags, 
155                                     AST->Diagnostics);
156
157   for (unsigned I = 0; I != NumRemappedFiles; ++I) {
158     // Create the file entry for the file that we're mapping from.
159     const FileEntry *FromFile
160       = AST->getFileManager().getVirtualFile(RemappedFiles[I].first,
161                                     RemappedFiles[I].second->getBufferSize(),
162                                              0);
163     if (!FromFile) {
164       Diags.Report(diag::err_fe_remap_missing_from_file)
165         << RemappedFiles[I].first;
166       delete RemappedFiles[I].second;
167       continue;
168     }
169     
170     // Override the contents of the "from" file with the contents of
171     // the "to" file.
172     AST->getSourceManager().overrideFileContents(FromFile, 
173                                                  RemappedFiles[I].second);    
174   }
175   
176   // Gather Info for preprocessor construction later on.
177
178   LangOptions LangInfo;
179   HeaderSearch &HeaderInfo = *AST->HeaderInfo.get();
180   std::string TargetTriple;
181   std::string Predefines;
182   unsigned Counter;
183
184   llvm::OwningPtr<PCHReader> Reader;
185   llvm::OwningPtr<ExternalASTSource> Source;
186
187   Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(),
188                              Diags));
189   Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple,
190                                            Predefines, Counter));
191
192   switch (Reader->ReadPCH(Filename)) {
193   case PCHReader::Success:
194     break;
195
196   case PCHReader::Failure:
197   case PCHReader::IgnorePCH:
198     Diags.Report(diag::err_fe_unable_to_load_pch);
199     return NULL;
200   }
201
202   AST->OriginalSourceFile = Reader->getOriginalSourceFile();
203
204   // PCH loaded successfully. Now create the preprocessor.
205
206   // Get information about the target being compiled for.
207   //
208   // FIXME: This is broken, we should store the TargetOptions in the PCH.
209   TargetOptions TargetOpts;
210   TargetOpts.ABI = "";
211   TargetOpts.CPU = "";
212   TargetOpts.Features.clear();
213   TargetOpts.Triple = TargetTriple;
214   AST->Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
215   AST->PP.reset(new Preprocessor(Diags, LangInfo, *AST->Target.get(),
216                                  AST->getSourceManager(), HeaderInfo));
217   Preprocessor &PP = *AST->PP.get();
218
219   PP.setPredefines(Reader->getSuggestedPredefines());
220   PP.setCounterValue(Counter);
221   Reader->setPreprocessor(PP);
222
223   // Create and initialize the ASTContext.
224
225   AST->Ctx.reset(new ASTContext(LangInfo,
226                                 AST->getSourceManager(),
227                                 *AST->Target.get(),
228                                 PP.getIdentifierTable(),
229                                 PP.getSelectorTable(),
230                                 PP.getBuiltinInfo(),
231                                 /* FreeMemory = */ false,
232                                 /* size_reserve = */0));
233   ASTContext &Context = *AST->Ctx.get();
234
235   Reader->InitializeContext(Context);
236
237   // Attach the PCH reader to the AST context as an external AST
238   // source, so that declarations will be deserialized from the
239   // PCH file as needed.
240   Source.reset(Reader.take());
241   Context.setExternalSource(Source);
242
243   return AST.take();
244 }
245
246 namespace {
247
248 class TopLevelDeclTrackerConsumer : public ASTConsumer {
249   ASTUnit &Unit;
250
251 public:
252   TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {}
253
254   void HandleTopLevelDecl(DeclGroupRef D) {
255     for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it)
256       Unit.getTopLevelDecls().push_back(*it);
257   }
258 };
259
260 class TopLevelDeclTrackerAction : public ASTFrontendAction {
261 public:
262   ASTUnit &Unit;
263
264   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
265                                          llvm::StringRef InFile) {
266     return new TopLevelDeclTrackerConsumer(Unit);
267   }
268
269 public:
270   TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {}
271
272   virtual bool hasCodeCompletionSupport() const { return false; }
273 };
274
275 }
276
277 ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI,
278                                              Diagnostic &Diags,
279                                              bool OnlyLocalDecls,
280                                              bool CaptureDiagnostics) {
281   // Create the compiler instance to use for building the AST.
282   CompilerInstance Clang;
283   llvm::OwningPtr<ASTUnit> AST;
284   llvm::OwningPtr<TopLevelDeclTrackerAction> Act;
285
286   Clang.setInvocation(CI);
287
288   Clang.setDiagnostics(&Diags);
289   Clang.setDiagnosticClient(Diags.getClient());
290
291   // Create the target instance.
292   Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(),
293                                                Clang.getTargetOpts()));
294   if (!Clang.hasTarget()) {
295     Clang.takeSourceManager();
296     Clang.takeFileManager();
297     Clang.takeDiagnosticClient();
298     Clang.takeDiagnostics();
299     return 0;
300   }
301
302   // Inform the target of the language options.
303   //
304   // FIXME: We shouldn't need to do this, the target should be immutable once
305   // created. This complexity should be lifted elsewhere.
306   Clang.getTarget().setForcedLangOptions(Clang.getLangOpts());
307
308   assert(Clang.getFrontendOpts().Inputs.size() == 1 &&
309          "Invocation must have exactly one source file!");
310   assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST &&
311          "FIXME: AST inputs not yet supported here!");
312
313   // Create the AST unit.
314   AST.reset(new ASTUnit(false));
315   AST->OnlyLocalDecls = OnlyLocalDecls;
316   AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second;
317
318   // Capture any diagnostics that would otherwise be dropped.
319   CaptureDroppedDiagnostics Capture(CaptureDiagnostics, 
320                                     Clang.getDiagnostics(),
321                                     AST->Diagnostics);
322
323   // Create a file manager object to provide access to and cache the filesystem.
324   Clang.setFileManager(&AST->getFileManager());
325
326   // Create the source manager.
327   Clang.setSourceManager(&AST->getSourceManager());
328
329   // Create the preprocessor.
330   Clang.createPreprocessor();
331
332   Act.reset(new TopLevelDeclTrackerAction(*AST));
333   if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second,
334                            /*IsAST=*/false))
335     goto error;
336
337   Act->Execute();
338
339   // Steal the created target, context, and preprocessor, and take back the
340   // source and file managers.
341   AST->Ctx.reset(Clang.takeASTContext());
342   AST->PP.reset(Clang.takePreprocessor());
343   Clang.takeSourceManager();
344   Clang.takeFileManager();
345   AST->Target.reset(Clang.takeTarget());
346
347   Act->EndSourceFile();
348
349   Clang.takeDiagnosticClient();
350   Clang.takeDiagnostics();
351   Clang.takeInvocation();
352
353   AST->Invocation.reset(Clang.takeInvocation());
354   return AST.take();
355
356 error:
357   Clang.takeSourceManager();
358   Clang.takeFileManager();
359   Clang.takeDiagnosticClient();
360   Clang.takeDiagnostics();
361   return 0;
362 }
363
364 ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin,
365                                       const char **ArgEnd,
366                                       Diagnostic &Diags,
367                                       llvm::StringRef ResourceFilesPath,
368                                       bool OnlyLocalDecls,
369                                       RemappedFile *RemappedFiles,
370                                       unsigned NumRemappedFiles,
371                                       bool CaptureDiagnostics) {
372   llvm::SmallVector<const char *, 16> Args;
373   Args.push_back("<clang>"); // FIXME: Remove dummy argument.
374   Args.insert(Args.end(), ArgBegin, ArgEnd);
375
376   // FIXME: Find a cleaner way to force the driver into restricted modes. We
377   // also want to force it to use clang.
378   Args.push_back("-fsyntax-only");
379
380   // FIXME: We shouldn't have to pass in the path info.
381   driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(),
382                            "a.out", false, Diags);
383
384   // Don't check that inputs exist, they have been remapped.
385   TheDriver.setCheckInputsExist(false);
386
387   llvm::OwningPtr<driver::Compilation> C(
388     TheDriver.BuildCompilation(Args.size(), Args.data()));
389
390   // We expect to get back exactly one command job, if we didn't something
391   // failed.
392   const driver::JobList &Jobs = C->getJobs();
393   if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) {
394     llvm::SmallString<256> Msg;
395     llvm::raw_svector_ostream OS(Msg);
396     C->PrintJob(OS, C->getJobs(), "; ", true);
397     Diags.Report(diag::err_fe_expected_compiler_job) << OS.str();
398     return 0;
399   }
400
401   const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
402   if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") {
403     Diags.Report(diag::err_fe_expected_clang_command);
404     return 0;
405   }
406
407   const driver::ArgStringList &CCArgs = Cmd->getArguments();
408   llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation);
409   CompilerInvocation::CreateFromArgs(*CI, (const char**) CCArgs.data(),
410                                      (const char**) CCArgs.data()+CCArgs.size(),
411                                      Diags);
412
413   // Override any files that need remapping
414   for (unsigned I = 0; I != NumRemappedFiles; ++I)
415     CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first,
416                                               RemappedFiles[I].second);
417   
418   // Override the resources path.
419   CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath;
420
421   CI->getFrontendOpts().DisableFree = true;
422   return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls,
423                                     CaptureDiagnostics);
424 }