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