1 //===--- FrontendActions.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/ASTReader.h"
23 #include "clang/Serialization/ASTWriter.h"
24 #include "llvm/ADT/OwningPtr.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/system_error.h"
30 using namespace clang;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
38 return new ASTConsumer();
41 void InitOnlyAction::ExecuteAction() {
44 //===----------------------------------------------------------------------===//
45 // AST Consumer Actions
46 //===----------------------------------------------------------------------===//
48 ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
50 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
51 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
55 ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
57 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter);
60 ASTConsumer *ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI,
62 return CreateASTDeclNodeLister();
65 ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
68 if (CI.getFrontendOpts().OutputFile.empty())
71 OS = CI.createDefaultOutputFile(false, InFile);
73 return CreateASTDumperXML(*OS);
76 ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
78 return CreateASTViewer();
81 ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
83 return CreateDeclContextPrinter();
86 ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
89 std::string OutputFile;
91 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
94 if (!CI.getFrontendOpts().RelocatablePCH)
96 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS);
99 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
101 std::string &Sysroot,
102 std::string &OutputFile,
104 Sysroot = CI.getHeaderSearchOpts().Sysroot;
105 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
106 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
110 // We use createOutputFile here because this is exposed via libclang, and we
111 // must disable the RemoveFileOnSignal behavior.
112 // We use a temporary to avoid race conditions.
113 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
114 /*RemoveFileOnSignal=*/false, InFile,
115 /*Extension=*/"", /*useTemporary=*/true);
119 OutputFile = CI.getFrontendOpts().OutputFile;
123 ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
126 std::string OutputFile;
128 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
131 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
135 static SmallVectorImpl<char> &
136 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
137 Includes.append(RHS.begin(), RHS.end());
141 static void addHeaderInclude(StringRef HeaderName,
142 SmallVectorImpl<char> &Includes,
143 const LangOptions &LangOpts) {
145 Includes += "#import \"";
147 Includes += "#include \"";
148 Includes += HeaderName;
152 static void addHeaderInclude(const FileEntry *Header,
153 SmallVectorImpl<char> &Includes,
154 const LangOptions &LangOpts) {
155 addHeaderInclude(Header->getName(), Includes, LangOpts);
158 /// \brief Collect the set of header includes needed to construct the given
159 /// module and update the TopHeaders file set of the module.
161 /// \param Module The module we're collecting includes from.
163 /// \param Includes Will be augmented with the set of \#includes or \#imports
164 /// needed to load all of the named headers.
165 static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
166 FileManager &FileMgr,
168 clang::Module *Module,
169 SmallVectorImpl<char> &Includes) {
170 // Don't collect any headers for unavailable modules.
171 if (!Module->isAvailable())
174 // Add includes for each of these headers.
175 for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
176 const FileEntry *Header = Module->Headers[I];
177 Module->addTopHeader(Header);
178 addHeaderInclude(Header, Includes, LangOpts);
181 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
182 Module->addTopHeader(UmbrellaHeader);
183 if (Module->Parent) {
184 // Include the umbrella header for submodules.
185 addHeaderInclude(UmbrellaHeader, Includes, LangOpts);
187 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
188 // Add all of the headers we find in this subdirectory.
190 SmallString<128> DirNative;
191 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
192 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
194 Dir != DirEnd && !EC; Dir.increment(EC)) {
195 // Check whether this entry has an extension typically associated with
197 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
198 .Cases(".h", ".H", ".hh", ".hpp", true)
202 // If this header is marked 'unavailable' in this module, don't include
204 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
205 if (ModMap.isHeaderInUnavailableModule(Header))
207 Module->addTopHeader(Header);
210 // Include this header umbrella header for submodules.
211 addHeaderInclude(Dir->path(), Includes, LangOpts);
215 // Recurse into submodules.
216 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
217 SubEnd = Module->submodule_end();
218 Sub != SubEnd; ++Sub)
219 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
222 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
223 StringRef Filename) {
224 // Find the module map file.
225 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
227 CI.getDiagnostics().Report(diag::err_module_map_not_found)
232 // Parse the module map file.
233 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
234 if (HS.loadModuleMapFile(ModuleMap))
237 if (CI.getLangOpts().CurrentModule.empty()) {
238 CI.getDiagnostics().Report(diag::err_missing_module_name);
240 // FIXME: Eventually, we could consider asking whether there was just
241 // a single module described in the module map, and use that as a
242 // default. Then it would be fairly trivial to just "compile" a module
243 // map with a single module (the common case).
247 // Dig out the module definition.
248 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
249 /*AllowSearch=*/false);
251 CI.getDiagnostics().Report(diag::err_missing_module)
252 << CI.getLangOpts().CurrentModule << Filename;
257 // Check whether we can build this module at all.
259 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Feature)) {
260 CI.getDiagnostics().Report(diag::err_module_unavailable)
261 << Module->getFullModuleName()
267 FileManager &FileMgr = CI.getFileManager();
269 // Collect the set of #includes we need to build the module.
270 SmallString<256> HeaderContents;
271 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
272 addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts());
273 collectModuleHeaderIncludes(CI.getLangOpts(), FileMgr,
274 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
275 Module, HeaderContents);
277 llvm::MemoryBuffer *InputBuffer =
278 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
279 Module::getModuleInputBufferName());
280 // Ownership of InputBuffer will be transfered to the SourceManager.
281 setCurrentInput(FrontendInputFile(InputBuffer, getCurrentFileKind(),
286 bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
288 std::string &Sysroot,
289 std::string &OutputFile,
291 // If no output file was provided, figure out where this module would go
292 // in the module cache.
293 if (CI.getFrontendOpts().OutputFile.empty()) {
294 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
295 SmallString<256> ModuleFileName(HS.getModuleCachePath());
296 llvm::sys::path::append(ModuleFileName,
297 CI.getLangOpts().CurrentModule + ".pcm");
298 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
301 // We use createOutputFile here because this is exposed via libclang, and we
302 // must disable the RemoveFileOnSignal behavior.
303 // We use a temporary to avoid race conditions.
304 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
305 /*RemoveFileOnSignal=*/false, InFile,
306 /*Extension=*/"", /*useTemporary=*/true,
307 /*CreateMissingDirectories=*/true);
311 OutputFile = CI.getFrontendOpts().OutputFile;
315 ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
317 return new ASTConsumer();
320 ASTConsumer *DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
322 return new ASTConsumer();
326 /// \brief AST reader listener that dumps module information for a module
328 class DumpModuleInfoListener : public ASTReaderListener {
329 llvm::raw_ostream &Out;
332 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
334 #define DUMP_BOOLEAN(Value, Text) \
335 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
337 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
340 << (FullVersion == getClangFullRepositoryVersion()? "this"
342 << " Clang: " << FullVersion << "\n";
343 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
346 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
348 Out.indent(2) << "Language options:\n";
349 #define LANGOPT(Name, Bits, Default, Description) \
350 DUMP_BOOLEAN(LangOpts.Name, Description);
351 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
352 Out.indent(4) << Description << ": " \
353 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
354 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
355 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
356 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
357 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
358 #include "clang/Basic/LangOptions.def"
362 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
364 Out.indent(2) << "Target options:\n";
365 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
366 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
367 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
368 Out.indent(4) << " C++ ABI: " << TargetOpts.CXXABI << "\n";
369 Out.indent(4) << " Linker version: " << TargetOpts.LinkerVersion << "\n";
371 if (!TargetOpts.FeaturesAsWritten.empty()) {
372 Out.indent(4) << "Target features:\n";
373 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
375 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
382 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
384 Out.indent(2) << "Header search options:\n";
385 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
386 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
387 "Use builtin include directories [-nobuiltininc]");
388 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
389 "Use standard system include directories [-nostdinc]");
390 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
391 "Use standard C++ include directories [-nostdinc++]");
392 DUMP_BOOLEAN(HSOpts.UseLibcxx,
393 "Use libc++ (rather than libstdc++) [-stdlib=]");
397 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
399 std::string &SuggestedPredefines) {
400 Out.indent(2) << "Preprocessor options:\n";
401 DUMP_BOOLEAN(PPOpts.UsePredefines,
402 "Uses compiler/target-specific predefines [-undef]");
403 DUMP_BOOLEAN(PPOpts.DetailedRecord,
404 "Uses detailed preprocessing record (for indexing)");
406 if (!PPOpts.Macros.empty()) {
407 Out.indent(4) << "Predefined macros:\n";
410 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
411 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
418 Out << I->first << "\n";
426 void DumpModuleInfoAction::ExecuteAction() {
427 // Set up the output file.
428 llvm::OwningPtr<llvm::raw_fd_ostream> OutFile;
429 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
430 if (!OutputFileName.empty() && OutputFileName != "-") {
431 std::string ErrorInfo;
432 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str().c_str(),
435 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
437 Out << "Information for module file '" << getCurrentFile() << "':\n";
438 DumpModuleInfoListener Listener(Out);
439 ASTReader::readASTFileControlBlock(getCurrentFile(),
440 getCompilerInstance().getFileManager(),
444 //===----------------------------------------------------------------------===//
445 // Preprocessor Actions
446 //===----------------------------------------------------------------------===//
448 void DumpRawTokensAction::ExecuteAction() {
449 Preprocessor &PP = getCompilerInstance().getPreprocessor();
450 SourceManager &SM = PP.getSourceManager();
452 // Start lexing the specified input file.
453 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
454 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
455 RawLex.SetKeepWhitespaceMode(true);
458 RawLex.LexFromRawLexer(RawTok);
459 while (RawTok.isNot(tok::eof)) {
460 PP.DumpToken(RawTok, true);
461 llvm::errs() << "\n";
462 RawLex.LexFromRawLexer(RawTok);
466 void DumpTokensAction::ExecuteAction() {
467 Preprocessor &PP = getCompilerInstance().getPreprocessor();
468 // Start preprocessing the specified input file.
470 PP.EnterMainSourceFile();
473 PP.DumpToken(Tok, true);
474 llvm::errs() << "\n";
475 } while (Tok.isNot(tok::eof));
478 void GeneratePTHAction::ExecuteAction() {
479 CompilerInstance &CI = getCompilerInstance();
480 if (CI.getFrontendOpts().OutputFile.empty() ||
481 CI.getFrontendOpts().OutputFile == "-") {
482 // FIXME: Don't fail this way.
483 // FIXME: Verify that we can actually seek in the given file.
484 llvm::report_fatal_error("PTH requires a seekable file for output!");
486 llvm::raw_fd_ostream *OS =
487 CI.createDefaultOutputFile(true, getCurrentFile());
490 CacheTokens(CI.getPreprocessor(), OS);
493 void PreprocessOnlyAction::ExecuteAction() {
494 Preprocessor &PP = getCompilerInstance().getPreprocessor();
496 // Ignore unknown pragmas.
497 PP.AddPragmaHandler(new EmptyPragmaHandler());
500 // Start parsing the specified input file.
501 PP.EnterMainSourceFile();
504 } while (Tok.isNot(tok::eof));
507 void PrintPreprocessedAction::ExecuteAction() {
508 CompilerInstance &CI = getCompilerInstance();
509 // Output file may need to be set to 'Binary', to avoid converting Unix style
510 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
512 // Look to see what type of line endings the file uses. If there's a
513 // CRLF, then we won't open the file up in binary mode. If there is
514 // just an LF or CR, then we will open the file up in binary mode.
515 // In this fashion, the output format should match the input format, unless
516 // the input format has inconsistent line endings.
518 // This should be a relatively fast operation since most files won't have
519 // all of their source code on a single line. However, that is still a
520 // concern, so if we scan for too long, we'll just assume the file should
521 // be opened in binary mode.
522 bool BinaryMode = true;
523 bool InvalidFile = false;
524 const SourceManager& SM = CI.getSourceManager();
525 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
528 const char *cur = Buffer->getBufferStart();
529 const char *end = Buffer->getBufferEnd();
530 const char *next = (cur != end) ? cur + 1 : end;
532 // Limit ourselves to only scanning 256 characters into the source
533 // file. This is mostly a sanity check in case the file has no
534 // newlines whatsoever.
535 if (end - cur > 256) end = cur + 256;
538 if (*cur == 0x0D) { // CR
539 if (*next == 0x0A) // CRLF
543 } else if (*cur == 0x0A) // LF
550 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
553 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
554 CI.getPreprocessorOutputOpts());
557 void PrintPreambleAction::ExecuteAction() {
558 switch (getCurrentFileKind()) {
569 case IK_PreprocessedC:
570 case IK_PreprocessedCXX:
571 case IK_PreprocessedObjC:
572 case IK_PreprocessedObjCXX:
575 // We can't do anything with these.
579 CompilerInstance &CI = getCompilerInstance();
580 llvm::MemoryBuffer *Buffer
581 = CI.getFileManager().getBufferForFile(getCurrentFile());
583 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
584 llvm::outs().write(Buffer->getBufferStart(), Preamble);