ASTUnit.h revision c4421e966d77a18f815284175b3fcbb46f36fa39
15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//===--- ASTUnit.h - ASTUnit utility ----------------------------*- C++ -*-===// 25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// The LLVM Compiler Infrastructure 45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 55c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// This file is distributed under the University of Illinois Open Source 65c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// License. See LICENSE.TXT for details. 75c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 85c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//===----------------------------------------------------------------------===// 95c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// ASTUnit utility class. 115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//===----------------------------------------------------------------------===// 135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#ifndef LLVM_CLANG_FRONTEND_ASTUNIT_H 155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#define LLVM_CLANG_FRONTEND_ASTUNIT_H 165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Index/ASTLocation.h" 185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Frontend/PCHBitCodes.h" 195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Sema/Sema.h" 205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Lex/PreprocessingRecord.h" 215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/SourceManager.h" 225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/FileManager.h" 235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang-c/Index.h" 245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/IntrusiveRefCntPtr.h" 255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/OwningPtr.h" 265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/SmallVector.h" 275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/StringMap.h" 285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/System/Path.h" 295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/Support/Timer.h" 305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <map> 315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <string> 325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <vector> 335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <cassert> 345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <utility> 355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <sys/types.h> 365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace llvm { 385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class MemoryBuffer; 395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu} 405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace clang { 425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass ASTContext; 435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass CodeCompleteConsumer; 445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass CompilerInvocation; 455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass Decl; 465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass Diagnostic; 475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass FileEntry; 485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass FileManager; 495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass HeaderSearch; 505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass Preprocessor; 515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass SourceManager; 525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass TargetInfo; 535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuusing namespace idx; 555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// \brief Utility class for loading a ASTContext from a PCH file. 575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// 585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass ASTUnit { 595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liupublic: 605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu typedef std::map<FileID, std::vector<PreprocessedEntity *> > 615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu PreprocessedEntitiesByFileMap; 625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuprivate: 635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::IntrusiveRefCntPtr<Diagnostic> Diagnostics; 645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<FileManager> FileMgr; 655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<SourceManager> SourceMgr; 665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<HeaderSearch> HeaderInfo; 675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<TargetInfo> Target; 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) llvm::OwningPtr<Preprocessor> PP; 695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<ASTContext> Ctx; 705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The AST consumer that received information about the translation 725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// unit as it was parsed or loaded. 735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<ASTConsumer> Consumer; 745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief The semantic analysis object used to type-check the translation 761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// unit. 771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci llvm::OwningPtr<Sema> TheSema; 781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// Optional owned invocation, just used to make the invocation used in 801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// LoadFromCommandLine available. 811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci llvm::OwningPtr<CompilerInvocation> Invocation; 825c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // OnlyLocalDecls - when true, walking this AST should only visit declarations 845c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // that come from the AST itself, not from included precompiled headers. 855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // FIXME: This is temporary; eventually, CIndex will always do this. 86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) bool OnlyLocalDecls; 87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 885c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief Whether to capture any diagnostics produced. 891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool CaptureDiagnostics; 901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief Track whether the main file was loaded from an AST or not. 921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool MainFileIsAST; 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief Whether this AST represents a complete translation unit. 951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool CompleteTranslationUnit; 965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// Track the top-level decls which appeared in an ASTUnit which was loaded 985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// from a source file. 995c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // 100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // FIXME: This is just an optimization hack to avoid deserializing large parts 101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // of a PCH file when using the Index library on an ASTUnit loaded from 1025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // source. In the long term we should make the Index library use efficient and 1035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // more scalable search mechanisms. 1045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu std::vector<Decl*> TopLevelDecls; 1055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// The name of the original source file used to generate this ASTUnit. 1075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu std::string OriginalSourceFile; 1085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // Critical optimization when using clang_getCursor(). 1105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ASTLocation LastLoc; 1115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The set of diagnostics produced when creating this 1135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// translation unit. 1145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::SmallVector<StoredDiagnostic, 4> StoredDiagnostics; 1155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief Temporary files that should be removed when the ASTUnit is 1175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// destroyed. 1185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::SmallVector<llvm::sys::Path, 4> TemporaryFiles; 1195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief A mapping from file IDs to the set of preprocessed entities 1215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// stored in that file. 1225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 1235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// FIXME: This is just an optimization hack to avoid searching through 1245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// many preprocessed entities during cursor traversal in the CIndex library. 1255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// Ideally, we would just be able to perform a binary search within the 1265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// list of preprocessed entities. 1275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu PreprocessedEntitiesByFileMap PreprocessedEntitiesByFile; 1285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief Simple hack to allow us to assert that ASTUnit is not being 1305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// used concurrently, which is not supported. 131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// 1325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// Clients should create instances of the ConcurrencyCheck class whenever 1335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// using the ASTUnit in a way that isn't intended to be concurrent, which is 1345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// just about any usage. 1355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu unsigned int ConcurrencyCheckValue; 1365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu static const unsigned int CheckLocked = 28573289; 1375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu static const unsigned int CheckUnlocked = 9803453; 1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief Counter that determines when we want to try building a 1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// precompiled preamble. 1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// 1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// If zero, we will never build a precompiled preamble. Otherwise, 1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// it's treated as a counter that decrements each time we reparse 1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// without the benefit of a precompiled preamble. When it hits 1, 1455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// we'll attempt to rebuild the precompiled header. This way, if 1465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// building the precompiled preamble fails, we won't try again for 1475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// some number of calls. 1485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu unsigned PreambleRebuildCounter; 1495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The file in which the precompiled preamble is stored. 1515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu std::string PreambleFile; 1525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief The contents of the preamble that has been precompiled to 1545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \c PreambleFile. 1555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu std::vector<char> Preamble; 1565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief Whether the preamble ends at the start of a new line. 1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// 1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// Used to inform the lexer as to whether it's starting at the beginning of 1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// a line after skipping the preamble. 1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool PreambleEndsAtStartOfLine; 1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief The size of the source buffer that we've reserved for the main 1645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// file within the precompiled preamble. 165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) unsigned PreambleReservedSize; 1665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief Keeps track of the files that were used when computing the 1685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// preamble, with both their buffer size and their modification time. 1695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 1705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// If any of the files have changed from one compile to the next, 1715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// the preamble must be thrown away. 1725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::StringMap<std::pair<off_t, time_t> > FilesInPreamble; 1735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief When non-NULL, this is the buffer used to store the contents of 1755c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// the main file when it has been padded for use with the precompiled 1765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// preamble. 1775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::MemoryBuffer *SavedMainFileBuffer; 1785c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1795c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The number of warnings that occurred while parsing the preamble. 1805c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 1815c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// This value will be used to restore the state of the \c Diagnostic object 1825c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// when re-using the precompiled preamble. Note that only the 1835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// number of warnings matters, since we will not save the preamble 1845c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// when any errors are present. 1855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu unsigned NumWarningsInPreamble; 1865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1875c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The number of diagnostics that were stored when parsing 1885c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// the precompiled preamble. 1895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 1905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// This value is used to determine how many of the stored 1915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// diagnostics should be retained when reparsing in the presence of 1925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// a precompiled preamble. 1935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu unsigned NumStoredDiagnosticsInPreamble; 1945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The group of timers associated with this translation unit. 1965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::OwningPtr<llvm::TimerGroup> TimerGroup; 1975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief A list of the PCH ID numbers for each of the top-level 199cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// declarations parsed within the precompiled preamble. 2005c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu std::vector<pch::DeclID> TopLevelDeclsInPreamble; 2015c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 2035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \defgroup CodeCompleteCaching Code-completion caching 2045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 2055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \{ 2061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// 2071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief Whether we should be caching code-completion results. 2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool ShouldCacheCodeCompletionResults; 2101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccipublic: 2121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief A cached code-completion result, which may be introduced in one of 2135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// many different contexts. 2145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu struct CachedCodeCompletionResult { 2155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The code-completion string corresponding to this completion 216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// result. 2175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu CodeCompletionString *Completion; 2185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief A bitmask that indicates which code-completion contexts should 2205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// contain this completion result. 2215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 2221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// The bits in the bitmask correspond to the values of 2231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// CodeCompleteContext::Kind. To map from a completion context kind to a 2241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// bit, subtract one from the completion context kind and shift 1 by that 2251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// number of bits. Many completions can occur in several different 2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// contexts. 2271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci unsigned ShowInContexts; 2281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief The priority given to this code-completion result. 2305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu unsigned Priority; 231cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief The libclang cursor kind corresponding to this code-completion 2335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// result. 2345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu CXCursorKind Kind; 2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief The simplified type class for a non-macro completion result. 2371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci SimplifiedTypeClass TypeClass; 2381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci }; 2391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciprivate: 2411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci /// \brief The set of cached code-completion results. 2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci std::vector<CachedCodeCompletionResult> CachedCompletionResults; 2435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 244cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// \brief Cache any "global" code-completion results, so that we can avoid 245cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// recomputing them with each completion. 2465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu void CacheCodeCompletionResults(); 2475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief Clear out and deallocate 249cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) void ClearCachedCompletionResults(); 250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 2515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 2525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \} 2535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// 2545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The timers we've created from the various parses, reparses, etc. 256cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) /// involved in this translation unit. 257cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::vector<llvm::Timer *> Timers; 2585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT 2605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT 2615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu explicit ASTUnit(bool MainFileIsAST); 2635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 264cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) void CleanTemporaryFiles(); 2655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu bool Parse(llvm::MemoryBuffer *OverrideMainBuffer); 2665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > 2685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ComputePreamble(CompilerInvocation &Invocation, 2695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu unsigned MaxLines, bool &CreatedBuffer); 2705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci llvm::MemoryBuffer *getMainBufferWithPrecompiledPreamble( 2721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci bool AllowRebuild = true, 2731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci unsigned MaxLines = 0); 2741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void RealizeTopLevelDeclsFromPreamble(); 2751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccipublic: 2771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci class ConcurrencyCheck { 2785c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu volatile ASTUnit &Self; 2791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 2801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci public: 2811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci explicit ConcurrencyCheck(ASTUnit &Self) 2821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci : Self(Self) 2831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci { 2841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci assert(Self.ConcurrencyCheckValue == CheckUnlocked && 2851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci "Concurrent access to ASTUnit!"); 2865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu Self.ConcurrencyCheckValue = CheckLocked; 287cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) } 288cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 2895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ~ConcurrencyCheck() { 2905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu Self.ConcurrencyCheckValue = CheckUnlocked; 2915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu } 292cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) }; 293cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) friend class ConcurrencyCheck; 2945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ~ASTUnit(); 2965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu bool isMainFileAST() const { return MainFileIsAST; } 2985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 2995c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const Diagnostic &getDiagnostics() const { return *Diagnostics; } 3001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Diagnostic &getDiagnostics() { return *Diagnostics; } 3011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 3021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const SourceManager &getSourceManager() const { return *SourceMgr; } 3031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci SourceManager &getSourceManager() { return *SourceMgr; } 3045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 3055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const Preprocessor &getPreprocessor() const { return *PP.get(); } 3065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu Preprocessor &getPreprocessor() { return *PP.get(); } 3075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 308 const ASTContext &getASTContext() const { return *Ctx.get(); } 309 ASTContext &getASTContext() { return *Ctx.get(); } 310 311 bool hasSema() const { return TheSema; } 312 Sema &getSema() const { 313 assert(TheSema && "ASTUnit does not have a Sema object!"); 314 return *TheSema; 315 } 316 317 const FileManager &getFileManager() const { return *FileMgr; } 318 FileManager &getFileManager() { return *FileMgr; } 319 320 const std::string &getOriginalSourceFileName(); 321 const std::string &getPCHFileName(); 322 323 /// \brief Add a temporary file that the ASTUnit depends on. 324 /// 325 /// This file will be erased when the ASTUnit is destroyed. 326 void addTemporaryFile(const llvm::sys::Path &TempFile) { 327 TemporaryFiles.push_back(TempFile); 328 } 329 330 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 331 332 /// \brief Retrieve the maximum PCH level of declarations that a 333 /// traversal of the translation unit should consider. 334 unsigned getMaxPCHLevel() const; 335 336 void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; } 337 ASTLocation getLastASTLocation() const { return LastLoc; } 338 339 typedef std::vector<Decl *>::iterator top_level_iterator; 340 341 top_level_iterator top_level_begin() { 342 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 343 if (!TopLevelDeclsInPreamble.empty()) 344 RealizeTopLevelDeclsFromPreamble(); 345 return TopLevelDecls.begin(); 346 } 347 348 top_level_iterator top_level_end() { 349 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 350 if (!TopLevelDeclsInPreamble.empty()) 351 RealizeTopLevelDeclsFromPreamble(); 352 return TopLevelDecls.end(); 353 } 354 355 std::size_t top_level_size() const { 356 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 357 return TopLevelDeclsInPreamble.size() + TopLevelDecls.size(); 358 } 359 360 bool top_level_empty() const { 361 assert(!isMainFileAST() && "Invalid call for AST based ASTUnit!"); 362 return TopLevelDeclsInPreamble.empty() && TopLevelDecls.empty(); 363 } 364 365 /// \brief Add a new top-level declaration. 366 void addTopLevelDecl(Decl *D) { 367 TopLevelDecls.push_back(D); 368 } 369 370 /// \brief Add a new top-level declaration, identified by its ID in 371 /// the precompiled preamble. 372 void addTopLevelDeclFromPreamble(pch::DeclID D) { 373 TopLevelDeclsInPreamble.push_back(D); 374 } 375 376 /// \brief Retrieve the mapping from File IDs to the preprocessed entities 377 /// within that file. 378 PreprocessedEntitiesByFileMap &getPreprocessedEntitiesByFile() { 379 return PreprocessedEntitiesByFile; 380 } 381 382 // Retrieve the diagnostics associated with this AST 383 typedef const StoredDiagnostic *stored_diag_iterator; 384 stored_diag_iterator stored_diag_begin() const { 385 return StoredDiagnostics.begin(); 386 } 387 stored_diag_iterator stored_diag_end() const { 388 return StoredDiagnostics.end(); 389 } 390 unsigned stored_diag_size() const { return StoredDiagnostics.size(); } 391 392 llvm::SmallVector<StoredDiagnostic, 4> &getStoredDiagnostics() { 393 return StoredDiagnostics; 394 } 395 396 typedef std::vector<CachedCodeCompletionResult>::iterator 397 cached_completion_iterator; 398 399 cached_completion_iterator cached_completion_begin() { 400 return CachedCompletionResults.begin(); 401 } 402 403 cached_completion_iterator cached_completion_end() { 404 return CachedCompletionResults.end(); 405 } 406 407 unsigned cached_completion_size() const { 408 return CachedCompletionResults.size(); 409 } 410 411 /// \brief Whether this AST represents a complete translation unit. 412 /// 413 /// If false, this AST is only a partial translation unit, e.g., one 414 /// that might still be used as a precompiled header or preamble. 415 bool isCompleteTranslationUnit() const { return CompleteTranslationUnit; } 416 417 /// \brief A mapping from a file name to the memory buffer that stores the 418 /// remapped contents of that file. 419 typedef std::pair<std::string, const llvm::MemoryBuffer *> RemappedFile; 420 421 /// \brief Create a ASTUnit from a PCH file. 422 /// 423 /// \param Filename - The PCH file to load. 424 /// 425 /// \param Diags - The diagnostics engine to use for reporting errors; its 426 /// lifetime is expected to extend past that of the returned ASTUnit. 427 /// 428 /// \returns - The initialized ASTUnit or null if the PCH failed to load. 429 static ASTUnit *LoadFromPCHFile(const std::string &Filename, 430 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 431 bool OnlyLocalDecls = false, 432 RemappedFile *RemappedFiles = 0, 433 unsigned NumRemappedFiles = 0, 434 bool CaptureDiagnostics = false); 435 436 /// LoadFromCompilerInvocation - Create an ASTUnit from a source file, via a 437 /// CompilerInvocation object. 438 /// 439 /// \param CI - The compiler invocation to use; it must have exactly one input 440 /// source file. The ASTUnit takes ownership of the CompilerInvocation object. 441 /// 442 /// \param Diags - The diagnostics engine to use for reporting errors; its 443 /// lifetime is expected to extend past that of the returned ASTUnit. 444 // 445 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 446 // shouldn't need to specify them at construction time. 447 static ASTUnit *LoadFromCompilerInvocation(CompilerInvocation *CI, 448 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 449 bool OnlyLocalDecls = false, 450 bool CaptureDiagnostics = false, 451 bool PrecompilePreamble = false, 452 bool CompleteTranslationUnit = true, 453 bool CacheCodeCompletionResults = false); 454 455 /// LoadFromCommandLine - Create an ASTUnit from a vector of command line 456 /// arguments, which must specify exactly one source file. 457 /// 458 /// \param ArgBegin - The beginning of the argument vector. 459 /// 460 /// \param ArgEnd - The end of the argument vector. 461 /// 462 /// \param Diags - The diagnostics engine to use for reporting errors; its 463 /// lifetime is expected to extend past that of the returned ASTUnit. 464 /// 465 /// \param ResourceFilesPath - The path to the compiler resource files. 466 // 467 // FIXME: Move OnlyLocalDecls, UseBumpAllocator to setters on the ASTUnit, we 468 // shouldn't need to specify them at construction time. 469 static ASTUnit *LoadFromCommandLine(const char **ArgBegin, 470 const char **ArgEnd, 471 llvm::IntrusiveRefCntPtr<Diagnostic> Diags, 472 llvm::StringRef ResourceFilesPath, 473 bool OnlyLocalDecls = false, 474 RemappedFile *RemappedFiles = 0, 475 unsigned NumRemappedFiles = 0, 476 bool CaptureDiagnostics = false, 477 bool PrecompilePreamble = false, 478 bool CompleteTranslationUnit = true, 479 bool CacheCodeCompletionResults = false); 480 481 /// \brief Reparse the source files using the same command-line options that 482 /// were originally used to produce this translation unit. 483 /// 484 /// \returns True if a failure occurred that causes the ASTUnit not to 485 /// contain any translation-unit information, false otherwise. 486 bool Reparse(RemappedFile *RemappedFiles = 0, 487 unsigned NumRemappedFiles = 0); 488 489 /// \brief Perform code completion at the given file, line, and 490 /// column within this translation unit. 491 /// 492 /// \param File The file in which code completion will occur. 493 /// 494 /// \param Line The line at which code completion will occur. 495 /// 496 /// \param Column The column at which code completion will occur. 497 /// 498 /// \param IncludeMacros Whether to include macros in the code-completion 499 /// results. 500 /// 501 /// \param IncludeCodePatterns Whether to include code patterns (such as a 502 /// for loop) in the code-completion results. 503 /// 504 /// FIXME: The Diag, LangOpts, SourceMgr, FileMgr, and 505 /// StoredDiagnostics parameters are all disgusting hacks. They will 506 /// go away. 507 void CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, 508 RemappedFile *RemappedFiles, unsigned NumRemappedFiles, 509 bool IncludeMacros, bool IncludeCodePatterns, 510 CodeCompleteConsumer &Consumer, 511 Diagnostic &Diag, LangOptions &LangOpts, 512 SourceManager &SourceMgr, FileManager &FileMgr, 513 llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics); 514 515 /// \brief Save this translation unit to a file with the given name. 516 /// 517 /// \returns True if an error occurred, false otherwise. 518 bool Save(llvm::StringRef File); 519}; 520 521} // namespace clang 522 523#endif 524