blob: baaf93b167bc2b7dd97850f064b7984ed3d8091d [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
Manman Renffd3e9d2017-01-09 19:20:18 +0000137bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI,
138 StringRef Filename) {
139 CI.getLangOpts().CompilingPCH = true;
140 return true;
141}
142
David Blaikie6beb6aa2014-08-10 19:56:51 +0000143std::unique_ptr<ASTConsumer>
144GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
145 StringRef InFile) {
Richard Smithbbcc9f02016-08-26 00:14:38 +0000146 std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000147 if (!OS)
Craig Topper49a27902014-05-22 04:46:25 +0000148 return nullptr;
149
Richard Smithbbcc9f02016-08-26 00:14:38 +0000150 std::string OutputFile = CI.getFrontendOpts().OutputFile;
151 std::string Sysroot;
152
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000153 auto Buffer = std::make_shared<PCHBuffer>();
154 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000155
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000156 Consumers.push_back(llvm::make_unique<PCHGenerator>(
Richard Smithbd97f352016-08-25 18:26:30 +0000157 CI.getPreprocessor(), OutputFile, Sysroot,
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000158 Buffer, CI.getFrontendOpts().ModuleFileExtensions,
159 /*AllowASTWithErrors=*/false,
160 /*IncludeTimestamps=*/
161 +CI.getFrontendOpts().BuildingImplicitModule));
Adrian Prantl03914062015-09-18 22:10:59 +0000162 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
Peter Collingbourne03f89072016-07-15 00:55:40 +0000163 CI, InFile, OutputFile, std::move(OS), Buffer));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000164 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000165}
166
Richard Smithbbcc9f02016-08-26 00:14:38 +0000167bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
168 CompilerInstance &CI, StringRef Filename) {
Richard Smithf74d9462017-04-28 01:49:42 +0000169 return GenerateModuleAction::BeginSourceFileAction(CI, Filename);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000170}
171
Peter Collingbourne03f89072016-07-15 00:55:40 +0000172std::unique_ptr<raw_pwrite_stream>
Richard Smithbbcc9f02016-08-26 00:14:38 +0000173GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
174 StringRef InFile) {
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000175 // If no output file was provided, figure out where this module would go
176 // in the module cache.
177 if (CI.getFrontendOpts().OutputFile.empty()) {
Richard Smithf74d9462017-04-28 01:49:42 +0000178 StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
179 if (ModuleMapFile.empty())
180 ModuleMapFile = InFile;
181
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000182 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000183 CI.getFrontendOpts().OutputFile =
Richard Smithf74d9462017-04-28 01:49:42 +0000184 HS.getModuleFileName(CI.getLangOpts().CurrentModule, ModuleMapFile,
Manman Ren11f2a472016-08-18 17:42:15 +0000185 /*UsePrebuiltPath=*/false);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000186 }
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000187
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000188 // We use createOutputFile here because this is exposed via libclang, and we
189 // must disable the RemoveFileOnSignal behavior.
190 // We use a temporary to avoid race conditions.
Richard Smithbbcc9f02016-08-26 00:14:38 +0000191 return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
192 /*RemoveFileOnSignal=*/false, InFile,
193 /*Extension=*/"", /*useTemporary=*/true,
194 /*CreateMissingDirectories=*/true);
195}
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000196
Richard Smithbbcc9f02016-08-26 00:14:38 +0000197bool GenerateModuleInterfaceAction::BeginSourceFileAction(CompilerInstance &CI,
198 StringRef Filename) {
199 if (!CI.getLangOpts().ModulesTS) {
200 CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts);
201 return false;
202 }
203
204 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
205
206 return GenerateModuleAction::BeginSourceFileAction(CI, Filename);
207}
208
209std::unique_ptr<raw_pwrite_stream>
210GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
211 StringRef InFile) {
212 return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000213}
214
Etienne Bergeron98de8052016-05-12 19:51:18 +0000215SyntaxOnlyAction::~SyntaxOnlyAction() {
216}
217
David Blaikie6beb6aa2014-08-10 19:56:51 +0000218std::unique_ptr<ASTConsumer>
219SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
220 return llvm::make_unique<ASTConsumer>();
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000221}
222
David Blaikie6beb6aa2014-08-10 19:56:51 +0000223std::unique_ptr<ASTConsumer>
224DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
225 StringRef InFile) {
226 return llvm::make_unique<ASTConsumer>();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000227}
228
David Blaikie6beb6aa2014-08-10 19:56:51 +0000229std::unique_ptr<ASTConsumer>
230VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
231 return llvm::make_unique<ASTConsumer>();
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000232}
233
234void VerifyPCHAction::ExecuteAction() {
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000235 CompilerInstance &CI = getCompilerInstance();
236 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
237 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000238 std::unique_ptr<ASTReader> Reader(new ASTReader(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000239 CI.getPreprocessor(), CI.getASTContext(), CI.getPCHContainerReader(),
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000240 CI.getFrontendOpts().ModuleFileExtensions,
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000241 Sysroot.empty() ? "" : Sysroot.c_str(),
242 /*DisableValidation*/ false,
243 /*AllowPCHWithCompilerErrors*/ false,
244 /*AllowConfigurationMismatch*/ true,
245 /*ValidateSystemInputs*/ true));
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000246
247 Reader->ReadAST(getCurrentFile(),
248 Preamble ? serialization::MK_Preamble
249 : serialization::MK_PCH,
250 SourceLocation(),
251 ASTReader::ARR_ConfigurationMismatch);
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000252}
253
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000254namespace {
255 /// \brief AST reader listener that dumps module information for a module
256 /// file.
257 class DumpModuleInfoListener : public ASTReaderListener {
258 llvm::raw_ostream &Out;
259
260 public:
261 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
262
263#define DUMP_BOOLEAN(Value, Text) \
264 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
265
Craig Topperafa7cb32014-03-13 06:07:04 +0000266 bool ReadFullVersionInformation(StringRef FullVersion) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000267 Out.indent(2)
268 << "Generated by "
269 << (FullVersion == getClangFullRepositoryVersion()? "this"
270 : "a different")
271 << " Clang: " << FullVersion << "\n";
272 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
273 }
274
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000275 void ReadModuleName(StringRef ModuleName) override {
276 Out.indent(2) << "Module name: " << ModuleName << "\n";
277 }
278 void ReadModuleMapFile(StringRef ModuleMapPath) override {
279 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
280 }
281
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000282 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
283 bool AllowCompatibleDifferences) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000284 Out.indent(2) << "Language options:\n";
285#define LANGOPT(Name, Bits, Default, Description) \
286 DUMP_BOOLEAN(LangOpts.Name, Description);
287#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
288 Out.indent(4) << Description << ": " \
289 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
290#define VALUE_LANGOPT(Name, Bits, Default, Description) \
291 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
292#define BENIGN_LANGOPT(Name, Bits, Default, Description)
293#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
294#include "clang/Basic/LangOptions.def"
Ben Langmuircd98cb72015-06-23 18:20:18 +0000295
296 if (!LangOpts.ModuleFeatures.empty()) {
297 Out.indent(4) << "Module features:\n";
298 for (StringRef Feature : LangOpts.ModuleFeatures)
299 Out.indent(6) << Feature << "\n";
300 }
301
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000302 return false;
303 }
304
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000305 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
306 bool AllowCompatibleDifferences) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000307 Out.indent(2) << "Target options:\n";
308 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
309 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
310 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000311
312 if (!TargetOpts.FeaturesAsWritten.empty()) {
313 Out.indent(4) << "Target features:\n";
314 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
315 I != N; ++I) {
316 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
317 }
318 }
319
320 return false;
321 }
322
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000323 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
324 bool Complain) override {
Ben Langmuirb92de022014-04-29 16:25:26 +0000325 Out.indent(2) << "Diagnostic options:\n";
326#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
327#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
328 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
329#define VALUE_DIAGOPT(Name, Bits, Default) \
330 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
331#include "clang/Basic/DiagnosticOptions.def"
332
Richard Smith3be1cb22014-08-07 00:24:21 +0000333 Out.indent(4) << "Diagnostic flags:\n";
334 for (const std::string &Warning : DiagOpts->Warnings)
Ben Langmuirb92de022014-04-29 16:25:26 +0000335 Out.indent(6) << "-W" << Warning << "\n";
Richard Smith3be1cb22014-08-07 00:24:21 +0000336 for (const std::string &Remark : DiagOpts->Remarks)
337 Out.indent(6) << "-R" << Remark << "\n";
Ben Langmuirb92de022014-04-29 16:25:26 +0000338
339 return false;
340 }
341
Craig Topperafa7cb32014-03-13 06:07:04 +0000342 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000343 StringRef SpecificModuleCachePath,
Craig Topperafa7cb32014-03-13 06:07:04 +0000344 bool Complain) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000345 Out.indent(2) << "Header search options:\n";
346 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
Argyrios Kyrtzidisbdff27d2017-02-25 18:14:31 +0000347 Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000348 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000349 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
350 "Use builtin include directories [-nobuiltininc]");
351 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
352 "Use standard system include directories [-nostdinc]");
353 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
354 "Use standard C++ include directories [-nostdinc++]");
355 DUMP_BOOLEAN(HSOpts.UseLibcxx,
356 "Use libc++ (rather than libstdc++) [-stdlib=]");
357 return false;
358 }
359
Craig Topperafa7cb32014-03-13 06:07:04 +0000360 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
361 bool Complain,
362 std::string &SuggestedPredefines) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000363 Out.indent(2) << "Preprocessor options:\n";
364 DUMP_BOOLEAN(PPOpts.UsePredefines,
365 "Uses compiler/target-specific predefines [-undef]");
366 DUMP_BOOLEAN(PPOpts.DetailedRecord,
367 "Uses detailed preprocessing record (for indexing)");
368
369 if (!PPOpts.Macros.empty()) {
370 Out.indent(4) << "Predefined macros:\n";
371 }
372
373 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
374 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
375 I != IEnd; ++I) {
376 Out.indent(6);
377 if (I->second)
378 Out << "-U";
379 else
380 Out << "-D";
381 Out << I->first << "\n";
382 }
383 return false;
384 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000385
386 /// Indicates that a particular module file extension has been read.
387 void readModuleFileExtension(
388 const ModuleFileExtensionMetadata &Metadata) override {
389 Out.indent(2) << "Module file extension '"
390 << Metadata.BlockName << "' " << Metadata.MajorVersion
391 << "." << Metadata.MinorVersion;
392 if (!Metadata.UserInfo.empty()) {
393 Out << ": ";
394 Out.write_escaped(Metadata.UserInfo);
395 }
396
397 Out << "\n";
398 }
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000399#undef DUMP_BOOLEAN
400 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000401}
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000402
Adrian Prantl576b2db2016-08-17 23:13:53 +0000403bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
404 // The Object file reader also supports raw ast files and there is no point in
405 // being strict about the module file format in -module-file-info mode.
406 CI.getHeaderSearchOpts().ModuleFormat = "obj";
407 return true;
408}
409
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000410void DumpModuleInfoAction::ExecuteAction() {
411 // Set up the output file.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000412 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000413 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
414 if (!OutputFileName.empty() && OutputFileName != "-") {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000415 std::error_code EC;
416 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
417 llvm::sys::fs::F_Text));
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000418 }
419 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
420
421 Out << "Information for module file '" << getCurrentFile() << "':\n";
Adrian Prantl99e765b2016-08-17 23:14:00 +0000422 auto &FileMgr = getCompilerInstance().getFileManager();
423 auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
424 StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
425 bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
426 Magic[2] == 'C' && Magic[3] == 'H');
427 Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
Adrian Prantl576b2db2016-08-17 23:13:53 +0000428
Manman Ren47a44452016-07-26 17:12:17 +0000429 Preprocessor &PP = getCompilerInstance().getPreprocessor();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000430 DumpModuleInfoListener Listener(Out);
Manman Ren47a44452016-07-26 17:12:17 +0000431 HeaderSearchOptions &HSOpts =
432 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000433 ASTReader::readASTFileControlBlock(
Adrian Prantl99e765b2016-08-17 23:14:00 +0000434 getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
Manman Ren47a44452016-07-26 17:12:17 +0000435 /*FindModuleFileExtensions=*/true, Listener,
436 HSOpts.ModulesValidateDiagnosticOptions);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000437}
438
Daniel Dunbar88357802009-11-14 10:42:57 +0000439//===----------------------------------------------------------------------===//
440// Preprocessor Actions
441//===----------------------------------------------------------------------===//
442
443void DumpRawTokensAction::ExecuteAction() {
444 Preprocessor &PP = getCompilerInstance().getPreprocessor();
445 SourceManager &SM = PP.getSourceManager();
446
447 // Start lexing the specified input file.
Chris Lattner710bb872009-11-30 04:18:44 +0000448 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikiebbafb8a2012-03-11 07:00:24 +0000449 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar88357802009-11-14 10:42:57 +0000450 RawLex.SetKeepWhitespaceMode(true);
451
452 Token RawTok;
453 RawLex.LexFromRawLexer(RawTok);
454 while (RawTok.isNot(tok::eof)) {
455 PP.DumpToken(RawTok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000456 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000457 RawLex.LexFromRawLexer(RawTok);
458 }
459}
460
461void DumpTokensAction::ExecuteAction() {
462 Preprocessor &PP = getCompilerInstance().getPreprocessor();
463 // Start preprocessing the specified input file.
464 Token Tok;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000465 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000466 do {
467 PP.Lex(Tok);
468 PP.DumpToken(Tok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000469 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000470 } while (Tok.isNot(tok::eof));
471}
472
473void GeneratePTHAction::ExecuteAction() {
474 CompilerInstance &CI = getCompilerInstance();
Peter Collingbourne03f89072016-07-15 00:55:40 +0000475 std::unique_ptr<raw_pwrite_stream> OS =
476 CI.createDefaultOutputFile(true, getCurrentFile());
NAKAMURA Takumi5594d792015-04-13 08:43:40 +0000477 if (!OS)
478 return;
Daniel Dunbar75546992009-12-03 09:13:30 +0000479
Peter Collingbourne03f89072016-07-15 00:55:40 +0000480 CacheTokens(CI.getPreprocessor(), OS.get());
Daniel Dunbar88357802009-11-14 10:42:57 +0000481}
482
Daniel Dunbar88357802009-11-14 10:42:57 +0000483void PreprocessOnlyAction::ExecuteAction() {
484 Preprocessor &PP = getCompilerInstance().getPreprocessor();
485
Daniel Dunbard839e772010-06-11 20:10:12 +0000486 // Ignore unknown pragmas.
Lubos Lunak576a0412014-05-01 12:54:03 +0000487 PP.IgnorePragmas();
Daniel Dunbard839e772010-06-11 20:10:12 +0000488
Daniel Dunbar88357802009-11-14 10:42:57 +0000489 Token Tok;
490 // Start parsing the specified input file.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000491 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000492 do {
493 PP.Lex(Tok);
494 } while (Tok.isNot(tok::eof));
495}
496
Daniel Dunbar88357802009-11-14 10:42:57 +0000497void PrintPreprocessedAction::ExecuteAction() {
498 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor52da28b2011-09-23 23:43:36 +0000499 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroff3d38a682010-01-05 17:33:23 +0000500 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor52da28b2011-09-23 23:43:36 +0000501 //
502 // Look to see what type of line endings the file uses. If there's a
503 // CRLF, then we won't open the file up in binary mode. If there is
504 // just an LF or CR, then we will open the file up in binary mode.
505 // In this fashion, the output format should match the input format, unless
506 // the input format has inconsistent line endings.
507 //
508 // This should be a relatively fast operation since most files won't have
509 // all of their source code on a single line. However, that is still a
510 // concern, so if we scan for too long, we'll just assume the file should
511 // be opened in binary mode.
512 bool BinaryMode = true;
513 bool InvalidFile = false;
514 const SourceManager& SM = CI.getSourceManager();
515 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
516 &InvalidFile);
517 if (!InvalidFile) {
518 const char *cur = Buffer->getBufferStart();
519 const char *end = Buffer->getBufferEnd();
520 const char *next = (cur != end) ? cur + 1 : end;
521
522 // Limit ourselves to only scanning 256 characters into the source
523 // file. This is mostly a sanity check in case the file has no
524 // newlines whatsoever.
525 if (end - cur > 256) end = cur + 256;
526
527 while (next < end) {
528 if (*cur == 0x0D) { // CR
529 if (*next == 0x0A) // CRLF
530 BinaryMode = false;
531
532 break;
533 } else if (*cur == 0x0A) // LF
534 break;
535
Richard Trieucc3949d2016-02-18 22:34:54 +0000536 ++cur;
537 ++next;
Douglas Gregor52da28b2011-09-23 23:43:36 +0000538 }
539 }
540
Peter Collingbourne03f89072016-07-15 00:55:40 +0000541 std::unique_ptr<raw_ostream> OS =
542 CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar75546992009-12-03 09:13:30 +0000543 if (!OS) return;
544
Richard Smith8128f332017-05-05 22:18:51 +0000545 // If we're preprocessing a module map, start by dumping the contents of the
546 // module itself before switching to the input buffer.
547 auto &Input = getCurrentInput();
548 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
549 if (Input.isFile())
550 (*OS) << "# 1 \"" << Input.getFile() << "\"\n";
551 // FIXME: Include additional information here so that we don't need the
552 // original source files to exist on disk.
553 getCurrentModule()->print(*OS);
554 (*OS) << "#pragma clang module contents\n";
555 }
556
Peter Collingbourne03f89072016-07-15 00:55:40 +0000557 DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
Daniel Dunbar88357802009-11-14 10:42:57 +0000558 CI.getPreprocessorOutputOpts());
559}
Douglas Gregoraf82e352010-07-20 20:18:03 +0000560
561void PrintPreambleAction::ExecuteAction() {
Richard Smith40c0efa2017-04-26 18:57:40 +0000562 switch (getCurrentFileKind().getLanguage()) {
563 case InputKind::C:
564 case InputKind::CXX:
565 case InputKind::ObjC:
566 case InputKind::ObjCXX:
567 case InputKind::OpenCL:
568 case InputKind::CUDA:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000569 break;
570
Richard Smith40c0efa2017-04-26 18:57:40 +0000571 case InputKind::Unknown:
572 case InputKind::Asm:
573 case InputKind::LLVM_IR:
574 case InputKind::RenderScript:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000575 // We can't do anything with these.
576 return;
577 }
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000578
Richard Smith40c0efa2017-04-26 18:57:40 +0000579 // We don't expect to find any #include directives in a preprocessed input.
580 if (getCurrentFileKind().isPreprocessed())
581 return;
582
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000583 CompilerInstance &CI = getCompilerInstance();
Benjamin Kramera8857962014-10-26 22:44:13 +0000584 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
585 if (Buffer) {
Rafael Espindolacd0b3802014-08-12 15:46:24 +0000586 unsigned Preamble =
Benjamin Kramera8857962014-10-26 22:44:13 +0000587 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
588 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
Douglas Gregoraf82e352010-07-20 20:18:03 +0000589 }
590}