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