FrontendAction.cpp revision 92b97f2d279405751447488504816d82e1906f4f
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/ASTConsumer.h" 12#include "clang/AST/ASTContext.h" 13#include "clang/AST/DeclGroup.h" 14#include "clang/Lex/HeaderSearch.h" 15#include "clang/Lex/Preprocessor.h" 16#include "clang/Frontend/ASTUnit.h" 17#include "clang/Frontend/CompilerInstance.h" 18#include "clang/Frontend/FrontendDiagnostic.h" 19#include "clang/Frontend/FrontendPluginRegistry.h" 20#include "clang/Frontend/MultiplexConsumer.h" 21#include "clang/Parse/ParseAST.h" 22#include "clang/Serialization/ASTDeserializationListener.h" 23#include "llvm/Support/MemoryBuffer.h" 24#include "llvm/Support/Timer.h" 25#include "llvm/Support/ErrorHandling.h" 26#include "llvm/Support/raw_ostream.h" 27using namespace clang; 28 29namespace { 30 31/// \brief Dumps deserialized declarations. 32class DeserializedDeclsDumper : public ASTDeserializationListener { 33 ASTDeserializationListener *Previous; 34 35public: 36 DeserializedDeclsDumper(ASTDeserializationListener *Previous) 37 : Previous(Previous) { } 38 39 virtual void DeclRead(serialization::DeclID ID, const Decl *D) { 40 llvm::outs() << "PCH DECL: " << D->getDeclKindName(); 41 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 42 llvm::outs() << " - " << ND->getNameAsString(); 43 llvm::outs() << "\n"; 44 45 if (Previous) 46 Previous->DeclRead(ID, D); 47 } 48}; 49 50 /// \brief Checks deserialized declarations and emits error if a name 51 /// matches one given in command-line using -error-on-deserialized-decl. 52 class DeserializedDeclsChecker : public ASTDeserializationListener { 53 ASTContext &Ctx; 54 std::set<std::string> NamesToCheck; 55 ASTDeserializationListener *Previous; 56 57 public: 58 DeserializedDeclsChecker(ASTContext &Ctx, 59 const std::set<std::string> &NamesToCheck, 60 ASTDeserializationListener *Previous) 61 : Ctx(Ctx), NamesToCheck(NamesToCheck), Previous(Previous) { } 62 63 virtual void DeclRead(serialization::DeclID ID, const Decl *D) { 64 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 65 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) { 66 unsigned DiagID 67 = Ctx.getDiagnostics().getCustomDiagID(Diagnostic::Error, 68 "%0 was deserialized"); 69 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID) 70 << ND->getNameAsString(); 71 } 72 73 if (Previous) 74 Previous->DeclRead(ID, D); 75 } 76}; 77 78} // end anonymous namespace 79 80FrontendAction::FrontendAction() : Instance(0) {} 81 82FrontendAction::~FrontendAction() {} 83 84void FrontendAction::setCurrentFile(llvm::StringRef Value, InputKind Kind, 85 ASTUnit *AST) { 86 CurrentFile = Value; 87 CurrentFileKind = Kind; 88 CurrentASTUnit.reset(AST); 89} 90 91ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI, 92 llvm::StringRef InFile) { 93 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile); 94 if (!Consumer) 95 return 0; 96 97 if (CI.getFrontendOpts().AddPluginActions.size() == 0) 98 return Consumer; 99 100 // Make sure the non-plugin consumer is first, so that plugins can't 101 // modifiy the AST. 102 std::vector<ASTConsumer*> Consumers(1, Consumer); 103 104 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size(); 105 i != e; ++i) { 106 // This is O(|plugins| * |add_plugins|), but since both numbers are 107 // way below 50 in practice, that's ok. 108 for (FrontendPluginRegistry::iterator 109 it = FrontendPluginRegistry::begin(), 110 ie = FrontendPluginRegistry::end(); 111 it != ie; ++it) { 112 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) { 113 llvm::OwningPtr<PluginASTAction> P(it->instantiate()); 114 FrontendAction* c = P.get(); 115 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i])) 116 Consumers.push_back(c->CreateASTConsumer(CI, InFile)); 117 } 118 } 119 } 120 121 return new MultiplexConsumer(Consumers); 122} 123 124bool FrontendAction::BeginSourceFile(CompilerInstance &CI, 125 llvm::StringRef Filename, 126 InputKind InputKind) { 127 assert(!Instance && "Already processing a source file!"); 128 assert(!Filename.empty() && "Unexpected empty filename!"); 129 setCurrentFile(Filename, InputKind); 130 setCompilerInstance(&CI); 131 132 // AST files follow a very different path, since they share objects via the 133 // AST unit. 134 if (InputKind == IK_AST) { 135 assert(!usesPreprocessorOnly() && 136 "Attempt to pass AST file to preprocessor only action!"); 137 assert(hasASTFileSupport() && 138 "This action does not have AST file support!"); 139 140 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics()); 141 std::string Error; 142 ASTUnit *AST = ASTUnit::LoadFromASTFile(Filename, Diags, 143 CI.getFileSystemOpts()); 144 if (!AST) 145 goto failure; 146 147 setCurrentFile(Filename, InputKind, AST); 148 149 // Set the shared objects, these are reset when we finish processing the 150 // file, otherwise the CompilerInstance will happily destroy them. 151 CI.setFileManager(&AST->getFileManager()); 152 CI.setSourceManager(&AST->getSourceManager()); 153 CI.setPreprocessor(&AST->getPreprocessor()); 154 CI.setASTContext(&AST->getASTContext()); 155 156 // Initialize the action. 157 if (!BeginSourceFileAction(CI, Filename)) 158 goto failure; 159 160 /// Create the AST consumer. 161 CI.setASTConsumer(CreateWrappedASTConsumer(CI, Filename)); 162 if (!CI.hasASTConsumer()) 163 goto failure; 164 165 return true; 166 } 167 168 // Set up the file and source managers, if needed. 169 if (!CI.hasFileManager()) 170 CI.createFileManager(); 171 if (!CI.hasSourceManager()) 172 CI.createSourceManager(CI.getFileManager()); 173 174 // IR files bypass the rest of initialization. 175 if (InputKind == IK_LLVM_IR) { 176 assert(hasIRSupport() && 177 "This action does not have IR file support!"); 178 179 // Inform the diagnostic client we are processing a source file. 180 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0); 181 182 // Initialize the action. 183 if (!BeginSourceFileAction(CI, Filename)) 184 goto failure; 185 186 return true; 187 } 188 189 // Set up the preprocessor. 190 CI.createPreprocessor(); 191 192 // Inform the diagnostic client we are processing a source file. 193 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 194 &CI.getPreprocessor()); 195 196 // Initialize the action. 197 if (!BeginSourceFileAction(CI, Filename)) 198 goto failure; 199 200 /// Create the AST context and consumer unless this is a preprocessor only 201 /// action. 202 if (!usesPreprocessorOnly()) { 203 CI.createASTContext(); 204 205 llvm::OwningPtr<ASTConsumer> Consumer( 206 CreateWrappedASTConsumer(CI, Filename)); 207 if (!Consumer) 208 goto failure; 209 210 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener()); 211 212 /// Use PCH? 213 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { 214 assert(hasPCHSupport() && "This action does not have PCH support!"); 215 ASTDeserializationListener *DeserialListener 216 = CI.getInvocation().getFrontendOpts().ChainedPCH ? 217 Consumer->GetASTDeserializationListener() : 0; 218 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) 219 DeserialListener = new DeserializedDeclsDumper(DeserialListener); 220 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) 221 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(), 222 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn, 223 DeserialListener); 224 CI.createPCHExternalASTSource( 225 CI.getPreprocessorOpts().ImplicitPCHInclude, 226 CI.getPreprocessorOpts().DisablePCHValidation, 227 CI.getPreprocessorOpts().DisableStatCache, 228 DeserialListener); 229 if (!CI.getASTContext().getExternalSource()) 230 goto failure; 231 } 232 233 CI.setASTConsumer(Consumer.take()); 234 if (!CI.hasASTConsumer()) 235 goto failure; 236 } 237 238 // Initialize builtin info as long as we aren't using an external AST 239 // source. 240 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) { 241 Preprocessor &PP = CI.getPreprocessor(); 242 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), 243 PP.getLangOptions()); 244 } 245 246 return true; 247 248 // If we failed, reset state since the client will not end up calling the 249 // matching EndSourceFile(). 250 failure: 251 if (isCurrentFileAST()) { 252 CI.takeASTContext(); 253 CI.takePreprocessor(); 254 CI.takeSourceManager(); 255 CI.takeFileManager(); 256 } 257 258 CI.getDiagnosticClient().EndSourceFile(); 259 setCurrentFile("", IK_None); 260 setCompilerInstance(0); 261 return false; 262} 263 264void FrontendAction::Execute() { 265 CompilerInstance &CI = getCompilerInstance(); 266 267 // Initialize the main file entry. This needs to be delayed until after PCH 268 // has loaded. 269 if (isCurrentFileAST()) { 270 // Set the main file ID to an empty file. 271 // 272 // FIXME: We probably shouldn't need this, but for now this is the 273 // simplest way to reuse the logic in ParseAST. 274 const char *EmptyStr = ""; 275 llvm::MemoryBuffer *SB = 276 llvm::MemoryBuffer::getMemBuffer(EmptyStr, "<dummy input>"); 277 CI.getSourceManager().createMainFileIDForMemBuffer(SB); 278 } else { 279 if (!CI.InitializeSourceManager(getCurrentFile())) 280 return; 281 } 282 283 if (CI.hasFrontendTimer()) { 284 llvm::TimeRegion Timer(CI.getFrontendTimer()); 285 ExecuteAction(); 286 } 287 else ExecuteAction(); 288} 289 290void FrontendAction::EndSourceFile() { 291 CompilerInstance &CI = getCompilerInstance(); 292 293 // Inform the diagnostic client we are done with this source file. 294 CI.getDiagnosticClient().EndSourceFile(); 295 296 // Finalize the action. 297 EndSourceFileAction(); 298 299 // Release the consumer and the AST, in that order since the consumer may 300 // perform actions in its destructor which require the context. 301 // 302 // FIXME: There is more per-file stuff we could just drop here? 303 if (CI.getFrontendOpts().DisableFree) { 304 CI.takeASTConsumer(); 305 if (!isCurrentFileAST()) { 306 CI.takeSema(); 307 CI.takeASTContext(); 308 } 309 } else { 310 if (!isCurrentFileAST()) { 311 CI.setSema(0); 312 CI.setASTContext(0); 313 } 314 CI.setASTConsumer(0); 315 } 316 317 // Inform the preprocessor we are done. 318 if (CI.hasPreprocessor()) 319 CI.getPreprocessor().EndSourceFile(); 320 321 if (CI.getFrontendOpts().ShowStats) { 322 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n"; 323 CI.getPreprocessor().PrintStats(); 324 CI.getPreprocessor().getIdentifierTable().PrintStats(); 325 CI.getPreprocessor().getHeaderSearchInfo().PrintStats(); 326 CI.getSourceManager().PrintStats(); 327 llvm::errs() << "\n"; 328 } 329 330 // Cleanup the output streams, and erase the output files if we encountered 331 // an error. 332 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred()); 333 334 if (isCurrentFileAST()) { 335 CI.takeSema(); 336 CI.takeASTContext(); 337 CI.takePreprocessor(); 338 CI.takeSourceManager(); 339 CI.takeFileManager(); 340 } 341 342 setCompilerInstance(0); 343 setCurrentFile("", IK_None); 344} 345 346//===----------------------------------------------------------------------===// 347// Utility Actions 348//===----------------------------------------------------------------------===// 349 350void ASTFrontendAction::ExecuteAction() { 351 CompilerInstance &CI = getCompilerInstance(); 352 353 // FIXME: Move the truncation aspect of this into Sema, we delayed this till 354 // here so the source manager would be initialized. 355 if (hasCodeCompletionSupport() && 356 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty()) 357 CI.createCodeCompletionConsumer(); 358 359 // Use a code completion consumer? 360 CodeCompleteConsumer *CompletionConsumer = 0; 361 if (CI.hasCodeCompletionConsumer()) 362 CompletionConsumer = &CI.getCodeCompletionConsumer(); 363 364 if (!CI.hasSema()) 365 CI.createSema(usesCompleteTranslationUnit(), CompletionConsumer); 366 367 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats); 368} 369 370ASTConsumer * 371PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI, 372 llvm::StringRef InFile) { 373 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!"); 374} 375