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