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