Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 1 | //===--- FrontendActions.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/FrontendActions.h" |
| 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/Basic/FileManager.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 13 | #include "clang/Frontend/ASTConsumers.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/FrontendDiagnostic.h" |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 16 | #include "clang/Frontend/MultiplexConsumer.h" |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/Utils.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 20 | #include "clang/Lex/PreprocessorOptions.h" |
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" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 27 | #include <memory> |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 28 | #include <system_error> |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 29 | |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 1c201fb | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 33 | // Custom Actions |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 36 | std::unique_ptr<ASTConsumer> |
| 37 | InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 38 | return llvm::make_unique<ASTConsumer>(); |
Daniel Dunbar | 1c201fb | 2010-03-19 19:44:04 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void InitOnlyAction::ExecuteAction() { |
| 42 | } |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 45 | // AST Consumer Actions |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 48 | std::unique_ptr<ASTConsumer> |
| 49 | ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 50 | if (std::unique_ptr<raw_ostream> OS = |
| 51 | CI.createDefaultOutputFile(false, InFile)) |
| 52 | return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 53 | return nullptr; |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 56 | std::unique_ptr<ASTConsumer> |
| 57 | ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 58 | return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter, |
Richard Smith | 35f986d | 2014-08-11 22:11:07 +0000 | [diff] [blame] | 59 | CI.getFrontendOpts().ASTDumpDecls, |
Richard Smith | 6ea0582 | 2013-06-24 01:45:33 +0000 | [diff] [blame] | 60 | CI.getFrontendOpts().ASTDumpLookups); |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 63 | std::unique_ptr<ASTConsumer> |
| 64 | ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Alexander Kornienko | 4de0359 | 2012-07-31 09:37:40 +0000 | [diff] [blame] | 65 | return CreateASTDeclNodeLister(); |
| 66 | } |
| 67 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 68 | std::unique_ptr<ASTConsumer> |
| 69 | ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 70 | return CreateASTViewer(); |
| 71 | } |
| 72 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 73 | std::unique_ptr<ASTConsumer> |
| 74 | DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, |
| 75 | StringRef InFile) { |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 76 | return CreateDeclContextPrinter(); |
| 77 | } |
| 78 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 79 | std::unique_ptr<ASTConsumer> |
| 80 | GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 81 | std::string Sysroot; |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 82 | std::string OutputFile; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 83 | std::unique_ptr<raw_pwrite_stream> OS = |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 84 | ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile); |
| 85 | if (!OS) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 86 | return nullptr; |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | c567ba2 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 88 | if (!CI.getFrontendOpts().RelocatablePCH) |
| 89 | Sysroot.clear(); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 90 | |
| 91 | auto Buffer = std::make_shared<PCHBuffer>(); |
| 92 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
| 93 | Consumers.push_back(llvm::make_unique<PCHGenerator>( |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 94 | CI.getPreprocessor(), OutputFile, nullptr, Sysroot, |
Pierre Gousseau | 533a893 | 2016-07-13 14:21:11 +0000 | [diff] [blame] | 95 | Buffer, CI.getFrontendOpts().ModuleFileExtensions, |
| 96 | /*AllowASTWithErrors*/false, |
| 97 | /*IncludeTimestamps*/ |
| 98 | +CI.getFrontendOpts().IncludeTimestamps)); |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 99 | Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 100 | CI, InFile, OutputFile, std::move(OS), Buffer)); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 101 | |
| 102 | return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 105 | std::unique_ptr<raw_pwrite_stream> |
| 106 | GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
| 107 | StringRef InFile, |
| 108 | std::string &Sysroot, |
| 109 | std::string &OutputFile) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 110 | Sysroot = CI.getHeaderSearchOpts().Sysroot; |
| 111 | if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { |
Sebastian Redl | ea68af4 | 2010-08-17 17:55:38 +0000 | [diff] [blame] | 112 | CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 113 | return nullptr; |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Daniel Dunbar | a2867c7 | 2011-01-31 22:00:44 +0000 | [diff] [blame] | 116 | // We use createOutputFile here because this is exposed via libclang, and we |
| 117 | // must disable the RemoveFileOnSignal behavior. |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 118 | // We use a temporary to avoid race conditions. |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 119 | std::unique_ptr<raw_pwrite_stream> OS = |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 120 | CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
| 121 | /*RemoveFileOnSignal=*/false, InFile, |
| 122 | /*Extension=*/"", /*useTemporary=*/true); |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 123 | if (!OS) |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 124 | return nullptr; |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 125 | |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 126 | OutputFile = CI.getFrontendOpts().OutputFile; |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 127 | return OS; |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 130 | std::unique_ptr<ASTConsumer> |
| 131 | GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, |
| 132 | StringRef InFile) { |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 133 | std::string Sysroot; |
| 134 | std::string OutputFile; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 135 | std::unique_ptr<raw_pwrite_stream> OS = |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 136 | ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile); |
| 137 | if (!OS) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 138 | return nullptr; |
| 139 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 140 | auto Buffer = std::make_shared<PCHBuffer>(); |
| 141 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 142 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 143 | Consumers.push_back(llvm::make_unique<PCHGenerator>( |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 144 | CI.getPreprocessor(), OutputFile, Module, Sysroot, |
| 145 | Buffer, CI.getFrontendOpts().ModuleFileExtensions, |
| 146 | /*AllowASTWithErrors=*/false, |
| 147 | /*IncludeTimestamps=*/ |
| 148 | +CI.getFrontendOpts().BuildingImplicitModule)); |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 149 | Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 150 | CI, InFile, OutputFile, std::move(OS), Buffer)); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 151 | return llvm::make_unique<MultiplexConsumer>(std::move(Consumers)); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Argyrios Kyrtzidis | 130190f | 2012-10-10 02:12:39 +0000 | [diff] [blame] | 154 | static SmallVectorImpl<char> & |
| 155 | operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { |
| 156 | Includes.append(RHS.begin(), RHS.end()); |
| 157 | return Includes; |
| 158 | } |
| 159 | |
Davide Italiano | 75df821 | 2016-03-02 06:09:18 +0000 | [diff] [blame] | 160 | static void addHeaderInclude(StringRef HeaderName, |
| 161 | SmallVectorImpl<char> &Includes, |
| 162 | const LangOptions &LangOpts, |
| 163 | bool IsExternC) { |
Richard Smith | 9bca298 | 2014-03-08 00:03:56 +0000 | [diff] [blame] | 164 | if (IsExternC && LangOpts.CPlusPlus) |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 165 | Includes += "extern \"C\" {\n"; |
Argyrios Kyrtzidis | 130190f | 2012-10-10 02:12:39 +0000 | [diff] [blame] | 166 | if (LangOpts.ObjC1) |
| 167 | Includes += "#import \""; |
| 168 | else |
| 169 | Includes += "#include \""; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 170 | |
| 171 | Includes += HeaderName; |
| 172 | |
Argyrios Kyrtzidis | 130190f | 2012-10-10 02:12:39 +0000 | [diff] [blame] | 173 | Includes += "\"\n"; |
Richard Smith | 9bca298 | 2014-03-08 00:03:56 +0000 | [diff] [blame] | 174 | if (IsExternC && LangOpts.CPlusPlus) |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 175 | Includes += "}\n"; |
Argyrios Kyrtzidis | 130190f | 2012-10-10 02:12:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 178 | /// \brief Collect the set of header includes needed to construct the given |
Argyrios Kyrtzidis | c597c8c | 2012-10-05 00:22:33 +0000 | [diff] [blame] | 179 | /// module and update the TopHeaders file set of the module. |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 180 | /// |
| 181 | /// \param Module The module we're collecting includes from. |
Douglas Gregor | e5626c4 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 182 | /// |
James Dennett | c423c53 | 2012-06-15 21:48:19 +0000 | [diff] [blame] | 183 | /// \param Includes Will be augmented with the set of \#includes or \#imports |
Douglas Gregor | e5626c4 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 184 | /// needed to load all of the named headers. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 185 | static std::error_code |
Richard Smith | 723928c | 2014-03-11 02:02:47 +0000 | [diff] [blame] | 186 | collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, |
| 187 | ModuleMap &ModMap, clang::Module *Module, |
| 188 | SmallVectorImpl<char> &Includes) { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 189 | // Don't collect any headers for unavailable modules. |
| 190 | if (!Module->isAvailable()) |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 191 | return std::error_code(); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | e5626c4 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 193 | // Add includes for each of these headers. |
Richard Smith | f7aeda1 | 2016-01-08 22:36:45 +0000 | [diff] [blame] | 194 | for (auto HK : {Module::HK_Normal, Module::HK_Private}) { |
| 195 | for (Module::Header &H : Module->Headers[HK]) { |
| 196 | Module->addTopHeader(H.Entry); |
| 197 | // Use the path as specified in the module map file. We'll look for this |
| 198 | // file relative to the module build directory (the directory containing |
| 199 | // the module map file) so this will find the same file that we found |
| 200 | // while parsing the module map. |
Davide Italiano | 75df821 | 2016-03-02 06:09:18 +0000 | [diff] [blame] | 201 | addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC); |
Richard Smith | f7aeda1 | 2016-01-08 22:36:45 +0000 | [diff] [blame] | 202 | } |
Douglas Gregor | 3ec6663 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 203 | } |
Lawrence Crowl | b53e548 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 204 | // Note that Module->PrivateHeaders will not be a TopHeader. |
Douglas Gregor | e5626c4 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 205 | |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 206 | if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) { |
| 207 | Module->addTopHeader(UmbrellaHeader.Entry); |
Davide Italiano | 75df821 | 2016-03-02 06:09:18 +0000 | [diff] [blame] | 208 | if (Module->Parent) |
Douglas Gregor | 3ec6663 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 209 | // Include the umbrella header for submodules. |
Davide Italiano | 75df821 | 2016-03-02 06:09:18 +0000 | [diff] [blame] | 210 | addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts, |
| 211 | Module->IsExternC); |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 212 | } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) { |
Douglas Gregor | d2a8fe1 | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 213 | // Add all of the headers we find in this subdirectory. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 214 | std::error_code EC; |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 215 | SmallString<128> DirNative; |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 216 | llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative); |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 217 | |
| 218 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
| 219 | for (vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End; |
| 220 | Dir != End && !EC; Dir.increment(EC)) { |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 221 | // Check whether this entry has an extension typically associated with |
| 222 | // headers. |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 223 | if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName())) |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 224 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 225 | .Default(false)) |
| 226 | continue; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 227 | |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 228 | const FileEntry *Header = FileMgr.getFile(Dir->getName()); |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 229 | // FIXME: This shouldn't happen unless there is a file system race. Is |
| 230 | // that worth diagnosing? |
| 231 | if (!Header) |
| 232 | continue; |
| 233 | |
Douglas Gregor | d2a8fe1 | 2012-01-05 00:04:05 +0000 | [diff] [blame] | 234 | // If this header is marked 'unavailable' in this module, don't include |
| 235 | // it. |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 236 | if (ModMap.isHeaderUnavailableInModule(Header, Module)) |
| 237 | continue; |
| 238 | |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 239 | // Compute the relative path from the directory to this file. |
| 240 | SmallVector<StringRef, 16> Components; |
Bruno Cardoso Lopes | b171a59 | 2016-05-16 16:46:01 +0000 | [diff] [blame] | 241 | auto PathIt = llvm::sys::path::rbegin(Dir->getName()); |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 242 | for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt) |
| 243 | Components.push_back(*PathIt); |
| 244 | SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten); |
| 245 | for (auto It = Components.rbegin(), End = Components.rend(); It != End; |
| 246 | ++It) |
| 247 | llvm::sys::path::append(RelativeHeader, *It); |
| 248 | |
Richard Smith | 723928c | 2014-03-11 02:02:47 +0000 | [diff] [blame] | 249 | // Include this header as part of the umbrella directory. |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 250 | Module->addTopHeader(Header); |
Davide Italiano | 75df821 | 2016-03-02 06:09:18 +0000 | [diff] [blame] | 251 | addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC); |
Douglas Gregor | 524e33e | 2011-12-08 19:11:24 +0000 | [diff] [blame] | 252 | } |
Richard Smith | 723928c | 2014-03-11 02:02:47 +0000 | [diff] [blame] | 253 | |
| 254 | if (EC) |
| 255 | return EC; |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 256 | } |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 258 | // Recurse into submodules. |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 259 | for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), |
| 260 | SubEnd = Module->submodule_end(); |
Douglas Gregor | e5626c4 | 2011-12-06 17:15:11 +0000 | [diff] [blame] | 261 | Sub != SubEnd; ++Sub) |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 262 | if (std::error_code Err = collectModuleHeaderIncludes( |
Richard Smith | 723928c | 2014-03-11 02:02:47 +0000 | [diff] [blame] | 263 | LangOpts, FileMgr, ModMap, *Sub, Includes)) |
| 264 | return Err; |
| 265 | |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 266 | return std::error_code(); |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 269 | bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI, |
| 270 | StringRef Filename) { |
Richard Smith | 7e82e01 | 2016-02-19 22:25:36 +0000 | [diff] [blame] | 271 | CI.getLangOpts().CompilingModule = true; |
| 272 | |
Richard Smith | fb1e7f7 | 2015-08-14 05:02:58 +0000 | [diff] [blame] | 273 | // Find the module map file. |
| 274 | const FileEntry *ModuleMap = |
| 275 | CI.getFileManager().getFile(Filename, /*openFile*/true); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 276 | if (!ModuleMap) { |
| 277 | CI.getDiagnostics().Report(diag::err_module_map_not_found) |
| 278 | << Filename; |
| 279 | return false; |
| 280 | } |
| 281 | |
Richard Smith | a8cfffa | 2015-11-26 02:04:16 +0000 | [diff] [blame] | 282 | // Set up embedding for any specified files. Do this before we load any |
| 283 | // source files, including the primary module map for the compilation. |
| 284 | for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) { |
| 285 | if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true)) |
| 286 | CI.getSourceManager().setFileIsTransient(FE); |
| 287 | else |
| 288 | CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F; |
| 289 | } |
| 290 | if (CI.getFrontendOpts().ModulesEmbedAllFiles) |
| 291 | CI.getSourceManager().setAllFilesAreTransient(true); |
| 292 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 293 | // Parse the module map file. |
| 294 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
Douglas Gregor | 963c553 | 2013-06-21 16:28:10 +0000 | [diff] [blame] | 295 | if (HS.loadModuleMapFile(ModuleMap, IsSystem)) |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 296 | return false; |
| 297 | |
| 298 | if (CI.getLangOpts().CurrentModule.empty()) { |
| 299 | CI.getDiagnostics().Report(diag::err_missing_module_name); |
| 300 | |
| 301 | // FIXME: Eventually, we could consider asking whether there was just |
| 302 | // a single module described in the module map, and use that as a |
| 303 | // default. Then it would be fairly trivial to just "compile" a module |
| 304 | // map with a single module (the common case). |
| 305 | return false; |
| 306 | } |
Richard Smith | 7bea1d4 | 2014-03-05 20:55:36 +0000 | [diff] [blame] | 307 | |
| 308 | // If we're being run from the command-line, the module build stack will not |
| 309 | // have been filled in yet, so complete it now in order to allow us to detect |
| 310 | // module cycles. |
| 311 | SourceManager &SourceMgr = CI.getSourceManager(); |
| 312 | if (SourceMgr.getModuleBuildStack().empty()) |
| 313 | SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule, |
| 314 | FullSourceLoc(SourceLocation(), SourceMgr)); |
| 315 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 316 | // Dig out the module definition. |
Douglas Gregor | 279a6c3 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 317 | Module = HS.lookupModule(CI.getLangOpts().CurrentModule, |
| 318 | /*AllowSearch=*/false); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 319 | if (!Module) { |
| 320 | CI.getDiagnostics().Report(diag::err_missing_module) |
| 321 | << CI.getLangOpts().CurrentModule << Filename; |
| 322 | |
| 323 | return false; |
| 324 | } |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 325 | |
| 326 | // Check whether we can build this module at all. |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 327 | clang::Module::Requirement Requirement; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 328 | clang::Module::UnresolvedHeaderDirective MissingHeader; |
Daniel Jasper | 0761a8a | 2013-12-17 10:31:37 +0000 | [diff] [blame] | 329 | if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement, |
| 330 | MissingHeader)) { |
| 331 | if (MissingHeader.FileNameLoc.isValid()) { |
Ben Langmuir | ec8c975 | 2014-04-18 22:07:31 +0000 | [diff] [blame] | 332 | CI.getDiagnostics().Report(MissingHeader.FileNameLoc, |
| 333 | diag::err_module_header_missing) |
Daniel Jasper | 0761a8a | 2013-12-17 10:31:37 +0000 | [diff] [blame] | 334 | << MissingHeader.IsUmbrella << MissingHeader.FileName; |
| 335 | } else { |
| 336 | CI.getDiagnostics().Report(diag::err_module_unavailable) |
| 337 | << Module->getFullModuleName() |
| 338 | << Requirement.second << Requirement.first; |
| 339 | } |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 340 | |
| 341 | return false; |
| 342 | } |
| 343 | |
Ben Langmuir | 9d6448b | 2014-08-09 00:57:23 +0000 | [diff] [blame] | 344 | if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) { |
| 345 | Module->IsInferred = true; |
| 346 | HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing); |
| 347 | } else { |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 348 | ModuleMapForUniquing = ModuleMap; |
Ben Langmuir | 9d6448b | 2014-08-09 00:57:23 +0000 | [diff] [blame] | 349 | } |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 350 | |
Argyrios Kyrtzidis | 130190f | 2012-10-10 02:12:39 +0000 | [diff] [blame] | 351 | FileManager &FileMgr = CI.getFileManager(); |
| 352 | |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 353 | // Collect the set of #includes we need to build the module. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 354 | SmallString<256> HeaderContents; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 355 | std::error_code Err = std::error_code(); |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 356 | if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) |
Davide Italiano | 75df821 | 2016-03-02 06:09:18 +0000 | [diff] [blame] | 357 | addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents, |
| 358 | CI.getLangOpts(), Module->IsExternC); |
| 359 | Err = collectModuleHeaderIncludes( |
Richard Smith | 723928c | 2014-03-11 02:02:47 +0000 | [diff] [blame] | 360 | CI.getLangOpts(), FileMgr, |
| 361 | CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module, |
| 362 | HeaderContents); |
| 363 | |
| 364 | if (Err) { |
| 365 | CI.getDiagnostics().Report(diag::err_module_cannot_create_includes) |
| 366 | << Module->getFullModuleName() << Err.message(); |
| 367 | return false; |
| 368 | } |
Douglas Gregor | 4332b32 | 2011-11-16 17:04:00 +0000 | [diff] [blame] | 369 | |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 370 | // Inform the preprocessor that includes from within the input buffer should |
| 371 | // be resolved relative to the build directory of the module map file. |
| 372 | CI.getPreprocessor().setMainFileDir(Module->Directory); |
| 373 | |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 374 | std::unique_ptr<llvm::MemoryBuffer> InputBuffer = |
Argyrios Kyrtzidis | 5259524 | 2012-11-15 18:57:27 +0000 | [diff] [blame] | 375 | llvm::MemoryBuffer::getMemBufferCopy(HeaderContents, |
| 376 | Module::getModuleInputBufferName()); |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 377 | // Ownership of InputBuffer will be transferred to the SourceManager. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 378 | setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(), |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 379 | Module->IsSystem)); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 383 | std::unique_ptr<raw_pwrite_stream> |
| 384 | GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI, |
| 385 | StringRef InFile, |
| 386 | std::string &Sysroot, |
| 387 | std::string &OutputFile) { |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 388 | // If no output file was provided, figure out where this module would go |
| 389 | // in the module cache. |
| 390 | if (CI.getFrontendOpts().OutputFile.empty()) { |
| 391 | HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 392 | CI.getFrontendOpts().OutputFile = |
| 393 | HS.getModuleFileName(CI.getLangOpts().CurrentModule, |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame^] | 394 | ModuleMapForUniquing->getName(), |
| 395 | /*UsePrebuiltPath=*/false); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 396 | } |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 398 | // We use createOutputFile here because this is exposed via libclang, and we |
| 399 | // must disable the RemoveFileOnSignal behavior. |
| 400 | // We use a temporary to avoid race conditions. |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 401 | std::unique_ptr<raw_pwrite_stream> OS = |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 402 | CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, |
| 403 | /*RemoveFileOnSignal=*/false, InFile, |
| 404 | /*Extension=*/"", /*useTemporary=*/true, |
| 405 | /*CreateMissingDirectories=*/true); |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 406 | if (!OS) |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 407 | return nullptr; |
| 408 | |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 409 | OutputFile = CI.getFrontendOpts().OutputFile; |
Rafael Espindola | bfd25d4 | 2015-04-10 13:14:31 +0000 | [diff] [blame] | 410 | return OS; |
Douglas Gregor | 2b20cb8 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Etienne Bergeron | 98de805 | 2016-05-12 19:51:18 +0000 | [diff] [blame] | 413 | SyntaxOnlyAction::~SyntaxOnlyAction() { |
| 414 | } |
| 415 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 416 | std::unique_ptr<ASTConsumer> |
| 417 | SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 418 | return llvm::make_unique<ASTConsumer>(); |
Daniel Dunbar | 02bd2d7 | 2009-11-14 10:42:46 +0000 | [diff] [blame] | 419 | } |
| 420 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 421 | std::unique_ptr<ASTConsumer> |
| 422 | DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, |
| 423 | StringRef InFile) { |
| 424 | return llvm::make_unique<ASTConsumer>(); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 425 | } |
| 426 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 427 | std::unique_ptr<ASTConsumer> |
| 428 | VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
| 429 | return llvm::make_unique<ASTConsumer>(); |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void VerifyPCHAction::ExecuteAction() { |
Ben Langmuir | 3d4417c | 2014-02-07 17:31:11 +0000 | [diff] [blame] | 433 | CompilerInstance &CI = getCompilerInstance(); |
| 434 | bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
| 435 | const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 436 | std::unique_ptr<ASTReader> Reader(new ASTReader( |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 437 | CI.getPreprocessor(), CI.getASTContext(), CI.getPCHContainerReader(), |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 438 | CI.getFrontendOpts().ModuleFileExtensions, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 439 | Sysroot.empty() ? "" : Sysroot.c_str(), |
| 440 | /*DisableValidation*/ false, |
| 441 | /*AllowPCHWithCompilerErrors*/ false, |
| 442 | /*AllowConfigurationMismatch*/ true, |
| 443 | /*ValidateSystemInputs*/ true)); |
Ben Langmuir | 3d4417c | 2014-02-07 17:31:11 +0000 | [diff] [blame] | 444 | |
| 445 | Reader->ReadAST(getCurrentFile(), |
| 446 | Preamble ? serialization::MK_Preamble |
| 447 | : serialization::MK_PCH, |
| 448 | SourceLocation(), |
| 449 | ASTReader::ARR_ConfigurationMismatch); |
Ben Langmuir | 2cb4a78 | 2014-02-05 22:21:15 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 452 | namespace { |
| 453 | /// \brief AST reader listener that dumps module information for a module |
| 454 | /// file. |
| 455 | class DumpModuleInfoListener : public ASTReaderListener { |
| 456 | llvm::raw_ostream &Out; |
| 457 | |
| 458 | public: |
| 459 | DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { } |
| 460 | |
| 461 | #define DUMP_BOOLEAN(Value, Text) \ |
| 462 | Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n" |
| 463 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 464 | bool ReadFullVersionInformation(StringRef FullVersion) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 465 | Out.indent(2) |
| 466 | << "Generated by " |
| 467 | << (FullVersion == getClangFullRepositoryVersion()? "this" |
| 468 | : "a different") |
| 469 | << " Clang: " << FullVersion << "\n"; |
| 470 | return ASTReaderListener::ReadFullVersionInformation(FullVersion); |
| 471 | } |
| 472 | |
Ben Langmuir | 4f5212a | 2014-04-14 22:12:44 +0000 | [diff] [blame] | 473 | void ReadModuleName(StringRef ModuleName) override { |
| 474 | Out.indent(2) << "Module name: " << ModuleName << "\n"; |
| 475 | } |
| 476 | void ReadModuleMapFile(StringRef ModuleMapPath) override { |
| 477 | Out.indent(2) << "Module map file: " << ModuleMapPath << "\n"; |
| 478 | } |
| 479 | |
Richard Smith | 1e2cf0d | 2014-10-31 02:28:58 +0000 | [diff] [blame] | 480 | bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, |
| 481 | bool AllowCompatibleDifferences) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 482 | Out.indent(2) << "Language options:\n"; |
| 483 | #define LANGOPT(Name, Bits, Default, Description) \ |
| 484 | DUMP_BOOLEAN(LangOpts.Name, Description); |
| 485 | #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ |
| 486 | Out.indent(4) << Description << ": " \ |
| 487 | << static_cast<unsigned>(LangOpts.get##Name()) << "\n"; |
| 488 | #define VALUE_LANGOPT(Name, Bits, Default, Description) \ |
| 489 | Out.indent(4) << Description << ": " << LangOpts.Name << "\n"; |
| 490 | #define BENIGN_LANGOPT(Name, Bits, Default, Description) |
| 491 | #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) |
| 492 | #include "clang/Basic/LangOptions.def" |
Ben Langmuir | cd98cb7 | 2015-06-23 18:20:18 +0000 | [diff] [blame] | 493 | |
| 494 | if (!LangOpts.ModuleFeatures.empty()) { |
| 495 | Out.indent(4) << "Module features:\n"; |
| 496 | for (StringRef Feature : LangOpts.ModuleFeatures) |
| 497 | Out.indent(6) << Feature << "\n"; |
| 498 | } |
| 499 | |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 500 | return false; |
| 501 | } |
| 502 | |
Chandler Carruth | 0d745bc | 2015-03-14 04:47:43 +0000 | [diff] [blame] | 503 | bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, |
| 504 | bool AllowCompatibleDifferences) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 505 | Out.indent(2) << "Target options:\n"; |
| 506 | Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n"; |
| 507 | Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n"; |
| 508 | Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n"; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 509 | |
| 510 | if (!TargetOpts.FeaturesAsWritten.empty()) { |
| 511 | Out.indent(4) << "Target features:\n"; |
| 512 | for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); |
| 513 | I != N; ++I) { |
| 514 | Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n"; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | return false; |
| 519 | } |
| 520 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 521 | bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, |
| 522 | bool Complain) override { |
Ben Langmuir | b92de02 | 2014-04-29 16:25:26 +0000 | [diff] [blame] | 523 | Out.indent(2) << "Diagnostic options:\n"; |
| 524 | #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name); |
| 525 | #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ |
| 526 | Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n"; |
| 527 | #define VALUE_DIAGOPT(Name, Bits, Default) \ |
| 528 | Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n"; |
| 529 | #include "clang/Basic/DiagnosticOptions.def" |
| 530 | |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 531 | Out.indent(4) << "Diagnostic flags:\n"; |
| 532 | for (const std::string &Warning : DiagOpts->Warnings) |
Ben Langmuir | b92de02 | 2014-04-29 16:25:26 +0000 | [diff] [blame] | 533 | Out.indent(6) << "-W" << Warning << "\n"; |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 534 | for (const std::string &Remark : DiagOpts->Remarks) |
| 535 | Out.indent(6) << "-R" << Remark << "\n"; |
Ben Langmuir | b92de02 | 2014-04-29 16:25:26 +0000 | [diff] [blame] | 536 | |
| 537 | return false; |
| 538 | } |
| 539 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 540 | bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 541 | StringRef SpecificModuleCachePath, |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 542 | bool Complain) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 543 | Out.indent(2) << "Header search options:\n"; |
| 544 | Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n"; |
Argyrios Kyrtzidis | bd0b651 | 2015-02-19 20:12:20 +0000 | [diff] [blame] | 545 | Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n"; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 546 | DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes, |
| 547 | "Use builtin include directories [-nobuiltininc]"); |
| 548 | DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes, |
| 549 | "Use standard system include directories [-nostdinc]"); |
| 550 | DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes, |
| 551 | "Use standard C++ include directories [-nostdinc++]"); |
| 552 | DUMP_BOOLEAN(HSOpts.UseLibcxx, |
| 553 | "Use libc++ (rather than libstdc++) [-stdlib=]"); |
| 554 | return false; |
| 555 | } |
| 556 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 557 | bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, |
| 558 | bool Complain, |
| 559 | std::string &SuggestedPredefines) override { |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 560 | Out.indent(2) << "Preprocessor options:\n"; |
| 561 | DUMP_BOOLEAN(PPOpts.UsePredefines, |
| 562 | "Uses compiler/target-specific predefines [-undef]"); |
| 563 | DUMP_BOOLEAN(PPOpts.DetailedRecord, |
| 564 | "Uses detailed preprocessing record (for indexing)"); |
| 565 | |
| 566 | if (!PPOpts.Macros.empty()) { |
| 567 | Out.indent(4) << "Predefined macros:\n"; |
| 568 | } |
| 569 | |
| 570 | for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator |
| 571 | I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end(); |
| 572 | I != IEnd; ++I) { |
| 573 | Out.indent(6); |
| 574 | if (I->second) |
| 575 | Out << "-U"; |
| 576 | else |
| 577 | Out << "-D"; |
| 578 | Out << I->first << "\n"; |
| 579 | } |
| 580 | return false; |
| 581 | } |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 582 | |
| 583 | /// Indicates that a particular module file extension has been read. |
| 584 | void readModuleFileExtension( |
| 585 | const ModuleFileExtensionMetadata &Metadata) override { |
| 586 | Out.indent(2) << "Module file extension '" |
| 587 | << Metadata.BlockName << "' " << Metadata.MajorVersion |
| 588 | << "." << Metadata.MinorVersion; |
| 589 | if (!Metadata.UserInfo.empty()) { |
| 590 | Out << ": "; |
| 591 | Out.write_escaped(Metadata.UserInfo); |
| 592 | } |
| 593 | |
| 594 | Out << "\n"; |
| 595 | } |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 596 | #undef DUMP_BOOLEAN |
| 597 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 598 | } |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 599 | |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 600 | bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) { |
| 601 | // The Object file reader also supports raw ast files and there is no point in |
| 602 | // being strict about the module file format in -module-file-info mode. |
| 603 | CI.getHeaderSearchOpts().ModuleFormat = "obj"; |
| 604 | return true; |
| 605 | } |
| 606 | |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 607 | void DumpModuleInfoAction::ExecuteAction() { |
| 608 | // Set up the output file. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 609 | std::unique_ptr<llvm::raw_fd_ostream> OutFile; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 610 | StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile; |
| 611 | if (!OutputFileName.empty() && OutputFileName != "-") { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 612 | std::error_code EC; |
| 613 | OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC, |
| 614 | llvm::sys::fs::F_Text)); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 615 | } |
| 616 | llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs(); |
| 617 | |
| 618 | Out << "Information for module file '" << getCurrentFile() << "':\n"; |
Adrian Prantl | 99e765b | 2016-08-17 23:14:00 +0000 | [diff] [blame] | 619 | auto &FileMgr = getCompilerInstance().getFileManager(); |
| 620 | auto Buffer = FileMgr.getBufferForFile(getCurrentFile()); |
| 621 | StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer(); |
| 622 | bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' && |
| 623 | Magic[2] == 'C' && Magic[3] == 'H'); |
| 624 | Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n"; |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 625 | |
Manman Ren | 47a4445 | 2016-07-26 17:12:17 +0000 | [diff] [blame] | 626 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 627 | DumpModuleInfoListener Listener(Out); |
Manman Ren | 47a4445 | 2016-07-26 17:12:17 +0000 | [diff] [blame] | 628 | HeaderSearchOptions &HSOpts = |
| 629 | PP.getHeaderSearchInfo().getHeaderSearchOpts(); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 630 | ASTReader::readASTFileControlBlock( |
Adrian Prantl | 99e765b | 2016-08-17 23:14:00 +0000 | [diff] [blame] | 631 | getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(), |
Manman Ren | 47a4445 | 2016-07-26 17:12:17 +0000 | [diff] [blame] | 632 | /*FindModuleFileExtensions=*/true, Listener, |
| 633 | HSOpts.ModulesValidateDiagnosticOptions); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 636 | //===----------------------------------------------------------------------===// |
| 637 | // Preprocessor Actions |
| 638 | //===----------------------------------------------------------------------===// |
| 639 | |
| 640 | void DumpRawTokensAction::ExecuteAction() { |
| 641 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 642 | SourceManager &SM = PP.getSourceManager(); |
| 643 | |
| 644 | // Start lexing the specified input file. |
Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 645 | const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 646 | Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 647 | RawLex.SetKeepWhitespaceMode(true); |
| 648 | |
| 649 | Token RawTok; |
| 650 | RawLex.LexFromRawLexer(RawTok); |
| 651 | while (RawTok.isNot(tok::eof)) { |
| 652 | PP.DumpToken(RawTok, true); |
Daniel Dunbar | 7502462 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 653 | llvm::errs() << "\n"; |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 654 | RawLex.LexFromRawLexer(RawTok); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void DumpTokensAction::ExecuteAction() { |
| 659 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 660 | // Start preprocessing the specified input file. |
| 661 | Token Tok; |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 662 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 663 | do { |
| 664 | PP.Lex(Tok); |
| 665 | PP.DumpToken(Tok, true); |
Daniel Dunbar | 7502462 | 2009-11-25 10:27:48 +0000 | [diff] [blame] | 666 | llvm::errs() << "\n"; |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 667 | } while (Tok.isNot(tok::eof)); |
| 668 | } |
| 669 | |
| 670 | void GeneratePTHAction::ExecuteAction() { |
| 671 | CompilerInstance &CI = getCompilerInstance(); |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 672 | std::unique_ptr<raw_pwrite_stream> OS = |
| 673 | CI.createDefaultOutputFile(true, getCurrentFile()); |
NAKAMURA Takumi | 5594d79 | 2015-04-13 08:43:40 +0000 | [diff] [blame] | 674 | if (!OS) |
| 675 | return; |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 676 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 677 | CacheTokens(CI.getPreprocessor(), OS.get()); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 680 | void PreprocessOnlyAction::ExecuteAction() { |
| 681 | Preprocessor &PP = getCompilerInstance().getPreprocessor(); |
| 682 | |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 683 | // Ignore unknown pragmas. |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 684 | PP.IgnorePragmas(); |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 685 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 686 | Token Tok; |
| 687 | // Start parsing the specified input file. |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 688 | PP.EnterMainSourceFile(); |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 689 | do { |
| 690 | PP.Lex(Tok); |
| 691 | } while (Tok.isNot(tok::eof)); |
| 692 | } |
| 693 | |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 694 | void PrintPreprocessedAction::ExecuteAction() { |
| 695 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 696 | // 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] | 697 | // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 698 | // |
| 699 | // Look to see what type of line endings the file uses. If there's a |
| 700 | // CRLF, then we won't open the file up in binary mode. If there is |
| 701 | // just an LF or CR, then we will open the file up in binary mode. |
| 702 | // In this fashion, the output format should match the input format, unless |
| 703 | // the input format has inconsistent line endings. |
| 704 | // |
| 705 | // This should be a relatively fast operation since most files won't have |
| 706 | // all of their source code on a single line. However, that is still a |
| 707 | // concern, so if we scan for too long, we'll just assume the file should |
| 708 | // be opened in binary mode. |
| 709 | bool BinaryMode = true; |
| 710 | bool InvalidFile = false; |
| 711 | const SourceManager& SM = CI.getSourceManager(); |
| 712 | const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), |
| 713 | &InvalidFile); |
| 714 | if (!InvalidFile) { |
| 715 | const char *cur = Buffer->getBufferStart(); |
| 716 | const char *end = Buffer->getBufferEnd(); |
| 717 | const char *next = (cur != end) ? cur + 1 : end; |
| 718 | |
| 719 | // Limit ourselves to only scanning 256 characters into the source |
| 720 | // file. This is mostly a sanity check in case the file has no |
| 721 | // newlines whatsoever. |
| 722 | if (end - cur > 256) end = cur + 256; |
| 723 | |
| 724 | while (next < end) { |
| 725 | if (*cur == 0x0D) { // CR |
| 726 | if (*next == 0x0A) // CRLF |
| 727 | BinaryMode = false; |
| 728 | |
| 729 | break; |
| 730 | } else if (*cur == 0x0A) // LF |
| 731 | break; |
| 732 | |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 733 | ++cur; |
| 734 | ++next; |
Douglas Gregor | 52da28b | 2011-09-23 23:43:36 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 738 | std::unique_ptr<raw_ostream> OS = |
| 739 | CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); |
Daniel Dunbar | 7554699 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 740 | if (!OS) return; |
| 741 | |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 742 | DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(), |
Daniel Dunbar | 8835780 | 2009-11-14 10:42:57 +0000 | [diff] [blame] | 743 | CI.getPreprocessorOutputOpts()); |
| 744 | } |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 745 | |
| 746 | void PrintPreambleAction::ExecuteAction() { |
| 747 | switch (getCurrentFileKind()) { |
| 748 | case IK_C: |
| 749 | case IK_CXX: |
| 750 | case IK_ObjC: |
| 751 | case IK_ObjCXX: |
| 752 | case IK_OpenCL: |
Peter Collingbourne | 62089b8 | 2010-12-01 03:15:20 +0000 | [diff] [blame] | 753 | case IK_CUDA: |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 754 | break; |
| 755 | |
| 756 | case IK_None: |
| 757 | case IK_Asm: |
| 758 | case IK_PreprocessedC: |
Artem Belevich | 83a6dcc | 2015-03-19 17:32:06 +0000 | [diff] [blame] | 759 | case IK_PreprocessedCuda: |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 760 | case IK_PreprocessedCXX: |
| 761 | case IK_PreprocessedObjC: |
| 762 | case IK_PreprocessedObjCXX: |
| 763 | case IK_AST: |
| 764 | case IK_LLVM_IR: |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 765 | case IK_RenderScript: |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 766 | // We can't do anything with these. |
| 767 | return; |
| 768 | } |
Rafael Espindola | 6406f7b | 2014-08-26 19:54:40 +0000 | [diff] [blame] | 769 | |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 770 | CompilerInstance &CI = getCompilerInstance(); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 771 | auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile()); |
| 772 | if (Buffer) { |
Rafael Espindola | cd0b380 | 2014-08-12 15:46:24 +0000 | [diff] [blame] | 773 | unsigned Preamble = |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 774 | Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first; |
| 775 | llvm::outs().write((*Buffer)->getBufferStart(), Preamble); |
Douglas Gregor | af82e35 | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 776 | } |
| 777 | } |