blob: c5567a09636b5031d2f8849b1813f215d1cfcd5b [file] [log] [blame]
Daniel Dunbar02bd2d72009-11-14 10:42:46 +00001//===--- 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/Basic/FileManager.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000013#include "clang/Frontend/ASTConsumers.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000014#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000015#include "clang/Frontend/FrontendDiagnostic.h"
Adrian Prantlbb165fb2015-06-20 18:53:08 +000016#include "clang/Frontend/MultiplexConsumer.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000017#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderSearch.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Lex/Preprocessor.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000020#include "clang/Lex/PreprocessorOptions.h"
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000021#include "clang/Serialization/ASTReader.h"
Sebastian Redl1914c6f2010-08-18 23:56:37 +000022#include "clang/Serialization/ASTWriter.h"
Douglas Gregor524e33e2011-12-08 19:11:24 +000023#include "llvm/Support/FileSystem.h"
Douglas Gregoraf82e352010-07-20 20:18:03 +000024#include "llvm/Support/MemoryBuffer.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000025#include "llvm/Support/Path.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000026#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000027#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000028#include <system_error>
Douglas Gregor52da28b2011-09-23 23:43:36 +000029
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000030using namespace clang;
31
Daniel Dunbar88357802009-11-14 10:42:57 +000032//===----------------------------------------------------------------------===//
Daniel Dunbar1c201fb2010-03-19 19:44:04 +000033// Custom Actions
34//===----------------------------------------------------------------------===//
35
David Blaikie6beb6aa2014-08-10 19:56:51 +000036std::unique_ptr<ASTConsumer>
37InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
38 return llvm::make_unique<ASTConsumer>();
Daniel Dunbar1c201fb2010-03-19 19:44:04 +000039}
40
41void InitOnlyAction::ExecuteAction() {
42}
43
44//===----------------------------------------------------------------------===//
Daniel Dunbar88357802009-11-14 10:42:57 +000045// AST Consumer Actions
46//===----------------------------------------------------------------------===//
47
David Blaikie6beb6aa2014-08-10 19:56:51 +000048std::unique_ptr<ASTConsumer>
49ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Peter Collingbourne03f89072016-07-15 00:55:40 +000050 if (std::unique_ptr<raw_ostream> OS =
51 CI.createDefaultOutputFile(false, InFile))
52 return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
Craig Topper49a27902014-05-22 04:46:25 +000053 return nullptr;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000054}
55
David Blaikie6beb6aa2014-08-10 19:56:51 +000056std::unique_ptr<ASTConsumer>
57ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Richard Smith6ea05822013-06-24 01:45:33 +000058 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
Richard Smith35f986d2014-08-11 22:11:07 +000059 CI.getFrontendOpts().ASTDumpDecls,
Richard Smith3a36ac12017-03-09 22:00:01 +000060 CI.getFrontendOpts().ASTDumpAll,
Richard Smith6ea05822013-06-24 01:45:33 +000061 CI.getFrontendOpts().ASTDumpLookups);
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000062}
63
David Blaikie6beb6aa2014-08-10 19:56:51 +000064std::unique_ptr<ASTConsumer>
65ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Alexander Kornienko4de03592012-07-31 09:37:40 +000066 return CreateASTDeclNodeLister();
67}
68
David Blaikie6beb6aa2014-08-10 19:56:51 +000069std::unique_ptr<ASTConsumer>
70ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000071 return CreateASTViewer();
72}
73
David Blaikie6beb6aa2014-08-10 19:56:51 +000074std::unique_ptr<ASTConsumer>
75DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
76 StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000077 return CreateDeclContextPrinter();
78}
79
David Blaikie6beb6aa2014-08-10 19:56:51 +000080std::unique_ptr<ASTConsumer>
81GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +000082 std::string Sysroot;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +000083 std::string OutputFile;
Peter Collingbourne03f89072016-07-15 00:55:40 +000084 std::unique_ptr<raw_pwrite_stream> OS =
Rafael Espindola47de1492015-04-10 12:54:53 +000085 ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
86 if (!OS)
Craig Topper49a27902014-05-22 04:46:25 +000087 return nullptr;
Douglas Gregor48c8cd32010-08-03 08:14:03 +000088
Douglas Gregorc567ba22011-07-22 16:35:34 +000089 if (!CI.getFrontendOpts().RelocatablePCH)
90 Sysroot.clear();
Adrian Prantlbb165fb2015-06-20 18:53:08 +000091
92 auto Buffer = std::make_shared<PCHBuffer>();
93 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
94 Consumers.push_back(llvm::make_unique<PCHGenerator>(
Richard Smithbd97f352016-08-25 18:26:30 +000095 CI.getPreprocessor(), OutputFile, Sysroot,
Pierre Gousseau533a8932016-07-13 14:21:11 +000096 Buffer, CI.getFrontendOpts().ModuleFileExtensions,
Argyrios Kyrtzidiscf486b22017-02-27 03:52:36 +000097 /*AllowASTWithErrors*/CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Pierre Gousseau533a8932016-07-13 14:21:11 +000098 /*IncludeTimestamps*/
99 +CI.getFrontendOpts().IncludeTimestamps));
Adrian Prantl03914062015-09-18 22:10:59 +0000100 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
Peter Collingbourne03f89072016-07-15 00:55:40 +0000101 CI, InFile, OutputFile, std::move(OS), Buffer));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000102
103 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000104}
105
Peter Collingbourne03f89072016-07-15 00:55:40 +0000106std::unique_ptr<raw_pwrite_stream>
107GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
108 StringRef InFile,
109 std::string &Sysroot,
110 std::string &OutputFile) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000111 Sysroot = CI.getHeaderSearchOpts().Sysroot;
112 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redlea68af42010-08-17 17:55:38 +0000113 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Rafael Espindola47de1492015-04-10 12:54:53 +0000114 return nullptr;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000115 }
116
Daniel Dunbara2867c72011-01-31 22:00:44 +0000117 // We use createOutputFile here because this is exposed via libclang, and we
118 // must disable the RemoveFileOnSignal behavior.
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000119 // We use a temporary to avoid race conditions.
Peter Collingbourne03f89072016-07-15 00:55:40 +0000120 std::unique_ptr<raw_pwrite_stream> OS =
Rafael Espindola47de1492015-04-10 12:54:53 +0000121 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
122 /*RemoveFileOnSignal=*/false, InFile,
123 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar75546992009-12-03 09:13:30 +0000124 if (!OS)
Rafael Espindola47de1492015-04-10 12:54:53 +0000125 return nullptr;
Daniel Dunbar75546992009-12-03 09:13:30 +0000126
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000127 OutputFile = CI.getFrontendOpts().OutputFile;
Rafael Espindola47de1492015-04-10 12:54:53 +0000128 return OS;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000129}
130
Argyrios Kyrtzidiscf486b22017-02-27 03:52:36 +0000131bool GeneratePCHAction::shouldEraseOutputFiles() {
132 if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
133 return false;
134 return ASTFrontendAction::shouldEraseOutputFiles();
135}
136
Richard Smithd9259c22017-06-09 01:36:10 +0000137bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
Manman Renffd3e9d2017-01-09 19:20:18 +0000138 CI.getLangOpts().CompilingPCH = true;
139 return true;
140}
141
David Blaikie6beb6aa2014-08-10 19:56:51 +0000142std::unique_ptr<ASTConsumer>
143GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
144 StringRef InFile) {
Richard Smithbbcc9f02016-08-26 00:14:38 +0000145 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000146 if (!OS)
Craig Topper49a27902014-05-22 04:46:25 +0000147 return nullptr;
148
Richard Smithbbcc9f02016-08-26 00:14:38 +0000149 std::string OutputFile = CI.getFrontendOpts().OutputFile;
150 std::string Sysroot;
151
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000152 auto Buffer = std::make_shared<PCHBuffer>();
153 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000154
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000155 Consumers.push_back(llvm::make_unique<PCHGenerator>(
Richard Smithbd97f352016-08-25 18:26:30 +0000156 CI.getPreprocessor(), OutputFile, Sysroot,
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000157 Buffer, CI.getFrontendOpts().ModuleFileExtensions,
158 /*AllowASTWithErrors=*/false,
159 /*IncludeTimestamps=*/
160 +CI.getFrontendOpts().BuildingImplicitModule));
Adrian Prantl03914062015-09-18 22:10:59 +0000161 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
Peter Collingbourne03f89072016-07-15 00:55:40 +0000162 CI, InFile, OutputFile, std::move(OS), Buffer));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000163 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000164}
165
Peter Collingbourne03f89072016-07-15 00:55:40 +0000166std::unique_ptr<raw_pwrite_stream>
Richard Smithbbcc9f02016-08-26 00:14:38 +0000167GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
168 StringRef InFile) {
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000169 // If no output file was provided, figure out where this module would go
170 // in the module cache.
171 if (CI.getFrontendOpts().OutputFile.empty()) {
Richard Smithf74d9462017-04-28 01:49:42 +0000172 StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
173 if (ModuleMapFile.empty())
174 ModuleMapFile = InFile;
175
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000176 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000177 CI.getFrontendOpts().OutputFile =
Richard Smithf74d9462017-04-28 01:49:42 +0000178 HS.getModuleFileName(CI.getLangOpts().CurrentModule, ModuleMapFile,
Manman Ren11f2a472016-08-18 17:42:15 +0000179 /*UsePrebuiltPath=*/false);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000180 }
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000181
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000182 // We use createOutputFile here because this is exposed via libclang, and we
183 // must disable the RemoveFileOnSignal behavior.
184 // We use a temporary to avoid race conditions.
Richard Smithbbcc9f02016-08-26 00:14:38 +0000185 return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
186 /*RemoveFileOnSignal=*/false, InFile,
187 /*Extension=*/"", /*useTemporary=*/true,
188 /*CreateMissingDirectories=*/true);
189}
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000190
Richard Smithd9259c22017-06-09 01:36:10 +0000191bool GenerateModuleInterfaceAction::BeginSourceFileAction(
192 CompilerInstance &CI) {
Richard Smithbbcc9f02016-08-26 00:14:38 +0000193 if (!CI.getLangOpts().ModulesTS) {
194 CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts);
195 return false;
196 }
197
198 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
199
Richard Smithd9259c22017-06-09 01:36:10 +0000200 return GenerateModuleAction::BeginSourceFileAction(CI);
Richard Smithbbcc9f02016-08-26 00:14:38 +0000201}
202
203std::unique_ptr<raw_pwrite_stream>
204GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
205 StringRef InFile) {
206 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000207}
208
Etienne Bergeron98de8052016-05-12 19:51:18 +0000209SyntaxOnlyAction::~SyntaxOnlyAction() {
210}
211
David Blaikie6beb6aa2014-08-10 19:56:51 +0000212std::unique_ptr<ASTConsumer>
213SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
214 return llvm::make_unique<ASTConsumer>();
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000215}
216
David Blaikie6beb6aa2014-08-10 19:56:51 +0000217std::unique_ptr<ASTConsumer>
218DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
219 StringRef InFile) {
220 return llvm::make_unique<ASTConsumer>();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000221}
222
David Blaikie6beb6aa2014-08-10 19:56:51 +0000223std::unique_ptr<ASTConsumer>
224VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
225 return llvm::make_unique<ASTConsumer>();
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000226}
227
228void VerifyPCHAction::ExecuteAction() {
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000229 CompilerInstance &CI = getCompilerInstance();
230 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
231 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000232 std::unique_ptr<ASTReader> Reader(new ASTReader(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000233 CI.getPreprocessor(), CI.getASTContext(), CI.getPCHContainerReader(),
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000234 CI.getFrontendOpts().ModuleFileExtensions,
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000235 Sysroot.empty() ? "" : Sysroot.c_str(),
236 /*DisableValidation*/ false,
237 /*AllowPCHWithCompilerErrors*/ false,
238 /*AllowConfigurationMismatch*/ true,
239 /*ValidateSystemInputs*/ true));
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000240
241 Reader->ReadAST(getCurrentFile(),
242 Preamble ? serialization::MK_Preamble
243 : serialization::MK_PCH,
244 SourceLocation(),
245 ASTReader::ARR_ConfigurationMismatch);
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000246}
247
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000248namespace {
249 /// \brief AST reader listener that dumps module information for a module
250 /// file.
251 class DumpModuleInfoListener : public ASTReaderListener {
252 llvm::raw_ostream &Out;
253
254 public:
255 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
256
257#define DUMP_BOOLEAN(Value, Text) \
258 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
259
Craig Topperafa7cb32014-03-13 06:07:04 +0000260 bool ReadFullVersionInformation(StringRef FullVersion) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000261 Out.indent(2)
262 << "Generated by "
263 << (FullVersion == getClangFullRepositoryVersion()? "this"
264 : "a different")
265 << " Clang: " << FullVersion << "\n";
266 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
267 }
268
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000269 void ReadModuleName(StringRef ModuleName) override {
270 Out.indent(2) << "Module name: " << ModuleName << "\n";
271 }
272 void ReadModuleMapFile(StringRef ModuleMapPath) override {
273 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
274 }
275
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000276 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
277 bool AllowCompatibleDifferences) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000278 Out.indent(2) << "Language options:\n";
279#define LANGOPT(Name, Bits, Default, Description) \
280 DUMP_BOOLEAN(LangOpts.Name, Description);
281#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
282 Out.indent(4) << Description << ": " \
283 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
284#define VALUE_LANGOPT(Name, Bits, Default, Description) \
285 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
286#define BENIGN_LANGOPT(Name, Bits, Default, Description)
287#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
288#include "clang/Basic/LangOptions.def"
Ben Langmuircd98cb72015-06-23 18:20:18 +0000289
290 if (!LangOpts.ModuleFeatures.empty()) {
291 Out.indent(4) << "Module features:\n";
292 for (StringRef Feature : LangOpts.ModuleFeatures)
293 Out.indent(6) << Feature << "\n";
294 }
295
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000296 return false;
297 }
298
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000299 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
300 bool AllowCompatibleDifferences) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000301 Out.indent(2) << "Target options:\n";
302 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
303 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
304 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000305
306 if (!TargetOpts.FeaturesAsWritten.empty()) {
307 Out.indent(4) << "Target features:\n";
308 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
309 I != N; ++I) {
310 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
311 }
312 }
313
314 return false;
315 }
316
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000317 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
318 bool Complain) override {
Ben Langmuirb92de022014-04-29 16:25:26 +0000319 Out.indent(2) << "Diagnostic options:\n";
320#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
321#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
322 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
323#define VALUE_DIAGOPT(Name, Bits, Default) \
324 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
325#include "clang/Basic/DiagnosticOptions.def"
326
Richard Smith3be1cb22014-08-07 00:24:21 +0000327 Out.indent(4) << "Diagnostic flags:\n";
328 for (const std::string &Warning : DiagOpts->Warnings)
Ben Langmuirb92de022014-04-29 16:25:26 +0000329 Out.indent(6) << "-W" << Warning << "\n";
Richard Smith3be1cb22014-08-07 00:24:21 +0000330 for (const std::string &Remark : DiagOpts->Remarks)
331 Out.indent(6) << "-R" << Remark << "\n";
Ben Langmuirb92de022014-04-29 16:25:26 +0000332
333 return false;
334 }
335
Craig Topperafa7cb32014-03-13 06:07:04 +0000336 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000337 StringRef SpecificModuleCachePath,
Craig Topperafa7cb32014-03-13 06:07:04 +0000338 bool Complain) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000339 Out.indent(2) << "Header search options:\n";
340 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
Argyrios Kyrtzidisbdff27d2017-02-25 18:14:31 +0000341 Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000342 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000343 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
344 "Use builtin include directories [-nobuiltininc]");
345 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
346 "Use standard system include directories [-nostdinc]");
347 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
348 "Use standard C++ include directories [-nostdinc++]");
349 DUMP_BOOLEAN(HSOpts.UseLibcxx,
350 "Use libc++ (rather than libstdc++) [-stdlib=]");
351 return false;
352 }
353
Craig Topperafa7cb32014-03-13 06:07:04 +0000354 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
355 bool Complain,
356 std::string &SuggestedPredefines) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000357 Out.indent(2) << "Preprocessor options:\n";
358 DUMP_BOOLEAN(PPOpts.UsePredefines,
359 "Uses compiler/target-specific predefines [-undef]");
360 DUMP_BOOLEAN(PPOpts.DetailedRecord,
361 "Uses detailed preprocessing record (for indexing)");
362
363 if (!PPOpts.Macros.empty()) {
364 Out.indent(4) << "Predefined macros:\n";
365 }
366
367 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
368 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
369 I != IEnd; ++I) {
370 Out.indent(6);
371 if (I->second)
372 Out << "-U";
373 else
374 Out << "-D";
375 Out << I->first << "\n";
376 }
377 return false;
378 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000379
380 /// Indicates that a particular module file extension has been read.
381 void readModuleFileExtension(
382 const ModuleFileExtensionMetadata &Metadata) override {
383 Out.indent(2) << "Module file extension '"
384 << Metadata.BlockName << "' " << Metadata.MajorVersion
385 << "." << Metadata.MinorVersion;
386 if (!Metadata.UserInfo.empty()) {
387 Out << ": ";
388 Out.write_escaped(Metadata.UserInfo);
389 }
390
391 Out << "\n";
392 }
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000393#undef DUMP_BOOLEAN
394 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000395}
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000396
Adrian Prantl576b2db2016-08-17 23:13:53 +0000397bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
398 // The Object file reader also supports raw ast files and there is no point in
399 // being strict about the module file format in -module-file-info mode.
400 CI.getHeaderSearchOpts().ModuleFormat = "obj";
401 return true;
402}
403
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000404void DumpModuleInfoAction::ExecuteAction() {
405 // Set up the output file.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000406 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000407 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
408 if (!OutputFileName.empty() && OutputFileName != "-") {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000409 std::error_code EC;
410 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
411 llvm::sys::fs::F_Text));
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000412 }
413 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
414
415 Out << "Information for module file '" << getCurrentFile() << "':\n";
Adrian Prantl99e765b2016-08-17 23:14:00 +0000416 auto &FileMgr = getCompilerInstance().getFileManager();
417 auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
418 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
419 bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
420 Magic[2] == 'C' && Magic[3] == 'H');
421 Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
Adrian Prantl576b2db2016-08-17 23:13:53 +0000422
Manman Ren47a44452016-07-26 17:12:17 +0000423 Preprocessor &PP = getCompilerInstance().getPreprocessor();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000424 DumpModuleInfoListener Listener(Out);
Manman Ren47a44452016-07-26 17:12:17 +0000425 HeaderSearchOptions &HSOpts =
426 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000427 ASTReader::readASTFileControlBlock(
Adrian Prantl99e765b2016-08-17 23:14:00 +0000428 getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
Manman Ren47a44452016-07-26 17:12:17 +0000429 /*FindModuleFileExtensions=*/true, Listener,
430 HSOpts.ModulesValidateDiagnosticOptions);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000431}
432
Daniel Dunbar88357802009-11-14 10:42:57 +0000433//===----------------------------------------------------------------------===//
434// Preprocessor Actions
435//===----------------------------------------------------------------------===//
436
437void DumpRawTokensAction::ExecuteAction() {
438 Preprocessor &PP = getCompilerInstance().getPreprocessor();
439 SourceManager &SM = PP.getSourceManager();
440
441 // Start lexing the specified input file.
Chris Lattner710bb872009-11-30 04:18:44 +0000442 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikiebbafb8a2012-03-11 07:00:24 +0000443 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar88357802009-11-14 10:42:57 +0000444 RawLex.SetKeepWhitespaceMode(true);
445
446 Token RawTok;
447 RawLex.LexFromRawLexer(RawTok);
448 while (RawTok.isNot(tok::eof)) {
449 PP.DumpToken(RawTok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000450 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000451 RawLex.LexFromRawLexer(RawTok);
452 }
453}
454
455void DumpTokensAction::ExecuteAction() {
456 Preprocessor &PP = getCompilerInstance().getPreprocessor();
457 // Start preprocessing the specified input file.
458 Token Tok;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000459 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000460 do {
461 PP.Lex(Tok);
462 PP.DumpToken(Tok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000463 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000464 } while (Tok.isNot(tok::eof));
465}
466
467void GeneratePTHAction::ExecuteAction() {
468 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000469 std::unique_ptr<raw_pwrite_stream> OS =
470 CI.createDefaultOutputFile(true, getCurrentFile());
NAKAMURA Takumi5594d792015-04-13 08:43:40 +0000471 if (!OS)
472 return;
Daniel Dunbar75546992009-12-03 09:13:30 +0000473
Peter Collingbourne03f89072016-07-15 00:55:40 +0000474 CacheTokens(CI.getPreprocessor(), OS.get());
Daniel Dunbar88357802009-11-14 10:42:57 +0000475}
476
Daniel Dunbar88357802009-11-14 10:42:57 +0000477void PreprocessOnlyAction::ExecuteAction() {
478 Preprocessor &PP = getCompilerInstance().getPreprocessor();
479
Daniel Dunbard839e772010-06-11 20:10:12 +0000480 // Ignore unknown pragmas.
Lubos Lunak576a0412014-05-01 12:54:03 +0000481 PP.IgnorePragmas();
Daniel Dunbard839e772010-06-11 20:10:12 +0000482
Daniel Dunbar88357802009-11-14 10:42:57 +0000483 Token Tok;
484 // Start parsing the specified input file.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000485 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000486 do {
487 PP.Lex(Tok);
488 } while (Tok.isNot(tok::eof));
489}
490
Daniel Dunbar88357802009-11-14 10:42:57 +0000491void PrintPreprocessedAction::ExecuteAction() {
492 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor52da28b2011-09-23 23:43:36 +0000493 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroff3d38a682010-01-05 17:33:23 +0000494 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor52da28b2011-09-23 23:43:36 +0000495 //
496 // Look to see what type of line endings the file uses. If there's a
497 // CRLF, then we won't open the file up in binary mode. If there is
498 // just an LF or CR, then we will open the file up in binary mode.
499 // In this fashion, the output format should match the input format, unless
500 // the input format has inconsistent line endings.
501 //
502 // This should be a relatively fast operation since most files won't have
503 // all of their source code on a single line. However, that is still a
504 // concern, so if we scan for too long, we'll just assume the file should
505 // be opened in binary mode.
506 bool BinaryMode = true;
507 bool InvalidFile = false;
508 const SourceManager& SM = CI.getSourceManager();
509 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
510 &InvalidFile);
511 if (!InvalidFile) {
512 const char *cur = Buffer->getBufferStart();
513 const char *end = Buffer->getBufferEnd();
514 const char *next = (cur != end) ? cur + 1 : end;
515
516 // Limit ourselves to only scanning 256 characters into the source
517 // file. This is mostly a sanity check in case the file has no
518 // newlines whatsoever.
519 if (end - cur > 256) end = cur + 256;
520
521 while (next < end) {
522 if (*cur == 0x0D) { // CR
523 if (*next == 0x0A) // CRLF
524 BinaryMode = false;
525
526 break;
527 } else if (*cur == 0x0A) // LF
528 break;
529
Richard Trieucc3949d2016-02-18 22:34:54 +0000530 ++cur;
531 ++next;
Douglas Gregor52da28b2011-09-23 23:43:36 +0000532 }
533 }
534
Peter Collingbourne03f89072016-07-15 00:55:40 +0000535 std::unique_ptr<raw_ostream> OS =
536 CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar75546992009-12-03 09:13:30 +0000537 if (!OS) return;
538
Richard Smith8128f332017-05-05 22:18:51 +0000539 // If we're preprocessing a module map, start by dumping the contents of the
540 // module itself before switching to the input buffer.
541 auto &Input = getCurrentInput();
542 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
Richard Smithc784e962017-06-01 20:10:35 +0000543 if (Input.isFile()) {
544 (*OS) << "# 1 \"";
545 OS->write_escaped(Input.getFile());
546 (*OS) << "\"\n";
547 }
Richard Smith8128f332017-05-05 22:18:51 +0000548 // FIXME: Include additional information here so that we don't need the
549 // original source files to exist on disk.
550 getCurrentModule()->print(*OS);
551 (*OS) << "#pragma clang module contents\n";
552 }
553
Peter Collingbourne03f89072016-07-15 00:55:40 +0000554 DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
Daniel Dunbar88357802009-11-14 10:42:57 +0000555 CI.getPreprocessorOutputOpts());
556}
Douglas Gregoraf82e352010-07-20 20:18:03 +0000557
558void PrintPreambleAction::ExecuteAction() {
Richard Smith40c0efa2017-04-26 18:57:40 +0000559 switch (getCurrentFileKind().getLanguage()) {
560 case InputKind::C:
561 case InputKind::CXX:
562 case InputKind::ObjC:
563 case InputKind::ObjCXX:
564 case InputKind::OpenCL:
565 case InputKind::CUDA:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000566 break;
567
Richard Smith40c0efa2017-04-26 18:57:40 +0000568 case InputKind::Unknown:
569 case InputKind::Asm:
570 case InputKind::LLVM_IR:
571 case InputKind::RenderScript:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000572 // We can't do anything with these.
573 return;
574 }
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000575
Richard Smith40c0efa2017-04-26 18:57:40 +0000576 // We don't expect to find any #include directives in a preprocessed input.
577 if (getCurrentFileKind().isPreprocessed())
578 return;
579
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000580 CompilerInstance &CI = getCompilerInstance();
Benjamin Kramera8857962014-10-26 22:44:13 +0000581 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
582 if (Buffer) {
Rafael Espindolacd0b3802014-08-12 15:46:24 +0000583 unsigned Preamble =
Benjamin Kramera8857962014-10-26 22:44:13 +0000584 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
585 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
Douglas Gregoraf82e352010-07-20 20:18:03 +0000586 }
587}