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