Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 1 | //===--- 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 Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/ASTConsumers.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/MultiplexConsumer.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 20 | #include "clang/Lex/PreprocessorOptions.h" |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 21 | #include "clang/Sema/TemplateInstCallback.h" |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 22 | #include "clang/Serialization/ASTReader.h" |
Sebastian Redl | 1914c6f | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 23 | #include "clang/Serialization/ASTWriter.h" |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/YAMLTraits.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 29 | #include <memory> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 30 | #include <system_error> |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 31 | |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) { |
| 36 | return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer() |
| 37 | : nullptr; |
| 38 | } |
| 39 | |
| 40 | void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) { |
| 41 | if (Action.hasCodeCompletionSupport() && |
| 42 | !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) |
| 43 | CI.createCodeCompletionConsumer(); |
| 44 | |
| 45 | if (!CI.hasSema()) |
| 46 | CI.createSema(Action.getTranslationUnitKind(), |
| 47 | GetCodeCompletionConsumer(CI)); |
| 48 | } |
| 49 | } // namespace |
| 50 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 51 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 1c201fb | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 52 | // Custom Actions |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 55 | std::unique_ptr<ASTConsumer> |
| 56 | InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 57 | return llvm::make_unique<ASTConsumer>(); |
Daniel Dunbar | 1c201fb | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void InitOnlyAction::ExecuteAction() { |
| 61 | } |
| 62 | |
| 63 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 64 | // AST Consumer Actions |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 67 | std::unique_ptr<ASTConsumer> |
| 68 | ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 69 | if (std::unique_ptr<raw_ostream> OS = |
| 70 | CI.createDefaultOutputFile(false, InFile)) |
| 71 | return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 72 | return nullptr; |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 73 | } |
| 74 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 75 | std::unique_ptr<ASTConsumer> |
| 76 | ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 77 | return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter, |
Richard Smith | 35f986d | 2014-08-11 22:11:07 +0000 | [diff] [blame] | 78 | CI.getFrontendOpts().ASTDumpDecls, |
Richard Smith | 3a36ac1 | 2017-03-09 22:00:01 +0000 | [diff] [blame] | 79 | CI.getFrontendOpts().ASTDumpAll, |
Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 80 | CI.getFrontendOpts().ASTDumpLookups); |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 81 | } |
| 82 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 83 | std::unique_ptr<ASTConsumer> |
| 84 | ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 85 | return CreateASTDeclNodeLister(); |
| 86 | } |
| 87 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 88 | std::unique_ptr<ASTConsumer> |
| 89 | ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 90 | return CreateASTViewer(); |
| 91 | } |
| 92 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 93 | std::unique_ptr<ASTConsumer> |
| 94 | DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 95 | StringRef InFile) { |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 96 | return CreateDeclContextPrinter(); |
| 97 | } |
| 98 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 99 | std::unique_ptr<ASTConsumer> |
| 100 | GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 101 | std::string Sysroot; |
Ilya Biryukov | 417085a | 2017-11-16 16:25:01 +0000 | [diff] [blame] | 102 | if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot)) |
| 103 | return nullptr; |
| 104 | |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 105 | std::string OutputFile; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 106 | std::unique_ptr<raw_pwrite_stream> OS = |
Ilya Biryukov | 417085a | 2017-11-16 16:25:01 +0000 | [diff] [blame] | 107 | CreateOutputFile(CI, InFile, /*ref*/ OutputFile); |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 108 | if (!OS) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 109 | return nullptr; |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | c567ba2 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 111 | if (!CI.getFrontendOpts().RelocatablePCH) |
| 112 | Sysroot.clear(); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 113 | |
| 114 | auto Buffer = std::make_shared<PCHBuffer>(); |
| 115 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
| 116 | Consumers.push_back(llvm::make_unique<PCHGenerator>( |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 117 | CI.getPreprocessor(), OutputFile, Sysroot, |
Pierre Gousseau | 533a893 | 2016-07-13 14:21:11 +0000 | [diff] [blame] | 118 | Buffer, CI.getFrontendOpts().ModuleFileExtensions, |
Argyrios Kyrtzidis | cf486b2 | 2017-02-27 03:52:36 +0000 | [diff] [blame] | 119 | /*AllowASTWithErrors*/CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, |
Pierre Gousseau | 533a893 | 2016-07-13 14:21:11 +0000 | [diff] [blame] | 120 | /*IncludeTimestamps*/ |
| 121 | +CI.getFrontendOpts().IncludeTimestamps)); |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 122 | Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 123 | CI, InFile, OutputFile, std::move(OS), Buffer)); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 124 | |
| 125 | return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Ilya Biryukov | 417085a | 2017-11-16 16:25:01 +0000 | [diff] [blame] | 128 | bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
| 129 | std::string &Sysroot) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 130 | Sysroot = CI.getHeaderSearchOpts().Sysroot; |
| 131 | if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { |
Sebastian Redl | ea68af4 | 2010-08-17 17:55:38 +0000 | [diff] [blame] | 132 | CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); |
Ilya Biryukov | 417085a | 2017-11-16 16:25:01 +0000 | [diff] [blame] | 133 | return false; |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Ilya Biryukov | 417085a | 2017-11-16 16:25:01 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
| 139 | std::unique_ptr<llvm::raw_pwrite_stream> |
| 140 | GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile, |
| 141 | std::string &OutputFile) { |
Daniel Dunbar | a2867c7 | 2011-01-31 22:00:44 +0000 | [diff] [blame] | 142 | // We use createOutputFile here because this is exposed via libclang, and we |
| 143 | // must disable the RemoveFileOnSignal behavior. |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 144 | // We use a temporary to avoid race conditions. |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 145 | std::unique_ptr<raw_pwrite_stream> OS = |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 146 | CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
| 147 | /*RemoveFileOnSignal=*/false, InFile, |
| 148 | /*Extension=*/"", /*useTemporary=*/true); |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 149 | if (!OS) |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 150 | return nullptr; |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 151 | |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 152 | OutputFile = CI.getFrontendOpts().OutputFile; |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 153 | return OS; |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Argyrios Kyrtzidis | cf486b2 | 2017-02-27 03:52:36 +0000 | [diff] [blame] | 156 | bool GeneratePCHAction::shouldEraseOutputFiles() { |
| 157 | if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors) |
| 158 | return false; |
| 159 | return ASTFrontendAction::shouldEraseOutputFiles(); |
| 160 | } |
| 161 | |
Richard Smith | d9259c2 | 2017-06-09 01:36:10 +0000 | [diff] [blame] | 162 | bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) { |
Manman Ren | ffd3e9d | 2017-01-09 19:20:18 +0000 | [diff] [blame] | 163 | CI.getLangOpts().CompilingPCH = true; |
| 164 | return true; |
| 165 | } |
| 166 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 167 | std::unique_ptr<ASTConsumer> |
| 168 | GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, |
| 169 | StringRef InFile) { |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 170 | std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile); |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 171 | if (!OS) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 172 | return nullptr; |
| 173 | |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 174 | std::string OutputFile = CI.getFrontendOpts().OutputFile; |
| 175 | std::string Sysroot; |
| 176 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 177 | auto Buffer = std::make_shared<PCHBuffer>(); |
| 178 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 179 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 180 | Consumers.push_back(llvm::make_unique<PCHGenerator>( |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 181 | CI.getPreprocessor(), OutputFile, Sysroot, |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 182 | Buffer, CI.getFrontendOpts().ModuleFileExtensions, |
| 183 | /*AllowASTWithErrors=*/false, |
| 184 | /*IncludeTimestamps=*/ |
| 185 | +CI.getFrontendOpts().BuildingImplicitModule)); |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 186 | Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 187 | CI, InFile, OutputFile, std::move(OS), Buffer)); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 188 | return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Richard Smith | 1f2bd35 | 2017-07-06 21:05:56 +0000 | [diff] [blame] | 191 | bool GenerateModuleFromModuleMapAction::BeginSourceFileAction( |
| 192 | CompilerInstance &CI) { |
| 193 | if (!CI.getLangOpts().Modules) { |
| 194 | CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules); |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | return GenerateModuleAction::BeginSourceFileAction(CI); |
| 199 | } |
| 200 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 201 | std::unique_ptr<raw_pwrite_stream> |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 202 | GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI, |
| 203 | StringRef InFile) { |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 204 | // If no output file was provided, figure out where this module would go |
| 205 | // in the module cache. |
| 206 | if (CI.getFrontendOpts().OutputFile.empty()) { |
Richard Smith | f74d946 | 2017-04-28 01:49:42 +0000 | [diff] [blame] | 207 | StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap; |
| 208 | if (ModuleMapFile.empty()) |
| 209 | ModuleMapFile = InFile; |
| 210 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 211 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 212 | CI.getFrontendOpts().OutputFile = |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 213 | HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule, |
| 214 | ModuleMapFile); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 215 | } |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 217 | // We use createOutputFile here because this is exposed via libclang, and we |
| 218 | // must disable the RemoveFileOnSignal behavior. |
| 219 | // We use a temporary to avoid race conditions. |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 220 | return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
| 221 | /*RemoveFileOnSignal=*/false, InFile, |
| 222 | /*Extension=*/"", /*useTemporary=*/true, |
| 223 | /*CreateMissingDirectories=*/true); |
| 224 | } |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 225 | |
Richard Smith | d9259c2 | 2017-06-09 01:36:10 +0000 | [diff] [blame] | 226 | bool GenerateModuleInterfaceAction::BeginSourceFileAction( |
| 227 | CompilerInstance &CI) { |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 228 | if (!CI.getLangOpts().ModulesTS) { |
| 229 | CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface); |
| 234 | |
Richard Smith | d9259c2 | 2017-06-09 01:36:10 +0000 | [diff] [blame] | 235 | return GenerateModuleAction::BeginSourceFileAction(CI); |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | std::unique_ptr<raw_pwrite_stream> |
| 239 | GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI, |
| 240 | StringRef InFile) { |
| 241 | return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm"); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Etienne Bergeron | 98de805 | 2016-05-12 19:51:18 +0000 | [diff] [blame] | 244 | SyntaxOnlyAction::~SyntaxOnlyAction() { |
| 245 | } |
| 246 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 247 | std::unique_ptr<ASTConsumer> |
| 248 | SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 249 | return llvm::make_unique<ASTConsumer>(); |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 250 | } |
| 251 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 252 | std::unique_ptr<ASTConsumer> |
| 253 | DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, |
| 254 | StringRef InFile) { |
| 255 | return llvm::make_unique<ASTConsumer>(); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 256 | } |
| 257 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 258 | std::unique_ptr<ASTConsumer> |
| 259 | VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 260 | return llvm::make_unique<ASTConsumer>(); |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void VerifyPCHAction::ExecuteAction() { |
Ben Langmuir | 3d4417c | 2014-02-07 17:31:11 +0000 | [diff] [blame] | 264 | CompilerInstance &CI = getCompilerInstance(); |
| 265 | bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
| 266 | const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 267 | std::unique_ptr<ASTReader> Reader(new ASTReader( |
Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 268 | CI.getPreprocessor(), &CI.getASTContext(), CI.getPCHContainerReader(), |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 269 | CI.getFrontendOpts().ModuleFileExtensions, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 270 | Sysroot.empty() ? "" : Sysroot.c_str(), |
| 271 | /*DisableValidation*/ false, |
| 272 | /*AllowPCHWithCompilerErrors*/ false, |
| 273 | /*AllowConfigurationMismatch*/ true, |
| 274 | /*ValidateSystemInputs*/ true)); |
Ben Langmuir | 3d4417c | 2014-02-07 17:31:11 +0000 | [diff] [blame] | 275 | |
| 276 | Reader->ReadAST(getCurrentFile(), |
| 277 | Preamble ? serialization::MK_Preamble |
| 278 | : serialization::MK_PCH, |
| 279 | SourceLocation(), |
| 280 | ASTReader::ARR_ConfigurationMismatch); |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 283 | namespace { |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 284 | struct TemplightEntry { |
| 285 | std::string Name; |
| 286 | std::string Kind; |
| 287 | std::string Event; |
| 288 | std::string DefinitionLocation; |
| 289 | std::string PointOfInstantiation; |
| 290 | }; |
| 291 | } // namespace |
| 292 | |
| 293 | namespace llvm { |
| 294 | namespace yaml { |
| 295 | template <> struct MappingTraits<TemplightEntry> { |
| 296 | static void mapping(IO &io, TemplightEntry &fields) { |
| 297 | io.mapRequired("name", fields.Name); |
| 298 | io.mapRequired("kind", fields.Kind); |
| 299 | io.mapRequired("event", fields.Event); |
| 300 | io.mapRequired("orig", fields.DefinitionLocation); |
| 301 | io.mapRequired("poi", fields.PointOfInstantiation); |
| 302 | } |
| 303 | }; |
| 304 | } // namespace yaml |
| 305 | } // namespace llvm |
| 306 | |
| 307 | namespace { |
| 308 | class DefaultTemplateInstCallback : public TemplateInstantiationCallback { |
| 309 | using CodeSynthesisContext = Sema::CodeSynthesisContext; |
| 310 | |
| 311 | public: |
Gabor Horvath | 6e0dbb0 | 2018-02-10 14:26:53 +0000 | [diff] [blame^] | 312 | void initialize(const Sema &) override {} |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 313 | |
Gabor Horvath | 6e0dbb0 | 2018-02-10 14:26:53 +0000 | [diff] [blame^] | 314 | void finalize(const Sema &) override {} |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 315 | |
Gabor Horvath | 6e0dbb0 | 2018-02-10 14:26:53 +0000 | [diff] [blame^] | 316 | void atTemplateBegin(const Sema &TheSema, |
| 317 | const CodeSynthesisContext &Inst) override { |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 318 | displayTemplightEntry<true>(llvm::outs(), TheSema, Inst); |
| 319 | } |
| 320 | |
Gabor Horvath | 6e0dbb0 | 2018-02-10 14:26:53 +0000 | [diff] [blame^] | 321 | void atTemplateEnd(const Sema &TheSema, |
| 322 | const CodeSynthesisContext &Inst) override { |
Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 323 | displayTemplightEntry<false>(llvm::outs(), TheSema, Inst); |
| 324 | } |
| 325 | |
| 326 | private: |
| 327 | static std::string toString(CodeSynthesisContext::SynthesisKind Kind) { |
| 328 | switch (Kind) { |
| 329 | case CodeSynthesisContext::TemplateInstantiation: |
| 330 | return "TemplateInstantiation"; |
| 331 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: |
| 332 | return "DefaultTemplateArgumentInstantiation"; |
| 333 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: |
| 334 | return "DefaultFunctionArgumentInstantiation"; |
| 335 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: |
| 336 | return "ExplicitTemplateArgumentSubstitution"; |
| 337 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: |
| 338 | return "DeducedTemplateArgumentSubstitution"; |
| 339 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: |
| 340 | return "PriorTemplateArgumentSubstitution"; |
| 341 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: |
| 342 | return "DefaultTemplateArgumentChecking"; |
| 343 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
| 344 | return "ExceptionSpecInstantiation"; |
| 345 | case CodeSynthesisContext::DeclaringSpecialMember: |
| 346 | return "DeclaringSpecialMember"; |
| 347 | case CodeSynthesisContext::DefiningSynthesizedFunction: |
| 348 | return "DefiningSynthesizedFunction"; |
| 349 | case CodeSynthesisContext::Memoization: |
| 350 | return "Memoization"; |
| 351 | } |
| 352 | return ""; |
| 353 | } |
| 354 | |
| 355 | template <bool BeginInstantiation> |
| 356 | static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema, |
| 357 | const CodeSynthesisContext &Inst) { |
| 358 | std::string YAML; |
| 359 | { |
| 360 | llvm::raw_string_ostream OS(YAML); |
| 361 | llvm::yaml::Output YO(OS); |
| 362 | TemplightEntry Entry = |
| 363 | getTemplightEntry<BeginInstantiation>(TheSema, Inst); |
| 364 | llvm::yaml::EmptyContext Context; |
| 365 | llvm::yaml::yamlize(YO, Entry, true, Context); |
| 366 | } |
| 367 | Out << "---" << YAML << "\n"; |
| 368 | } |
| 369 | |
| 370 | template <bool BeginInstantiation> |
| 371 | static TemplightEntry getTemplightEntry(const Sema &TheSema, |
| 372 | const CodeSynthesisContext &Inst) { |
| 373 | TemplightEntry Entry; |
| 374 | Entry.Kind = toString(Inst.Kind); |
| 375 | Entry.Event = BeginInstantiation ? "Begin" : "End"; |
| 376 | if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) { |
| 377 | llvm::raw_string_ostream OS(Entry.Name); |
| 378 | NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true); |
| 379 | const PresumedLoc DefLoc = |
| 380 | TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation()); |
| 381 | if(!DefLoc.isInvalid()) |
| 382 | Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" + |
| 383 | std::to_string(DefLoc.getLine()) + ":" + |
| 384 | std::to_string(DefLoc.getColumn()); |
| 385 | } |
| 386 | const PresumedLoc PoiLoc = |
| 387 | TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation); |
| 388 | if (!PoiLoc.isInvalid()) { |
| 389 | Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" + |
| 390 | std::to_string(PoiLoc.getLine()) + ":" + |
| 391 | std::to_string(PoiLoc.getColumn()); |
| 392 | } |
| 393 | return Entry; |
| 394 | } |
| 395 | }; |
| 396 | } // namespace |
| 397 | |
| 398 | std::unique_ptr<ASTConsumer> |
| 399 | TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 400 | return llvm::make_unique<ASTConsumer>(); |
| 401 | } |
| 402 | |
| 403 | void TemplightDumpAction::ExecuteAction() { |
| 404 | CompilerInstance &CI = getCompilerInstance(); |
| 405 | |
| 406 | // This part is normally done by ASTFrontEndAction, but needs to happen |
| 407 | // before Templight observers can be created |
| 408 | // FIXME: Move the truncation aspect of this into Sema, we delayed this till |
| 409 | // here so the source manager would be initialized. |
| 410 | EnsureSemaIsCreated(CI, *this); |
| 411 | |
| 412 | CI.getSema().TemplateInstCallbacks.push_back( |
| 413 | llvm::make_unique<DefaultTemplateInstCallback>()); |
| 414 | ASTFrontendAction::ExecuteAction(); |
| 415 | } |
| 416 | |
| 417 | namespace { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 418 | /// \brief AST reader listener that dumps module information for a module |
| 419 | /// file. |
| 420 | class DumpModuleInfoListener : public ASTReaderListener { |
| 421 | llvm::raw_ostream &Out; |
| 422 | |
| 423 | public: |
| 424 | DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { } |
| 425 | |
| 426 | #define DUMP_BOOLEAN(Value, Text) \ |
| 427 | Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n" |
| 428 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 429 | bool ReadFullVersionInformation(StringRef FullVersion) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 430 | Out.indent(2) |
| 431 | << "Generated by " |
| 432 | << (FullVersion == getClangFullRepositoryVersion()? "this" |
| 433 | : "a different") |
| 434 | << " Clang: " << FullVersion << "\n"; |
| 435 | return ASTReaderListener::ReadFullVersionInformation(FullVersion); |
| 436 | } |
| 437 | |
Ben Langmuir | 4f5212a | 2014-04-14 22:12:44 +0000 | [diff] [blame] | 438 | void ReadModuleName(StringRef ModuleName) override { |
| 439 | Out.indent(2) << "Module name: " << ModuleName << "\n"; |
| 440 | } |
| 441 | void ReadModuleMapFile(StringRef ModuleMapPath) override { |
| 442 | Out.indent(2) << "Module map file: " << ModuleMapPath << "\n"; |
| 443 | } |
| 444 | |
Richard Smith | 1e2cf0d | 2014-10-31 02:28:58 +0000 | [diff] [blame] | 445 | bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, |
| 446 | bool AllowCompatibleDifferences) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 447 | Out.indent(2) << "Language options:\n"; |
| 448 | #define LANGOPT(Name, Bits, Default, Description) \ |
| 449 | DUMP_BOOLEAN(LangOpts.Name, Description); |
| 450 | #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ |
| 451 | Out.indent(4) << Description << ": " \ |
| 452 | << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; |
| 453 | #define VALUE_LANGOPT(Name, Bits, Default, Description) \ |
| 454 | Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; |
| 455 | #define BENIGN_LANGOPT(Name, Bits, Default, Description) |
| 456 | #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) |
| 457 | #include "clang/Basic/LangOptions.def" |
Ben Langmuir | cd98cb7 | 2015-06-23 18:20:18 +0000 | [diff] [blame] | 458 | |
| 459 | if (!LangOpts.ModuleFeatures.empty()) { |
| 460 | Out.indent(4) << "Module features:\n"; |
| 461 | for (StringRef Feature : LangOpts.ModuleFeatures) |
| 462 | Out.indent(6) << Feature << "\n"; |
| 463 | } |
| 464 | |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 465 | return false; |
| 466 | } |
| 467 | |
Chandler Carruth | 0d745bc | 2015-03-14 04:47:43 +0000 | [diff] [blame] | 468 | bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, |
| 469 | bool AllowCompatibleDifferences) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 470 | Out.indent(2) << "Target options:\n"; |
| 471 | Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n"; |
| 472 | Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n"; |
| 473 | Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n"; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 474 | |
| 475 | if (!TargetOpts.FeaturesAsWritten.empty()) { |
| 476 | Out.indent(4) << "Target features:\n"; |
| 477 | for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); |
| 478 | I != N; ++I) { |
| 479 | Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n"; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return false; |
| 484 | } |
| 485 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 486 | bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, |
| 487 | bool Complain) override { |
Ben Langmuir | b92de02 | 2014-04-29 16:25:26 +0000 | [diff] [blame] | 488 | Out.indent(2) << "Diagnostic options:\n"; |
| 489 | #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); |
| 490 | #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ |
| 491 | Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; |
| 492 | #define VALUE_DIAGOPT(Name, Bits, Default) \ |
| 493 | Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; |
| 494 | #include "clang/Basic/DiagnosticOptions.def" |
| 495 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 496 | Out.indent(4) << "Diagnostic flags:\n"; |
| 497 | for (const std::string &Warning : DiagOpts->Warnings) |
Ben Langmuir | b92de02 | 2014-04-29 16:25:26 +0000 | [diff] [blame] | 498 | Out.indent(6) << "-W" << Warning << "\n"; |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 499 | for (const std::string &Remark : DiagOpts->Remarks) |
| 500 | Out.indent(6) << "-R" << Remark << "\n"; |
Ben Langmuir | b92de02 | 2014-04-29 16:25:26 +0000 | [diff] [blame] | 501 | |
| 502 | return false; |
| 503 | } |
| 504 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 505 | bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 506 | StringRef SpecificModuleCachePath, |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 507 | bool Complain) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 508 | Out.indent(2) << "Header search options:\n"; |
| 509 | Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n"; |
Argyrios Kyrtzidis | bdff27d | 2017-02-25 18:14:31 +0000 | [diff] [blame] | 510 | Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n"; |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 511 | Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n"; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 512 | DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes, |
| 513 | "Use builtin include directories [-nobuiltininc]"); |
| 514 | DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes, |
| 515 | "Use standard system include directories [-nostdinc]"); |
| 516 | DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes, |
| 517 | "Use standard C++ include directories [-nostdinc++]"); |
| 518 | DUMP_BOOLEAN(HSOpts.UseLibcxx, |
| 519 | "Use libc++ (rather than libstdc++) [-stdlib=]"); |
| 520 | return false; |
| 521 | } |
| 522 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 523 | bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, |
| 524 | bool Complain, |
| 525 | std::string &SuggestedPredefines) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 526 | Out.indent(2) << "Preprocessor options:\n"; |
| 527 | DUMP_BOOLEAN(PPOpts.UsePredefines, |
| 528 | "Uses compiler/target-specific predefines [-undef]"); |
| 529 | DUMP_BOOLEAN(PPOpts.DetailedRecord, |
| 530 | "Uses detailed preprocessing record (for indexing)"); |
| 531 | |
| 532 | if (!PPOpts.Macros.empty()) { |
| 533 | Out.indent(4) << "Predefined macros:\n"; |
| 534 | } |
| 535 | |
| 536 | for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator |
| 537 | I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end(); |
| 538 | I != IEnd; ++I) { |
| 539 | Out.indent(6); |
| 540 | if (I->second) |
| 541 | Out << "-U"; |
| 542 | else |
| 543 | Out << "-D"; |
| 544 | Out << I->first << "\n"; |
| 545 | } |
| 546 | return false; |
| 547 | } |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 548 | |
| 549 | /// Indicates that a particular module file extension has been read. |
| 550 | void readModuleFileExtension( |
| 551 | const ModuleFileExtensionMetadata &Metadata) override { |
| 552 | Out.indent(2) << "Module file extension '" |
| 553 | << Metadata.BlockName << "' " << Metadata.MajorVersion |
| 554 | << "." << Metadata.MinorVersion; |
| 555 | if (!Metadata.UserInfo.empty()) { |
| 556 | Out << ": "; |
| 557 | Out.write_escaped(Metadata.UserInfo); |
| 558 | } |
| 559 | |
| 560 | Out << "\n"; |
| 561 | } |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 562 | #undef DUMP_BOOLEAN |
| 563 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 564 | } |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 565 | |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 566 | bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) { |
| 567 | // The Object file reader also supports raw ast files and there is no point in |
| 568 | // being strict about the module file format in -module-file-info mode. |
| 569 | CI.getHeaderSearchOpts().ModuleFormat = "obj"; |
| 570 | return true; |
| 571 | } |
| 572 | |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 573 | void DumpModuleInfoAction::ExecuteAction() { |
| 574 | // Set up the output file. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 575 | std::unique_ptr<llvm::raw_fd_ostream> OutFile; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 576 | StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; |
| 577 | if (!OutputFileName.empty() && OutputFileName != "-") { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 578 | std::error_code EC; |
| 579 | OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, |
| 580 | llvm::sys::fs::F_Text)); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 581 | } |
| 582 | llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); |
| 583 | |
| 584 | Out << "Information for module file '" << getCurrentFile() << "':\n"; |
Adrian Prantl | 99e765b | 2016-08-17 23:14:00 +0000 | [diff] [blame] | 585 | auto &FileMgr = getCompilerInstance().getFileManager(); |
| 586 | auto Buffer = FileMgr.getBufferForFile(getCurrentFile()); |
| 587 | StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer(); |
| 588 | bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' && |
| 589 | Magic[2] == 'C' && Magic[3] == 'H'); |
| 590 | Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n"; |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 591 | |
Manman Ren | 47a4445 | 2016-07-26 17:12:17 +0000 | [diff] [blame] | 592 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 593 | DumpModuleInfoListener Listener(Out); |
Manman Ren | 47a4445 | 2016-07-26 17:12:17 +0000 | [diff] [blame] | 594 | HeaderSearchOptions &HSOpts = |
| 595 | PP.getHeaderSearchInfo().getHeaderSearchOpts(); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 596 | ASTReader::readASTFileControlBlock( |
Adrian Prantl | 99e765b | 2016-08-17 23:14:00 +0000 | [diff] [blame] | 597 | getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(), |
Manman Ren | 47a4445 | 2016-07-26 17:12:17 +0000 | [diff] [blame] | 598 | /*FindModuleFileExtensions=*/true, Listener, |
| 599 | HSOpts.ModulesValidateDiagnosticOptions); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 602 | //===----------------------------------------------------------------------===// |
| 603 | // Preprocessor Actions |
| 604 | //===----------------------------------------------------------------------===// |
| 605 | |
| 606 | void DumpRawTokensAction::ExecuteAction() { |
| 607 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 608 | SourceManager &SM = PP.getSourceManager(); |
| 609 | |
| 610 | // Start lexing the specified input file. |
Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 611 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 612 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 613 | RawLex.SetKeepWhitespaceMode(true); |
| 614 | |
| 615 | Token RawTok; |
| 616 | RawLex.LexFromRawLexer(RawTok); |
| 617 | while (RawTok.isNot(tok::eof)) { |
| 618 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 7502462 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 619 | llvm::errs() << "\n"; |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 620 | RawLex.LexFromRawLexer(RawTok); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void DumpTokensAction::ExecuteAction() { |
| 625 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 626 | // Start preprocessing the specified input file. |
| 627 | Token Tok; |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 628 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 629 | do { |
| 630 | PP.Lex(Tok); |
| 631 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 7502462 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 632 | llvm::errs() << "\n"; |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 633 | } while (Tok.isNot(tok::eof)); |
| 634 | } |
| 635 | |
| 636 | void GeneratePTHAction::ExecuteAction() { |
| 637 | CompilerInstance &CI = getCompilerInstance(); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 638 | std::unique_ptr<raw_pwrite_stream> OS = |
| 639 | CI.createDefaultOutputFile(true, getCurrentFile()); |
NAKAMURA Takumi | 5594d79 | 2015-04-13 08:43:40 +0000 | [diff] [blame] | 640 | if (!OS) |
| 641 | return; |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 642 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 643 | CacheTokens(CI.getPreprocessor(), OS.get()); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 646 | void PreprocessOnlyAction::ExecuteAction() { |
| 647 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 648 | |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 649 | // Ignore unknown pragmas. |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 650 | PP.IgnorePragmas(); |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 651 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 652 | Token Tok; |
| 653 | // Start parsing the specified input file. |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 654 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 655 | do { |
| 656 | PP.Lex(Tok); |
| 657 | } while (Tok.isNot(tok::eof)); |
| 658 | } |
| 659 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 660 | void PrintPreprocessedAction::ExecuteAction() { |
| 661 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 662 | // Output file may need to be set to 'Binary', to avoid converting Unix style |
Steve Naroff | 3d38a68 | 2010-01-05 17:33:23 +0000 | [diff] [blame] | 663 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 664 | // |
| 665 | // Look to see what type of line endings the file uses. If there's a |
| 666 | // CRLF, then we won't open the file up in binary mode. If there is |
| 667 | // just an LF or CR, then we will open the file up in binary mode. |
| 668 | // In this fashion, the output format should match the input format, unless |
| 669 | // the input format has inconsistent line endings. |
| 670 | // |
| 671 | // This should be a relatively fast operation since most files won't have |
| 672 | // all of their source code on a single line. However, that is still a |
| 673 | // concern, so if we scan for too long, we'll just assume the file should |
| 674 | // be opened in binary mode. |
| 675 | bool BinaryMode = true; |
| 676 | bool InvalidFile = false; |
| 677 | const SourceManager& SM = CI.getSourceManager(); |
| 678 | const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), |
| 679 | &InvalidFile); |
| 680 | if (!InvalidFile) { |
| 681 | const char *cur = Buffer->getBufferStart(); |
| 682 | const char *end = Buffer->getBufferEnd(); |
| 683 | const char *next = (cur != end) ? cur + 1 : end; |
| 684 | |
| 685 | // Limit ourselves to only scanning 256 characters into the source |
| 686 | // file. This is mostly a sanity check in case the file has no |
| 687 | // newlines whatsoever. |
| 688 | if (end - cur > 256) end = cur + 256; |
Aaron Ballman | 4fc36ae | 2017-06-16 20:52:59 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 690 | while (next < end) { |
| 691 | if (*cur == 0x0D) { // CR |
| 692 | if (*next == 0x0A) // CRLF |
| 693 | BinaryMode = false; |
| 694 | |
| 695 | break; |
| 696 | } else if (*cur == 0x0A) // LF |
| 697 | break; |
| 698 | |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 699 | ++cur; |
| 700 | ++next; |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 704 | std::unique_ptr<raw_ostream> OS = |
| 705 | CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 706 | if (!OS) return; |
| 707 | |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 708 | // If we're preprocessing a module map, start by dumping the contents of the |
| 709 | // module itself before switching to the input buffer. |
| 710 | auto &Input = getCurrentInput(); |
| 711 | if (Input.getKind().getFormat() == InputKind::ModuleMap) { |
Richard Smith | c784e96 | 2017-06-01 20:10:35 +0000 | [diff] [blame] | 712 | if (Input.isFile()) { |
| 713 | (*OS) << "# 1 \""; |
| 714 | OS->write_escaped(Input.getFile()); |
| 715 | (*OS) << "\"\n"; |
| 716 | } |
Richard Smith | 8128f33 | 2017-05-05 22:18:51 +0000 | [diff] [blame] | 717 | // FIXME: Include additional information here so that we don't need the |
| 718 | // original source files to exist on disk. |
| 719 | getCurrentModule()->print(*OS); |
| 720 | (*OS) << "#pragma clang module contents\n"; |
| 721 | } |
| 722 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 723 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(), |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 724 | CI.getPreprocessorOutputOpts()); |
| 725 | } |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 726 | |
| 727 | void PrintPreambleAction::ExecuteAction() { |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 728 | switch (getCurrentFileKind().getLanguage()) { |
| 729 | case InputKind::C: |
| 730 | case InputKind::CXX: |
| 731 | case InputKind::ObjC: |
| 732 | case InputKind::ObjCXX: |
| 733 | case InputKind::OpenCL: |
| 734 | case InputKind::CUDA: |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 735 | break; |
| 736 | |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 737 | case InputKind::Unknown: |
| 738 | case InputKind::Asm: |
| 739 | case InputKind::LLVM_IR: |
| 740 | case InputKind::RenderScript: |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 741 | // We can't do anything with these. |
| 742 | return; |
| 743 | } |
Rafael Espindola | 6406f7b | 2014-08-26 19:54:40 +0000 | [diff] [blame] | 744 | |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 745 | // We don't expect to find any #include directives in a preprocessed input. |
| 746 | if (getCurrentFileKind().isPreprocessed()) |
| 747 | return; |
| 748 | |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 749 | CompilerInstance &CI = getCompilerInstance(); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 750 | auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); |
| 751 | if (Buffer) { |
Rafael Espindola | cd0b380 | 2014-08-12 15:46:24 +0000 | [diff] [blame] | 752 | unsigned Preamble = |
Cameron Desrochers | 84fd064 | 2017-09-20 19:03:37 +0000 | [diff] [blame] | 753 | Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size; |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 754 | llvm::outs().write((*Buffer)->getBufferStart(), Preamble); |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 755 | } |
| 756 | } |