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/CompilerInstance.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/MultiplexConsumer.h"
17 #include "clang/Frontend/Utils.h"
18 #include "clang/Lex/HeaderSearch.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "clang/Serialization/ASTReader.h"
22 #include "clang/Serialization/ASTWriter.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
28 #include <system_error>
30 using namespace clang;
32 //===----------------------------------------------------------------------===//
34 //===----------------------------------------------------------------------===//
36 std::unique_ptr<ASTConsumer>
37 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
38 return llvm::make_unique<ASTConsumer>();
41 void InitOnlyAction::ExecuteAction() {
44 //===----------------------------------------------------------------------===//
45 // AST Consumer Actions
46 //===----------------------------------------------------------------------===//
48 std::unique_ptr<ASTConsumer>
49 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
50 if (std::unique_ptr<raw_ostream> OS =
51 CI.createDefaultOutputFile(false, InFile))
52 return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
56 std::unique_ptr<ASTConsumer>
57 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
58 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
59 CI.getFrontendOpts().ASTDumpDecls,
60 CI.getFrontendOpts().ASTDumpAll,
61 CI.getFrontendOpts().ASTDumpLookups);
64 std::unique_ptr<ASTConsumer>
65 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
66 return CreateASTDeclNodeLister();
69 std::unique_ptr<ASTConsumer>
70 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
71 return CreateASTViewer();
74 std::unique_ptr<ASTConsumer>
75 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
77 return CreateDeclContextPrinter();
80 std::unique_ptr<ASTConsumer>
81 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
83 if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
86 std::string OutputFile;
87 std::unique_ptr<raw_pwrite_stream> OS =
88 CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
92 if (!CI.getFrontendOpts().RelocatablePCH)
95 auto Buffer = std::make_shared<PCHBuffer>();
96 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
97 Consumers.push_back(llvm::make_unique<PCHGenerator>(
98 CI.getPreprocessor(), OutputFile, Sysroot,
99 Buffer, CI.getFrontendOpts().ModuleFileExtensions,
100 /*AllowASTWithErrors*/CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
101 /*IncludeTimestamps*/
102 +CI.getFrontendOpts().IncludeTimestamps));
103 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
104 CI, InFile, OutputFile, std::move(OS), Buffer));
106 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
109 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
110 std::string &Sysroot) {
111 Sysroot = CI.getHeaderSearchOpts().Sysroot;
112 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
113 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
120 std::unique_ptr<llvm::raw_pwrite_stream>
121 GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
122 std::string &OutputFile) {
123 // We use createOutputFile here because this is exposed via libclang, and we
124 // must disable the RemoveFileOnSignal behavior.
125 // We use a temporary to avoid race conditions.
126 std::unique_ptr<raw_pwrite_stream> OS =
127 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
128 /*RemoveFileOnSignal=*/false, InFile,
129 /*Extension=*/"", /*useTemporary=*/true);
133 OutputFile = CI.getFrontendOpts().OutputFile;
137 bool GeneratePCHAction::shouldEraseOutputFiles() {
138 if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
140 return ASTFrontendAction::shouldEraseOutputFiles();
143 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
144 CI.getLangOpts().CompilingPCH = true;
148 std::unique_ptr<ASTConsumer>
149 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
151 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
155 std::string OutputFile = CI.getFrontendOpts().OutputFile;
158 auto Buffer = std::make_shared<PCHBuffer>();
159 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
161 Consumers.push_back(llvm::make_unique<PCHGenerator>(
162 CI.getPreprocessor(), OutputFile, Sysroot,
163 Buffer, CI.getFrontendOpts().ModuleFileExtensions,
164 /*AllowASTWithErrors=*/false,
165 /*IncludeTimestamps=*/
166 +CI.getFrontendOpts().BuildingImplicitModule));
167 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
168 CI, InFile, OutputFile, std::move(OS), Buffer));
169 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
172 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
173 CompilerInstance &CI) {
174 if (!CI.getLangOpts().Modules) {
175 CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
179 return GenerateModuleAction::BeginSourceFileAction(CI);
182 std::unique_ptr<raw_pwrite_stream>
183 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
185 // If no output file was provided, figure out where this module would go
186 // in the module cache.
187 if (CI.getFrontendOpts().OutputFile.empty()) {
188 StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
189 if (ModuleMapFile.empty())
190 ModuleMapFile = InFile;
192 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
193 CI.getFrontendOpts().OutputFile =
194 HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
198 // We use createOutputFile here because this is exposed via libclang, and we
199 // must disable the RemoveFileOnSignal behavior.
200 // We use a temporary to avoid race conditions.
201 return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
202 /*RemoveFileOnSignal=*/false, InFile,
203 /*Extension=*/"", /*useTemporary=*/true,
204 /*CreateMissingDirectories=*/true);
207 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
208 CompilerInstance &CI) {
209 if (!CI.getLangOpts().ModulesTS) {
210 CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts);
214 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
216 return GenerateModuleAction::BeginSourceFileAction(CI);
219 std::unique_ptr<raw_pwrite_stream>
220 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
222 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
225 SyntaxOnlyAction::~SyntaxOnlyAction() {
228 std::unique_ptr<ASTConsumer>
229 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
230 return llvm::make_unique<ASTConsumer>();
233 std::unique_ptr<ASTConsumer>
234 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
236 return llvm::make_unique<ASTConsumer>();
239 std::unique_ptr<ASTConsumer>
240 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
241 return llvm::make_unique<ASTConsumer>();
244 void VerifyPCHAction::ExecuteAction() {
245 CompilerInstance &CI = getCompilerInstance();
246 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
247 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
248 std::unique_ptr<ASTReader> Reader(new ASTReader(
249 CI.getPreprocessor(), &CI.getASTContext(), CI.getPCHContainerReader(),
250 CI.getFrontendOpts().ModuleFileExtensions,
251 Sysroot.empty() ? "" : Sysroot.c_str(),
252 /*DisableValidation*/ false,
253 /*AllowPCHWithCompilerErrors*/ false,
254 /*AllowConfigurationMismatch*/ true,
255 /*ValidateSystemInputs*/ true));
257 Reader->ReadAST(getCurrentFile(),
258 Preamble ? serialization::MK_Preamble
259 : serialization::MK_PCH,
261 ASTReader::ARR_ConfigurationMismatch);
265 /// \brief AST reader listener that dumps module information for a module
267 class DumpModuleInfoListener : public ASTReaderListener {
268 llvm::raw_ostream &Out;
271 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
273 #define DUMP_BOOLEAN(Value, Text) \
274 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
276 bool ReadFullVersionInformation(StringRef FullVersion) override {
279 << (FullVersion == getClangFullRepositoryVersion()? "this"
281 << " Clang: " << FullVersion << "\n";
282 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
285 void ReadModuleName(StringRef ModuleName) override {
286 Out.indent(2) << "Module name: " << ModuleName << "\n";
288 void ReadModuleMapFile(StringRef ModuleMapPath) override {
289 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
292 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
293 bool AllowCompatibleDifferences) override {
294 Out.indent(2) << "Language options:\n";
295 #define LANGOPT(Name, Bits, Default, Description) \
296 DUMP_BOOLEAN(LangOpts.Name, Description);
297 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
298 Out.indent(4) << Description << ": " \
299 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
300 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
301 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
302 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
303 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
304 #include "clang/Basic/LangOptions.def"
306 if (!LangOpts.ModuleFeatures.empty()) {
307 Out.indent(4) << "Module features:\n";
308 for (StringRef Feature : LangOpts.ModuleFeatures)
309 Out.indent(6) << Feature << "\n";
315 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
316 bool AllowCompatibleDifferences) override {
317 Out.indent(2) << "Target options:\n";
318 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
319 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
320 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
322 if (!TargetOpts.FeaturesAsWritten.empty()) {
323 Out.indent(4) << "Target features:\n";
324 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
326 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
333 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
334 bool Complain) override {
335 Out.indent(2) << "Diagnostic options:\n";
336 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
337 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
338 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
339 #define VALUE_DIAGOPT(Name, Bits, Default) \
340 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
341 #include "clang/Basic/DiagnosticOptions.def"
343 Out.indent(4) << "Diagnostic flags:\n";
344 for (const std::string &Warning : DiagOpts->Warnings)
345 Out.indent(6) << "-W" << Warning << "\n";
346 for (const std::string &Remark : DiagOpts->Remarks)
347 Out.indent(6) << "-R" << Remark << "\n";
352 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
353 StringRef SpecificModuleCachePath,
354 bool Complain) override {
355 Out.indent(2) << "Header search options:\n";
356 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
357 Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
358 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
359 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
360 "Use builtin include directories [-nobuiltininc]");
361 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
362 "Use standard system include directories [-nostdinc]");
363 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
364 "Use standard C++ include directories [-nostdinc++]");
365 DUMP_BOOLEAN(HSOpts.UseLibcxx,
366 "Use libc++ (rather than libstdc++) [-stdlib=]");
370 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
372 std::string &SuggestedPredefines) override {
373 Out.indent(2) << "Preprocessor options:\n";
374 DUMP_BOOLEAN(PPOpts.UsePredefines,
375 "Uses compiler/target-specific predefines [-undef]");
376 DUMP_BOOLEAN(PPOpts.DetailedRecord,
377 "Uses detailed preprocessing record (for indexing)");
379 if (!PPOpts.Macros.empty()) {
380 Out.indent(4) << "Predefined macros:\n";
383 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
384 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
391 Out << I->first << "\n";
396 /// Indicates that a particular module file extension has been read.
397 void readModuleFileExtension(
398 const ModuleFileExtensionMetadata &Metadata) override {
399 Out.indent(2) << "Module file extension '"
400 << Metadata.BlockName << "' " << Metadata.MajorVersion
401 << "." << Metadata.MinorVersion;
402 if (!Metadata.UserInfo.empty()) {
404 Out.write_escaped(Metadata.UserInfo);
413 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
414 // The Object file reader also supports raw ast files and there is no point in
415 // being strict about the module file format in -module-file-info mode.
416 CI.getHeaderSearchOpts().ModuleFormat = "obj";
420 void DumpModuleInfoAction::ExecuteAction() {
421 // Set up the output file.
422 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
423 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
424 if (!OutputFileName.empty() && OutputFileName != "-") {
426 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
427 llvm::sys::fs::F_Text));
429 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
431 Out << "Information for module file '" << getCurrentFile() << "':\n";
432 auto &FileMgr = getCompilerInstance().getFileManager();
433 auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
434 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
435 bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
436 Magic[2] == 'C' && Magic[3] == 'H');
437 Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
439 Preprocessor &PP = getCompilerInstance().getPreprocessor();
440 DumpModuleInfoListener Listener(Out);
441 HeaderSearchOptions &HSOpts =
442 PP.getHeaderSearchInfo().getHeaderSearchOpts();
443 ASTReader::readASTFileControlBlock(
444 getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
445 /*FindModuleFileExtensions=*/true, Listener,
446 HSOpts.ModulesValidateDiagnosticOptions);
449 //===----------------------------------------------------------------------===//
450 // Preprocessor Actions
451 //===----------------------------------------------------------------------===//
453 void DumpRawTokensAction::ExecuteAction() {
454 Preprocessor &PP = getCompilerInstance().getPreprocessor();
455 SourceManager &SM = PP.getSourceManager();
457 // Start lexing the specified input file.
458 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
459 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
460 RawLex.SetKeepWhitespaceMode(true);
463 RawLex.LexFromRawLexer(RawTok);
464 while (RawTok.isNot(tok::eof)) {
465 PP.DumpToken(RawTok, true);
466 llvm::errs() << "\n";
467 RawLex.LexFromRawLexer(RawTok);
471 void DumpTokensAction::ExecuteAction() {
472 Preprocessor &PP = getCompilerInstance().getPreprocessor();
473 // Start preprocessing the specified input file.
475 PP.EnterMainSourceFile();
478 PP.DumpToken(Tok, true);
479 llvm::errs() << "\n";
480 } while (Tok.isNot(tok::eof));
483 void GeneratePTHAction::ExecuteAction() {
484 CompilerInstance &CI = getCompilerInstance();
485 std::unique_ptr<raw_pwrite_stream> OS =
486 CI.createDefaultOutputFile(true, getCurrentFile());
490 CacheTokens(CI.getPreprocessor(), OS.get());
493 void PreprocessOnlyAction::ExecuteAction() {
494 Preprocessor &PP = getCompilerInstance().getPreprocessor();
496 // Ignore unknown pragmas.
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
551 std::unique_ptr<raw_ostream> OS =
552 CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
555 // If we're preprocessing a module map, start by dumping the contents of the
556 // module itself before switching to the input buffer.
557 auto &Input = getCurrentInput();
558 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
559 if (Input.isFile()) {
561 OS->write_escaped(Input.getFile());
564 // FIXME: Include additional information here so that we don't need the
565 // original source files to exist on disk.
566 getCurrentModule()->print(*OS);
567 (*OS) << "#pragma clang module contents\n";
570 DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
571 CI.getPreprocessorOutputOpts());
574 void PrintPreambleAction::ExecuteAction() {
575 switch (getCurrentFileKind().getLanguage()) {
578 case InputKind::ObjC:
579 case InputKind::ObjCXX:
580 case InputKind::OpenCL:
581 case InputKind::CUDA:
584 case InputKind::Unknown:
586 case InputKind::LLVM_IR:
587 case InputKind::RenderScript:
588 // We can't do anything with these.
592 // We don't expect to find any #include directives in a preprocessed input.
593 if (getCurrentFileKind().isPreprocessed())
596 CompilerInstance &CI = getCompilerInstance();
597 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
600 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
601 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);