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