Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 1 | //===--- FrontendAction.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/FrontendAction.h" |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/Lex/HeaderSearch.h" |
| 14 | #include "clang/Lex/Preprocessor.h" |
| 15 | #include "clang/Frontend/ASTUnit.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Frontend/FrontendDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 18 | #include "clang/Parse/ParseAST.h" |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame^] | 19 | #include "clang/Serialization/ASTDeserializationListener.h" |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/Timer.h" |
| 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | using namespace clang; |
| 25 | |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame^] | 26 | namespace { |
| 27 | |
| 28 | /// \brief Dumps deserialized declarations. |
| 29 | class DeserializedDeclsDumper : public ASTDeserializationListener { |
| 30 | ASTDeserializationListener *Previous; |
| 31 | |
| 32 | public: |
| 33 | DeserializedDeclsDumper(ASTDeserializationListener *Previous) |
| 34 | : Previous(Previous) { } |
| 35 | |
| 36 | virtual void DeclRead(serialization::DeclID ID, const Decl *D) { |
| 37 | llvm::outs() << "PCH DECL: " << D->getDeclKindName(); |
| 38 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 39 | llvm::outs() << " - " << ND->getNameAsString(); |
| 40 | llvm::outs() << "\n"; |
| 41 | |
| 42 | if (Previous) |
| 43 | Previous->DeclRead(ID, D); |
| 44 | } |
| 45 | |
| 46 | virtual void SetReader(ASTReader *Reader) {} |
| 47 | virtual void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) {} |
| 48 | virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {} |
| 49 | virtual void SelectorRead(serialization::SelectorID iD, Selector Sel) {} |
| 50 | virtual void MacroDefinitionRead(serialization::MacroID, |
| 51 | MacroDefinition *MD) {} |
| 52 | }; |
| 53 | |
| 54 | } // end anonymous namespace |
| 55 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 56 | FrontendAction::FrontendAction() : Instance(0) {} |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 57 | |
| 58 | FrontendAction::~FrontendAction() {} |
| 59 | |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 60 | void FrontendAction::setCurrentFile(llvm::StringRef Value, InputKind Kind, |
| 61 | ASTUnit *AST) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 62 | CurrentFile = Value; |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 63 | CurrentFileKind = Kind; |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 64 | CurrentASTUnit.reset(AST); |
| 65 | } |
| 66 | |
| 67 | bool FrontendAction::BeginSourceFile(CompilerInstance &CI, |
| 68 | llvm::StringRef Filename, |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 69 | InputKind InputKind) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 70 | assert(!Instance && "Already processing a source file!"); |
| 71 | assert(!Filename.empty() && "Unexpected empty filename!"); |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 72 | setCurrentFile(Filename, InputKind); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 73 | setCompilerInstance(&CI); |
| 74 | |
| 75 | // AST files follow a very different path, since they share objects via the |
| 76 | // AST unit. |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 77 | if (InputKind == IK_AST) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 78 | assert(!usesPreprocessorOnly() && |
| 79 | "Attempt to pass AST file to preprocessor only action!"); |
Daniel Dunbar | eb58d83 | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 80 | assert(hasASTFileSupport() && |
| 81 | "This action does not have AST file support!"); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 83 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics()); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 84 | std::string Error; |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 85 | ASTUnit *AST = ASTUnit::LoadFromASTFile(Filename, Diags); |
Daniel Dunbar | 5262fda | 2009-12-03 01:45:44 +0000 | [diff] [blame] | 86 | if (!AST) |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 87 | goto failure; |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 88 | |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 89 | setCurrentFile(Filename, InputKind, AST); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 90 | |
| 91 | // Set the shared objects, these are reset when we finish processing the |
| 92 | // file, otherwise the CompilerInstance will happily destroy them. |
| 93 | CI.setFileManager(&AST->getFileManager()); |
| 94 | CI.setSourceManager(&AST->getSourceManager()); |
| 95 | CI.setPreprocessor(&AST->getPreprocessor()); |
| 96 | CI.setASTContext(&AST->getASTContext()); |
| 97 | |
| 98 | // Initialize the action. |
| 99 | if (!BeginSourceFileAction(CI, Filename)) |
| 100 | goto failure; |
| 101 | |
| 102 | /// Create the AST consumer. |
| 103 | CI.setASTConsumer(CreateASTConsumer(CI, Filename)); |
| 104 | if (!CI.hasASTConsumer()) |
| 105 | goto failure; |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 110 | // Set up the file and source managers, if needed. |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 111 | if (!CI.hasFileManager()) |
| 112 | CI.createFileManager(); |
| 113 | if (!CI.hasSourceManager()) |
| 114 | CI.createSourceManager(); |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 115 | |
| 116 | // IR files bypass the rest of initialization. |
| 117 | if (InputKind == IK_LLVM_IR) { |
| 118 | assert(hasIRSupport() && |
| 119 | "This action does not have IR file support!"); |
| 120 | |
| 121 | // Inform the diagnostic client we are processing a source file. |
| 122 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0); |
| 123 | |
| 124 | // Initialize the action. |
| 125 | if (!BeginSourceFileAction(CI, Filename)) |
| 126 | goto failure; |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | // Set up the preprocessor. |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 132 | CI.createPreprocessor(); |
| 133 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 134 | // Inform the diagnostic client we are processing a source file. |
| 135 | CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), |
| 136 | &CI.getPreprocessor()); |
| 137 | |
| 138 | // Initialize the action. |
| 139 | if (!BeginSourceFileAction(CI, Filename)) |
| 140 | goto failure; |
| 141 | |
| 142 | /// Create the AST context and consumer unless this is a preprocessor only |
| 143 | /// action. |
| 144 | if (!usesPreprocessorOnly()) { |
| 145 | CI.createASTContext(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 146 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 147 | llvm::OwningPtr<ASTConsumer> Consumer(CreateASTConsumer(CI, Filename)); |
| 148 | |
| 149 | /// Use PCH? |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 150 | if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 151 | assert(hasPCHSupport() && "This action does not have PCH support!"); |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame^] | 152 | ASTDeserializationListener *DeserialListener |
| 153 | = CI.getInvocation().getFrontendOpts().ChainedPCH ? |
| 154 | Consumer->GetASTDeserializationListener() : 0; |
| 155 | if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) |
| 156 | DeserialListener = new DeserializedDeclsDumper(DeserialListener); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 157 | CI.createPCHExternalASTSource( |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 158 | CI.getPreprocessorOpts().ImplicitPCHInclude, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 159 | CI.getPreprocessorOpts().DisablePCHValidation, |
Argyrios Kyrtzidis | b972858 | 2010-10-14 20:14:18 +0000 | [diff] [blame^] | 160 | DeserialListener); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 161 | if (!CI.getASTContext().getExternalSource()) |
| 162 | goto failure; |
| 163 | } |
Sebastian Redl | 77f4603 | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 164 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 165 | CI.setASTConsumer(Consumer.take()); |
Sebastian Redl | 77f4603 | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 166 | if (!CI.hasASTConsumer()) |
| 167 | goto failure; |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Initialize builtin info as long as we aren't using an external AST |
| 171 | // source. |
| 172 | if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) { |
| 173 | Preprocessor &PP = CI.getPreprocessor(); |
| 174 | PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), |
| 175 | PP.getLangOptions().NoBuiltin); |
| 176 | } |
| 177 | |
| 178 | return true; |
| 179 | |
| 180 | // If we failed, reset state since the client will not end up calling the |
| 181 | // matching EndSourceFile(). |
| 182 | failure: |
| 183 | if (isCurrentFileAST()) { |
| 184 | CI.takeASTContext(); |
| 185 | CI.takePreprocessor(); |
| 186 | CI.takeSourceManager(); |
| 187 | CI.takeFileManager(); |
| 188 | } |
| 189 | |
| 190 | CI.getDiagnosticClient().EndSourceFile(); |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 191 | setCurrentFile("", IK_None); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 192 | setCompilerInstance(0); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | void FrontendAction::Execute() { |
| 197 | CompilerInstance &CI = getCompilerInstance(); |
| 198 | |
| 199 | // Initialize the main file entry. This needs to be delayed until after PCH |
| 200 | // has loaded. |
| 201 | if (isCurrentFileAST()) { |
| 202 | // Set the main file ID to an empty file. |
| 203 | // |
| 204 | // FIXME: We probably shouldn't need this, but for now this is the |
| 205 | // simplest way to reuse the logic in ParseAST. |
| 206 | const char *EmptyStr = ""; |
| 207 | llvm::MemoryBuffer *SB = |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 208 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>"); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 209 | CI.getSourceManager().createMainFileIDForMemBuffer(SB); |
| 210 | } else { |
| 211 | if (!CI.InitializeSourceManager(getCurrentFile())) |
| 212 | return; |
| 213 | } |
| 214 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 215 | if (CI.hasFrontendTimer()) { |
| 216 | llvm::TimeRegion Timer(CI.getFrontendTimer()); |
| 217 | ExecuteAction(); |
| 218 | } |
| 219 | else ExecuteAction(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void FrontendAction::EndSourceFile() { |
| 223 | CompilerInstance &CI = getCompilerInstance(); |
| 224 | |
| 225 | // Finalize the action. |
| 226 | EndSourceFileAction(); |
| 227 | |
| 228 | // Release the consumer and the AST, in that order since the consumer may |
| 229 | // perform actions in its destructor which require the context. |
| 230 | // |
| 231 | // FIXME: There is more per-file stuff we could just drop here? |
| 232 | if (CI.getFrontendOpts().DisableFree) { |
| 233 | CI.takeASTConsumer(); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 234 | if (!isCurrentFileAST()) { |
| 235 | CI.takeSema(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 236 | CI.takeASTContext(); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 237 | } |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 238 | } else { |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 239 | if (!isCurrentFileAST()) { |
| 240 | CI.setSema(0); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 241 | CI.setASTContext(0); |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 242 | } |
| 243 | CI.setASTConsumer(0); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 246 | // Inform the preprocessor we are done. |
| 247 | if (CI.hasPreprocessor()) |
| 248 | CI.getPreprocessor().EndSourceFile(); |
| 249 | |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 250 | if (CI.getFrontendOpts().ShowStats) { |
| 251 | llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; |
| 252 | CI.getPreprocessor().PrintStats(); |
| 253 | CI.getPreprocessor().getIdentifierTable().PrintStats(); |
| 254 | CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); |
| 255 | CI.getSourceManager().PrintStats(); |
| 256 | llvm::errs() << "\n"; |
| 257 | } |
| 258 | |
| 259 | // Cleanup the output streams, and erase the output files if we encountered |
| 260 | // an error. |
Kovarththanan Rajaratnam | e51dd7b | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 261 | CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().getNumErrors()); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 262 | |
| 263 | // Inform the diagnostic client we are done with this source file. |
| 264 | CI.getDiagnosticClient().EndSourceFile(); |
| 265 | |
| 266 | if (isCurrentFileAST()) { |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 267 | CI.takeSema(); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 268 | CI.takeASTContext(); |
| 269 | CI.takePreprocessor(); |
| 270 | CI.takeSourceManager(); |
| 271 | CI.takeFileManager(); |
| 272 | } |
| 273 | |
| 274 | setCompilerInstance(0); |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 275 | setCurrentFile("", IK_None); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | //===----------------------------------------------------------------------===// |
| 279 | // Utility Actions |
| 280 | //===----------------------------------------------------------------------===// |
| 281 | |
| 282 | void ASTFrontendAction::ExecuteAction() { |
| 283 | CompilerInstance &CI = getCompilerInstance(); |
| 284 | |
| 285 | // FIXME: Move the truncation aspect of this into Sema, we delayed this till |
| 286 | // here so the source manager would be initialized. |
| 287 | if (hasCodeCompletionSupport() && |
| 288 | !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) |
| 289 | CI.createCodeCompletionConsumer(); |
| 290 | |
| 291 | // Use a code completion consumer? |
| 292 | CodeCompleteConsumer *CompletionConsumer = 0; |
| 293 | if (CI.hasCodeCompletionConsumer()) |
| 294 | CompletionConsumer = &CI.getCodeCompletionConsumer(); |
| 295 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 296 | if (!CI.hasSema()) |
| 297 | CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer); |
| 298 | |
| 299 | ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | ASTConsumer * |
| 303 | PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, |
| 304 | llvm::StringRef InFile) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 305 | llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); |
Daniel Dunbar | 4ee2409 | 2009-11-14 10:42:35 +0000 | [diff] [blame] | 306 | } |