ASTUnit.h revision ea94bbc4769697143e717df9b0310f874102b6c1
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/PCHBitCodes.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 a PCH 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 The number of warnings that occurred while parsing the preamble. 181 /// 182 /// This value will be used to restore the state of the \c Diagnostic object 183 /// when re-using the precompiled preamble. Note that only the 184 /// number of warnings matters, since we will not save the preamble 185 /// when any errors are present. 186 unsigned NumWarningsInPreamble; 187 188 /// \brief The number of diagnostics that were stored when parsing 189 /// the precompiled preamble. 190 /// 191 /// This value is used to determine how many of the stored 192 /// diagnostics should be retained when reparsing in the presence of 193 /// a precompiled preamble. 194 unsigned NumStoredDiagnosticsInPreamble; 195 196 /// \brief The group of timers associated with this translation unit. 197 llvm::OwningPtr<llvm::TimerGroup> TimerGroup; 198 199 /// \brief A list of the PCH ID numbers for each of the top-level 200 /// declarations parsed within the precompiled preamble. 201 std::vector<pch::DeclID> TopLevelDeclsInPreamble; 202 203 /// 204 /// \defgroup CodeCompleteCaching Code-completion caching 205 /// 206 /// \{ 207 /// 208 209 /// \brief Whether we should be caching code-completion results. 210 bool ShouldCacheCodeCompletionResults; 211 212public: 213 /// \brief A cached code-completion result, which may be introduced in one of 214 /// many different contexts. 215 struct CachedCodeCompletionResult { 216 /// \brief The code-completion string corresponding to this completion 217 /// result. 218 CodeCompletionString *Completion; 219 220 /// \brief A bitmask that indicates which code-completion contexts should 221 /// contain this completion result. 222 /// 223 /// The bits in the bitmask correspond to the values of 224 /// CodeCompleteContext::Kind. To map from a completion context kind to a 225 /// bit, subtract one from the completion context kind and shift 1 by that 226 /// number of bits. Many completions can occur in several different 227 /// contexts. 228 unsigned ShowInContexts; 229 230 /// \brief The priority given to this code-completion result. 231 unsigned Priority; 232 233 /// \brief The libclang cursor kind corresponding to this code-completion 234 /// result. 235 CXCursorKind Kind; 236 237 /// \brief The simplified type class for a non-macro completion result. 238 SimplifiedTypeClass TypeClass; 239 240 /// \brief The type of a non-macro completion result, stored as a unique 241 /// integer used by the string map of cached completion types. 242 /// 243 /// This value will be zero if the type is not known, or a unique value 244 /// determined by the formatted type string. Se \c CachedCompletionTypes 245 /// for more information. 246 unsigned Type; 247 }; 248 249 /// \brief Retrieve the mapping from formatted type names to unique type 250 /// identifiers. 251 llvm::StringMap<unsigned> &getCachedCompletionTypes() { 252 return CachedCompletionTypes; 253 } 254 255private: 256 /// \brief The set of cached code-completion results. 257 std::vector<CachedCodeCompletionResult> CachedCompletionResults; 258 259 /// \brief A mapping from the formatted type name to a unique number for that 260 /// type, which is used for type equality comparisons. 261 llvm::StringMap<unsigned> CachedCompletionTypes; 262 263 /// \brief The number of top-level declarations present the last time we 264 /// cached code-completion results. 265 /// 266 /// The value is used to help detect when we should repopulate the global 267 /// completion cache. 268 unsigned NumTopLevelDeclsAtLastCompletionCache; 269 270 /// \brief The number of reparses left until we'll consider updating the 271 /// code-completion cache. 272 /// 273 /// This is meant to avoid thrashing during reparsing, by not allowing the 274 /// code-completion cache to be updated on every reparse. 275 unsigned CacheCodeCompletionCoolDown; 276 277 /// \brief Bit used by CIndex to mark when a translation unit may be in an 278 /// inconsistent state, and is not safe to free. 279 unsigned UnsafeToFree : 1; 280 281 /// \brief Cache any "global" code-completion results, so that we can avoid 282 /// recomputing them with each completion. 283 void CacheCodeCompletionResults(); 284 285 /// \brief Clear out and deallocate 286 void ClearCachedCompletionResults(); 287 288 /// 289 /// \} 290 /// 291 292 /// \brief The timers we've created from the various parses, reparses, etc. 293 /// involved in this translation unit. 294 std::vector<llvm::Timer *> Timers; 295 296 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 297 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 298 299 explicit ASTUnit(bool MainFileIsAST); 300 301 void CleanTemporaryFiles(); 302 bool Parse(llvm::MemoryBuffer *OverrideMainBuffer); 303 304 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 305 ComputePreamble(CompilerInvocation &Invocation, 306 unsigned MaxLines, bool &CreatedBuffer); 307 308 llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble( 309 bool AllowRebuild = true, 310 unsigned MaxLines = 0); 311 void RealizeTopLevelDeclsFromPreamble(); 312 313public: 314 class ConcurrencyCheck { 315 volatile ASTUnit &Self; 316 317 public: 318 explicit ConcurrencyCheck(ASTUnit &Self) 319 : Self(Self) 320 { 321 assert(Self.ConcurrencyCheckValue == CheckUnlocked && 322 "Concurrent access to ASTUnit!"); 323 Self.ConcurrencyCheckValue = CheckLocked; 324 } 325 326 ~ConcurrencyCheck() { 327 Self.ConcurrencyCheckValue = CheckUnlocked; 328 } 329 }; 330 friend class ConcurrencyCheck; 331 332 ~ASTUnit(); 333 334 bool isMainFileAST() const { return MainFileIsAST; } 335 336 bool isUnsafeToFree() const { return UnsafeToFree; } 337 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; } 338 339 const Diagnostic &getDiagnostics() const { return *Diagnostics; } 340 Diagnostic &getDiagnostics() { return *Diagnostics; } 341 342 const SourceManager &getSourceManager() const { return *SourceMgr; } 343 SourceManager &getSourceManager() { return *SourceMgr; } 344 345 const Preprocessor &getPreprocessor() const { return *PP.get(); } 346 Preprocessor &getPreprocessor() { return *PP.get(); } 347 348 const ASTContext &getASTContext() const { return *Ctx.get(); } 349 ASTContext &getASTContext() { return *Ctx.get(); } 350 351 bool hasSema() const { return TheSema; } 352 Sema &getSema() const { 353 assert(TheSema && "ASTUnit does not have a Sema object!"); 354 return *TheSema; 355 } 356 357 const FileManager &getFileManager() const { return *FileMgr; } 358 FileManager &getFileManager() { return *FileMgr; } 359 360 const std::string &getOriginalSourceFileName(); 361 const std::string &getPCHFileName(); 362 363 /// \brief Add a temporary file that the ASTUnit depends on. 364 /// 365 /// This file will be erased when the ASTUnit is destroyed. 366 void addTemporaryFile(const llvm::sys::Path &TempFile) { 367 TemporaryFiles.push_back(TempFile); 368 } 369 370 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 371 372 /// \brief Retrieve the maximum PCH level of declarations that a 373 /// traversal of the translation unit should consider. 374 unsigned getMaxPCHLevel() const; 375 376 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 377 ASTLocation getLastASTLocation() const { return LastLoc; } 378 379 typedef std::vector<Decl *>::iterator top_level_iterator; 380 381 top_level_iterator top_level_begin() { 382 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 383 if (!TopLevelDeclsInPreamble.empty()) 384 RealizeTopLevelDeclsFromPreamble(); 385 return TopLevelDecls.begin(); 386 } 387 388 top_level_iterator top_level_end() { 389 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 390 if (!TopLevelDeclsInPreamble.empty()) 391 RealizeTopLevelDeclsFromPreamble(); 392 return TopLevelDecls.end(); 393 } 394 395 std::size_t top_level_size() const { 396 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 397 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size(); 398 } 399 400 bool top_level_empty() const { 401 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 402 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty(); 403 } 404 405 /// \brief Add a new top-level declaration. 406 void addTopLevelDecl(Decl *D) { 407 TopLevelDecls.push_back(D); 408 } 409 410 /// \brief Add a new top-level declaration, identified by its ID in 411 /// the precompiled preamble. 412 void addTopLevelDeclFromPreamble(pch::DeclID D) { 413 TopLevelDeclsInPreamble.push_back(D); 414 } 415 416 /// \brief Retrieve the mapping from File IDs to the preprocessed entities 417 /// within that file. 418 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() { 419 return PreprocessedEntitiesByFile; 420 } 421 422 // Retrieve the diagnostics associated with this AST 423 typedef const StoredDiagnostic *stored_diag_iterator; 424 stored_diag_iterator stored_diag_begin() const { 425 return StoredDiagnostics.begin(); 426 } 427 stored_diag_iterator stored_diag_end() const { 428 return StoredDiagnostics.end(); 429 } 430 unsigned stored_diag_size() const { return StoredDiagnostics.size(); } 431 432 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() { 433 return StoredDiagnostics; 434 } 435 436 typedef std::vector<CachedCodeCompletionResult>::iterator 437 cached_completion_iterator; 438 439 cached_completion_iterator cached_completion_begin() { 440 return CachedCompletionResults.begin(); 441 } 442 443 cached_completion_iterator cached_completion_end() { 444 return CachedCompletionResults.end(); 445 } 446 447 unsigned cached_completion_size() const { 448 return CachedCompletionResults.size(); 449 } 450 451 /// \brief Whether this AST represents a complete translation unit. 452 /// 453 /// If false, this AST is only a partial translation unit, e.g., one 454 /// that might still be used as a precompiled header or preamble. 455 bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; } 456 457 /// \brief A mapping from a file name to the memory buffer that stores the 458 /// remapped contents of that file. 459 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile; 460 461 /// \brief Create a ASTUnit from a PCH file. 462 /// 463 /// \param Filename - The PCH file to load. 464 /// 465 /// \param Diags - The diagnostics engine to use for reporting errors; its 466 /// lifetime is expected to extend past that of the returned ASTUnit. 467 /// 468 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 469 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 470 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 471 bool OnlyLocalDecls = false, 472 RemappedFile *RemappedFiles = 0, 473 unsigned NumRemappedFiles = 0, 474 bool CaptureDiagnostics = false); 475 476 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 477 /// CompilerInvocation object. 478 /// 479 /// \param CI - The compiler invocation to use; it must have exactly one input 480 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 481 /// 482 /// \param Diags - The diagnostics engine to use for reporting errors; its 483 /// lifetime is expected to extend past that of the returned ASTUnit. 484 // 485 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 486 // shouldn't need to specify them at construction time. 487 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 488 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 489 bool OnlyLocalDecls = false, 490 bool CaptureDiagnostics = false, 491 bool PrecompilePreamble = false, 492 bool CompleteTranslationUnit = true, 493 bool CacheCodeCompletionResults = false); 494 495 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 496 /// arguments, which must specify exactly one source file. 497 /// 498 /// \param ArgBegin - The beginning of the argument vector. 499 /// 500 /// \param ArgEnd - The end of the argument vector. 501 /// 502 /// \param Diags - The diagnostics engine to use for reporting errors; its 503 /// lifetime is expected to extend past that of the returned ASTUnit. 504 /// 505 /// \param ResourceFilesPath - The path to the compiler resource files. 506 // 507 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 508 // shouldn't need to specify them at construction time. 509 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 510 const char **ArgEnd, 511 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 512 llvm::StringRef ResourceFilesPath, 513 bool OnlyLocalDecls = false, 514 RemappedFile *RemappedFiles = 0, 515 unsigned NumRemappedFiles = 0, 516 bool CaptureDiagnostics = false, 517 bool PrecompilePreamble = false, 518 bool CompleteTranslationUnit = true, 519 bool CacheCodeCompletionResults = false); 520 521 /// \brief Reparse the source files using the same command-line options that 522 /// were originally used to produce this translation unit. 523 /// 524 /// \returns True if a failure occurred that causes the ASTUnit not to 525 /// contain any translation-unit information, false otherwise. 526 bool Reparse(RemappedFile *RemappedFiles = 0, 527 unsigned NumRemappedFiles = 0); 528 529 /// \brief Perform code completion at the given file, line, and 530 /// column within this translation unit. 531 /// 532 /// \param File The file in which code completion will occur. 533 /// 534 /// \param Line The line at which code completion will occur. 535 /// 536 /// \param Column The column at which code completion will occur. 537 /// 538 /// \param IncludeMacros Whether to include macros in the code-completion 539 /// results. 540 /// 541 /// \param IncludeCodePatterns Whether to include code patterns (such as a 542 /// for loop) in the code-completion results. 543 /// 544 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, and 545 /// StoredDiagnostics parameters are all disgusting hacks. They will 546 /// go away. 547 void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, 548 RemappedFile *RemappedFiles, unsigned NumRemappedFiles, 549 bool IncludeMacros, bool IncludeCodePatterns, 550 CodeCompleteConsumer &Consumer, 551 Diagnostic &Diag, LangOptions &LangOpts, 552 SourceManager &SourceMgr, FileManager &FileMgr, 553 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics); 554 555 /// \brief Save this translation unit to a file with the given name. 556 /// 557 /// \returns True if an error occurred, false otherwise. 558 bool Save(llvm::StringRef File); 559}; 560 561} // namespace clang 562 563#endif 564