blob: 79c1df43dd7918f68a09725ca65b7cb842c36b12 [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"
14#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000016#include "clang/Frontend/FrontendDiagnostic.h"
Adrian Prantlbb165fb2015-06-20 18:53:08 +000017#include "clang/Frontend/MultiplexConsumer.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000018#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Lex/HeaderSearch.h"
20#include "clang/Lex/Pragma.h"
21#include "clang/Lex/Preprocessor.h"
22#include "clang/Parse/Parser.h"
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000023#include "clang/Serialization/ASTReader.h"
Sebastian Redl1914c6f2010-08-18 23:56:37 +000024#include "clang/Serialization/ASTWriter.h"
Douglas Gregor524e33e2011-12-08 19:11:24 +000025#include "llvm/Support/FileSystem.h"
Douglas Gregoraf82e352010-07-20 20:18:03 +000026#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000027#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000028#include <memory>
Rafael Espindola8a8e5542014-06-12 17:19:42 +000029#include <system_error>
Douglas Gregor52da28b2011-09-23 23:43:36 +000030
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000031using namespace clang;
32
Daniel Dunbar88357802009-11-14 10:42:57 +000033//===----------------------------------------------------------------------===//
Daniel Dunbar1c201fb2010-03-19 19:44:04 +000034// Custom Actions
35//===----------------------------------------------------------------------===//
36
David Blaikie6beb6aa2014-08-10 19:56:51 +000037std::unique_ptr<ASTConsumer>
38InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
39 return llvm::make_unique<ASTConsumer>();
Daniel Dunbar1c201fb2010-03-19 19:44:04 +000040}
41
42void InitOnlyAction::ExecuteAction() {
43}
44
45//===----------------------------------------------------------------------===//
Daniel Dunbar88357802009-11-14 10:42:57 +000046// AST Consumer Actions
47//===----------------------------------------------------------------------===//
48
David Blaikie6beb6aa2014-08-10 19:56:51 +000049std::unique_ptr<ASTConsumer>
50ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000051 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000052 return CreateASTPrinter(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 Smith6ea05822013-06-24 01:45:33 +000060 CI.getFrontendOpts().ASTDumpLookups);
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000061}
62
David Blaikie6beb6aa2014-08-10 19:56:51 +000063std::unique_ptr<ASTConsumer>
64ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Alexander Kornienko4de03592012-07-31 09:37:40 +000065 return CreateASTDeclNodeLister();
66}
67
David Blaikie6beb6aa2014-08-10 19:56:51 +000068std::unique_ptr<ASTConsumer>
69ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000070 return CreateASTViewer();
71}
72
David Blaikie6beb6aa2014-08-10 19:56:51 +000073std::unique_ptr<ASTConsumer>
74DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
75 StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000076 return CreateDeclContextPrinter();
77}
78
David Blaikie6beb6aa2014-08-10 19:56:51 +000079std::unique_ptr<ASTConsumer>
80GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +000081 std::string Sysroot;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +000082 std::string OutputFile;
Adrian Prantlbb165fb2015-06-20 18:53:08 +000083 raw_pwrite_stream *OS =
Rafael Espindola47de1492015-04-10 12:54:53 +000084 ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
85 if (!OS)
Craig Topper49a27902014-05-22 04:46:25 +000086 return nullptr;
Douglas Gregor48c8cd32010-08-03 08:14:03 +000087
Douglas Gregorc567ba22011-07-22 16:35:34 +000088 if (!CI.getFrontendOpts().RelocatablePCH)
89 Sysroot.clear();
Adrian Prantlbb165fb2015-06-20 18:53:08 +000090
91 auto Buffer = std::make_shared<PCHBuffer>();
92 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
93 Consumers.push_back(llvm::make_unique<PCHGenerator>(
Douglas Gregor6623e1f2015-11-03 18:33:07 +000094 CI.getPreprocessor(), OutputFile, nullptr, Sysroot,
95 Buffer, CI.getFrontendOpts().ModuleFileExtensions));
Adrian Prantl03914062015-09-18 22:10:59 +000096 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
Adrian Prantl1e63b2b2015-09-19 21:42:52 +000097 CI, InFile, OutputFile, OS, Buffer));
Adrian Prantlbb165fb2015-06-20 18:53:08 +000098
99 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000100}
101
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000102raw_pwrite_stream *GeneratePCHAction::ComputeASTConsumerArguments(
Rafael Espindola47de1492015-04-10 12:54:53 +0000103 CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
104 std::string &OutputFile) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000105 Sysroot = CI.getHeaderSearchOpts().Sysroot;
106 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redlea68af42010-08-17 17:55:38 +0000107 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Rafael Espindola47de1492015-04-10 12:54:53 +0000108 return nullptr;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000109 }
110
Daniel Dunbara2867c72011-01-31 22:00:44 +0000111 // We use createOutputFile here because this is exposed via libclang, and we
112 // must disable the RemoveFileOnSignal behavior.
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000113 // We use a temporary to avoid race conditions.
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000114 raw_pwrite_stream *OS =
Rafael Espindola47de1492015-04-10 12:54:53 +0000115 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
116 /*RemoveFileOnSignal=*/false, InFile,
117 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar75546992009-12-03 09:13:30 +0000118 if (!OS)
Rafael Espindola47de1492015-04-10 12:54:53 +0000119 return nullptr;
Daniel Dunbar75546992009-12-03 09:13:30 +0000120
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000121 OutputFile = CI.getFrontendOpts().OutputFile;
Rafael Espindola47de1492015-04-10 12:54:53 +0000122 return OS;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000123}
124
David Blaikie6beb6aa2014-08-10 19:56:51 +0000125std::unique_ptr<ASTConsumer>
126GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
127 StringRef InFile) {
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000128 std::string Sysroot;
129 std::string OutputFile;
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000130 raw_pwrite_stream *OS =
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000131 ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
132 if (!OS)
Craig Topper49a27902014-05-22 04:46:25 +0000133 return nullptr;
134
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000135 auto Buffer = std::make_shared<PCHBuffer>();
136 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000137
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000138 Consumers.push_back(llvm::make_unique<PCHGenerator>(
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000139 CI.getPreprocessor(), OutputFile, Module, Sysroot,
140 Buffer, CI.getFrontendOpts().ModuleFileExtensions,
141 /*AllowASTWithErrors=*/false,
142 /*IncludeTimestamps=*/
143 +CI.getFrontendOpts().BuildingImplicitModule));
Adrian Prantl03914062015-09-18 22:10:59 +0000144 Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
Adrian Prantl1e63b2b2015-09-19 21:42:52 +0000145 CI, InFile, OutputFile, OS, Buffer));
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000146 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000147}
148
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000149static SmallVectorImpl<char> &
150operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
151 Includes.append(RHS.begin(), RHS.end());
152 return Includes;
153}
154
Davide Italiano75df8212016-03-02 06:09:18 +0000155static void addHeaderInclude(StringRef HeaderName,
156 SmallVectorImpl<char> &Includes,
157 const LangOptions &LangOpts,
158 bool IsExternC) {
Richard Smith9bca2982014-03-08 00:03:56 +0000159 if (IsExternC && LangOpts.CPlusPlus)
Richard Smith77944862014-03-02 05:58:18 +0000160 Includes += "extern \"C\" {\n";
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000161 if (LangOpts.ObjC1)
162 Includes += "#import \"";
163 else
164 Includes += "#include \"";
Richard Smith3c1a41a2014-12-02 00:08:08 +0000165
166 Includes += HeaderName;
167
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000168 Includes += "\"\n";
Richard Smith9bca2982014-03-08 00:03:56 +0000169 if (IsExternC && LangOpts.CPlusPlus)
Richard Smith77944862014-03-02 05:58:18 +0000170 Includes += "}\n";
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000171}
172
Douglas Gregor4332b322011-11-16 17:04:00 +0000173/// \brief Collect the set of header includes needed to construct the given
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +0000174/// module and update the TopHeaders file set of the module.
Douglas Gregor4332b322011-11-16 17:04:00 +0000175///
176/// \param Module The module we're collecting includes from.
Douglas Gregore5626c42011-12-06 17:15:11 +0000177///
James Dennettc423c532012-06-15 21:48:19 +0000178/// \param Includes Will be augmented with the set of \#includes or \#imports
Douglas Gregore5626c42011-12-06 17:15:11 +0000179/// needed to load all of the named headers.
Rafael Espindolac0809172014-06-12 14:02:15 +0000180static std::error_code
Richard Smith723928c2014-03-11 02:02:47 +0000181collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
182 ModuleMap &ModMap, clang::Module *Module,
183 SmallVectorImpl<char> &Includes) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000184 // Don't collect any headers for unavailable modules.
185 if (!Module->isAvailable())
Rafael Espindolac0809172014-06-12 14:02:15 +0000186 return std::error_code();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000187
Douglas Gregore5626c42011-12-06 17:15:11 +0000188 // Add includes for each of these headers.
Richard Smithf7aeda12016-01-08 22:36:45 +0000189 for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
190 for (Module::Header &H : Module->Headers[HK]) {
191 Module->addTopHeader(H.Entry);
192 // Use the path as specified in the module map file. We'll look for this
193 // file relative to the module build directory (the directory containing
194 // the module map file) so this will find the same file that we found
195 // while parsing the module map.
Davide Italiano75df8212016-03-02 06:09:18 +0000196 addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
Richard Smithf7aeda12016-01-08 22:36:45 +0000197 }
Douglas Gregor3ec66632012-02-02 18:42:48 +0000198 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000199 // Note that Module->PrivateHeaders will not be a TopHeader.
Douglas Gregore5626c42011-12-06 17:15:11 +0000200
Richard Smith2b63d152015-05-16 02:28:53 +0000201 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
202 Module->addTopHeader(UmbrellaHeader.Entry);
Davide Italiano75df8212016-03-02 06:09:18 +0000203 if (Module->Parent)
Douglas Gregor3ec66632012-02-02 18:42:48 +0000204 // Include the umbrella header for submodules.
Davide Italiano75df8212016-03-02 06:09:18 +0000205 addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
206 Module->IsExternC);
Richard Smith2b63d152015-05-16 02:28:53 +0000207 } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000208 // Add all of the headers we find in this subdirectory.
Rafael Espindolac0809172014-06-12 14:02:15 +0000209 std::error_code EC;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000210 SmallString<128> DirNative;
Richard Smith2b63d152015-05-16 02:28:53 +0000211 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
Yaron Keren92e1b622015-03-18 10:17:07 +0000212 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC),
Douglas Gregorc1aaf8c2011-12-12 19:13:53 +0000213 DirEnd;
Douglas Gregor524e33e2011-12-08 19:11:24 +0000214 Dir != DirEnd && !EC; Dir.increment(EC)) {
215 // Check whether this entry has an extension typically associated with
216 // headers.
217 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
218 .Cases(".h", ".H", ".hh", ".hpp", true)
219 .Default(false))
220 continue;
Richard Smith3c1a41a2014-12-02 00:08:08 +0000221
222 const FileEntry *Header = FileMgr.getFile(Dir->path());
223 // FIXME: This shouldn't happen unless there is a file system race. Is
224 // that worth diagnosing?
225 if (!Header)
226 continue;
227
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000228 // If this header is marked 'unavailable' in this module, don't include
229 // it.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000230 if (ModMap.isHeaderUnavailableInModule(Header, Module))
231 continue;
232
Richard Smith2b63d152015-05-16 02:28:53 +0000233 // Compute the relative path from the directory to this file.
234 SmallVector<StringRef, 16> Components;
235 auto PathIt = llvm::sys::path::rbegin(Dir->path());
236 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
237 Components.push_back(*PathIt);
238 SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
239 for (auto It = Components.rbegin(), End = Components.rend(); It != End;
240 ++It)
241 llvm::sys::path::append(RelativeHeader, *It);
242
Richard Smith723928c2014-03-11 02:02:47 +0000243 // Include this header as part of the umbrella directory.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000244 Module->addTopHeader(Header);
Davide Italiano75df8212016-03-02 06:09:18 +0000245 addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
Douglas Gregor524e33e2011-12-08 19:11:24 +0000246 }
Richard Smith723928c2014-03-11 02:02:47 +0000247
248 if (EC)
249 return EC;
Douglas Gregor4332b322011-11-16 17:04:00 +0000250 }
Richard Smith3c1a41a2014-12-02 00:08:08 +0000251
Douglas Gregor4332b322011-11-16 17:04:00 +0000252 // Recurse into submodules.
Douglas Gregoreb90e832012-01-04 23:32:19 +0000253 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
254 SubEnd = Module->submodule_end();
Douglas Gregore5626c42011-12-06 17:15:11 +0000255 Sub != SubEnd; ++Sub)
Rafael Espindolac0809172014-06-12 14:02:15 +0000256 if (std::error_code Err = collectModuleHeaderIncludes(
Richard Smith723928c2014-03-11 02:02:47 +0000257 LangOpts, FileMgr, ModMap, *Sub, Includes))
258 return Err;
259
Rafael Espindolac0809172014-06-12 14:02:15 +0000260 return std::error_code();
Douglas Gregor4332b322011-11-16 17:04:00 +0000261}
262
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000263bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
264 StringRef Filename) {
Richard Smith7e82e012016-02-19 22:25:36 +0000265 CI.getLangOpts().CompilingModule = true;
266
Richard Smithfb1e7f72015-08-14 05:02:58 +0000267 // Find the module map file.
268 const FileEntry *ModuleMap =
269 CI.getFileManager().getFile(Filename, /*openFile*/true);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000270 if (!ModuleMap) {
271 CI.getDiagnostics().Report(diag::err_module_map_not_found)
272 << Filename;
273 return false;
274 }
275
Richard Smitha8cfffa2015-11-26 02:04:16 +0000276 // Set up embedding for any specified files. Do this before we load any
277 // source files, including the primary module map for the compilation.
278 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
279 if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true))
280 CI.getSourceManager().setFileIsTransient(FE);
281 else
282 CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
283 }
284 if (CI.getFrontendOpts().ModulesEmbedAllFiles)
285 CI.getSourceManager().setAllFilesAreTransient(true);
286
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000287 // Parse the module map file.
288 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Douglas Gregor963c5532013-06-21 16:28:10 +0000289 if (HS.loadModuleMapFile(ModuleMap, IsSystem))
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000290 return false;
291
292 if (CI.getLangOpts().CurrentModule.empty()) {
293 CI.getDiagnostics().Report(diag::err_missing_module_name);
294
295 // FIXME: Eventually, we could consider asking whether there was just
296 // a single module described in the module map, and use that as a
297 // default. Then it would be fairly trivial to just "compile" a module
298 // map with a single module (the common case).
299 return false;
300 }
Richard Smith7bea1d42014-03-05 20:55:36 +0000301
302 // If we're being run from the command-line, the module build stack will not
303 // have been filled in yet, so complete it now in order to allow us to detect
304 // module cycles.
305 SourceManager &SourceMgr = CI.getSourceManager();
306 if (SourceMgr.getModuleBuildStack().empty())
307 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
308 FullSourceLoc(SourceLocation(), SourceMgr));
309
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000310 // Dig out the module definition.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000311 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
312 /*AllowSearch=*/false);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000313 if (!Module) {
314 CI.getDiagnostics().Report(diag::err_missing_module)
315 << CI.getLangOpts().CurrentModule << Filename;
316
317 return false;
318 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000319
320 // Check whether we can build this module at all.
Richard Smitha3feee22013-10-28 22:18:19 +0000321 clang::Module::Requirement Requirement;
Richard Smith3c1a41a2014-12-02 00:08:08 +0000322 clang::Module::UnresolvedHeaderDirective MissingHeader;
Daniel Jasper0761a8a2013-12-17 10:31:37 +0000323 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
324 MissingHeader)) {
325 if (MissingHeader.FileNameLoc.isValid()) {
Ben Langmuirec8c9752014-04-18 22:07:31 +0000326 CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
327 diag::err_module_header_missing)
Daniel Jasper0761a8a2013-12-17 10:31:37 +0000328 << MissingHeader.IsUmbrella << MissingHeader.FileName;
329 } else {
330 CI.getDiagnostics().Report(diag::err_module_unavailable)
331 << Module->getFullModuleName()
332 << Requirement.second << Requirement.first;
333 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000334
335 return false;
336 }
337
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000338 if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
339 Module->IsInferred = true;
340 HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
341 } else {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000342 ModuleMapForUniquing = ModuleMap;
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000343 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000344
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000345 FileManager &FileMgr = CI.getFileManager();
346
Douglas Gregor4332b322011-11-16 17:04:00 +0000347 // Collect the set of #includes we need to build the module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000348 SmallString<256> HeaderContents;
Rafael Espindola8e650d72014-06-12 20:37:59 +0000349 std::error_code Err = std::error_code();
Richard Smith2b63d152015-05-16 02:28:53 +0000350 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader())
Davide Italiano75df8212016-03-02 06:09:18 +0000351 addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
352 CI.getLangOpts(), Module->IsExternC);
353 Err = collectModuleHeaderIncludes(
Richard Smith723928c2014-03-11 02:02:47 +0000354 CI.getLangOpts(), FileMgr,
355 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module,
356 HeaderContents);
357
358 if (Err) {
359 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
360 << Module->getFullModuleName() << Err.message();
361 return false;
362 }
Douglas Gregor4332b322011-11-16 17:04:00 +0000363
Richard Smith3c1a41a2014-12-02 00:08:08 +0000364 // Inform the preprocessor that includes from within the input buffer should
365 // be resolved relative to the build directory of the module map file.
366 CI.getPreprocessor().setMainFileDir(Module->Directory);
367
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000368 std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000369 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
370 Module::getModuleInputBufferName());
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000371 // Ownership of InputBuffer will be transferred to the SourceManager.
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000372 setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
Douglas Gregora686e1b2012-01-27 19:52:33 +0000373 Module->IsSystem));
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000374 return true;
375}
376
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000377raw_pwrite_stream *GenerateModuleAction::ComputeASTConsumerArguments(
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000378 CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
379 std::string &OutputFile) {
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000380 // If no output file was provided, figure out where this module would go
381 // in the module cache.
382 if (CI.getFrontendOpts().OutputFile.empty()) {
383 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000384 CI.getFrontendOpts().OutputFile =
385 HS.getModuleFileName(CI.getLangOpts().CurrentModule,
Ben Langmuird89dc562015-02-19 20:23:22 +0000386 ModuleMapForUniquing->getName());
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000387 }
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000388
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000389 // We use createOutputFile here because this is exposed via libclang, and we
390 // must disable the RemoveFileOnSignal behavior.
391 // We use a temporary to avoid race conditions.
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000392 raw_pwrite_stream *OS =
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000393 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
394 /*RemoveFileOnSignal=*/false, InFile,
395 /*Extension=*/"", /*useTemporary=*/true,
396 /*CreateMissingDirectories=*/true);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000397 if (!OS)
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000398 return nullptr;
399
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000400 OutputFile = CI.getFrontendOpts().OutputFile;
Rafael Espindolabfd25d42015-04-10 13:14:31 +0000401 return OS;
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000402}
403
Etienne Bergeron98de8052016-05-12 19:51:18 +0000404SyntaxOnlyAction::~SyntaxOnlyAction() {
405}
406
David Blaikie6beb6aa2014-08-10 19:56:51 +0000407std::unique_ptr<ASTConsumer>
408SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
409 return llvm::make_unique<ASTConsumer>();
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000410}
411
David Blaikie6beb6aa2014-08-10 19:56:51 +0000412std::unique_ptr<ASTConsumer>
413DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
414 StringRef InFile) {
415 return llvm::make_unique<ASTConsumer>();
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000416}
417
David Blaikie6beb6aa2014-08-10 19:56:51 +0000418std::unique_ptr<ASTConsumer>
419VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
420 return llvm::make_unique<ASTConsumer>();
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000421}
422
423void VerifyPCHAction::ExecuteAction() {
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000424 CompilerInstance &CI = getCompilerInstance();
425 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
426 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000427 std::unique_ptr<ASTReader> Reader(new ASTReader(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000428 CI.getPreprocessor(), CI.getASTContext(), CI.getPCHContainerReader(),
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000429 CI.getFrontendOpts().ModuleFileExtensions,
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000430 Sysroot.empty() ? "" : Sysroot.c_str(),
431 /*DisableValidation*/ false,
432 /*AllowPCHWithCompilerErrors*/ false,
433 /*AllowConfigurationMismatch*/ true,
434 /*ValidateSystemInputs*/ true));
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000435
436 Reader->ReadAST(getCurrentFile(),
437 Preamble ? serialization::MK_Preamble
438 : serialization::MK_PCH,
439 SourceLocation(),
440 ASTReader::ARR_ConfigurationMismatch);
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000441}
442
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000443namespace {
444 /// \brief AST reader listener that dumps module information for a module
445 /// file.
446 class DumpModuleInfoListener : public ASTReaderListener {
447 llvm::raw_ostream &Out;
448
449 public:
450 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
451
452#define DUMP_BOOLEAN(Value, Text) \
453 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
454
Craig Topperafa7cb32014-03-13 06:07:04 +0000455 bool ReadFullVersionInformation(StringRef FullVersion) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000456 Out.indent(2)
457 << "Generated by "
458 << (FullVersion == getClangFullRepositoryVersion()? "this"
459 : "a different")
460 << " Clang: " << FullVersion << "\n";
461 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
462 }
463
Ben Langmuir4f5212a2014-04-14 22:12:44 +0000464 void ReadModuleName(StringRef ModuleName) override {
465 Out.indent(2) << "Module name: " << ModuleName << "\n";
466 }
467 void ReadModuleMapFile(StringRef ModuleMapPath) override {
468 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
469 }
470
Richard Smith1e2cf0d2014-10-31 02:28:58 +0000471 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
472 bool AllowCompatibleDifferences) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000473 Out.indent(2) << "Language options:\n";
474#define LANGOPT(Name, Bits, Default, Description) \
475 DUMP_BOOLEAN(LangOpts.Name, Description);
476#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
477 Out.indent(4) << Description << ": " \
478 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
479#define VALUE_LANGOPT(Name, Bits, Default, Description) \
480 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
481#define BENIGN_LANGOPT(Name, Bits, Default, Description)
482#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
483#include "clang/Basic/LangOptions.def"
Ben Langmuircd98cb72015-06-23 18:20:18 +0000484
485 if (!LangOpts.ModuleFeatures.empty()) {
486 Out.indent(4) << "Module features:\n";
487 for (StringRef Feature : LangOpts.ModuleFeatures)
488 Out.indent(6) << Feature << "\n";
489 }
490
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000491 return false;
492 }
493
Chandler Carruth0d745bc2015-03-14 04:47:43 +0000494 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
495 bool AllowCompatibleDifferences) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000496 Out.indent(2) << "Target options:\n";
497 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
498 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
499 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000500
501 if (!TargetOpts.FeaturesAsWritten.empty()) {
502 Out.indent(4) << "Target features:\n";
503 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
504 I != N; ++I) {
505 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
506 }
507 }
508
509 return false;
510 }
511
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000512 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
513 bool Complain) override {
Ben Langmuirb92de022014-04-29 16:25:26 +0000514 Out.indent(2) << "Diagnostic options:\n";
515#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
516#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
517 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
518#define VALUE_DIAGOPT(Name, Bits, Default) \
519 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
520#include "clang/Basic/DiagnosticOptions.def"
521
Richard Smith3be1cb22014-08-07 00:24:21 +0000522 Out.indent(4) << "Diagnostic flags:\n";
523 for (const std::string &Warning : DiagOpts->Warnings)
Ben Langmuirb92de022014-04-29 16:25:26 +0000524 Out.indent(6) << "-W" << Warning << "\n";
Richard Smith3be1cb22014-08-07 00:24:21 +0000525 for (const std::string &Remark : DiagOpts->Remarks)
526 Out.indent(6) << "-R" << Remark << "\n";
Ben Langmuirb92de022014-04-29 16:25:26 +0000527
528 return false;
529 }
530
Craig Topperafa7cb32014-03-13 06:07:04 +0000531 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000532 StringRef SpecificModuleCachePath,
Craig Topperafa7cb32014-03-13 06:07:04 +0000533 bool Complain) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000534 Out.indent(2) << "Header search options:\n";
535 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000536 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000537 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
538 "Use builtin include directories [-nobuiltininc]");
539 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
540 "Use standard system include directories [-nostdinc]");
541 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
542 "Use standard C++ include directories [-nostdinc++]");
543 DUMP_BOOLEAN(HSOpts.UseLibcxx,
544 "Use libc++ (rather than libstdc++) [-stdlib=]");
545 return false;
546 }
547
Craig Topperafa7cb32014-03-13 06:07:04 +0000548 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
549 bool Complain,
550 std::string &SuggestedPredefines) override {
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000551 Out.indent(2) << "Preprocessor options:\n";
552 DUMP_BOOLEAN(PPOpts.UsePredefines,
553 "Uses compiler/target-specific predefines [-undef]");
554 DUMP_BOOLEAN(PPOpts.DetailedRecord,
555 "Uses detailed preprocessing record (for indexing)");
556
557 if (!PPOpts.Macros.empty()) {
558 Out.indent(4) << "Predefined macros:\n";
559 }
560
561 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
562 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
563 I != IEnd; ++I) {
564 Out.indent(6);
565 if (I->second)
566 Out << "-U";
567 else
568 Out << "-D";
569 Out << I->first << "\n";
570 }
571 return false;
572 }
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000573
574 /// Indicates that a particular module file extension has been read.
575 void readModuleFileExtension(
576 const ModuleFileExtensionMetadata &Metadata) override {
577 Out.indent(2) << "Module file extension '"
578 << Metadata.BlockName << "' " << Metadata.MajorVersion
579 << "." << Metadata.MinorVersion;
580 if (!Metadata.UserInfo.empty()) {
581 Out << ": ";
582 Out.write_escaped(Metadata.UserInfo);
583 }
584
585 Out << "\n";
586 }
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000587#undef DUMP_BOOLEAN
588 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000589}
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000590
591void DumpModuleInfoAction::ExecuteAction() {
592 // Set up the output file.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000593 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000594 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
595 if (!OutputFileName.empty() && OutputFileName != "-") {
Rafael Espindoladae941a2014-08-25 18:17:04 +0000596 std::error_code EC;
597 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
598 llvm::sys::fs::F_Text));
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000599 }
600 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
601
602 Out << "Information for module file '" << getCurrentFile() << "':\n";
603 DumpModuleInfoListener Listener(Out);
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000604 ASTReader::readASTFileControlBlock(
605 getCurrentFile(), getCompilerInstance().getFileManager(),
Douglas Gregor6623e1f2015-11-03 18:33:07 +0000606 getCompilerInstance().getPCHContainerReader(),
607 /*FindModuleFileExtensions=*/true, Listener);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000608}
609
Daniel Dunbar88357802009-11-14 10:42:57 +0000610//===----------------------------------------------------------------------===//
611// Preprocessor Actions
612//===----------------------------------------------------------------------===//
613
614void DumpRawTokensAction::ExecuteAction() {
615 Preprocessor &PP = getCompilerInstance().getPreprocessor();
616 SourceManager &SM = PP.getSourceManager();
617
618 // Start lexing the specified input file.
Chris Lattner710bb872009-11-30 04:18:44 +0000619 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikiebbafb8a2012-03-11 07:00:24 +0000620 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar88357802009-11-14 10:42:57 +0000621 RawLex.SetKeepWhitespaceMode(true);
622
623 Token RawTok;
624 RawLex.LexFromRawLexer(RawTok);
625 while (RawTok.isNot(tok::eof)) {
626 PP.DumpToken(RawTok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000627 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000628 RawLex.LexFromRawLexer(RawTok);
629 }
630}
631
632void DumpTokensAction::ExecuteAction() {
633 Preprocessor &PP = getCompilerInstance().getPreprocessor();
634 // Start preprocessing the specified input file.
635 Token Tok;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000636 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000637 do {
638 PP.Lex(Tok);
639 PP.DumpToken(Tok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000640 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000641 } while (Tok.isNot(tok::eof));
642}
643
644void GeneratePTHAction::ExecuteAction() {
645 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola2f16bc12015-04-14 15:15:49 +0000646 raw_pwrite_stream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
NAKAMURA Takumi5594d792015-04-13 08:43:40 +0000647 if (!OS)
648 return;
Daniel Dunbar75546992009-12-03 09:13:30 +0000649
Daniel Dunbar88357802009-11-14 10:42:57 +0000650 CacheTokens(CI.getPreprocessor(), OS);
651}
652
Daniel Dunbar88357802009-11-14 10:42:57 +0000653void PreprocessOnlyAction::ExecuteAction() {
654 Preprocessor &PP = getCompilerInstance().getPreprocessor();
655
Daniel Dunbard839e772010-06-11 20:10:12 +0000656 // Ignore unknown pragmas.
Lubos Lunak576a0412014-05-01 12:54:03 +0000657 PP.IgnorePragmas();
Daniel Dunbard839e772010-06-11 20:10:12 +0000658
Daniel Dunbar88357802009-11-14 10:42:57 +0000659 Token Tok;
660 // Start parsing the specified input file.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000661 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000662 do {
663 PP.Lex(Tok);
664 } while (Tok.isNot(tok::eof));
665}
666
Daniel Dunbar88357802009-11-14 10:42:57 +0000667void PrintPreprocessedAction::ExecuteAction() {
668 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor52da28b2011-09-23 23:43:36 +0000669 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroff3d38a682010-01-05 17:33:23 +0000670 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor52da28b2011-09-23 23:43:36 +0000671 //
672 // Look to see what type of line endings the file uses. If there's a
673 // CRLF, then we won't open the file up in binary mode. If there is
674 // just an LF or CR, then we will open the file up in binary mode.
675 // In this fashion, the output format should match the input format, unless
676 // the input format has inconsistent line endings.
677 //
678 // This should be a relatively fast operation since most files won't have
679 // all of their source code on a single line. However, that is still a
680 // concern, so if we scan for too long, we'll just assume the file should
681 // be opened in binary mode.
682 bool BinaryMode = true;
683 bool InvalidFile = false;
684 const SourceManager& SM = CI.getSourceManager();
685 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
686 &InvalidFile);
687 if (!InvalidFile) {
688 const char *cur = Buffer->getBufferStart();
689 const char *end = Buffer->getBufferEnd();
690 const char *next = (cur != end) ? cur + 1 : end;
691
692 // Limit ourselves to only scanning 256 characters into the source
693 // file. This is mostly a sanity check in case the file has no
694 // newlines whatsoever.
695 if (end - cur > 256) end = cur + 256;
696
697 while (next < end) {
698 if (*cur == 0x0D) { // CR
699 if (*next == 0x0A) // CRLF
700 BinaryMode = false;
701
702 break;
703 } else if (*cur == 0x0A) // LF
704 break;
705
Richard Trieucc3949d2016-02-18 22:34:54 +0000706 ++cur;
707 ++next;
Douglas Gregor52da28b2011-09-23 23:43:36 +0000708 }
709 }
710
711 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar75546992009-12-03 09:13:30 +0000712 if (!OS) return;
713
Daniel Dunbar88357802009-11-14 10:42:57 +0000714 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
715 CI.getPreprocessorOutputOpts());
716}
Douglas Gregoraf82e352010-07-20 20:18:03 +0000717
718void PrintPreambleAction::ExecuteAction() {
719 switch (getCurrentFileKind()) {
720 case IK_C:
721 case IK_CXX:
722 case IK_ObjC:
723 case IK_ObjCXX:
724 case IK_OpenCL:
Peter Collingbourne62089b82010-12-01 03:15:20 +0000725 case IK_CUDA:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000726 break;
727
728 case IK_None:
729 case IK_Asm:
730 case IK_PreprocessedC:
Artem Belevich83a6dcc2015-03-19 17:32:06 +0000731 case IK_PreprocessedCuda:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000732 case IK_PreprocessedCXX:
733 case IK_PreprocessedObjC:
734 case IK_PreprocessedObjCXX:
735 case IK_AST:
736 case IK_LLVM_IR:
737 // We can't do anything with these.
738 return;
739 }
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000740
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000741 CompilerInstance &CI = getCompilerInstance();
Benjamin Kramera8857962014-10-26 22:44:13 +0000742 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
743 if (Buffer) {
Rafael Espindolacd0b3802014-08-12 15:46:24 +0000744 unsigned Preamble =
Benjamin Kramera8857962014-10-26 22:44:13 +0000745 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
746 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
Douglas Gregoraf82e352010-07-20 20:18:03 +0000747 }
748}