ASTUnit.h revision 213f18b3d654de7d1c7cf4a329ea9d3db1c50b6a
1//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===// 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 utility class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H 15#define LLVM_CLANG_FRONTEND_ASTUNIT_H 16 17#include "clang/Index/ASTLocation.h" 18#include "clang/Serialization/ASTBitCodes.h" 19#include "clang/Sema/Sema.h" 20#include "clang/Sema/CodeCompleteConsumer.h" 21#include "clang/Lex/PreprocessingRecord.h" 22#include "clang/Basic/SourceManager.h" 23#include "clang/Basic/FileManager.h" 24#include "clang-c/Index.h" 25#include "llvm/ADT/IntrusiveRefCntPtr.h" 26#include "llvm/ADT/OwningPtr.h" 27#include "llvm/ADT/SmallVector.h" 28#include "llvm/ADT/StringMap.h" 29#include "llvm/System/Path.h" 30#include <map> 31#include <string> 32#include <vector> 33#include <cassert> 34#include <utility> 35#include <sys/types.h> 36 37namespace llvm { 38 class MemoryBuffer; 39} 40 41namespace clang { 42class ASTContext; 43class CodeCompleteConsumer; 44class CompilerInvocation; 45class Decl; 46class Diagnostic; 47class FileEntry; 48class FileManager; 49class HeaderSearch; 50class Preprocessor; 51class SourceManager; 52class TargetInfo; 53 54using namespace idx; 55 56/// \brief Utility class for loading a ASTContext from an AST file. 57/// 58class ASTUnit { 59public: 60 typedef std::map<FileID, std::vector<PreprocessedEntity *> > 61 PreprocessedEntitiesByFileMap; 62 63private: 64 llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics; 65 llvm::OwningPtr<FileManager> FileMgr; 66 llvm::OwningPtr<SourceManager> SourceMgr; 67 llvm::OwningPtr<HeaderSearch> HeaderInfo; 68 llvm::OwningPtr<TargetInfo> Target; 69 llvm::OwningPtr<Preprocessor> PP; 70 llvm::OwningPtr<ASTContext> Ctx; 71 72 /// \brief The AST consumer that received information about the translation 73 /// unit as it was parsed or loaded. 74 llvm::OwningPtr<ASTConsumer> Consumer; 75 76 /// \brief The semantic analysis object used to type-check the translation 77 /// unit. 78 llvm::OwningPtr<Sema> TheSema; 79 80 /// Optional owned invocation, just used to make the invocation used in 81 /// LoadFromCommandLine available. 82 llvm::OwningPtr<CompilerInvocation> Invocation; 83 84 // OnlyLocalDecls - when true, walking this AST should only visit declarations 85 // that come from the AST itself, not from included precompiled headers. 86 // FIXME: This is temporary; eventually, CIndex will always do this. 87 bool OnlyLocalDecls; 88 89 /// \brief Whether to capture any diagnostics produced. 90 bool CaptureDiagnostics; 91 92 /// \brief Track whether the main file was loaded from an AST or not. 93 bool MainFileIsAST; 94 95 /// \brief Whether this AST represents a complete translation unit. 96 bool CompleteTranslationUnit; 97 98 /// \brief Whether we should time each operation. 99 bool WantTiming; 100 101 /// Track the top-level decls which appeared in an ASTUnit which was loaded 102 /// from a source file. 103 // 104 // FIXME: This is just an optimization hack to avoid deserializing large parts 105 // of a PCH file when using the Index library on an ASTUnit loaded from 106 // source. In the long term we should make the Index library use efficient and 107 // more scalable search mechanisms. 108 std::vector<Decl*> TopLevelDecls; 109 110 /// The name of the original source file used to generate this ASTUnit. 111 std::string OriginalSourceFile; 112 113 // Critical optimization when using clang_getCursor(). 114 ASTLocation LastLoc; 115 116 /// \brief The set of diagnostics produced when creating this 117 /// translation unit. 118 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics; 119 120 /// \brief The number of stored diagnostics that come from the driver 121 /// itself. 122 /// 123 /// Diagnostics that come from the driver are retained from one parse to 124 /// the next. 125 unsigned NumStoredDiagnosticsFromDriver; 126 127 /// \brief Temporary files that should be removed when the ASTUnit is 128 /// destroyed. 129 llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles; 130 131 /// \brief A mapping from file IDs to the set of preprocessed entities 132 /// stored in that file. 133 /// 134 /// FIXME: This is just an optimization hack to avoid searching through 135 /// many preprocessed entities during cursor traversal in the CIndex library. 136 /// Ideally, we would just be able to perform a binary search within the 137 /// list of preprocessed entities. 138 PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile; 139 140 /// \brief Simple hack to allow us to assert that ASTUnit is not being 141 /// used concurrently, which is not supported. 142 /// 143 /// Clients should create instances of the ConcurrencyCheck class whenever 144 /// using the ASTUnit in a way that isn't intended to be concurrent, which is 145 /// just about any usage. 146 unsigned int ConcurrencyCheckValue; 147 static const unsigned int CheckLocked = 28573289; 148 static const unsigned int CheckUnlocked = 9803453; 149 150 /// \brief Counter that determines when we want to try building a 151 /// precompiled preamble. 152 /// 153 /// If zero, we will never build a precompiled preamble. Otherwise, 154 /// it's treated as a counter that decrements each time we reparse 155 /// without the benefit of a precompiled preamble. When it hits 1, 156 /// we'll attempt to rebuild the precompiled header. This way, if 157 /// building the precompiled preamble fails, we won't try again for 158 /// some number of calls. 159 unsigned PreambleRebuildCounter; 160 161 /// \brief The file in which the precompiled preamble is stored. 162 std::string PreambleFile; 163 164 /// \brief The contents of the preamble that has been precompiled to 165 /// \c PreambleFile. 166 std::vector<char> Preamble; 167 168 /// \brief Whether the preamble ends at the start of a new line. 169 /// 170 /// Used to inform the lexer as to whether it's starting at the beginning of 171 /// a line after skipping the preamble. 172 bool PreambleEndsAtStartOfLine; 173 174 /// \brief The size of the source buffer that we've reserved for the main 175 /// file within the precompiled preamble. 176 unsigned PreambleReservedSize; 177 178 /// \brief Keeps track of the files that were used when computing the 179 /// preamble, with both their buffer size and their modification time. 180 /// 181 /// If any of the files have changed from one compile to the next, 182 /// the preamble must be thrown away. 183 llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble; 184 185 /// \brief When non-NULL, this is the buffer used to store the contents of 186 /// the main file when it has been padded for use with the precompiled 187 /// preamble. 188 llvm::MemoryBuffer *SavedMainFileBuffer; 189 190 /// \brief When non-NULL, this is the buffer used to store the 191 /// contents of the preamble when it has been padded to build the 192 /// precompiled preamble. 193 llvm::MemoryBuffer *PreambleBuffer; 194 195 /// \brief The number of warnings that occurred while parsing the preamble. 196 /// 197 /// This value will be used to restore the state of the \c Diagnostic object 198 /// when re-using the precompiled preamble. Note that only the 199 /// number of warnings matters, since we will not save the preamble 200 /// when any errors are present. 201 unsigned NumWarningsInPreamble; 202 203 /// \brief The number of diagnostics that were stored when parsing 204 /// the precompiled preamble. 205 /// 206 /// This value is used to determine how many of the stored 207 /// diagnostics should be retained when reparsing in the presence of 208 /// a precompiled preamble. 209 unsigned NumStoredDiagnosticsInPreamble; 210 211 /// \brief A list of the serialization ID numbers for each of the top-level 212 /// declarations parsed within the precompiled preamble. 213 std::vector<serialization::DeclID> TopLevelDeclsInPreamble; 214 215 /// \brief Whether we should be caching code-completion results. 216 bool ShouldCacheCodeCompletionResults; 217 218public: 219 /// \brief A cached code-completion result, which may be introduced in one of 220 /// many different contexts. 221 struct CachedCodeCompletionResult { 222 /// \brief The code-completion string corresponding to this completion 223 /// result. 224 CodeCompletionString *Completion; 225 226 /// \brief A bitmask that indicates which code-completion contexts should 227 /// contain this completion result. 228 /// 229 /// The bits in the bitmask correspond to the values of 230 /// CodeCompleteContext::Kind. To map from a completion context kind to a 231 /// bit, subtract one from the completion context kind and shift 1 by that 232 /// number of bits. Many completions can occur in several different 233 /// contexts. 234 unsigned ShowInContexts; 235 236 /// \brief The priority given to this code-completion result. 237 unsigned Priority; 238 239 /// \brief The libclang cursor kind corresponding to this code-completion 240 /// result. 241 CXCursorKind Kind; 242 243 /// \brief The availability of this code-completion result. 244 CXAvailabilityKind Availability; 245 246 /// \brief The simplified type class for a non-macro completion result. 247 SimplifiedTypeClass TypeClass; 248 249 /// \brief The type of a non-macro completion result, stored as a unique 250 /// integer used by the string map of cached completion types. 251 /// 252 /// This value will be zero if the type is not known, or a unique value 253 /// determined by the formatted type string. Se \c CachedCompletionTypes 254 /// for more information. 255 unsigned Type; 256 }; 257 258 /// \brief Retrieve the mapping from formatted type names to unique type 259 /// identifiers. 260 llvm::StringMap<unsigned> &getCachedCompletionTypes() { 261 return CachedCompletionTypes; 262 } 263 264private: 265 /// \brief The set of cached code-completion results. 266 std::vector<CachedCodeCompletionResult> CachedCompletionResults; 267 268 /// \brief A mapping from the formatted type name to a unique number for that 269 /// type, which is used for type equality comparisons. 270 llvm::StringMap<unsigned> CachedCompletionTypes; 271 272 /// \brief The number of top-level declarations present the last time we 273 /// cached code-completion results. 274 /// 275 /// The value is used to help detect when we should repopulate the global 276 /// completion cache. 277 unsigned NumTopLevelDeclsAtLastCompletionCache; 278 279 /// \brief The number of reparses left until we'll consider updating the 280 /// code-completion cache. 281 /// 282 /// This is meant to avoid thrashing during reparsing, by not allowing the 283 /// code-completion cache to be updated on every reparse. 284 unsigned CacheCodeCompletionCoolDown; 285 286 /// \brief Bit used by CIndex to mark when a translation unit may be in an 287 /// inconsistent state, and is not safe to free. 288 unsigned UnsafeToFree : 1; 289 290 /// \brief Cache any "global" code-completion results, so that we can avoid 291 /// recomputing them with each completion. 292 void CacheCodeCompletionResults(); 293 294 /// \brief Clear out and deallocate 295 void ClearCachedCompletionResults(); 296 297 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 298 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 299 300 explicit ASTUnit(bool MainFileIsAST); 301 302 void CleanTemporaryFiles(); 303 bool Parse(llvm::MemoryBuffer *OverrideMainBuffer); 304 305 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 306 ComputePreamble(CompilerInvocation &Invocation, 307 unsigned MaxLines, bool &CreatedBuffer); 308 309 llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble( 310 CompilerInvocation PreambleInvocation, 311 bool AllowRebuild = true, 312 unsigned MaxLines = 0); 313 void RealizeTopLevelDeclsFromPreamble(); 314 315public: 316 class ConcurrencyCheck { 317 volatile ASTUnit &Self; 318 319 public: 320 explicit ConcurrencyCheck(ASTUnit &Self) 321 : Self(Self) 322 { 323 assert(Self.ConcurrencyCheckValue == CheckUnlocked && 324 "Concurrent access to ASTUnit!"); 325 Self.ConcurrencyCheckValue = CheckLocked; 326 } 327 328 ~ConcurrencyCheck() { 329 Self.ConcurrencyCheckValue = CheckUnlocked; 330 } 331 }; 332 friend class ConcurrencyCheck; 333 334 ~ASTUnit(); 335 336 bool isMainFileAST() const { return MainFileIsAST; } 337 338 bool isUnsafeToFree() const { return UnsafeToFree; } 339 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; } 340 341 const Diagnostic &getDiagnostics() const { return *Diagnostics; } 342 Diagnostic &getDiagnostics() { return *Diagnostics; } 343 344 const SourceManager &getSourceManager() const { return *SourceMgr; } 345 SourceManager &getSourceManager() { return *SourceMgr; } 346 347 const Preprocessor &getPreprocessor() const { return *PP.get(); } 348 Preprocessor &getPreprocessor() { return *PP.get(); } 349 350 const ASTContext &getASTContext() const { return *Ctx.get(); } 351 ASTContext &getASTContext() { return *Ctx.get(); } 352 353 bool hasSema() const { return TheSema; } 354 Sema &getSema() const { 355 assert(TheSema && "ASTUnit does not have a Sema object!"); 356 return *TheSema; 357 } 358 359 const FileManager &getFileManager() const { return *FileMgr; } 360 FileManager &getFileManager() { return *FileMgr; } 361 362 const std::string &getOriginalSourceFileName(); 363 const std::string &getASTFileName(); 364 365 /// \brief Add a temporary file that the ASTUnit depends on. 366 /// 367 /// This file will be erased when the ASTUnit is destroyed. 368 void addTemporaryFile(const llvm::sys::Path &TempFile) { 369 TemporaryFiles.push_back(TempFile); 370 } 371 372 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 373 374 /// \brief Retrieve the maximum PCH level of declarations that a 375 /// traversal of the translation unit should consider. 376 unsigned getMaxPCHLevel() const; 377 378 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 379 ASTLocation getLastASTLocation() const { return LastLoc; } 380 381 382 llvm::StringRef getMainFileName() const; 383 384 typedef std::vector<Decl *>::iterator top_level_iterator; 385 386 top_level_iterator top_level_begin() { 387 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 388 if (!TopLevelDeclsInPreamble.empty()) 389 RealizeTopLevelDeclsFromPreamble(); 390 return TopLevelDecls.begin(); 391 } 392 393 top_level_iterator top_level_end() { 394 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 395 if (!TopLevelDeclsInPreamble.empty()) 396 RealizeTopLevelDeclsFromPreamble(); 397 return TopLevelDecls.end(); 398 } 399 400 std::size_t top_level_size() const { 401 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 402 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size(); 403 } 404 405 bool top_level_empty() const { 406 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 407 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty(); 408 } 409 410 /// \brief Add a new top-level declaration. 411 void addTopLevelDecl(Decl *D) { 412 TopLevelDecls.push_back(D); 413 } 414 415 /// \brief Add a new top-level declaration, identified by its ID in 416 /// the precompiled preamble. 417 void addTopLevelDeclFromPreamble(serialization::DeclID D) { 418 TopLevelDeclsInPreamble.push_back(D); 419 } 420 421 /// \brief Retrieve the mapping from File IDs to the preprocessed entities 422 /// within that file. 423 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() { 424 return PreprocessedEntitiesByFile; 425 } 426 427 // Retrieve the diagnostics associated with this AST 428 typedef const StoredDiagnostic *stored_diag_iterator; 429 stored_diag_iterator stored_diag_begin() const { 430 return StoredDiagnostics.begin(); 431 } 432 stored_diag_iterator stored_diag_end() const { 433 return StoredDiagnostics.end(); 434 } 435 unsigned stored_diag_size() const { return StoredDiagnostics.size(); } 436 437 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() { 438 return StoredDiagnostics; 439 } 440 441 typedef std::vector<CachedCodeCompletionResult>::iterator 442 cached_completion_iterator; 443 444 cached_completion_iterator cached_completion_begin() { 445 return CachedCompletionResults.begin(); 446 } 447 448 cached_completion_iterator cached_completion_end() { 449 return CachedCompletionResults.end(); 450 } 451 452 unsigned cached_completion_size() const { 453 return CachedCompletionResults.size(); 454 } 455 456 /// \brief Whether this AST represents a complete translation unit. 457 /// 458 /// If false, this AST is only a partial translation unit, e.g., one 459 /// that might still be used as a precompiled header or preamble. 460 bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; } 461 462 /// \brief A mapping from a file name to the memory buffer that stores the 463 /// remapped contents of that file. 464 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile; 465 466 /// \brief Create a ASTUnit from an AST file. 467 /// 468 /// \param Filename - The AST file to load. 469 /// 470 /// \param Diags - The diagnostics engine to use for reporting errors; its 471 /// lifetime is expected to extend past that of the returned ASTUnit. 472 /// 473 /// \returns - The initialized ASTUnit or null if the AST failed to load. 474 static ASTUnit *LoadFromASTFile(const std::string &Filename, 475 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 476 bool OnlyLocalDecls = false, 477 RemappedFile *RemappedFiles = 0, 478 unsigned NumRemappedFiles = 0, 479 bool CaptureDiagnostics = false); 480 481private: 482 /// \brief Helper function for \c LoadFromCompilerInvocation() and 483 /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation. 484 /// 485 /// \param PrecompilePreamble Whether to precompile the preamble of this 486 /// translation unit, to improve the performance of reparsing. 487 /// 488 /// \returns \c true if a catastrophic failure occurred (which means that the 489 /// \c ASTUnit itself is invalid), or \c false otherwise. 490 bool LoadFromCompilerInvocation(bool PrecompilePreamble); 491 492public: 493 494 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 495 /// CompilerInvocation object. 496 /// 497 /// \param CI - The compiler invocation to use; it must have exactly one input 498 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 499 /// 500 /// \param Diags - The diagnostics engine to use for reporting errors; its 501 /// lifetime is expected to extend past that of the returned ASTUnit. 502 // 503 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 504 // shouldn't need to specify them at construction time. 505 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 506 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 507 bool OnlyLocalDecls = false, 508 bool CaptureDiagnostics = false, 509 bool PrecompilePreamble = false, 510 bool CompleteTranslationUnit = true, 511 bool CacheCodeCompletionResults = false); 512 513 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 514 /// arguments, which must specify exactly one source file. 515 /// 516 /// \param ArgBegin - The beginning of the argument vector. 517 /// 518 /// \param ArgEnd - The end of the argument vector. 519 /// 520 /// \param Diags - The diagnostics engine to use for reporting errors; its 521 /// lifetime is expected to extend past that of the returned ASTUnit. 522 /// 523 /// \param ResourceFilesPath - The path to the compiler resource files. 524 // 525 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 526 // shouldn't need to specify them at construction time. 527 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 528 const char **ArgEnd, 529 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 530 llvm::StringRef ResourceFilesPath, 531 bool OnlyLocalDecls = false, 532 RemappedFile *RemappedFiles = 0, 533 unsigned NumRemappedFiles = 0, 534 bool CaptureDiagnostics = false, 535 bool PrecompilePreamble = false, 536 bool CompleteTranslationUnit = true, 537 bool CacheCodeCompletionResults = false, 538 bool CXXPrecompilePreamble = false, 539 bool CXXChainedPCH = false); 540 541 /// \brief Reparse the source files using the same command-line options that 542 /// were originally used to produce this translation unit. 543 /// 544 /// \returns True if a failure occurred that causes the ASTUnit not to 545 /// contain any translation-unit information, false otherwise. 546 bool Reparse(RemappedFile *RemappedFiles = 0, 547 unsigned NumRemappedFiles = 0); 548 549 /// \brief Perform code completion at the given file, line, and 550 /// column within this translation unit. 551 /// 552 /// \param File The file in which code completion will occur. 553 /// 554 /// \param Line The line at which code completion will occur. 555 /// 556 /// \param Column The column at which code completion will occur. 557 /// 558 /// \param IncludeMacros Whether to include macros in the code-completion 559 /// results. 560 /// 561 /// \param IncludeCodePatterns Whether to include code patterns (such as a 562 /// for loop) in the code-completion results. 563 /// 564 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and 565 /// OwnedBuffers parameters are all disgusting hacks. They will go away. 566 void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, 567 RemappedFile *RemappedFiles, unsigned NumRemappedFiles, 568 bool IncludeMacros, bool IncludeCodePatterns, 569 CodeCompleteConsumer &Consumer, 570 Diagnostic &Diag, LangOptions &LangOpts, 571 SourceManager &SourceMgr, FileManager &FileMgr, 572 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, 573 llvm::SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers); 574 575 /// \brief Save this translation unit to a file with the given name. 576 /// 577 /// \returns True if an error occurred, false otherwise. 578 bool Save(llvm::StringRef File); 579}; 580 581} // namespace clang 582 583#endif 584