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,
58 CI.getFrontendOpts().ASTDumpLookups);
61 ASTConsumer *ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI,
63 return CreateASTDeclNodeLister();
66 ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
68 return CreateASTViewer();
71 ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
73 return CreateDeclContextPrinter();
76 ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
79 std::string OutputFile;
81 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
84 if (!CI.getFrontendOpts().RelocatablePCH)
86 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS);
89 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
92 std::string &OutputFile,
94 Sysroot = CI.getHeaderSearchOpts().Sysroot;
95 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
96 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
100 // We use createOutputFile here because this is exposed via libclang, and we
101 // must disable the RemoveFileOnSignal behavior.
102 // We use a temporary to avoid race conditions.
103 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
104 /*RemoveFileOnSignal=*/false, InFile,
105 /*Extension=*/"", /*useTemporary=*/true);
109 OutputFile = CI.getFrontendOpts().OutputFile;
113 ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
116 std::string OutputFile;
118 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
121 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
125 static SmallVectorImpl<char> &
126 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
127 Includes.append(RHS.begin(), RHS.end());
131 static void addHeaderInclude(StringRef HeaderName,
132 SmallVectorImpl<char> &Includes,
133 const LangOptions &LangOpts) {
135 Includes += "#import \"";
137 Includes += "#include \"";
138 Includes += HeaderName;
142 static void addHeaderInclude(const FileEntry *Header,
143 SmallVectorImpl<char> &Includes,
144 const LangOptions &LangOpts) {
145 addHeaderInclude(Header->getName(), Includes, LangOpts);
148 /// \brief Collect the set of header includes needed to construct the given
149 /// module and update the TopHeaders file set of the module.
151 /// \param Module The module we're collecting includes from.
153 /// \param Includes Will be augmented with the set of \#includes or \#imports
154 /// needed to load all of the named headers.
155 static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
156 FileManager &FileMgr,
158 clang::Module *Module,
159 SmallVectorImpl<char> &Includes) {
160 // Don't collect any headers for unavailable modules.
161 if (!Module->isAvailable())
164 // Add includes for each of these headers.
165 for (unsigned I = 0, N = Module->NormalHeaders.size(); I != N; ++I) {
166 const FileEntry *Header = Module->NormalHeaders[I];
167 Module->addTopHeader(Header);
168 addHeaderInclude(Header, Includes, LangOpts);
170 // Note that Module->PrivateHeaders will not be a TopHeader.
172 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
173 Module->addTopHeader(UmbrellaHeader);
174 if (Module->Parent) {
175 // Include the umbrella header for submodules.
176 addHeaderInclude(UmbrellaHeader, Includes, LangOpts);
178 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
179 // Add all of the headers we find in this subdirectory.
181 SmallString<128> DirNative;
182 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
183 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
185 Dir != DirEnd && !EC; Dir.increment(EC)) {
186 // Check whether this entry has an extension typically associated with
188 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
189 .Cases(".h", ".H", ".hh", ".hpp", true)
193 // If this header is marked 'unavailable' in this module, don't include
195 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
196 if (ModMap.isHeaderInUnavailableModule(Header))
198 Module->addTopHeader(Header);
201 // Include this header umbrella header for submodules.
202 addHeaderInclude(Dir->path(), Includes, LangOpts);
206 // Recurse into submodules.
207 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
208 SubEnd = Module->submodule_end();
209 Sub != SubEnd; ++Sub)
210 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
213 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
214 StringRef Filename) {
215 // Find the module map file.
216 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
218 CI.getDiagnostics().Report(diag::err_module_map_not_found)
223 // Parse the module map file.
224 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
225 if (HS.loadModuleMapFile(ModuleMap, IsSystem))
228 if (CI.getLangOpts().CurrentModule.empty()) {
229 CI.getDiagnostics().Report(diag::err_missing_module_name);
231 // FIXME: Eventually, we could consider asking whether there was just
232 // a single module described in the module map, and use that as a
233 // default. Then it would be fairly trivial to just "compile" a module
234 // map with a single module (the common case).
238 // Dig out the module definition.
239 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
240 /*AllowSearch=*/false);
242 CI.getDiagnostics().Report(diag::err_missing_module)
243 << CI.getLangOpts().CurrentModule << Filename;
248 // Check whether we can build this module at all.
249 clang::Module::Requirement Requirement;
250 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement)) {
251 CI.getDiagnostics().Report(diag::err_module_unavailable)
252 << Module->getFullModuleName()
253 << Requirement.second << Requirement.first;
258 FileManager &FileMgr = CI.getFileManager();
260 // Collect the set of #includes we need to build the module.
261 SmallString<256> HeaderContents;
262 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
263 addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts());
264 collectModuleHeaderIncludes(CI.getLangOpts(), FileMgr,
265 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
266 Module, HeaderContents);
268 llvm::MemoryBuffer *InputBuffer =
269 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
270 Module::getModuleInputBufferName());
271 // Ownership of InputBuffer will be transfered to the SourceManager.
272 setCurrentInput(FrontendInputFile(InputBuffer, getCurrentFileKind(),
277 bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
279 std::string &Sysroot,
280 std::string &OutputFile,
282 // If no output file was provided, figure out where this module would go
283 // in the module cache.
284 if (CI.getFrontendOpts().OutputFile.empty()) {
285 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
286 SmallString<256> ModuleFileName(HS.getModuleCachePath());
287 llvm::sys::path::append(ModuleFileName,
288 CI.getLangOpts().CurrentModule + ".pcm");
289 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
292 // We use createOutputFile here because this is exposed via libclang, and we
293 // must disable the RemoveFileOnSignal behavior.
294 // We use a temporary to avoid race conditions.
295 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
296 /*RemoveFileOnSignal=*/false, InFile,
297 /*Extension=*/"", /*useTemporary=*/true,
298 /*CreateMissingDirectories=*/true);
302 OutputFile = CI.getFrontendOpts().OutputFile;
306 ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
308 return new ASTConsumer();
311 ASTConsumer *DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
313 return new ASTConsumer();
317 /// \brief AST reader listener that dumps module information for a module
319 class DumpModuleInfoListener : public ASTReaderListener {
320 llvm::raw_ostream &Out;
323 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
325 #define DUMP_BOOLEAN(Value, Text) \
326 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
328 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
331 << (FullVersion == getClangFullRepositoryVersion()? "this"
333 << " Clang: " << FullVersion << "\n";
334 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
337 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
339 Out.indent(2) << "Language options:\n";
340 #define LANGOPT(Name, Bits, Default, Description) \
341 DUMP_BOOLEAN(LangOpts.Name, Description);
342 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
343 Out.indent(4) << Description << ": " \
344 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
345 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
346 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
347 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
348 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
349 #include "clang/Basic/LangOptions.def"
353 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
355 Out.indent(2) << "Target options:\n";
356 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
357 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
358 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
359 Out.indent(4) << " C++ ABI: " << TargetOpts.CXXABI << "\n";
360 Out.indent(4) << " Linker version: " << TargetOpts.LinkerVersion << "\n";
362 if (!TargetOpts.FeaturesAsWritten.empty()) {
363 Out.indent(4) << "Target features:\n";
364 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
366 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
373 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
375 Out.indent(2) << "Header search options:\n";
376 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
377 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
378 "Use builtin include directories [-nobuiltininc]");
379 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
380 "Use standard system include directories [-nostdinc]");
381 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
382 "Use standard C++ include directories [-nostdinc++]");
383 DUMP_BOOLEAN(HSOpts.UseLibcxx,
384 "Use libc++ (rather than libstdc++) [-stdlib=]");
388 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
390 std::string &SuggestedPredefines) {
391 Out.indent(2) << "Preprocessor options:\n";
392 DUMP_BOOLEAN(PPOpts.UsePredefines,
393 "Uses compiler/target-specific predefines [-undef]");
394 DUMP_BOOLEAN(PPOpts.DetailedRecord,
395 "Uses detailed preprocessing record (for indexing)");
397 if (!PPOpts.Macros.empty()) {
398 Out.indent(4) << "Predefined macros:\n";
401 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
402 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
409 Out << I->first << "\n";
417 void DumpModuleInfoAction::ExecuteAction() {
418 // Set up the output file.
419 llvm::OwningPtr<llvm::raw_fd_ostream> OutFile;
420 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
421 if (!OutputFileName.empty() && OutputFileName != "-") {
422 std::string ErrorInfo;
423 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str().c_str(),
426 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
428 Out << "Information for module file '" << getCurrentFile() << "':\n";
429 DumpModuleInfoListener Listener(Out);
430 ASTReader::readASTFileControlBlock(getCurrentFile(),
431 getCompilerInstance().getFileManager(),
435 //===----------------------------------------------------------------------===//
436 // Preprocessor Actions
437 //===----------------------------------------------------------------------===//
439 void DumpRawTokensAction::ExecuteAction() {
440 Preprocessor &PP = getCompilerInstance().getPreprocessor();
441 SourceManager &SM = PP.getSourceManager();
443 // Start lexing the specified input file.
444 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
445 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
446 RawLex.SetKeepWhitespaceMode(true);
449 RawLex.LexFromRawLexer(RawTok);
450 while (RawTok.isNot(tok::eof)) {
451 PP.DumpToken(RawTok, true);
452 llvm::errs() << "\n";
453 RawLex.LexFromRawLexer(RawTok);
457 void DumpTokensAction::ExecuteAction() {
458 Preprocessor &PP = getCompilerInstance().getPreprocessor();
459 // Start preprocessing the specified input file.
461 PP.EnterMainSourceFile();
464 PP.DumpToken(Tok, true);
465 llvm::errs() << "\n";
466 } while (Tok.isNot(tok::eof));
469 void GeneratePTHAction::ExecuteAction() {
470 CompilerInstance &CI = getCompilerInstance();
471 if (CI.getFrontendOpts().OutputFile.empty() ||
472 CI.getFrontendOpts().OutputFile == "-") {
473 // FIXME: Don't fail this way.
474 // FIXME: Verify that we can actually seek in the given file.
475 llvm::report_fatal_error("PTH requires a seekable file for output!");
477 llvm::raw_fd_ostream *OS =
478 CI.createDefaultOutputFile(true, getCurrentFile());
481 CacheTokens(CI.getPreprocessor(), OS);
484 void PreprocessOnlyAction::ExecuteAction() {
485 Preprocessor &PP = getCompilerInstance().getPreprocessor();
487 // Ignore unknown pragmas.
488 PP.AddPragmaHandler(new EmptyPragmaHandler());
491 // Start parsing the specified input file.
492 PP.EnterMainSourceFile();
495 } while (Tok.isNot(tok::eof));
498 void PrintPreprocessedAction::ExecuteAction() {
499 CompilerInstance &CI = getCompilerInstance();
500 // Output file may need to be set to 'Binary', to avoid converting Unix style
501 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
503 // Look to see what type of line endings the file uses. If there's a
504 // CRLF, then we won't open the file up in binary mode. If there is
505 // just an LF or CR, then we will open the file up in binary mode.
506 // In this fashion, the output format should match the input format, unless
507 // the input format has inconsistent line endings.
509 // This should be a relatively fast operation since most files won't have
510 // all of their source code on a single line. However, that is still a
511 // concern, so if we scan for too long, we'll just assume the file should
512 // be opened in binary mode.
513 bool BinaryMode = true;
514 bool InvalidFile = false;
515 const SourceManager& SM = CI.getSourceManager();
516 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
519 const char *cur = Buffer->getBufferStart();
520 const char *end = Buffer->getBufferEnd();
521 const char *next = (cur != end) ? cur + 1 : end;
523 // Limit ourselves to only scanning 256 characters into the source
524 // file. This is mostly a sanity check in case the file has no
525 // newlines whatsoever.
526 if (end - cur > 256) end = cur + 256;
529 if (*cur == 0x0D) { // CR
530 if (*next == 0x0A) // CRLF
534 } else if (*cur == 0x0A) // LF
541 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
544 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
545 CI.getPreprocessorOutputOpts());
548 void PrintPreambleAction::ExecuteAction() {
549 switch (getCurrentFileKind()) {
560 case IK_PreprocessedC:
561 case IK_PreprocessedCXX:
562 case IK_PreprocessedObjC:
563 case IK_PreprocessedObjCXX:
566 // We can't do anything with these.
570 CompilerInstance &CI = getCompilerInstance();
571 llvm::MemoryBuffer *Buffer
572 = CI.getFileManager().getBufferForFile(getCurrentFile());
574 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
575 llvm::outs().write(Buffer->getBufferStart(), Preamble);