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