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