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