1 //===--- FrontendActions.cpp ----------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Frontend/FrontendActions.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/Lex/HeaderSearch.h"
13 #include "clang/Lex/Pragma.h"
14 #include "clang/Lex/Preprocessor.h"
15 #include "clang/Parse/Parser.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Frontend/ASTConsumers.h"
18 #include "clang/Frontend/ASTUnit.h"
19 #include "clang/Frontend/CompilerInstance.h"
20 #include "clang/Frontend/FrontendDiagnostic.h"
21 #include "clang/Frontend/Utils.h"
22 #include "clang/Serialization/ASTWriter.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/system_error.h"
28 
29 using namespace clang;
30 
31 //===----------------------------------------------------------------------===//
32 // Custom Actions
33 //===----------------------------------------------------------------------===//
34 
35 ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
36                                                StringRef InFile) {
37   return new ASTConsumer();
38 }
39 
40 void InitOnlyAction::ExecuteAction() {
41 }
42 
43 //===----------------------------------------------------------------------===//
44 // AST Consumer Actions
45 //===----------------------------------------------------------------------===//
46 
47 ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
48                                                StringRef InFile) {
49   if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
50     return CreateASTPrinter(OS);
51   return 0;
52 }
53 
54 ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
55                                               StringRef InFile) {
56   return CreateASTDumper();
57 }
58 
59 ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
60                                                  StringRef InFile) {
61   raw_ostream *OS;
62   if (CI.getFrontendOpts().OutputFile.empty())
63     OS = &llvm::outs();
64   else
65     OS = CI.createDefaultOutputFile(false, InFile);
66   if (!OS) return 0;
67   return CreateASTDumperXML(*OS);
68 }
69 
70 ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
71                                               StringRef InFile) {
72   return CreateASTViewer();
73 }
74 
75 ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
76                                                        StringRef InFile) {
77   return CreateDeclContextPrinter();
78 }
79 
80 ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
81                                                   StringRef InFile) {
82   std::string Sysroot;
83   std::string OutputFile;
84   raw_ostream *OS = 0;
85   if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
86     return 0;
87 
88   if (!CI.getFrontendOpts().RelocatablePCH)
89     Sysroot.clear();
90   return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS);
91 }
92 
93 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
94                                                     StringRef InFile,
95                                                     std::string &Sysroot,
96                                                     std::string &OutputFile,
97                                                     raw_ostream *&OS) {
98   Sysroot = CI.getHeaderSearchOpts().Sysroot;
99   if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
100     CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
101     return true;
102   }
103 
104   // We use createOutputFile here because this is exposed via libclang, and we
105   // must disable the RemoveFileOnSignal behavior.
106   // We use a temporary to avoid race conditions.
107   OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
108                            /*RemoveFileOnSignal=*/false, InFile,
109                            /*Extension=*/"", /*useTemporary=*/true);
110   if (!OS)
111     return true;
112 
113   OutputFile = CI.getFrontendOpts().OutputFile;
114   return false;
115 }
116 
117 ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
118                                                      StringRef InFile) {
119   std::string Sysroot;
120   std::string OutputFile;
121   raw_ostream *OS = 0;
122   if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
123     return 0;
124 
125   return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
126                           Sysroot, OS);
127 }
128 
129 /// \brief Collect the set of header includes needed to construct the given
130 /// module.
131 ///
132 /// \param Module The module we're collecting includes from.
133 ///
134 /// \param Includes Will be augmented with the set of #includes or #imports
135 /// needed to load all of the named headers.
136 static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
137                                         FileManager &FileMgr,
138                                         ModuleMap &ModMap,
139                                         clang::Module *Module,
140                                         llvm::SmallString<256> &Includes) {
141   // Don't collect any headers for unavailable modules.
142   if (!Module->isAvailable())
143     return;
144 
145   // Add includes for each of these headers.
146   for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
147     if (LangOpts.ObjC1)
148       Includes += "#import \"";
149     else
150       Includes += "#include \"";
151     Includes += Module->Headers[I]->getName();
152     Includes += "\"\n";
153   }
154 
155   if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
156     if (Module->Parent) {
157       // Include the umbrella header for submodules.
158       if (LangOpts.ObjC1)
159         Includes += "#import \"";
160       else
161         Includes += "#include \"";
162       Includes += UmbrellaHeader->getName();
163       Includes += "\"\n";
164     }
165   } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
166     // Add all of the headers we find in this subdirectory.
167     llvm::error_code EC;
168     llvm::SmallString<128> DirNative;
169     llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
170     for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
171                                                      DirEnd;
172          Dir != DirEnd && !EC; Dir.increment(EC)) {
173       // Check whether this entry has an extension typically associated with
174       // headers.
175       if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
176           .Cases(".h", ".H", ".hh", ".hpp", true)
177           .Default(false))
178         continue;
179 
180       // If this header is marked 'unavailable' in this module, don't include
181       // it.
182       if (const FileEntry *Header = FileMgr.getFile(Dir->path()))
183         if (ModMap.isHeaderInUnavailableModule(Header))
184           continue;
185 
186       // Include this header umbrella header for submodules.
187       if (LangOpts.ObjC1)
188         Includes += "#import \"";
189       else
190         Includes += "#include \"";
191       Includes += Dir->path();
192       Includes += "\"\n";
193     }
194   }
195 
196   // Recurse into submodules.
197   for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
198                                       SubEnd = Module->submodule_end();
199        Sub != SubEnd; ++Sub)
200     collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
201 }
202 
203 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
204                                                  StringRef Filename) {
205   // Find the module map file.
206   const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
207   if (!ModuleMap)  {
208     CI.getDiagnostics().Report(diag::err_module_map_not_found)
209       << Filename;
210     return false;
211   }
212 
213   // Parse the module map file.
214   HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
215   if (HS.loadModuleMapFile(ModuleMap))
216     return false;
217 
218   if (CI.getLangOpts().CurrentModule.empty()) {
219     CI.getDiagnostics().Report(diag::err_missing_module_name);
220 
221     // FIXME: Eventually, we could consider asking whether there was just
222     // a single module described in the module map, and use that as a
223     // default. Then it would be fairly trivial to just "compile" a module
224     // map with a single module (the common case).
225     return false;
226   }
227 
228   // Dig out the module definition.
229   Module = HS.getModule(CI.getLangOpts().CurrentModule, /*AllowSearch=*/false);
230   if (!Module) {
231     CI.getDiagnostics().Report(diag::err_missing_module)
232       << CI.getLangOpts().CurrentModule << Filename;
233 
234     return false;
235   }
236 
237   // Check whether we can build this module at all.
238   StringRef Feature;
239   if (!Module->isAvailable(CI.getLangOpts(), Feature)) {
240     CI.getDiagnostics().Report(diag::err_module_unavailable)
241       << Module->getFullModuleName()
242       << Feature;
243 
244     return false;
245   }
246 
247   // Do we have an umbrella header for this module?
248   const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader();
249 
250   // Collect the set of #includes we need to build the module.
251   llvm::SmallString<256> HeaderContents;
252   collectModuleHeaderIncludes(CI.getLangOpts(), CI.getFileManager(),
253     CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
254     Module, HeaderContents);
255   if (UmbrellaHeader && HeaderContents.empty()) {
256     // Simple case: we have an umbrella header and there are no additional
257     // includes, we can just parse the umbrella header directly.
258     setCurrentInput(FrontendInputFile(UmbrellaHeader->getName(),
259                                       getCurrentFileKind()));
260     return true;
261   }
262 
263   FileManager &FileMgr = CI.getFileManager();
264   llvm::SmallString<128> HeaderName;
265   time_t ModTime;
266   if (UmbrellaHeader) {
267     // Read in the umbrella header.
268     // FIXME: Go through the source manager; the umbrella header may have
269     // been overridden.
270     std::string ErrorStr;
271     llvm::MemoryBuffer *UmbrellaContents
272       = FileMgr.getBufferForFile(UmbrellaHeader, &ErrorStr);
273     if (!UmbrellaContents) {
274       CI.getDiagnostics().Report(diag::err_missing_umbrella_header)
275         << UmbrellaHeader->getName() << ErrorStr;
276       return false;
277     }
278 
279     // Combine the contents of the umbrella header with the automatically-
280     // generated includes.
281     llvm::SmallString<256> OldContents = HeaderContents;
282     HeaderContents = UmbrellaContents->getBuffer();
283     HeaderContents += "\n\n";
284     HeaderContents += "/* Module includes */\n";
285     HeaderContents += OldContents;
286 
287     // Pretend that we're parsing the umbrella header.
288     HeaderName = UmbrellaHeader->getName();
289     ModTime = UmbrellaHeader->getModificationTime();
290 
291     delete UmbrellaContents;
292   } else {
293     // Pick an innocuous-sounding name for the umbrella header.
294     HeaderName = Module->Name + ".h";
295     if (FileMgr.getFile(HeaderName, /*OpenFile=*/false,
296                         /*CacheFailure=*/false)) {
297       // Try again!
298       HeaderName = Module->Name + "-module.h";
299       if (FileMgr.getFile(HeaderName, /*OpenFile=*/false,
300                           /*CacheFailure=*/false)) {
301         // Pick something ridiculous and go with it.
302         HeaderName = Module->Name + "-module.hmod";
303       }
304     }
305     ModTime = time(0);
306   }
307 
308   // Remap the contents of the header name we're using to our synthesized
309   // buffer.
310   const FileEntry *HeaderFile = FileMgr.getVirtualFile(HeaderName,
311                                                        HeaderContents.size(),
312                                                        ModTime);
313   llvm::MemoryBuffer *HeaderContentsBuf
314     = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents);
315   CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf);
316   setCurrentInput(FrontendInputFile(HeaderName, getCurrentFileKind()));
317   return true;
318 }
319 
320 bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
321                                                        StringRef InFile,
322                                                        std::string &Sysroot,
323                                                        std::string &OutputFile,
324                                                        raw_ostream *&OS) {
325   // If no output file was provided, figure out where this module would go
326   // in the module cache.
327   if (CI.getFrontendOpts().OutputFile.empty()) {
328     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
329     llvm::SmallString<256> ModuleFileName(HS.getModuleCachePath());
330     llvm::sys::path::append(ModuleFileName,
331                             CI.getLangOpts().CurrentModule + ".pcm");
332     CI.getFrontendOpts().OutputFile = ModuleFileName.str();
333   }
334 
335   // We use createOutputFile here because this is exposed via libclang, and we
336   // must disable the RemoveFileOnSignal behavior.
337   // We use a temporary to avoid race conditions.
338   OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
339                            /*RemoveFileOnSignal=*/false, InFile,
340                            /*Extension=*/"", /*useTemporary=*/true);
341   if (!OS)
342     return true;
343 
344   OutputFile = CI.getFrontendOpts().OutputFile;
345   return false;
346 }
347 
348 ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
349                                                  StringRef InFile) {
350   return new ASTConsumer();
351 }
352 
353 //===----------------------------------------------------------------------===//
354 // Preprocessor Actions
355 //===----------------------------------------------------------------------===//
356 
357 void DumpRawTokensAction::ExecuteAction() {
358   Preprocessor &PP = getCompilerInstance().getPreprocessor();
359   SourceManager &SM = PP.getSourceManager();
360 
361   // Start lexing the specified input file.
362   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
363   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
364   RawLex.SetKeepWhitespaceMode(true);
365 
366   Token RawTok;
367   RawLex.LexFromRawLexer(RawTok);
368   while (RawTok.isNot(tok::eof)) {
369     PP.DumpToken(RawTok, true);
370     llvm::errs() << "\n";
371     RawLex.LexFromRawLexer(RawTok);
372   }
373 }
374 
375 void DumpTokensAction::ExecuteAction() {
376   Preprocessor &PP = getCompilerInstance().getPreprocessor();
377   // Start preprocessing the specified input file.
378   Token Tok;
379   PP.EnterMainSourceFile();
380   do {
381     PP.Lex(Tok);
382     PP.DumpToken(Tok, true);
383     llvm::errs() << "\n";
384   } while (Tok.isNot(tok::eof));
385 }
386 
387 void GeneratePTHAction::ExecuteAction() {
388   CompilerInstance &CI = getCompilerInstance();
389   if (CI.getFrontendOpts().OutputFile.empty() ||
390       CI.getFrontendOpts().OutputFile == "-") {
391     // FIXME: Don't fail this way.
392     // FIXME: Verify that we can actually seek in the given file.
393     llvm::report_fatal_error("PTH requires a seekable file for output!");
394   }
395   llvm::raw_fd_ostream *OS =
396     CI.createDefaultOutputFile(true, getCurrentFile());
397   if (!OS) return;
398 
399   CacheTokens(CI.getPreprocessor(), OS);
400 }
401 
402 void PreprocessOnlyAction::ExecuteAction() {
403   Preprocessor &PP = getCompilerInstance().getPreprocessor();
404 
405   // Ignore unknown pragmas.
406   PP.AddPragmaHandler(new EmptyPragmaHandler());
407 
408   Token Tok;
409   // Start parsing the specified input file.
410   PP.EnterMainSourceFile();
411   do {
412     PP.Lex(Tok);
413   } while (Tok.isNot(tok::eof));
414 }
415 
416 void PrintPreprocessedAction::ExecuteAction() {
417   CompilerInstance &CI = getCompilerInstance();
418   // Output file may need to be set to 'Binary', to avoid converting Unix style
419   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
420   //
421   // Look to see what type of line endings the file uses. If there's a
422   // CRLF, then we won't open the file up in binary mode. If there is
423   // just an LF or CR, then we will open the file up in binary mode.
424   // In this fashion, the output format should match the input format, unless
425   // the input format has inconsistent line endings.
426   //
427   // This should be a relatively fast operation since most files won't have
428   // all of their source code on a single line. However, that is still a
429   // concern, so if we scan for too long, we'll just assume the file should
430   // be opened in binary mode.
431   bool BinaryMode = true;
432   bool InvalidFile = false;
433   const SourceManager& SM = CI.getSourceManager();
434   const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
435                                                      &InvalidFile);
436   if (!InvalidFile) {
437     const char *cur = Buffer->getBufferStart();
438     const char *end = Buffer->getBufferEnd();
439     const char *next = (cur != end) ? cur + 1 : end;
440 
441     // Limit ourselves to only scanning 256 characters into the source
442     // file.  This is mostly a sanity check in case the file has no
443     // newlines whatsoever.
444     if (end - cur > 256) end = cur + 256;
445 
446     while (next < end) {
447       if (*cur == 0x0D) {  // CR
448         if (*next == 0x0A)  // CRLF
449           BinaryMode = false;
450 
451         break;
452       } else if (*cur == 0x0A)  // LF
453         break;
454 
455       ++cur, ++next;
456     }
457   }
458 
459   raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
460   if (!OS) return;
461 
462   DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
463                            CI.getPreprocessorOutputOpts());
464 }
465 
466 void PrintPreambleAction::ExecuteAction() {
467   switch (getCurrentFileKind()) {
468   case IK_C:
469   case IK_CXX:
470   case IK_ObjC:
471   case IK_ObjCXX:
472   case IK_OpenCL:
473   case IK_CUDA:
474     break;
475 
476   case IK_None:
477   case IK_Asm:
478   case IK_PreprocessedC:
479   case IK_PreprocessedCXX:
480   case IK_PreprocessedObjC:
481   case IK_PreprocessedObjCXX:
482   case IK_AST:
483   case IK_LLVM_IR:
484     // We can't do anything with these.
485     return;
486   }
487 
488   CompilerInstance &CI = getCompilerInstance();
489   llvm::MemoryBuffer *Buffer
490       = CI.getFileManager().getBufferForFile(getCurrentFile());
491   if (Buffer) {
492     unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
493     llvm::outs().write(Buffer->getBufferStart(), Preamble);
494     delete Buffer;
495   }
496 }
497