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/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
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;
80 raw_ostream *OS = nullptr;
81 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
84 if (!CI.getFrontendOpts().RelocatablePCH)
86 return new PCHGenerator(CI.getPreprocessor(), OutputFile, nullptr, Sysroot,
90 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
93 std::string &OutputFile,
95 Sysroot = CI.getHeaderSearchOpts().Sysroot;
96 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
97 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
101 // We use createOutputFile here because this is exposed via libclang, and we
102 // must disable the RemoveFileOnSignal behavior.
103 // We use a temporary to avoid race conditions.
104 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
105 /*RemoveFileOnSignal=*/false, InFile,
106 /*Extension=*/"", /*useTemporary=*/true);
110 OutputFile = CI.getFrontendOpts().OutputFile;
114 ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
117 std::string OutputFile;
118 raw_ostream *OS = nullptr;
119 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
122 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
126 static SmallVectorImpl<char> &
127 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
128 Includes.append(RHS.begin(), RHS.end());
132 static std::error_code addHeaderInclude(StringRef HeaderName,
133 SmallVectorImpl<char> &Includes,
134 const LangOptions &LangOpts,
136 if (IsExternC && LangOpts.CPlusPlus)
137 Includes += "extern \"C\" {\n";
139 Includes += "#import \"";
141 Includes += "#include \"";
142 // Use an absolute path for the include; there's no reason to think that
143 // a relative path will work (. might not be on our include path) or that
144 // it will find the same file.
145 if (llvm::sys::path::is_absolute(HeaderName)) {
146 Includes += HeaderName;
148 SmallString<256> Header = HeaderName;
149 if (std::error_code Err = llvm::sys::fs::make_absolute(Header))
154 if (IsExternC && LangOpts.CPlusPlus)
156 return std::error_code();
159 static std::error_code addHeaderInclude(const FileEntry *Header,
160 SmallVectorImpl<char> &Includes,
161 const LangOptions &LangOpts,
163 return addHeaderInclude(Header->getName(), Includes, LangOpts, IsExternC);
166 /// \brief Collect the set of header includes needed to construct the given
167 /// module and update the TopHeaders file set of the module.
169 /// \param Module The module we're collecting includes from.
171 /// \param Includes Will be augmented with the set of \#includes or \#imports
172 /// needed to load all of the named headers.
173 static std::error_code
174 collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
175 ModuleMap &ModMap, clang::Module *Module,
176 SmallVectorImpl<char> &Includes) {
177 // Don't collect any headers for unavailable modules.
178 if (!Module->isAvailable())
179 return std::error_code();
181 // Add includes for each of these headers.
182 for (unsigned I = 0, N = Module->NormalHeaders.size(); I != N; ++I) {
183 const FileEntry *Header = Module->NormalHeaders[I];
184 Module->addTopHeader(Header);
185 if (std::error_code Err =
186 addHeaderInclude(Header, Includes, LangOpts, Module->IsExternC))
189 // Note that Module->PrivateHeaders will not be a TopHeader.
191 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
192 Module->addTopHeader(UmbrellaHeader);
193 if (Module->Parent) {
194 // Include the umbrella header for submodules.
195 if (std::error_code Err = addHeaderInclude(UmbrellaHeader, Includes,
196 LangOpts, Module->IsExternC))
199 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
200 // Add all of the headers we find in this subdirectory.
202 SmallString<128> DirNative;
203 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
204 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
206 Dir != DirEnd && !EC; Dir.increment(EC)) {
207 // Check whether this entry has an extension typically associated with
209 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
210 .Cases(".h", ".H", ".hh", ".hpp", true)
214 // If this header is marked 'unavailable' in this module, don't include
216 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
217 if (ModMap.isHeaderUnavailableInModule(Header, Module))
219 Module->addTopHeader(Header);
222 // Include this header as part of the umbrella directory.
223 if (std::error_code Err = addHeaderInclude(Dir->path(), Includes,
224 LangOpts, Module->IsExternC))
232 // Recurse into submodules.
233 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
234 SubEnd = Module->submodule_end();
235 Sub != SubEnd; ++Sub)
236 if (std::error_code Err = collectModuleHeaderIncludes(
237 LangOpts, FileMgr, ModMap, *Sub, Includes))
240 return std::error_code();
243 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
244 StringRef Filename) {
245 // Find the module map file.
246 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
248 CI.getDiagnostics().Report(diag::err_module_map_not_found)
253 // Parse the module map file.
254 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
255 if (HS.loadModuleMapFile(ModuleMap, IsSystem))
258 if (CI.getLangOpts().CurrentModule.empty()) {
259 CI.getDiagnostics().Report(diag::err_missing_module_name);
261 // FIXME: Eventually, we could consider asking whether there was just
262 // a single module described in the module map, and use that as a
263 // default. Then it would be fairly trivial to just "compile" a module
264 // map with a single module (the common case).
268 // If we're being run from the command-line, the module build stack will not
269 // have been filled in yet, so complete it now in order to allow us to detect
271 SourceManager &SourceMgr = CI.getSourceManager();
272 if (SourceMgr.getModuleBuildStack().empty())
273 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
274 FullSourceLoc(SourceLocation(), SourceMgr));
276 // Dig out the module definition.
277 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
278 /*AllowSearch=*/false);
280 CI.getDiagnostics().Report(diag::err_missing_module)
281 << CI.getLangOpts().CurrentModule << Filename;
286 // Check whether we can build this module at all.
287 clang::Module::Requirement Requirement;
288 clang::Module::HeaderDirective MissingHeader;
289 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
291 if (MissingHeader.FileNameLoc.isValid()) {
292 CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
293 diag::err_module_header_missing)
294 << MissingHeader.IsUmbrella << MissingHeader.FileName;
296 CI.getDiagnostics().Report(diag::err_module_unavailable)
297 << Module->getFullModuleName()
298 << Requirement.second << Requirement.first;
304 if (!ModuleMapForUniquing)
305 ModuleMapForUniquing = ModuleMap;
306 Module->ModuleMap = ModuleMapForUniquing;
307 assert(Module->ModuleMap && "missing module map file");
309 FileManager &FileMgr = CI.getFileManager();
311 // Collect the set of #includes we need to build the module.
312 SmallString<256> HeaderContents;
313 std::error_code Err = std::error_code();
314 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
315 Err = addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts(),
318 Err = collectModuleHeaderIncludes(
319 CI.getLangOpts(), FileMgr,
320 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module,
324 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
325 << Module->getFullModuleName() << Err.message();
329 llvm::MemoryBuffer *InputBuffer =
330 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
331 Module::getModuleInputBufferName());
332 // Ownership of InputBuffer will be transferred to the SourceManager.
333 setCurrentInput(FrontendInputFile(InputBuffer, getCurrentFileKind(),
338 bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
340 std::string &Sysroot,
341 std::string &OutputFile,
343 // If no output file was provided, figure out where this module would go
344 // in the module cache.
345 if (CI.getFrontendOpts().OutputFile.empty()) {
346 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
347 CI.getFrontendOpts().OutputFile =
348 HS.getModuleFileName(CI.getLangOpts().CurrentModule,
349 ModuleMapForUniquing->getName());
352 // We use createOutputFile here because this is exposed via libclang, and we
353 // must disable the RemoveFileOnSignal behavior.
354 // We use a temporary to avoid race conditions.
355 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
356 /*RemoveFileOnSignal=*/false, InFile,
357 /*Extension=*/"", /*useTemporary=*/true,
358 /*CreateMissingDirectories=*/true);
362 OutputFile = CI.getFrontendOpts().OutputFile;
366 ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
368 return new ASTConsumer();
371 ASTConsumer *DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
373 return new ASTConsumer();
376 ASTConsumer *VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI,
378 return new ASTConsumer();
381 void VerifyPCHAction::ExecuteAction() {
382 CompilerInstance &CI = getCompilerInstance();
383 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
384 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
385 std::unique_ptr<ASTReader> Reader(
386 new ASTReader(CI.getPreprocessor(), CI.getASTContext(),
387 Sysroot.empty() ? "" : Sysroot.c_str(),
388 /*DisableValidation*/ false,
389 /*AllowPCHWithCompilerErrors*/ false,
390 /*AllowConfigurationMismatch*/ true,
391 /*ValidateSystemInputs*/ true));
393 Reader->ReadAST(getCurrentFile(),
394 Preamble ? serialization::MK_Preamble
395 : serialization::MK_PCH,
397 ASTReader::ARR_ConfigurationMismatch);
401 /// \brief AST reader listener that dumps module information for a module
403 class DumpModuleInfoListener : public ASTReaderListener {
404 llvm::raw_ostream &Out;
407 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
409 #define DUMP_BOOLEAN(Value, Text) \
410 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
412 bool ReadFullVersionInformation(StringRef FullVersion) override {
415 << (FullVersion == getClangFullRepositoryVersion()? "this"
417 << " Clang: " << FullVersion << "\n";
418 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
421 void ReadModuleName(StringRef ModuleName) override {
422 Out.indent(2) << "Module name: " << ModuleName << "\n";
424 void ReadModuleMapFile(StringRef ModuleMapPath) override {
425 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
428 bool ReadLanguageOptions(const LangOptions &LangOpts,
429 bool Complain) override {
430 Out.indent(2) << "Language options:\n";
431 #define LANGOPT(Name, Bits, Default, Description) \
432 DUMP_BOOLEAN(LangOpts.Name, Description);
433 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
434 Out.indent(4) << Description << ": " \
435 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
436 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
437 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
438 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
439 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
440 #include "clang/Basic/LangOptions.def"
444 bool ReadTargetOptions(const TargetOptions &TargetOpts,
445 bool Complain) override {
446 Out.indent(2) << "Target options:\n";
447 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
448 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
449 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
451 if (!TargetOpts.FeaturesAsWritten.empty()) {
452 Out.indent(4) << "Target features:\n";
453 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
455 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
463 ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
464 bool Complain) override {
465 Out.indent(2) << "Diagnostic options:\n";
466 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
467 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
468 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
469 #define VALUE_DIAGOPT(Name, Bits, Default) \
470 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
471 #include "clang/Basic/DiagnosticOptions.def"
473 Out.indent(4) << "Diagnostic flags:\n";
474 for (const std::string &Warning : DiagOpts->Warnings)
475 Out.indent(6) << "-W" << Warning << "\n";
476 for (const std::string &Remark : DiagOpts->Remarks)
477 Out.indent(6) << "-R" << Remark << "\n";
482 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
483 bool Complain) override {
484 Out.indent(2) << "Header search options:\n";
485 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
486 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
487 "Use builtin include directories [-nobuiltininc]");
488 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
489 "Use standard system include directories [-nostdinc]");
490 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
491 "Use standard C++ include directories [-nostdinc++]");
492 DUMP_BOOLEAN(HSOpts.UseLibcxx,
493 "Use libc++ (rather than libstdc++) [-stdlib=]");
497 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
499 std::string &SuggestedPredefines) override {
500 Out.indent(2) << "Preprocessor options:\n";
501 DUMP_BOOLEAN(PPOpts.UsePredefines,
502 "Uses compiler/target-specific predefines [-undef]");
503 DUMP_BOOLEAN(PPOpts.DetailedRecord,
504 "Uses detailed preprocessing record (for indexing)");
506 if (!PPOpts.Macros.empty()) {
507 Out.indent(4) << "Predefined macros:\n";
510 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
511 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
518 Out << I->first << "\n";
526 void DumpModuleInfoAction::ExecuteAction() {
527 // Set up the output file.
528 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
529 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
530 if (!OutputFileName.empty() && OutputFileName != "-") {
531 std::string ErrorInfo;
532 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str().c_str(),
533 ErrorInfo, llvm::sys::fs::F_Text));
535 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
537 Out << "Information for module file '" << getCurrentFile() << "':\n";
538 DumpModuleInfoListener Listener(Out);
539 ASTReader::readASTFileControlBlock(getCurrentFile(),
540 getCompilerInstance().getFileManager(),
544 //===----------------------------------------------------------------------===//
545 // Preprocessor Actions
546 //===----------------------------------------------------------------------===//
548 void DumpRawTokensAction::ExecuteAction() {
549 Preprocessor &PP = getCompilerInstance().getPreprocessor();
550 SourceManager &SM = PP.getSourceManager();
552 // Start lexing the specified input file.
553 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
554 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
555 RawLex.SetKeepWhitespaceMode(true);
558 RawLex.LexFromRawLexer(RawTok);
559 while (RawTok.isNot(tok::eof)) {
560 PP.DumpToken(RawTok, true);
561 llvm::errs() << "\n";
562 RawLex.LexFromRawLexer(RawTok);
566 void DumpTokensAction::ExecuteAction() {
567 Preprocessor &PP = getCompilerInstance().getPreprocessor();
568 // Start preprocessing the specified input file.
570 PP.EnterMainSourceFile();
573 PP.DumpToken(Tok, true);
574 llvm::errs() << "\n";
575 } while (Tok.isNot(tok::eof));
578 void GeneratePTHAction::ExecuteAction() {
579 CompilerInstance &CI = getCompilerInstance();
580 if (CI.getFrontendOpts().OutputFile.empty() ||
581 CI.getFrontendOpts().OutputFile == "-") {
582 // FIXME: Don't fail this way.
583 // FIXME: Verify that we can actually seek in the given file.
584 llvm::report_fatal_error("PTH requires a seekable file for output!");
586 llvm::raw_fd_ostream *OS =
587 CI.createDefaultOutputFile(true, getCurrentFile());
590 CacheTokens(CI.getPreprocessor(), OS);
593 void PreprocessOnlyAction::ExecuteAction() {
594 Preprocessor &PP = getCompilerInstance().getPreprocessor();
596 // Ignore unknown pragmas.
600 // Start parsing the specified input file.
601 PP.EnterMainSourceFile();
604 } while (Tok.isNot(tok::eof));
607 void PrintPreprocessedAction::ExecuteAction() {
608 CompilerInstance &CI = getCompilerInstance();
609 // Output file may need to be set to 'Binary', to avoid converting Unix style
610 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
612 // Look to see what type of line endings the file uses. If there's a
613 // CRLF, then we won't open the file up in binary mode. If there is
614 // just an LF or CR, then we will open the file up in binary mode.
615 // In this fashion, the output format should match the input format, unless
616 // the input format has inconsistent line endings.
618 // This should be a relatively fast operation since most files won't have
619 // all of their source code on a single line. However, that is still a
620 // concern, so if we scan for too long, we'll just assume the file should
621 // be opened in binary mode.
622 bool BinaryMode = true;
623 bool InvalidFile = false;
624 const SourceManager& SM = CI.getSourceManager();
625 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
628 const char *cur = Buffer->getBufferStart();
629 const char *end = Buffer->getBufferEnd();
630 const char *next = (cur != end) ? cur + 1 : end;
632 // Limit ourselves to only scanning 256 characters into the source
633 // file. This is mostly a sanity check in case the file has no
634 // newlines whatsoever.
635 if (end - cur > 256) end = cur + 256;
638 if (*cur == 0x0D) { // CR
639 if (*next == 0x0A) // CRLF
643 } else if (*cur == 0x0A) // LF
650 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
653 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
654 CI.getPreprocessorOutputOpts());
657 void PrintPreambleAction::ExecuteAction() {
658 switch (getCurrentFileKind()) {
669 case IK_PreprocessedC:
670 case IK_PreprocessedCXX:
671 case IK_PreprocessedObjC:
672 case IK_PreprocessedObjCXX:
675 // We can't do anything with these.
679 CompilerInstance &CI = getCompilerInstance();
680 llvm::MemoryBuffer *Buffer
681 = CI.getFileManager().getBufferForFile(getCurrentFile());
683 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
684 llvm::outs().write(Buffer->getBufferStart(), Preamble);