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