ASTUnit.cpp revision faddc3e53a95c68f2c3a966e0f1e6eba110dafd6
1//===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===// 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// ASTUnit Implementation. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Frontend/ASTUnit.h" 15#include "clang/Frontend/PCHReader.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/ASTConsumer.h" 18#include "clang/AST/DeclVisitor.h" 19#include "clang/AST/StmtVisitor.h" 20#include "clang/Driver/Compilation.h" 21#include "clang/Driver/Driver.h" 22#include "clang/Driver/Job.h" 23#include "clang/Driver/Tool.h" 24#include "clang/Frontend/CompilerInstance.h" 25#include "clang/Frontend/FrontendActions.h" 26#include "clang/Frontend/FrontendDiagnostic.h" 27#include "clang/Frontend/FrontendOptions.h" 28#include "clang/Lex/HeaderSearch.h" 29#include "clang/Lex/Preprocessor.h" 30#include "clang/Basic/TargetOptions.h" 31#include "clang/Basic/TargetInfo.h" 32#include "clang/Basic/Diagnostic.h" 33#include "llvm/Support/MemoryBuffer.h" 34#include "llvm/System/Host.h" 35#include "llvm/System/Path.h" 36using namespace clang; 37 38ASTUnit::ASTUnit(bool _MainFileIsAST) 39 : MainFileIsAST(_MainFileIsAST), ConcurrencyCheckValue(CheckUnlocked) { } 40 41ASTUnit::~ASTUnit() { 42 ConcurrencyCheckValue = CheckLocked; 43 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 44 TemporaryFiles[I].eraseFromDisk(); 45} 46 47namespace { 48 49/// \brief Gathers information from PCHReader that will be used to initialize 50/// a Preprocessor. 51class PCHInfoCollector : public PCHReaderListener { 52 LangOptions &LangOpt; 53 HeaderSearch &HSI; 54 std::string &TargetTriple; 55 std::string &Predefines; 56 unsigned &Counter; 57 58 unsigned NumHeaderInfos; 59 60public: 61 PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, 62 std::string &TargetTriple, std::string &Predefines, 63 unsigned &Counter) 64 : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), 65 Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} 66 67 virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { 68 LangOpt = LangOpts; 69 return false; 70 } 71 72 virtual bool ReadTargetTriple(llvm::StringRef Triple) { 73 TargetTriple = Triple; 74 return false; 75 } 76 77 virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef, 78 FileID PCHBufferID, 79 llvm::StringRef OriginalFileName, 80 std::string &SuggestedPredefines) { 81 Predefines = PCHPredef; 82 return false; 83 } 84 85 virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { 86 HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); 87 } 88 89 virtual void ReadCounter(unsigned Value) { 90 Counter = Value; 91 } 92}; 93 94class StoredDiagnosticClient : public DiagnosticClient { 95 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; 96 97public: 98 explicit StoredDiagnosticClient( 99 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) 100 : StoredDiags(StoredDiags) { } 101 102 virtual void HandleDiagnostic(Diagnostic::Level Level, 103 const DiagnosticInfo &Info); 104}; 105 106/// \brief RAII object that optionally captures diagnostics, if 107/// there is no diagnostic client to capture them already. 108class CaptureDroppedDiagnostics { 109 Diagnostic &Diags; 110 StoredDiagnosticClient Client; 111 DiagnosticClient *PreviousClient; 112 113public: 114 CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, 115 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) 116 : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) 117 { 118 if (RequestCapture || Diags.getClient() == 0) 119 Diags.setClient(&Client); 120 } 121 122 ~CaptureDroppedDiagnostics() { 123 Diags.setClient(PreviousClient); 124 } 125}; 126 127} // anonymous namespace 128 129void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, 130 const DiagnosticInfo &Info) { 131 StoredDiags.push_back(StoredDiagnostic(Level, Info)); 132} 133 134const std::string &ASTUnit::getOriginalSourceFileName() { 135 return OriginalSourceFile; 136} 137 138const std::string &ASTUnit::getPCHFileName() { 139 assert(isMainFileAST() && "Not an ASTUnit from a PCH file!"); 140 return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName(); 141} 142 143ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, 144 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 145 bool OnlyLocalDecls, 146 RemappedFile *RemappedFiles, 147 unsigned NumRemappedFiles, 148 bool CaptureDiagnostics) { 149 llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); 150 151 if (!Diags.getPtr()) { 152 // No diagnostics engine was provided, so create our own diagnostics object 153 // with the default options. 154 DiagnosticOptions DiagOpts; 155 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 156 } 157 158 AST->OnlyLocalDecls = OnlyLocalDecls; 159 AST->Diagnostics = Diags; 160 AST->FileMgr.reset(new FileManager); 161 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); 162 AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); 163 164 // If requested, capture diagnostics in the ASTUnit. 165 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), 166 AST->StoredDiagnostics); 167 168 for (unsigned I = 0; I != NumRemappedFiles; ++I) { 169 // Create the file entry for the file that we're mapping from. 170 const FileEntry *FromFile 171 = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, 172 RemappedFiles[I].second->getBufferSize(), 173 0); 174 if (!FromFile) { 175 AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) 176 << RemappedFiles[I].first; 177 delete RemappedFiles[I].second; 178 continue; 179 } 180 181 // Override the contents of the "from" file with the contents of 182 // the "to" file. 183 AST->getSourceManager().overrideFileContents(FromFile, 184 RemappedFiles[I].second); 185 } 186 187 // Gather Info for preprocessor construction later on. 188 189 LangOptions LangInfo; 190 HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); 191 std::string TargetTriple; 192 std::string Predefines; 193 unsigned Counter; 194 195 llvm::OwningPtr<PCHReader> Reader; 196 llvm::OwningPtr<ExternalASTSource> Source; 197 198 Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), 199 AST->getDiagnostics())); 200 Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, 201 Predefines, Counter)); 202 203 switch (Reader->ReadPCH(Filename)) { 204 case PCHReader::Success: 205 break; 206 207 case PCHReader::Failure: 208 case PCHReader::IgnorePCH: 209 AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); 210 return NULL; 211 } 212 213 AST->OriginalSourceFile = Reader->getOriginalSourceFile(); 214 215 // PCH loaded successfully. Now create the preprocessor. 216 217 // Get information about the target being compiled for. 218 // 219 // FIXME: This is broken, we should store the TargetOptions in the PCH. 220 TargetOptions TargetOpts; 221 TargetOpts.ABI = ""; 222 TargetOpts.CPU = ""; 223 TargetOpts.Features.clear(); 224 TargetOpts.Triple = TargetTriple; 225 AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), 226 TargetOpts)); 227 AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, 228 *AST->Target.get(), 229 AST->getSourceManager(), HeaderInfo)); 230 Preprocessor &PP = *AST->PP.get(); 231 232 PP.setPredefines(Reader->getSuggestedPredefines()); 233 PP.setCounterValue(Counter); 234 Reader->setPreprocessor(PP); 235 236 // Create and initialize the ASTContext. 237 238 AST->Ctx.reset(new ASTContext(LangInfo, 239 AST->getSourceManager(), 240 *AST->Target.get(), 241 PP.getIdentifierTable(), 242 PP.getSelectorTable(), 243 PP.getBuiltinInfo(), 244 /* FreeMemory = */ false, 245 /* size_reserve = */0)); 246 ASTContext &Context = *AST->Ctx.get(); 247 248 Reader->InitializeContext(Context); 249 250 // Attach the PCH reader to the AST context as an external AST 251 // source, so that declarations will be deserialized from the 252 // PCH file as needed. 253 Source.reset(Reader.take()); 254 Context.setExternalSource(Source); 255 256 return AST.take(); 257} 258 259namespace { 260 261class TopLevelDeclTrackerConsumer : public ASTConsumer { 262 ASTUnit &Unit; 263 264public: 265 TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} 266 267 void HandleTopLevelDecl(DeclGroupRef D) { 268 for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { 269 Decl *D = *it; 270 // FIXME: Currently ObjC method declarations are incorrectly being 271 // reported as top-level declarations, even though their DeclContext 272 // is the containing ObjC @interface/@implementation. This is a 273 // fundamental problem in the parser right now. 274 if (isa<ObjCMethodDecl>(D)) 275 continue; 276 Unit.getTopLevelDecls().push_back(D); 277 } 278 } 279}; 280 281class TopLevelDeclTrackerAction : public ASTFrontendAction { 282public: 283 ASTUnit &Unit; 284 285 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, 286 llvm::StringRef InFile) { 287 return new TopLevelDeclTrackerConsumer(Unit); 288 } 289 290public: 291 TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} 292 293 virtual bool hasCodeCompletionSupport() const { return false; } 294}; 295 296} 297 298ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, 299 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 300 bool OnlyLocalDecls, 301 bool CaptureDiagnostics) { 302 // Create the compiler instance to use for building the AST. 303 CompilerInstance Clang; 304 llvm::OwningPtr<ASTUnit> AST; 305 llvm::OwningPtr<TopLevelDeclTrackerAction> Act; 306 307 if (!Diags.getPtr()) { 308 // No diagnostics engine was provided, so create our own diagnostics object 309 // with the default options. 310 DiagnosticOptions DiagOpts; 311 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 312 } 313 314 Clang.setInvocation(CI); 315 316 Clang.setDiagnostics(Diags.getPtr()); 317 Clang.setDiagnosticClient(Diags->getClient()); 318 319 // Create the target instance. 320 Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), 321 Clang.getTargetOpts())); 322 if (!Clang.hasTarget()) { 323 Clang.takeDiagnosticClient(); 324 return 0; 325 } 326 327 // Inform the target of the language options. 328 // 329 // FIXME: We shouldn't need to do this, the target should be immutable once 330 // created. This complexity should be lifted elsewhere. 331 Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); 332 333 assert(Clang.getFrontendOpts().Inputs.size() == 1 && 334 "Invocation must have exactly one source file!"); 335 assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && 336 "FIXME: AST inputs not yet supported here!"); 337 assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && 338 "IR inputs not support here!"); 339 340 // Create the AST unit. 341 AST.reset(new ASTUnit(false)); 342 AST->Diagnostics = Diags; 343 AST->FileMgr.reset(new FileManager); 344 AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); 345 AST->OnlyLocalDecls = OnlyLocalDecls; 346 AST->OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; 347 348 // Capture any diagnostics that would otherwise be dropped. 349 CaptureDroppedDiagnostics Capture(CaptureDiagnostics, 350 Clang.getDiagnostics(), 351 AST->StoredDiagnostics); 352 353 // Create a file manager object to provide access to and cache the filesystem. 354 Clang.setFileManager(&AST->getFileManager()); 355 356 // Create the source manager. 357 Clang.setSourceManager(&AST->getSourceManager()); 358 359 Act.reset(new TopLevelDeclTrackerAction(*AST)); 360 if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, 361 Clang.getFrontendOpts().Inputs[0].first)) 362 goto error; 363 364 Act->Execute(); 365 366 // Steal the created target, context, and preprocessor, and take back the 367 // source and file managers. 368 AST->Ctx.reset(Clang.takeASTContext()); 369 AST->PP.reset(Clang.takePreprocessor()); 370 Clang.takeSourceManager(); 371 Clang.takeFileManager(); 372 AST->Target.reset(Clang.takeTarget()); 373 374 Act->EndSourceFile(); 375 376 Clang.takeDiagnosticClient(); 377 Clang.takeInvocation(); 378 379 AST->Invocation.reset(Clang.takeInvocation()); 380 return AST.take(); 381 382error: 383 Clang.takeSourceManager(); 384 Clang.takeFileManager(); 385 Clang.takeDiagnosticClient(); 386 return 0; 387} 388 389ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, 390 const char **ArgEnd, 391 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 392 llvm::StringRef ResourceFilesPath, 393 bool OnlyLocalDecls, 394 RemappedFile *RemappedFiles, 395 unsigned NumRemappedFiles, 396 bool CaptureDiagnostics) { 397 if (!Diags.getPtr()) { 398 // No diagnostics engine was provided, so create our own diagnostics object 399 // with the default options. 400 DiagnosticOptions DiagOpts; 401 Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 402 } 403 404 llvm::SmallVector<const char *, 16> Args; 405 Args.push_back("<clang>"); // FIXME: Remove dummy argument. 406 Args.insert(Args.end(), ArgBegin, ArgEnd); 407 408 // FIXME: Find a cleaner way to force the driver into restricted modes. We 409 // also want to force it to use clang. 410 Args.push_back("-fsyntax-only"); 411 412 // FIXME: We shouldn't have to pass in the path info. 413 driver::Driver TheDriver("clang", "/", llvm::sys::getHostTriple(), 414 "a.out", false, false, *Diags); 415 416 // Don't check that inputs exist, they have been remapped. 417 TheDriver.setCheckInputsExist(false); 418 419 llvm::OwningPtr<driver::Compilation> C( 420 TheDriver.BuildCompilation(Args.size(), Args.data())); 421 422 // We expect to get back exactly one command job, if we didn't something 423 // failed. 424 const driver::JobList &Jobs = C->getJobs(); 425 if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { 426 llvm::SmallString<256> Msg; 427 llvm::raw_svector_ostream OS(Msg); 428 C->PrintJob(OS, C->getJobs(), "; ", true); 429 Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); 430 return 0; 431 } 432 433 const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); 434 if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { 435 Diags->Report(diag::err_fe_expected_clang_command); 436 return 0; 437 } 438 439 const driver::ArgStringList &CCArgs = Cmd->getArguments(); 440 llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); 441 CompilerInvocation::CreateFromArgs(*CI, 442 const_cast<const char **>(CCArgs.data()), 443 const_cast<const char **>(CCArgs.data()) + 444 CCArgs.size(), 445 *Diags); 446 447 // Override any files that need remapping 448 for (unsigned I = 0; I != NumRemappedFiles; ++I) 449 CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, 450 RemappedFiles[I].second); 451 452 // Override the resources path. 453 CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; 454 455 CI->getFrontendOpts().DisableFree = true; 456 return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, 457 CaptureDiagnostics); 458} 459