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