ASTUnit.h revision d99ef536b241071b6f4c01db6525dc03242ac30b
1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===// 2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// The LLVM Compiler Infrastructure 4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// License. See LICENSE.TXT for details. 7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//===----------------------------------------------------------------------===// 9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// ASTUnit utility class. 11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)//===----------------------------------------------------------------------===// 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define LLVM_CLANG_FRONTEND_ASTUNIT_H 165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "clang/Serialization/ASTBitCodes.h" 18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Sema/Sema.h" 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Sema/CodeCompleteConsumer.h" 20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Lex/ModuleLoader.h" 21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Lex/PreprocessingRecord.h" 22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Basic/LangOptions.h" 23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Basic/SourceManager.h" 24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "clang/Basic/FileManager.h" 25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang/Basic/FileSystemOptions.h" 26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "clang-c/Index.h" 27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/IntrusiveRefCntPtr.h" 28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/OwningPtr.h" 29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/ADT/SmallVector.h" 305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "llvm/ADT/StringMap.h" 315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "llvm/Support/Path.h" 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <map> 33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <string> 34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <vector> 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <cassert> 365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <utility> 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <sys/types.h> 38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace llvm { 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch class MemoryBuffer; 41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)} 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace clang { 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ASTContext; 45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ASTReader; 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class CodeCompleteConsumer; 475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class CompilerInvocation; 48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class CompilerInstance; 49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class Decl; 50116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass DiagnosticsEngine; 51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class FileEntry; 52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class FileManager; 53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class HeaderSearch; 54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class Preprocessor; 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class SourceManager; 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TargetInfo; 57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ASTFrontendAction; 58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)/// \brief Utility class for loading a ASTContext from an AST file. 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// 61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class ASTUnit : public ModuleLoader { 62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)private: 635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) IntrusiveRefCntPtr<LangOptions> LangOpts; 645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) IntrusiveRefCntPtr<DiagnosticsEngine> Diagnostics; 655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) IntrusiveRefCntPtr<FileManager> FileMgr; 665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) IntrusiveRefCntPtr<SourceManager> SourceMgr; 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) OwningPtr<HeaderSearch> HeaderInfo; 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) IntrusiveRefCntPtr<TargetInfo> Target; 69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) IntrusiveRefCntPtr<Preprocessor> PP; 70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) IntrusiveRefCntPtr<ASTContext> Ctx; 71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ASTReader *Reader; 72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch FileSystemOptions FileSystemOpts; 74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief The AST consumer that received information about the translation 76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// unit as it was parsed or loaded. 77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) OwningPtr<ASTConsumer> Consumer; 78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// \brief The semantic analysis object used to type-check the translation 80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// unit. 81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) OwningPtr<Sema> TheSema; 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// Optional owned invocation, just used to make the invocation used in 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// LoadFromCommandLine available. 85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) IntrusiveRefCntPtr<CompilerInvocation> Invocation; 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// \brief The set of target features. 88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// 89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// FIXME: each time we reparse, we need to restore the set of target 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// features from this vector, because TargetInfo::CreateTargetInfo() 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// mangles the target options in place. Yuck! 92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::vector<std::string> TargetFeatures; 93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // OnlyLocalDecls - when true, walking this AST should only visit declarations 95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // that come from the AST itself, not from included precompiled headers. 96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // FIXME: This is temporary; eventually, CIndex will always do this. 97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool OnlyLocalDecls; 98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief Whether to capture any diagnostics produced. 100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool CaptureDiagnostics; 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief Track whether the main file was loaded from an AST or not. 103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool MainFileIsAST; 104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief What kind of translation unit this AST represents. 106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) TranslationUnitKind TUKind; 107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief Whether we should time each operation. 109 bool WantTiming; 110 111 /// \brief Whether the ASTUnit should delete the remapped buffers. 112 bool OwnsRemappedFileBuffers; 113 114 /// Track the top-level decls which appeared in an ASTUnit which was loaded 115 /// from a source file. 116 // 117 // FIXME: This is just an optimization hack to avoid deserializing large parts 118 // of a PCH file when using the Index library on an ASTUnit loaded from 119 // source. In the long term we should make the Index library use efficient and 120 // more scalable search mechanisms. 121 std::vector<Decl*> TopLevelDecls; 122 123 /// \brief Sorted (by file offset) vector of pairs of file offset/Decl. 124 typedef SmallVector<std::pair<unsigned, Decl *>, 64> LocDeclsTy; 125 typedef llvm::DenseMap<FileID, LocDeclsTy *> FileDeclsTy; 126 127 /// \brief Map from FileID to the file-level declarations that it contains. 128 /// The files and decls are only local (and non-preamble) ones. 129 FileDeclsTy FileDecls; 130 131 /// The name of the original source file used to generate this ASTUnit. 132 std::string OriginalSourceFile; 133 134 /// \brief The set of diagnostics produced when creating the preamble. 135 SmallVector<StoredDiagnostic, 4> PreambleDiagnostics; 136 137 /// \brief The set of diagnostics produced when creating this 138 /// translation unit. 139 SmallVector<StoredDiagnostic, 4> StoredDiagnostics; 140 141 /// \brief The set of diagnostics produced when failing to parse, e.g. due 142 /// to failure to load the PCH. 143 SmallVector<StoredDiagnostic, 4> FailedParseDiagnostics; 144 145 /// \brief The number of stored diagnostics that come from the driver 146 /// itself. 147 /// 148 /// Diagnostics that come from the driver are retained from one parse to 149 /// the next. 150 unsigned NumStoredDiagnosticsFromDriver; 151 152 /// \brief Counter that determines when we want to try building a 153 /// precompiled preamble. 154 /// 155 /// If zero, we will never build a precompiled preamble. Otherwise, 156 /// it's treated as a counter that decrements each time we reparse 157 /// without the benefit of a precompiled preamble. When it hits 1, 158 /// we'll attempt to rebuild the precompiled header. This way, if 159 /// building the precompiled preamble fails, we won't try again for 160 /// some number of calls. 161 unsigned PreambleRebuildCounter; 162 163public: 164 class PreambleData { 165 const FileEntry *File; 166 std::vector<char> Buffer; 167 mutable unsigned NumLines; 168 169 public: 170 PreambleData() : File(0), NumLines(0) { } 171 172 void assign(const FileEntry *F, const char *begin, const char *end) { 173 File = F; 174 Buffer.assign(begin, end); 175 NumLines = 0; 176 } 177 178 void clear() { Buffer.clear(); File = 0; NumLines = 0; } 179 180 size_t size() const { return Buffer.size(); } 181 bool empty() const { return Buffer.empty(); } 182 183 const char *getBufferStart() const { return &Buffer[0]; } 184 185 unsigned getNumLines() const { 186 if (NumLines) 187 return NumLines; 188 countLines(); 189 return NumLines; 190 } 191 192 SourceRange getSourceRange(const SourceManager &SM) const { 193 SourceLocation FileLoc = SM.getLocForStartOfFile(SM.getPreambleFileID()); 194 return SourceRange(FileLoc, FileLoc.getLocWithOffset(size()-1)); 195 } 196 197 private: 198 void countLines() const; 199 }; 200 201 const PreambleData &getPreambleData() const { 202 return Preamble; 203 } 204 205private: 206 207 /// \brief The contents of the preamble that has been precompiled to 208 /// \c PreambleFile. 209 PreambleData Preamble; 210 211 /// \brief Whether the preamble ends at the start of a new line. 212 /// 213 /// Used to inform the lexer as to whether it's starting at the beginning of 214 /// a line after skipping the preamble. 215 bool PreambleEndsAtStartOfLine; 216 217 /// \brief The size of the source buffer that we've reserved for the main 218 /// file within the precompiled preamble. 219 unsigned PreambleReservedSize; 220 221 /// \brief Keeps track of the files that were used when computing the 222 /// preamble, with both their buffer size and their modification time. 223 /// 224 /// If any of the files have changed from one compile to the next, 225 /// the preamble must be thrown away. 226 llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble; 227 228 /// \brief When non-NULL, this is the buffer used to store the contents of 229 /// the main file when it has been padded for use with the precompiled 230 /// preamble. 231 llvm::MemoryBuffer *SavedMainFileBuffer; 232 233 /// \brief When non-NULL, this is the buffer used to store the 234 /// contents of the preamble when it has been padded to build the 235 /// precompiled preamble. 236 llvm::MemoryBuffer *PreambleBuffer; 237 238 /// \brief The number of warnings that occurred while parsing the preamble. 239 /// 240 /// This value will be used to restore the state of the \c DiagnosticsEngine 241 /// object when re-using the precompiled preamble. Note that only the 242 /// number of warnings matters, since we will not save the preamble 243 /// when any errors are present. 244 unsigned NumWarningsInPreamble; 245 246 /// \brief A list of the serialization ID numbers for each of the top-level 247 /// declarations parsed within the precompiled preamble. 248 std::vector<serialization::DeclID> TopLevelDeclsInPreamble; 249 250 /// \brief Whether we should be caching code-completion results. 251 bool ShouldCacheCodeCompletionResults : 1; 252 253 /// \brief Whether to include brief documentation within the set of code 254 /// completions cached. 255 bool IncludeBriefCommentsInCodeCompletion : 1; 256 257 /// \brief The language options used when we load an AST file. 258 LangOptions ASTFileLangOpts; 259 260 static void ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> &Diags, 261 const char **ArgBegin, const char **ArgEnd, 262 ASTUnit &AST, bool CaptureDiagnostics); 263 264 void TranslateStoredDiagnostics(ASTReader *MMan, StringRef ModName, 265 SourceManager &SrcMan, 266 const SmallVectorImpl<StoredDiagnostic> &Diags, 267 SmallVectorImpl<StoredDiagnostic> &Out); 268 269 void clearFileLevelDecls(); 270 271public: 272 /// \brief A cached code-completion result, which may be introduced in one of 273 /// many different contexts. 274 struct CachedCodeCompletionResult { 275 /// \brief The code-completion string corresponding to this completion 276 /// result. 277 CodeCompletionString *Completion; 278 279 /// \brief A bitmask that indicates which code-completion contexts should 280 /// contain this completion result. 281 /// 282 /// The bits in the bitmask correspond to the values of 283 /// CodeCompleteContext::Kind. To map from a completion context kind to a 284 /// bit, subtract one from the completion context kind and shift 1 by that 285 /// number of bits. Many completions can occur in several different 286 /// contexts. 287 unsigned ShowInContexts; 288 289 /// \brief The priority given to this code-completion result. 290 unsigned Priority; 291 292 /// \brief The libclang cursor kind corresponding to this code-completion 293 /// result. 294 CXCursorKind Kind; 295 296 /// \brief The availability of this code-completion result. 297 CXAvailabilityKind Availability; 298 299 /// \brief The simplified type class for a non-macro completion result. 300 SimplifiedTypeClass TypeClass; 301 302 /// \brief The type of a non-macro completion result, stored as a unique 303 /// integer used by the string map of cached completion types. 304 /// 305 /// This value will be zero if the type is not known, or a unique value 306 /// determined by the formatted type string. Se \c CachedCompletionTypes 307 /// for more information. 308 unsigned Type; 309 }; 310 311 /// \brief Retrieve the mapping from formatted type names to unique type 312 /// identifiers. 313 llvm::StringMap<unsigned> &getCachedCompletionTypes() { 314 return CachedCompletionTypes; 315 } 316 317 /// \brief Retrieve the allocator used to cache global code completions. 318 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> 319 getCachedCompletionAllocator() { 320 return CachedCompletionAllocator; 321 } 322 323 CodeCompletionTUInfo &getCodeCompletionTUInfo() { 324 if (!CCTUInfo) 325 CCTUInfo.reset(new CodeCompletionTUInfo( 326 new GlobalCodeCompletionAllocator)); 327 return *CCTUInfo; 328 } 329 330private: 331 /// \brief Allocator used to store cached code completions. 332 IntrusiveRefCntPtr<GlobalCodeCompletionAllocator> 333 CachedCompletionAllocator; 334 335 OwningPtr<CodeCompletionTUInfo> CCTUInfo; 336 337 /// \brief The set of cached code-completion results. 338 std::vector<CachedCodeCompletionResult> CachedCompletionResults; 339 340 /// \brief A mapping from the formatted type name to a unique number for that 341 /// type, which is used for type equality comparisons. 342 llvm::StringMap<unsigned> CachedCompletionTypes; 343 344 /// \brief A string hash of the top-level declaration and macro definition 345 /// names processed the last time that we reparsed the file. 346 /// 347 /// This hash value is used to determine when we need to refresh the 348 /// global code-completion cache. 349 unsigned CompletionCacheTopLevelHashValue; 350 351 /// \brief A string hash of the top-level declaration and macro definition 352 /// names processed the last time that we reparsed the precompiled preamble. 353 /// 354 /// This hash value is used to determine when we need to refresh the 355 /// global code-completion cache after a rebuild of the precompiled preamble. 356 unsigned PreambleTopLevelHashValue; 357 358 /// \brief The current hash value for the top-level declaration and macro 359 /// definition names 360 unsigned CurrentTopLevelHashValue; 361 362 /// \brief Bit used by CIndex to mark when a translation unit may be in an 363 /// inconsistent state, and is not safe to free. 364 unsigned UnsafeToFree : 1; 365 366 /// \brief Cache any "global" code-completion results, so that we can avoid 367 /// recomputing them with each completion. 368 void CacheCodeCompletionResults(); 369 370 /// \brief Clear out and deallocate 371 void ClearCachedCompletionResults(); 372 373 ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 374 ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 375 376 explicit ASTUnit(bool MainFileIsAST); 377 378 void CleanTemporaryFiles(); 379 bool Parse(llvm::MemoryBuffer *OverrideMainBuffer); 380 381 std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 382 ComputePreamble(CompilerInvocation &Invocation, 383 unsigned MaxLines, bool &CreatedBuffer); 384 385 llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble( 386 const CompilerInvocation &PreambleInvocationIn, 387 bool AllowRebuild = true, 388 unsigned MaxLines = 0); 389 void RealizeTopLevelDeclsFromPreamble(); 390 391 /// \brief Transfers ownership of the objects (like SourceManager) from 392 /// \param CI to this ASTUnit. 393 void transferASTDataFromCompilerInstance(CompilerInstance &CI); 394 395 /// \brief Allows us to assert that ASTUnit is not being used concurrently, 396 /// which is not supported. 397 /// 398 /// Clients should create instances of the ConcurrencyCheck class whenever 399 /// using the ASTUnit in a way that isn't intended to be concurrent, which is 400 /// just about any usage. 401 /// Becomes a noop in release mode; only useful for debug mode checking. 402 class ConcurrencyState { 403#ifndef NDEBUG 404 void *Mutex; // a llvm::sys::MutexImpl in debug; 405#endif 406 407 public: 408 ConcurrencyState(); 409 ~ConcurrencyState(); 410 411 void start(); 412 void finish(); 413 }; 414 ConcurrencyState ConcurrencyCheckValue; 415 416public: 417 class ConcurrencyCheck { 418 ASTUnit &Self; 419 420 public: 421 explicit ConcurrencyCheck(ASTUnit &Self) 422 : Self(Self) 423 { 424 Self.ConcurrencyCheckValue.start(); 425 } 426 ~ConcurrencyCheck() { 427 Self.ConcurrencyCheckValue.finish(); 428 } 429 }; 430 friend class ConcurrencyCheck; 431 432 ~ASTUnit(); 433 434 bool isMainFileAST() const { return MainFileIsAST; } 435 436 bool isUnsafeToFree() const { return UnsafeToFree; } 437 void setUnsafeToFree(bool Value) { UnsafeToFree = Value; } 438 439 const DiagnosticsEngine &getDiagnostics() const { return *Diagnostics; } 440 DiagnosticsEngine &getDiagnostics() { return *Diagnostics; } 441 442 const SourceManager &getSourceManager() const { return *SourceMgr; } 443 SourceManager &getSourceManager() { return *SourceMgr; } 444 445 const Preprocessor &getPreprocessor() const { return *PP; } 446 Preprocessor &getPreprocessor() { return *PP; } 447 448 const ASTContext &getASTContext() const { return *Ctx; } 449 ASTContext &getASTContext() { return *Ctx; } 450 451 void setASTContext(ASTContext *ctx) { Ctx = ctx; } 452 void setPreprocessor(Preprocessor *pp); 453 454 bool hasSema() const { return TheSema; } 455 Sema &getSema() const { 456 assert(TheSema && "ASTUnit does not have a Sema object!"); 457 return *TheSema; 458 } 459 460 const FileManager &getFileManager() const { return *FileMgr; } 461 FileManager &getFileManager() { return *FileMgr; } 462 463 const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; } 464 465 const std::string &getOriginalSourceFileName(); 466 467 /// \brief Add a temporary file that the ASTUnit depends on. 468 /// 469 /// This file will be erased when the ASTUnit is destroyed. 470 void addTemporaryFile(const llvm::sys::Path &TempFile); 471 472 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 473 474 bool getOwnsRemappedFileBuffers() const { return OwnsRemappedFileBuffers; } 475 void setOwnsRemappedFileBuffers(bool val) { OwnsRemappedFileBuffers = val; } 476 477 StringRef getMainFileName() const; 478 479 typedef std::vector<Decl *>::iterator top_level_iterator; 480 481 top_level_iterator top_level_begin() { 482 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 483 if (!TopLevelDeclsInPreamble.empty()) 484 RealizeTopLevelDeclsFromPreamble(); 485 return TopLevelDecls.begin(); 486 } 487 488 top_level_iterator top_level_end() { 489 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 490 if (!TopLevelDeclsInPreamble.empty()) 491 RealizeTopLevelDeclsFromPreamble(); 492 return TopLevelDecls.end(); 493 } 494 495 std::size_t top_level_size() const { 496 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 497 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size(); 498 } 499 500 bool top_level_empty() const { 501 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 502 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty(); 503 } 504 505 /// \brief Add a new top-level declaration. 506 void addTopLevelDecl(Decl *D) { 507 TopLevelDecls.push_back(D); 508 } 509 510 /// \brief Add a new local file-level declaration. 511 void addFileLevelDecl(Decl *D); 512 513 /// \brief Get the decls that are contained in a file in the Offset/Length 514 /// range. \arg Length can be 0 to indicate a point at \arg Offset instead of 515 /// a range. 516 void findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, 517 SmallVectorImpl<Decl *> &Decls); 518 519 /// \brief Add a new top-level declaration, identified by its ID in 520 /// the precompiled preamble. 521 void addTopLevelDeclFromPreamble(serialization::DeclID D) { 522 TopLevelDeclsInPreamble.push_back(D); 523 } 524 525 /// \brief Retrieve a reference to the current top-level name hash value. 526 /// 527 /// Note: This is used internally by the top-level tracking action 528 unsigned &getCurrentTopLevelHashValue() { return CurrentTopLevelHashValue; } 529 530 /// \brief Get the source location for the given file:line:col triplet. 531 /// 532 /// The difference with SourceManager::getLocation is that this method checks 533 /// whether the requested location points inside the precompiled preamble 534 /// in which case the returned source location will be a "loaded" one. 535 SourceLocation getLocation(const FileEntry *File, 536 unsigned Line, unsigned Col) const; 537 538 /// \brief Get the source location for the given file:offset pair. 539 SourceLocation getLocation(const FileEntry *File, unsigned Offset) const; 540 541 /// \brief If \arg Loc is a loaded location from the preamble, returns 542 /// the corresponding local location of the main file, otherwise it returns 543 /// \arg Loc. 544 SourceLocation mapLocationFromPreamble(SourceLocation Loc); 545 546 /// \brief If \arg Loc is a local location of the main file but inside the 547 /// preamble chunk, returns the corresponding loaded location from the 548 /// preamble, otherwise it returns \arg Loc. 549 SourceLocation mapLocationToPreamble(SourceLocation Loc); 550 551 bool isInPreambleFileID(SourceLocation Loc); 552 bool isInMainFileID(SourceLocation Loc); 553 SourceLocation getStartOfMainFileID(); 554 SourceLocation getEndOfPreambleFileID(); 555 556 /// \brief \see mapLocationFromPreamble. 557 SourceRange mapRangeFromPreamble(SourceRange R) { 558 return SourceRange(mapLocationFromPreamble(R.getBegin()), 559 mapLocationFromPreamble(R.getEnd())); 560 } 561 562 /// \brief \see mapLocationToPreamble. 563 SourceRange mapRangeToPreamble(SourceRange R) { 564 return SourceRange(mapLocationToPreamble(R.getBegin()), 565 mapLocationToPreamble(R.getEnd())); 566 } 567 568 // Retrieve the diagnostics associated with this AST 569 typedef StoredDiagnostic *stored_diag_iterator; 570 typedef const StoredDiagnostic *stored_diag_const_iterator; 571 stored_diag_const_iterator stored_diag_begin() const { 572 return StoredDiagnostics.begin(); 573 } 574 stored_diag_iterator stored_diag_begin() { 575 return StoredDiagnostics.begin(); 576 } 577 stored_diag_const_iterator stored_diag_end() const { 578 return StoredDiagnostics.end(); 579 } 580 stored_diag_iterator stored_diag_end() { 581 return StoredDiagnostics.end(); 582 } 583 unsigned stored_diag_size() const { return StoredDiagnostics.size(); } 584 585 stored_diag_iterator stored_diag_afterDriver_begin() { 586 if (NumStoredDiagnosticsFromDriver > StoredDiagnostics.size()) 587 NumStoredDiagnosticsFromDriver = 0; 588 return StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver; 589 } 590 591 typedef std::vector<CachedCodeCompletionResult>::iterator 592 cached_completion_iterator; 593 594 cached_completion_iterator cached_completion_begin() { 595 return CachedCompletionResults.begin(); 596 } 597 598 cached_completion_iterator cached_completion_end() { 599 return CachedCompletionResults.end(); 600 } 601 602 unsigned cached_completion_size() const { 603 return CachedCompletionResults.size(); 604 } 605 606 llvm::MemoryBuffer *getBufferForFile(StringRef Filename, 607 std::string *ErrorStr = 0); 608 609 /// \brief Determine what kind of translation unit this AST represents. 610 TranslationUnitKind getTranslationUnitKind() const { return TUKind; } 611 612 typedef llvm::PointerUnion<const char *, const llvm::MemoryBuffer *> 613 FilenameOrMemBuf; 614 /// \brief A mapping from a file name to the memory buffer that stores the 615 /// remapped contents of that file. 616 typedef std::pair<std::string, FilenameOrMemBuf> RemappedFile; 617 618 /// \brief Create a ASTUnit. Gets ownership of the passed CompilerInvocation. 619 static ASTUnit *create(CompilerInvocation *CI, 620 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 621 bool CaptureDiagnostics = false); 622 623 /// \brief Create a ASTUnit from an AST file. 624 /// 625 /// \param Filename - The AST file to load. 626 /// 627 /// \param Diags - The diagnostics engine to use for reporting errors; its 628 /// lifetime is expected to extend past that of the returned ASTUnit. 629 /// 630 /// \returns - The initialized ASTUnit or null if the AST failed to load. 631 static ASTUnit *LoadFromASTFile(const std::string &Filename, 632 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 633 const FileSystemOptions &FileSystemOpts, 634 bool OnlyLocalDecls = false, 635 RemappedFile *RemappedFiles = 0, 636 unsigned NumRemappedFiles = 0, 637 bool CaptureDiagnostics = false, 638 bool AllowPCHWithCompilerErrors = false); 639 640private: 641 /// \brief Helper function for \c LoadFromCompilerInvocation() and 642 /// \c LoadFromCommandLine(), which loads an AST from a compiler invocation. 643 /// 644 /// \param PrecompilePreamble Whether to precompile the preamble of this 645 /// translation unit, to improve the performance of reparsing. 646 /// 647 /// \returns \c true if a catastrophic failure occurred (which means that the 648 /// \c ASTUnit itself is invalid), or \c false otherwise. 649 bool LoadFromCompilerInvocation(bool PrecompilePreamble); 650 651public: 652 653 /// \brief Create an ASTUnit from a source file, via a CompilerInvocation 654 /// object, by invoking the optionally provided ASTFrontendAction. 655 /// 656 /// \param CI - The compiler invocation to use; it must have exactly one input 657 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 658 /// 659 /// \param Diags - The diagnostics engine to use for reporting errors; its 660 /// lifetime is expected to extend past that of the returned ASTUnit. 661 /// 662 /// \param Action - The ASTFrontendAction to invoke. Its ownership is not 663 /// transfered. 664 /// 665 /// \param Unit - optionally an already created ASTUnit. Its ownership is not 666 /// transfered. 667 /// 668 /// \param Persistent - if true the returned ASTUnit will be complete. 669 /// false means the caller is only interested in getting info through the 670 /// provided \see Action. 671 /// 672 /// \param ErrAST - If non-null and parsing failed without any AST to return 673 /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit 674 /// mainly to allow the caller to see the diagnostics. 675 /// This will only receive an ASTUnit if a new one was created. If an already 676 /// created ASTUnit was passed in \param Unit then the caller can check that. 677 /// 678 static ASTUnit *LoadFromCompilerInvocationAction(CompilerInvocation *CI, 679 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 680 ASTFrontendAction *Action = 0, 681 ASTUnit *Unit = 0, 682 bool Persistent = true, 683 StringRef ResourceFilesPath = StringRef(), 684 bool OnlyLocalDecls = false, 685 bool CaptureDiagnostics = false, 686 bool PrecompilePreamble = false, 687 bool CacheCodeCompletionResults = false, 688 bool IncludeBriefCommentsInCodeCompletion = false, 689 OwningPtr<ASTUnit> *ErrAST = 0); 690 691 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 692 /// CompilerInvocation object. 693 /// 694 /// \param CI - The compiler invocation to use; it must have exactly one input 695 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 696 /// 697 /// \param Diags - The diagnostics engine to use for reporting errors; its 698 /// lifetime is expected to extend past that of the returned ASTUnit. 699 // 700 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 701 // shouldn't need to specify them at construction time. 702 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 703 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 704 bool OnlyLocalDecls = false, 705 bool CaptureDiagnostics = false, 706 bool PrecompilePreamble = false, 707 TranslationUnitKind TUKind = TU_Complete, 708 bool CacheCodeCompletionResults = false, 709 bool IncludeBriefCommentsInCodeCompletion = false); 710 711 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 712 /// arguments, which must specify exactly one source file. 713 /// 714 /// \param ArgBegin - The beginning of the argument vector. 715 /// 716 /// \param ArgEnd - The end of the argument vector. 717 /// 718 /// \param Diags - The diagnostics engine to use for reporting errors; its 719 /// lifetime is expected to extend past that of the returned ASTUnit. 720 /// 721 /// \param ResourceFilesPath - The path to the compiler resource files. 722 /// 723 /// \param ErrAST - If non-null and parsing failed without any AST to return 724 /// (e.g. because the PCH could not be loaded), this accepts the ASTUnit 725 /// mainly to allow the caller to see the diagnostics. 726 /// 727 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 728 // shouldn't need to specify them at construction time. 729 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 730 const char **ArgEnd, 731 IntrusiveRefCntPtr<DiagnosticsEngine> Diags, 732 StringRef ResourceFilesPath, 733 bool OnlyLocalDecls = false, 734 bool CaptureDiagnostics = false, 735 RemappedFile *RemappedFiles = 0, 736 unsigned NumRemappedFiles = 0, 737 bool RemappedFilesKeepOriginalName = true, 738 bool PrecompilePreamble = false, 739 TranslationUnitKind TUKind = TU_Complete, 740 bool CacheCodeCompletionResults = false, 741 bool IncludeBriefCommentsInCodeCompletion = false, 742 bool AllowPCHWithCompilerErrors = false, 743 bool SkipFunctionBodies = false, 744 OwningPtr<ASTUnit> *ErrAST = 0); 745 746 /// \brief Reparse the source files using the same command-line options that 747 /// were originally used to produce this translation unit. 748 /// 749 /// \returns True if a failure occurred that causes the ASTUnit not to 750 /// contain any translation-unit information, false otherwise. 751 bool Reparse(RemappedFile *RemappedFiles = 0, 752 unsigned NumRemappedFiles = 0); 753 754 /// \brief Perform code completion at the given file, line, and 755 /// column within this translation unit. 756 /// 757 /// \param File The file in which code completion will occur. 758 /// 759 /// \param Line The line at which code completion will occur. 760 /// 761 /// \param Column The column at which code completion will occur. 762 /// 763 /// \param IncludeMacros Whether to include macros in the code-completion 764 /// results. 765 /// 766 /// \param IncludeCodePatterns Whether to include code patterns (such as a 767 /// for loop) in the code-completion results. 768 /// 769 /// \param IncludeBriefComments Whether to include brief documentation within 770 /// the set of code completions returned. 771 /// 772 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, StoredDiagnostics, and 773 /// OwnedBuffers parameters are all disgusting hacks. They will go away. 774 void CodeComplete(StringRef File, unsigned Line, unsigned Column, 775 RemappedFile *RemappedFiles, unsigned NumRemappedFiles, 776 bool IncludeMacros, bool IncludeCodePatterns, 777 bool IncludeBriefComments, 778 CodeCompleteConsumer &Consumer, 779 DiagnosticsEngine &Diag, LangOptions &LangOpts, 780 SourceManager &SourceMgr, FileManager &FileMgr, 781 SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, 782 SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers); 783 784 /// \brief Save this translation unit to a file with the given name. 785 /// 786 /// \returns An indication of whether the save was successful or not. 787 CXSaveError Save(StringRef File); 788 789 /// \brief Serialize this translation unit with the given output stream. 790 /// 791 /// \returns True if an error occurred, false otherwise. 792 bool serialize(raw_ostream &OS); 793 794 virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path, 795 Module::NameVisibilityKind Visibility, 796 bool IsInclusionDirective) { 797 // ASTUnit doesn't know how to load modules (not that this matters). 798 return 0; 799 } 800}; 801 802} // namespace clang 803 804#endif 805