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