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