ASTUnit.h revision 914ed9d30e9abf829a62aa996b083b1e47c19ff6
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/Frontend/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 "llvm/ADT/IntrusiveRefCntPtr.h" 24#include "llvm/ADT/OwningPtr.h" 25#include "llvm/ADT/SmallVector.h" 26#include "llvm/ADT/StringMap.h" 27#include "llvm/System/Path.h" 28#include "llvm/Support/Timer.h" 29#include <map> 30#include <string> 31#include <vector> 32#include <cassert> 33#include <utility> 34#include <sys/types.h> 35 36namespace llvm { 37 class MemoryBuffer; 38} 39 40namespace clang { 41class ASTContext; 42class CodeCompleteConsumer; 43class CompilerInvocation; 44class Decl; 45class Diagnostic; 46class FileEntry; 47class FileManager; 48class HeaderSearch; 49class Preprocessor; 50class SourceManager; 51class TargetInfo; 52 53using namespace idx; 54 55/// \brief Utility class for loading a ASTContext from a PCH file. 56/// 57class ASTUnit { 58public: 59 typedef std::map<FileID, std::vector<PreprocessedEntity *> > 60 PreprocessedEntitiesByFileMap; 61private: 62 llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics; 63 llvm::OwningPtr<FileManager> FileMgr; 64 llvm::OwningPtr<SourceManager> SourceMgr; 65 llvm::OwningPtr<HeaderSearch> HeaderInfo; 66 llvm::OwningPtr<TargetInfo> Target; 67 llvm::OwningPtr<Preprocessor> PP; 68 llvm::OwningPtr<ASTContext> Ctx; 69 70 /// \brief The AST consumer that received information about the translation 71 /// unit as it was parsed or loaded. 72 llvm::OwningPtr<ASTConsumer> Consumer; 73 74 /// \brief The semantic analysis object used to type-check the translation 75 /// unit. 76 llvm::OwningPtr<Sema> TheSema; 77 78 /// Optional owned invocation, just used to make the invocation used in 79 /// LoadFromCommandLine available. 80 llvm::OwningPtr<CompilerInvocation> Invocation; 81 82 // OnlyLocalDecls - when true, walking this AST should only visit declarations 83 // that come from the AST itself, not from included precompiled headers. 84 // FIXME: This is temporary; eventually, CIndex will always do this. 85 bool OnlyLocalDecls; 86 87 /// \brief Whether to capture any diagnostics produced. 88 bool CaptureDiagnostics; 89 90 /// \brief Track whether the main file was loaded from an AST or not. 91 bool MainFileIsAST; 92 93 /// \brief Whether this AST represents a complete translation unit. 94 bool CompleteTranslationUnit; 95 96 /// Track the top-level decls which appeared in an ASTUnit which was loaded 97 /// from a source file. 98 // 99 // FIXME: This is just an optimization hack to avoid deserializing large parts 100 // of a PCH file when using the Index library on an ASTUnit loaded from 101 // source. In the long term we should make the Index library use efficient and 102 // more scalable search mechanisms. 103 std::vector<Decl*> TopLevelDecls; 104 105 /// The name of the original source file used to generate this ASTUnit. 106 std::string OriginalSourceFile; 107 108 // Critical optimization when using clang_getCursor(). 109 ASTLocation LastLoc; 110 111 /// \brief The set of diagnostics produced when creating this 112 /// translation unit. 113 llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics; 114 115 /// \brief Temporary files that should be removed when the ASTUnit is 116 /// destroyed. 117 llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles; 118 119 /// \brief A mapping from file IDs to the set of preprocessed entities 120 /// stored in that file. 121 /// 122 /// FIXME: This is just an optimization hack to avoid searching through 123 /// many preprocessed entities during cursor traversal in the CIndex library. 124 /// Ideally, we would just be able to perform a binary search within the 125 /// list of preprocessed entities. 126 PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile; 127 128 /// \brief Simple hack to allow us to assert that ASTUnit is not being 129 /// used concurrently, which is not supported. 130 /// 131 /// Clients should create instances of the ConcurrencyCheck class whenever 132 /// using the ASTUnit in a way that isn't intended to be concurrent, which is 133 /// just about any usage. 134 unsigned int ConcurrencyCheckValue; 135 static const unsigned int CheckLocked = 28573289; 136 static const unsigned int CheckUnlocked = 9803453; 137 138 /// \brief Counter that determines when we want to try building a 139 /// precompiled preamble. 140 /// 141 /// If zero, we will never build a precompiled preamble. Otherwise, 142 /// it's treated as a counter that decrements each time we reparse 143 /// without the benefit of a precompiled preamble. When it hits 1, 144 /// we'll attempt to rebuild the precompiled header. This way, if 145 /// building the precompiled preamble fails, we won't try again for 146 /// some number of calls. 147 unsigned PreambleRebuildCounter; 148 149 /// \brief The file in which the precompiled preamble is stored. 150 std::string PreambleFile; 151 152 /// \brief The contents of the preamble that has been precompiled to 153 /// \c PreambleFile. 154 std::vector<char> Preamble; 155 156 /// \brief Whether the preamble ends at the start of a new line. 157 /// 158 /// Used to inform the lexer as to whether it's starting at the beginning of 159 /// a line after skipping the preamble. 160 bool PreambleEndsAtStartOfLine; 161 162 /// \brief The size of the source buffer that we've reserved for the main 163 /// file within the precompiled preamble. 164 unsigned PreambleReservedSize; 165 166 /// \brief Keeps track of the files that were used when computing the 167 /// preamble, with both their buffer size and their modification time. 168 /// 169 /// If any of the files have changed from one compile to the next, 170 /// the preamble must be thrown away. 171 llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble; 172 173 /// \brief When non-NULL, this is the buffer used to store the contents of 174 /// the main file when it has been padded for use with the precompiled 175 /// preamble. 176 llvm::MemoryBuffer *SavedMainFileBuffer; 177 178 /// \brief The number of warnings that occurred while parsing the preamble. 179 /// 180 /// This value will be used to restore the state of the \c Diagnostic object 181 /// when re-using the precompiled preamble. Note that only the 182 /// number of warnings matters, since we will not save the preamble 183 /// when any errors are present. 184 unsigned NumWarningsInPreamble; 185 186 /// \brief The number of diagnostics that were stored when parsing 187 /// the precompiled preamble. 188 /// 189 /// This value is used to determine how many of the stored 190 /// diagnostics should be retained when reparsing in the presence of 191 /// a precompiled preamble. 192 unsigned NumStoredDiagnosticsInPreamble; 193 194 /// \brief The group of timers associated with this translation unit. 195 llvm::OwningPtr<llvm::TimerGroup> TimerGroup; 196 197 /// \brief A list of the PCH ID numbers for each of the top-level 198 /// declarations parsed within the precompiled preamble. 199 std::vector<pch::DeclID> TopLevelDeclsInPreamble; 200 201 /// \brief The timers we've created from the various parses, reparses, etc. 202 /// involved in this translation unit. 203 std::vector<llvm::Timer *> Timers; 204 205 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 206 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 207 208 explicit ASTUnit(bool MainFileIsAST); 209 210 void CleanTemporaryFiles(); 211 bool Parse(llvm::MemoryBuffer *OverrideMainBuffer); 212 213 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 214 ComputePreamble(CompilerInvocation &Invocation, 215 unsigned MaxLines, bool &CreatedBuffer); 216 217 llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble( 218 bool AllowRebuild = true, 219 unsigned MaxLines = 0); 220 void RealizeTopLevelDeclsFromPreamble(); 221 222public: 223 class ConcurrencyCheck { 224 volatile ASTUnit &Self; 225 226 public: 227 explicit ConcurrencyCheck(ASTUnit &Self) 228 : Self(Self) 229 { 230 assert(Self.ConcurrencyCheckValue == CheckUnlocked && 231 "Concurrent access to ASTUnit!"); 232 Self.ConcurrencyCheckValue = CheckLocked; 233 } 234 235 ~ConcurrencyCheck() { 236 Self.ConcurrencyCheckValue = CheckUnlocked; 237 } 238 }; 239 friend class ConcurrencyCheck; 240 241 ~ASTUnit(); 242 243 bool isMainFileAST() const { return MainFileIsAST; } 244 245 const Diagnostic &getDiagnostics() const { return *Diagnostics; } 246 Diagnostic &getDiagnostics() { return *Diagnostics; } 247 248 const SourceManager &getSourceManager() const { return *SourceMgr; } 249 SourceManager &getSourceManager() { return *SourceMgr; } 250 251 const Preprocessor &getPreprocessor() const { return *PP.get(); } 252 Preprocessor &getPreprocessor() { return *PP.get(); } 253 254 const ASTContext &getASTContext() const { return *Ctx.get(); } 255 ASTContext &getASTContext() { return *Ctx.get(); } 256 257 bool hasSema() const { return TheSema; } 258 Sema &getSema() const { 259 assert(TheSema && "ASTUnit does not have a Sema object!"); 260 return *TheSema; 261 } 262 263 const FileManager &getFileManager() const { return *FileMgr; } 264 FileManager &getFileManager() { return *FileMgr; } 265 266 const std::string &getOriginalSourceFileName(); 267 const std::string &getPCHFileName(); 268 269 /// \brief Add a temporary file that the ASTUnit depends on. 270 /// 271 /// This file will be erased when the ASTUnit is destroyed. 272 void addTemporaryFile(const llvm::sys::Path &TempFile) { 273 TemporaryFiles.push_back(TempFile); 274 } 275 276 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 277 278 /// \brief Retrieve the maximum PCH level of declarations that a 279 /// traversal of the translation unit should consider. 280 unsigned getMaxPCHLevel() const; 281 282 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 283 ASTLocation getLastASTLocation() const { return LastLoc; } 284 285 typedef std::vector<Decl *>::iterator top_level_iterator; 286 287 top_level_iterator top_level_begin() { 288 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 289 if (!TopLevelDeclsInPreamble.empty()) 290 RealizeTopLevelDeclsFromPreamble(); 291 return TopLevelDecls.begin(); 292 } 293 294 top_level_iterator top_level_end() { 295 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 296 if (!TopLevelDeclsInPreamble.empty()) 297 RealizeTopLevelDeclsFromPreamble(); 298 return TopLevelDecls.end(); 299 } 300 301 std::size_t top_level_size() const { 302 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 303 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size(); 304 } 305 306 bool top_level_empty() const { 307 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 308 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty(); 309 } 310 311 /// \brief Add a new top-level declaration. 312 void addTopLevelDecl(Decl *D) { 313 TopLevelDecls.push_back(D); 314 } 315 316 /// \brief Add a new top-level declaration, identified by its ID in 317 /// the precompiled preamble. 318 void addTopLevelDeclFromPreamble(pch::DeclID D) { 319 TopLevelDeclsInPreamble.push_back(D); 320 } 321 322 /// \brief Retrieve the mapping from File IDs to the preprocessed entities 323 /// within that file. 324 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() { 325 return PreprocessedEntitiesByFile; 326 } 327 328 // Retrieve the diagnostics associated with this AST 329 typedef const StoredDiagnostic *stored_diag_iterator; 330 stored_diag_iterator stored_diag_begin() const { 331 return StoredDiagnostics.begin(); 332 } 333 stored_diag_iterator stored_diag_end() const { 334 return StoredDiagnostics.end(); 335 } 336 unsigned stored_diag_size() const { return StoredDiagnostics.size(); } 337 338 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() { 339 return StoredDiagnostics; 340 } 341 342 /// \brief Whether this AST represents a complete translation unit. 343 /// 344 /// If false, this AST is only a partial translation unit, e.g., one 345 /// that might still be used as a precompiled header or preamble. 346 bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; } 347 348 /// \brief A mapping from a file name to the memory buffer that stores the 349 /// remapped contents of that file. 350 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile; 351 352 /// \brief Create a ASTUnit from a PCH file. 353 /// 354 /// \param Filename - The PCH file to load. 355 /// 356 /// \param Diags - The diagnostics engine to use for reporting errors; its 357 /// lifetime is expected to extend past that of the returned ASTUnit. 358 /// 359 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 360 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 361 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 362 bool OnlyLocalDecls = false, 363 RemappedFile *RemappedFiles = 0, 364 unsigned NumRemappedFiles = 0, 365 bool CaptureDiagnostics = false); 366 367 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 368 /// CompilerInvocation object. 369 /// 370 /// \param CI - The compiler invocation to use; it must have exactly one input 371 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 372 /// 373 /// \param Diags - The diagnostics engine to use for reporting errors; its 374 /// lifetime is expected to extend past that of the returned ASTUnit. 375 // 376 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 377 // shouldn't need to specify them at construction time. 378 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 379 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 380 bool OnlyLocalDecls = false, 381 bool CaptureDiagnostics = false, 382 bool PrecompilePreamble = false, 383 bool CompleteTranslationUnit = true); 384 385 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 386 /// arguments, which must specify exactly one source file. 387 /// 388 /// \param ArgBegin - The beginning of the argument vector. 389 /// 390 /// \param ArgEnd - The end of the argument vector. 391 /// 392 /// \param Diags - The diagnostics engine to use for reporting errors; its 393 /// lifetime is expected to extend past that of the returned ASTUnit. 394 /// 395 /// \param ResourceFilesPath - The path to the compiler resource files. 396 // 397 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 398 // shouldn't need to specify them at construction time. 399 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 400 const char **ArgEnd, 401 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 402 llvm::StringRef ResourceFilesPath, 403 bool OnlyLocalDecls = false, 404 RemappedFile *RemappedFiles = 0, 405 unsigned NumRemappedFiles = 0, 406 bool CaptureDiagnostics = false, 407 bool PrecompilePreamble = false, 408 bool CompleteTranslationUnit = true); 409 410 /// \brief Reparse the source files using the same command-line options that 411 /// were originally used to produce this translation unit. 412 /// 413 /// \returns True if a failure occurred that causes the ASTUnit not to 414 /// contain any translation-unit information, false otherwise. 415 bool Reparse(RemappedFile *RemappedFiles = 0, 416 unsigned NumRemappedFiles = 0); 417 418 /// \brief Perform code completion at the given file, line, and 419 /// column within this translation unit. 420 /// 421 /// \param File The file in which code completion will occur. 422 /// 423 /// \param Line The line at which code completion will occur. 424 /// 425 /// \param Column The column at which code completion will occur. 426 /// 427 /// \param IncludeMacros Whether to include macros in the code-completion 428 /// results. 429 /// 430 /// \param IncludeCodePatterns Whether to include code patterns (such as a 431 /// for loop) in the code-completion results. 432 /// 433 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, and 434 /// StoredDiagnostics parameters are all disgusting hacks. They will 435 /// go away. 436 void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, 437 RemappedFile *RemappedFiles, unsigned NumRemappedFiles, 438 bool IncludeMacros, bool IncludeCodePatterns, 439 CodeCompleteConsumer &Consumer, 440 Diagnostic &Diag, LangOptions &LangOpts, 441 SourceManager &SourceMgr, FileManager &FileMgr, 442 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics); 443}; 444 445} // namespace clang 446 447#endif 448