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