1d5a2a073SDaniel Dunbar //===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===// 2d5a2a073SDaniel Dunbar // 3d5a2a073SDaniel Dunbar // The LLVM Compiler Infrastructure 4d5a2a073SDaniel Dunbar // 5d5a2a073SDaniel Dunbar // This file is distributed under the University of Illinois Open Source 6d5a2a073SDaniel Dunbar // License. See LICENSE.TXT for details. 7d5a2a073SDaniel Dunbar // 8d5a2a073SDaniel Dunbar //===----------------------------------------------------------------------===// 9d5a2a073SDaniel Dunbar // 10d5a2a073SDaniel Dunbar // This file implements the Clang-C Source Indexing library hooks for 11d5a2a073SDaniel Dunbar // code completion. 12d5a2a073SDaniel Dunbar // 13d5a2a073SDaniel Dunbar //===----------------------------------------------------------------------===// 14d5a2a073SDaniel Dunbar 15d5a2a073SDaniel Dunbar #include "CIndexer.h" 167df92ae9STed Kremenek #include "CXTranslationUnit.h" 174b4f3699STed Kremenek #include "CXString.h" 18*63745d59SDouglas Gregor #include "CXCursor.h" 19d5a2a073SDaniel Dunbar #include "CIndexDiagnostic.h" 20*63745d59SDouglas Gregor #include "clang/AST/Type.h" 21*63745d59SDouglas Gregor #include "clang/AST/Decl.h" 22*63745d59SDouglas Gregor #include "clang/AST/DeclObjC.h" 23d5a2a073SDaniel Dunbar #include "clang/Basic/SourceManager.h" 24d5a2a073SDaniel Dunbar #include "clang/Basic/FileManager.h" 258e984da8SDouglas Gregor #include "clang/Frontend/ASTUnit.h" 26d5a2a073SDaniel Dunbar #include "clang/Frontend/CompilerInstance.h" 27d5a2a073SDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 28d5a2a073SDaniel Dunbar #include "clang/Sema/CodeCompleteConsumer.h" 29028d3e4dSDouglas Gregor #include "llvm/ADT/SmallString.h" 30d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 319aeaa4d6SDouglas Gregor #include "llvm/Support/Atomic.h" 3277af1c56SDaniel Dunbar #include "llvm/Support/CrashRecoveryContext.h" 33d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 34028d3e4dSDouglas Gregor #include "llvm/Support/Timer.h" 35028d3e4dSDouglas Gregor #include "llvm/Support/raw_ostream.h" 368aaf4995SMichael J. Spencer #include "llvm/Support/Program.h" 37d6009ffbSDouglas Gregor #include <cstdlib> 38d6009ffbSDouglas Gregor #include <cstdio> 39d5a2a073SDaniel Dunbar 40028d3e4dSDouglas Gregor 41d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 42d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 43d5a2a073SDaniel Dunbar #include <arpa/inet.h> 44d5a2a073SDaniel Dunbar #include <sys/socket.h> 45d5a2a073SDaniel Dunbar #include <sys/types.h> 46d5a2a073SDaniel Dunbar #include <unistd.h> 47d5a2a073SDaniel Dunbar #endif 48d5a2a073SDaniel Dunbar 49d5a2a073SDaniel Dunbar using namespace clang; 50d5a2a073SDaniel Dunbar using namespace clang::cxstring; 51d5a2a073SDaniel Dunbar 52d5a2a073SDaniel Dunbar extern "C" { 53d5a2a073SDaniel Dunbar 54d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 55d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 56d5a2a073SDaniel Dunbar unsigned chunk_number) { 57b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 58d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 59d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 60d5a2a073SDaniel Dunbar 61d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 62d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 63d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 64d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 65d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 66d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 67d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 68d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 69d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 70d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 71d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 72d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 73d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 104d5a2a073SDaniel Dunbar } 105d5a2a073SDaniel Dunbar 106d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 107d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 108d5a2a073SDaniel Dunbar } 109d5a2a073SDaniel Dunbar 110d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 111d5a2a073SDaniel Dunbar unsigned chunk_number) { 112b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 113d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 11491554285STed Kremenek return createCXString((const char*)0); 115d5a2a073SDaniel Dunbar 116d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 117d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 118d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 119d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 120d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 121d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 122d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 123d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 124d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 125d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 126d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 127d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 128d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 129d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 13609737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 1378ed5b770SDouglas Gregor return createCXString((*CCStr)[chunk_number].Text, false); 13809737ee8SDouglas Gregor 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 140d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 141d5a2a073SDaniel Dunbar return createCXString(""); 142d5a2a073SDaniel Dunbar } 143d5a2a073SDaniel Dunbar 144d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 14591554285STed Kremenek return createCXString((const char*)0); 146d5a2a073SDaniel Dunbar } 147d5a2a073SDaniel Dunbar 148d5a2a073SDaniel Dunbar 149d5a2a073SDaniel Dunbar CXCompletionString 150d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 151d5a2a073SDaniel Dunbar unsigned chunk_number) { 152b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 153d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 154d5a2a073SDaniel Dunbar return 0; 155d5a2a073SDaniel Dunbar 156d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 157d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 158d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 159d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 160d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 161d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 162d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 163d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 164d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 165d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 166d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 167d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 168d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 169d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 170d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 171d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 172d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 173d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 177d5a2a073SDaniel Dunbar return 0; 178d5a2a073SDaniel Dunbar 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 180d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 181d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 182d5a2a073SDaniel Dunbar } 183d5a2a073SDaniel Dunbar 184d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 185d5a2a073SDaniel Dunbar return 0; 186d5a2a073SDaniel Dunbar } 187d5a2a073SDaniel Dunbar 188d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 189b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 190d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 191d5a2a073SDaniel Dunbar } 192d5a2a073SDaniel Dunbar 193a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 194b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 19547bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 196a2db793fSDouglas Gregor } 197a2db793fSDouglas Gregor 198f757a12dSDouglas Gregor enum CXAvailabilityKind 199f757a12dSDouglas Gregor clang_getCompletionAvailability(CXCompletionString completion_string) { 200b278aafbSDouglas Gregor CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; 201b278aafbSDouglas Gregor return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability()) 202b278aafbSDouglas Gregor : CXAvailability_Available; 203f757a12dSDouglas Gregor } 204f757a12dSDouglas Gregor 205d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 206d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 207d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 208c30dcecbSAnders Carlsson AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts); 209d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 210d5a2a073SDaniel Dunbar 211d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 212d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 213d5a2a073SDaniel Dunbar 214d5a2a073SDaniel Dunbar /// \brief Diag object 2158e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 216d5a2a073SDaniel Dunbar 217d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 218d5a2a073SDaniel Dunbar LangOptions LangOpts; 219d5a2a073SDaniel Dunbar 2205159f616SChris Lattner FileSystemOptions FileSystemOpts; 2215159f616SChris Lattner 222d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 2235e14d39aSTed Kremenek llvm::IntrusiveRefCntPtr<FileManager> FileMgr; 224d5a2a073SDaniel Dunbar 22571731d6bSArgyrios Kyrtzidis /// \brief Source manager, used for diagnostics. 2265e14d39aSTed Kremenek llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr; 22771731d6bSArgyrios Kyrtzidis 228d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 229d5a2a073SDaniel Dunbar /// with the code-completion results. 230d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 231d8a5dba9SDouglas Gregor 2323f5a9efbSChris Lattner /// \brief Temporary buffers that will be deleted once we have finished with 2333f5a9efbSChris Lattner /// the code-completion results. 234d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 235b278aafbSDouglas Gregor 236162b712dSDouglas Gregor /// \brief Allocator used to store globally cached code-completion results. 237162b712dSDouglas Gregor llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> 238162b712dSDouglas Gregor CachedCompletionAllocator; 239162b712dSDouglas Gregor 240b278aafbSDouglas Gregor /// \brief Allocator used to store code completion results. 2416c7cb188SDouglas Gregor clang::CodeCompletionAllocator CodeCompletionAllocator; 2422132584dSDouglas Gregor 2432132584dSDouglas Gregor /// \brief Context under which completion occurred. 2442132584dSDouglas Gregor enum clang::CodeCompletionContext::Kind ContextKind; 2452132584dSDouglas Gregor 2462132584dSDouglas Gregor /// \brief A bitfield representing the acceptable completions for the 2472132584dSDouglas Gregor /// current context. 2482132584dSDouglas Gregor unsigned long long Contexts; 249*63745d59SDouglas Gregor 250*63745d59SDouglas Gregor enum CXCursorKind ContainerKind; 251*63745d59SDouglas Gregor CXString ContainerUSR; 252*63745d59SDouglas Gregor 253*63745d59SDouglas Gregor unsigned ContainerIsIncomplete; 254d5a2a073SDaniel Dunbar }; 255d5a2a073SDaniel Dunbar 25668dbaeadSDouglas Gregor /// \brief Tracks the number of code-completion result objects that are 25768dbaeadSDouglas Gregor /// currently active. 25868dbaeadSDouglas Gregor /// 25968dbaeadSDouglas Gregor /// Used for debugging purposes only. 2609aeaa4d6SDouglas Gregor static llvm::sys::cas_flag CodeCompletionResultObjects; 26168dbaeadSDouglas Gregor 262c30dcecbSAnders Carlsson AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( 263c30dcecbSAnders Carlsson const FileSystemOptions& FileSystemOpts) 264d0040648SArgyrios Kyrtzidis : CXCodeCompleteResults(), 265d0040648SArgyrios Kyrtzidis Diag(new Diagnostic( 266d0040648SArgyrios Kyrtzidis llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))), 267c30dcecbSAnders Carlsson FileSystemOpts(FileSystemOpts), 2685e14d39aSTed Kremenek FileMgr(new FileManager(FileSystemOpts)), 2695e14d39aSTed Kremenek SourceMgr(new SourceManager(*Diag, *FileMgr)) { 27068dbaeadSDouglas Gregor if (getenv("LIBCLANG_OBJTRACKING")) { 2719aeaa4d6SDouglas Gregor llvm::sys::AtomicIncrement(&CodeCompletionResultObjects); 27268dbaeadSDouglas Gregor fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects); 27368dbaeadSDouglas Gregor } 27468dbaeadSDouglas Gregor } 275d5a2a073SDaniel Dunbar 276d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 277d5a2a073SDaniel Dunbar delete [] Results; 278d5a2a073SDaniel Dunbar 279*63745d59SDouglas Gregor clang_disposeString(ContainerUSR); 280*63745d59SDouglas Gregor 281d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 282d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 283d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 284d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 28568dbaeadSDouglas Gregor 28668dbaeadSDouglas Gregor if (getenv("LIBCLANG_OBJTRACKING")) { 2879aeaa4d6SDouglas Gregor llvm::sys::AtomicDecrement(&CodeCompletionResultObjects); 28868dbaeadSDouglas Gregor fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects); 28968dbaeadSDouglas Gregor } 290d5a2a073SDaniel Dunbar } 291d5a2a073SDaniel Dunbar 2928e984da8SDouglas Gregor } // end extern "C" 2938e984da8SDouglas Gregor 2942132584dSDouglas Gregor static unsigned long long getContextsForContextKind( 2952132584dSDouglas Gregor enum CodeCompletionContext::Kind kind, 2962132584dSDouglas Gregor Sema &S) { 2972132584dSDouglas Gregor unsigned long long contexts = 0; 2982132584dSDouglas Gregor switch (kind) { 2992132584dSDouglas Gregor case CodeCompletionContext::CCC_OtherWithMacros: { 3002132584dSDouglas Gregor //We can allow macros here, but we don't know what else is permissible 3012132584dSDouglas Gregor //So we'll say the only thing permissible are macros 3022132584dSDouglas Gregor contexts = CXCompletionContext_MacroName; 3032132584dSDouglas Gregor break; 3042132584dSDouglas Gregor } 3052132584dSDouglas Gregor case CodeCompletionContext::CCC_TopLevel: 3062132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCIvarList: 3072132584dSDouglas Gregor case CodeCompletionContext::CCC_ClassStructUnion: 3082132584dSDouglas Gregor case CodeCompletionContext::CCC_Type: { 3092132584dSDouglas Gregor contexts = CXCompletionContext_AnyType | 3102132584dSDouglas Gregor CXCompletionContext_ObjCInterface; 3112132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 3122132584dSDouglas Gregor contexts |= CXCompletionContext_EnumTag | 3132132584dSDouglas Gregor CXCompletionContext_UnionTag | 3142132584dSDouglas Gregor CXCompletionContext_StructTag | 3152132584dSDouglas Gregor CXCompletionContext_ClassTag | 3162132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3172132584dSDouglas Gregor } 3182132584dSDouglas Gregor break; 3192132584dSDouglas Gregor } 3202132584dSDouglas Gregor case CodeCompletionContext::CCC_Statement: { 3212132584dSDouglas Gregor contexts = CXCompletionContext_AnyType | 3222132584dSDouglas Gregor CXCompletionContext_ObjCInterface | 3232132584dSDouglas Gregor CXCompletionContext_AnyValue; 3242132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 3252132584dSDouglas Gregor contexts |= CXCompletionContext_EnumTag | 3262132584dSDouglas Gregor CXCompletionContext_UnionTag | 3272132584dSDouglas Gregor CXCompletionContext_StructTag | 3282132584dSDouglas Gregor CXCompletionContext_ClassTag | 3292132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3302132584dSDouglas Gregor } 3312132584dSDouglas Gregor break; 3322132584dSDouglas Gregor } 3332132584dSDouglas Gregor case CodeCompletionContext::CCC_Expression: { 3342132584dSDouglas Gregor contexts = CXCompletionContext_AnyValue; 3352132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 3362132584dSDouglas Gregor contexts |= CXCompletionContext_AnyType | 3372132584dSDouglas Gregor CXCompletionContext_ObjCInterface | 3382132584dSDouglas Gregor CXCompletionContext_EnumTag | 3392132584dSDouglas Gregor CXCompletionContext_UnionTag | 3402132584dSDouglas Gregor CXCompletionContext_StructTag | 3412132584dSDouglas Gregor CXCompletionContext_ClassTag | 3422132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3432132584dSDouglas Gregor } 3442132584dSDouglas Gregor break; 3452132584dSDouglas Gregor } 3462132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCMessageReceiver: { 3472132584dSDouglas Gregor contexts = CXCompletionContext_ObjCObjectValue | 3482132584dSDouglas Gregor CXCompletionContext_ObjCSelectorValue | 3492132584dSDouglas Gregor CXCompletionContext_ObjCInterface; 3502132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 3512132584dSDouglas Gregor contexts |= CXCompletionContext_CXXClassTypeValue | 3522132584dSDouglas Gregor CXCompletionContext_AnyType | 3532132584dSDouglas Gregor CXCompletionContext_EnumTag | 3542132584dSDouglas Gregor CXCompletionContext_UnionTag | 3552132584dSDouglas Gregor CXCompletionContext_StructTag | 3562132584dSDouglas Gregor CXCompletionContext_ClassTag | 3572132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3582132584dSDouglas Gregor } 3592132584dSDouglas Gregor break; 3602132584dSDouglas Gregor } 3612132584dSDouglas Gregor case CodeCompletionContext::CCC_DotMemberAccess: { 3622132584dSDouglas Gregor contexts = CXCompletionContext_DotMemberAccess; 3632132584dSDouglas Gregor break; 3642132584dSDouglas Gregor } 3652132584dSDouglas Gregor case CodeCompletionContext::CCC_ArrowMemberAccess: { 3662132584dSDouglas Gregor contexts = CXCompletionContext_ArrowMemberAccess; 3672132584dSDouglas Gregor break; 3682132584dSDouglas Gregor } 3692132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCPropertyAccess: { 3702132584dSDouglas Gregor contexts = CXCompletionContext_ObjCPropertyAccess; 3712132584dSDouglas Gregor break; 3722132584dSDouglas Gregor } 3732132584dSDouglas Gregor case CodeCompletionContext::CCC_EnumTag: { 3742132584dSDouglas Gregor contexts = CXCompletionContext_EnumTag | 3752132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3762132584dSDouglas Gregor break; 3772132584dSDouglas Gregor } 3782132584dSDouglas Gregor case CodeCompletionContext::CCC_UnionTag: { 3792132584dSDouglas Gregor contexts = CXCompletionContext_UnionTag | 3802132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3812132584dSDouglas Gregor break; 3822132584dSDouglas Gregor } 3832132584dSDouglas Gregor case CodeCompletionContext::CCC_ClassOrStructTag: { 3842132584dSDouglas Gregor contexts = CXCompletionContext_StructTag | 3852132584dSDouglas Gregor CXCompletionContext_ClassTag | 3862132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 3872132584dSDouglas Gregor break; 3882132584dSDouglas Gregor } 3892132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCProtocolName: { 3902132584dSDouglas Gregor contexts = CXCompletionContext_ObjCProtocol; 3912132584dSDouglas Gregor break; 3922132584dSDouglas Gregor } 3932132584dSDouglas Gregor case CodeCompletionContext::CCC_Namespace: { 3942132584dSDouglas Gregor contexts = CXCompletionContext_Namespace; 3952132584dSDouglas Gregor break; 3962132584dSDouglas Gregor } 3972132584dSDouglas Gregor case CodeCompletionContext::CCC_PotentiallyQualifiedName: { 3982132584dSDouglas Gregor contexts = CXCompletionContext_NestedNameSpecifier; 3992132584dSDouglas Gregor break; 4002132584dSDouglas Gregor } 4012132584dSDouglas Gregor case CodeCompletionContext::CCC_MacroNameUse: { 4022132584dSDouglas Gregor contexts = CXCompletionContext_MacroName; 4032132584dSDouglas Gregor break; 4042132584dSDouglas Gregor } 4052132584dSDouglas Gregor case CodeCompletionContext::CCC_NaturalLanguage: { 4062132584dSDouglas Gregor contexts = CXCompletionContext_NaturalLanguage; 4072132584dSDouglas Gregor break; 4082132584dSDouglas Gregor } 4092132584dSDouglas Gregor case CodeCompletionContext::CCC_SelectorName: { 4102132584dSDouglas Gregor contexts = CXCompletionContext_ObjCSelectorName; 4112132584dSDouglas Gregor break; 4122132584dSDouglas Gregor } 4132132584dSDouglas Gregor case CodeCompletionContext::CCC_ParenthesizedExpression: { 4142132584dSDouglas Gregor contexts = CXCompletionContext_AnyType | 4152132584dSDouglas Gregor CXCompletionContext_ObjCInterface | 4162132584dSDouglas Gregor CXCompletionContext_AnyValue; 4172132584dSDouglas Gregor if (S.getLangOptions().CPlusPlus) { 4182132584dSDouglas Gregor contexts |= CXCompletionContext_EnumTag | 4192132584dSDouglas Gregor CXCompletionContext_UnionTag | 4202132584dSDouglas Gregor CXCompletionContext_StructTag | 4212132584dSDouglas Gregor CXCompletionContext_ClassTag | 4222132584dSDouglas Gregor CXCompletionContext_NestedNameSpecifier; 4232132584dSDouglas Gregor } 4242132584dSDouglas Gregor break; 4252132584dSDouglas Gregor } 4262132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCInstanceMessage: { 4272132584dSDouglas Gregor contexts = CXCompletionContext_ObjCInstanceMessage; 4282132584dSDouglas Gregor break; 4292132584dSDouglas Gregor } 4302132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCClassMessage: { 4312132584dSDouglas Gregor contexts = CXCompletionContext_ObjCClassMessage; 4322132584dSDouglas Gregor break; 4332132584dSDouglas Gregor } 4342132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCSuperclass: { 4352132584dSDouglas Gregor contexts = CXCompletionContext_ObjCInterface; 4362132584dSDouglas Gregor break; 4372132584dSDouglas Gregor } 4382132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCCategoryName: { 4392132584dSDouglas Gregor contexts = CXCompletionContext_ObjCCategory; 4402132584dSDouglas Gregor break; 4412132584dSDouglas Gregor } 4422132584dSDouglas Gregor case CodeCompletionContext::CCC_Other: 4432132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCInterface: 4442132584dSDouglas Gregor case CodeCompletionContext::CCC_ObjCImplementation: 4452132584dSDouglas Gregor case CodeCompletionContext::CCC_Name: 4462132584dSDouglas Gregor case CodeCompletionContext::CCC_MacroName: 4472132584dSDouglas Gregor case CodeCompletionContext::CCC_PreprocessorExpression: 4482132584dSDouglas Gregor case CodeCompletionContext::CCC_PreprocessorDirective: 4492132584dSDouglas Gregor case CodeCompletionContext::CCC_TypeQualifiers: { 4502132584dSDouglas Gregor //Only Clang results should be accepted, so we'll set all of the other 4512132584dSDouglas Gregor //context bits to 0 (i.e. the empty set) 4522132584dSDouglas Gregor contexts = CXCompletionContext_Unexposed; 4532132584dSDouglas Gregor break; 4542132584dSDouglas Gregor } 4552132584dSDouglas Gregor case CodeCompletionContext::CCC_Recovery: { 4562132584dSDouglas Gregor //We don't know what the current context is, so we'll return unknown 4572132584dSDouglas Gregor //This is the equivalent of setting all of the other context bits 4582132584dSDouglas Gregor contexts = CXCompletionContext_Unknown; 4592132584dSDouglas Gregor break; 4602132584dSDouglas Gregor } 4612132584dSDouglas Gregor } 4622132584dSDouglas Gregor return contexts; 4632132584dSDouglas Gregor } 4642132584dSDouglas Gregor 4658e984da8SDouglas Gregor namespace { 4668e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 4678e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 46836e3b5c7SDouglas Gregor llvm::SmallVector<CXCompletionResult, 16> StoredResults; 469*63745d59SDouglas Gregor CXTranslationUnit *TU; 4708e984da8SDouglas Gregor public: 471*63745d59SDouglas Gregor CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results, 472*63745d59SDouglas Gregor CXTranslationUnit *TranslationUnit) 47339982197SDouglas Gregor : CodeCompleteConsumer(true, false, true, false), 474*63745d59SDouglas Gregor AllocatedResults(Results), TU(TranslationUnit) { } 47536e3b5c7SDouglas Gregor ~CaptureCompletionResults() { Finish(); } 4768e984da8SDouglas Gregor 47700c37ef0SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, 47800c37ef0SDouglas Gregor CodeCompletionContext Context, 479276321a9SJohn McCall CodeCompletionResult *Results, 4808e984da8SDouglas Gregor unsigned NumResults) { 48136e3b5c7SDouglas Gregor StoredResults.reserve(StoredResults.size() + NumResults); 4828e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 483b278aafbSDouglas Gregor CodeCompletionString *StoredCompletion 484b278aafbSDouglas Gregor = Results[I].CreateCodeCompletionString(S, 485b278aafbSDouglas Gregor AllocatedResults.CodeCompletionAllocator); 48636e3b5c7SDouglas Gregor 48736e3b5c7SDouglas Gregor CXCompletionResult R; 48836e3b5c7SDouglas Gregor R.CursorKind = Results[I].CursorKind; 48936e3b5c7SDouglas Gregor R.CompletionString = StoredCompletion; 49036e3b5c7SDouglas Gregor StoredResults.push_back(R); 4918e984da8SDouglas Gregor } 4922132584dSDouglas Gregor 493*63745d59SDouglas Gregor enum CodeCompletionContext::Kind contextKind = Context.getKind(); 4942132584dSDouglas Gregor 495*63745d59SDouglas Gregor AllocatedResults.ContextKind = contextKind; 496*63745d59SDouglas Gregor AllocatedResults.Contexts = getContextsForContextKind(contextKind, S); 497*63745d59SDouglas Gregor 498*63745d59SDouglas Gregor QualType baseType = Context.getBaseType(); 499*63745d59SDouglas Gregor NamedDecl *D = NULL; 500*63745d59SDouglas Gregor 501*63745d59SDouglas Gregor if (!baseType.isNull()) { 502*63745d59SDouglas Gregor // Get the declaration for a class/struct/union/enum type 503*63745d59SDouglas Gregor if (const TagType *Tag = baseType->getAs<TagType>()) 504*63745d59SDouglas Gregor D = Tag->getDecl(); 505*63745d59SDouglas Gregor // Get the @interface declaration for a (possibly-qualified) Objective-C 506*63745d59SDouglas Gregor // object pointer type, e.g., NSString* 507*63745d59SDouglas Gregor else if (const ObjCObjectPointerType *ObjPtr = 508*63745d59SDouglas Gregor baseType->getAs<ObjCObjectPointerType>()) 509*63745d59SDouglas Gregor D = ObjPtr->getInterfaceDecl(); 510*63745d59SDouglas Gregor // Get the @interface declaration for an Objective-C object type 511*63745d59SDouglas Gregor else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>()) 512*63745d59SDouglas Gregor D = Obj->getInterface(); 513*63745d59SDouglas Gregor // Get the class for a C++ injected-class-name 514*63745d59SDouglas Gregor else if (const InjectedClassNameType *Injected = 515*63745d59SDouglas Gregor baseType->getAs<InjectedClassNameType>()) 516*63745d59SDouglas Gregor D = Injected->getDecl(); 517*63745d59SDouglas Gregor } 518*63745d59SDouglas Gregor 519*63745d59SDouglas Gregor if (D != NULL) { 520*63745d59SDouglas Gregor CXCursor cursor = cxcursor::MakeCXCursor(D, *TU); 521*63745d59SDouglas Gregor 522*63745d59SDouglas Gregor CXCursorKind cursorKind = clang_getCursorKind(cursor); 523*63745d59SDouglas Gregor CXString cursorUSR = clang_getCursorUSR(cursor); 524*63745d59SDouglas Gregor 525*63745d59SDouglas Gregor AllocatedResults.ContainerKind = cursorKind; 526*63745d59SDouglas Gregor AllocatedResults.ContainerUSR = cursorUSR; 527*63745d59SDouglas Gregor const Type *type = baseType.getTypePtrOrNull(); 528*63745d59SDouglas Gregor if (type != NULL) { 529*63745d59SDouglas Gregor AllocatedResults.ContainerIsIncomplete = type->isIncompleteType(); 530*63745d59SDouglas Gregor } 531*63745d59SDouglas Gregor else { 532*63745d59SDouglas Gregor AllocatedResults.ContainerIsIncomplete = 1; 533*63745d59SDouglas Gregor } 534*63745d59SDouglas Gregor } 535*63745d59SDouglas Gregor else { 536*63745d59SDouglas Gregor AllocatedResults.ContainerKind = CXCursor_InvalidCode; 537*63745d59SDouglas Gregor AllocatedResults.ContainerUSR = createCXString(""); 538*63745d59SDouglas Gregor AllocatedResults.ContainerIsIncomplete = 1; 539*63745d59SDouglas Gregor } 5408e984da8SDouglas Gregor } 54149f67ce4SDouglas Gregor 54236e3b5c7SDouglas Gregor virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 54336e3b5c7SDouglas Gregor OverloadCandidate *Candidates, 54436e3b5c7SDouglas Gregor unsigned NumCandidates) { 54536e3b5c7SDouglas Gregor StoredResults.reserve(StoredResults.size() + NumCandidates); 54636e3b5c7SDouglas Gregor for (unsigned I = 0; I != NumCandidates; ++I) { 547b278aafbSDouglas Gregor CodeCompletionString *StoredCompletion 548b278aafbSDouglas Gregor = Candidates[I].CreateSignatureString(CurrentArg, S, 549b278aafbSDouglas Gregor AllocatedResults.CodeCompletionAllocator); 55036e3b5c7SDouglas Gregor 55136e3b5c7SDouglas Gregor CXCompletionResult R; 55236e3b5c7SDouglas Gregor R.CursorKind = CXCursor_NotImplemented; 55336e3b5c7SDouglas Gregor R.CompletionString = StoredCompletion; 55436e3b5c7SDouglas Gregor StoredResults.push_back(R); 55536e3b5c7SDouglas Gregor } 55636e3b5c7SDouglas Gregor } 55736e3b5c7SDouglas Gregor 558bcbf46c7SDouglas Gregor virtual CodeCompletionAllocator &getAllocator() { 559b278aafbSDouglas Gregor return AllocatedResults.CodeCompletionAllocator; 560b278aafbSDouglas Gregor } 561b278aafbSDouglas Gregor 56236e3b5c7SDouglas Gregor private: 56336e3b5c7SDouglas Gregor void Finish() { 56436e3b5c7SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; 56536e3b5c7SDouglas Gregor AllocatedResults.NumResults = StoredResults.size(); 56636e3b5c7SDouglas Gregor std::memcpy(AllocatedResults.Results, StoredResults.data(), 56736e3b5c7SDouglas Gregor StoredResults.size() * sizeof(CXCompletionResult)); 56836e3b5c7SDouglas Gregor StoredResults.clear(); 56936e3b5c7SDouglas Gregor } 5708e984da8SDouglas Gregor }; 5718e984da8SDouglas Gregor } 5728e984da8SDouglas Gregor 5738e984da8SDouglas Gregor extern "C" { 57477af1c56SDaniel Dunbar struct CodeCompleteAtInfo { 57577af1c56SDaniel Dunbar CXTranslationUnit TU; 57677af1c56SDaniel Dunbar const char *complete_filename; 57777af1c56SDaniel Dunbar unsigned complete_line; 57877af1c56SDaniel Dunbar unsigned complete_column; 57977af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files; 58077af1c56SDaniel Dunbar unsigned num_unsaved_files; 58177af1c56SDaniel Dunbar unsigned options; 58277af1c56SDaniel Dunbar CXCodeCompleteResults *result; 58377af1c56SDaniel Dunbar }; 58477af1c56SDaniel Dunbar void clang_codeCompleteAt_Impl(void *UserData) { 58577af1c56SDaniel Dunbar CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 58677af1c56SDaniel Dunbar CXTranslationUnit TU = CCAI->TU; 58777af1c56SDaniel Dunbar const char *complete_filename = CCAI->complete_filename; 58877af1c56SDaniel Dunbar unsigned complete_line = CCAI->complete_line; 58977af1c56SDaniel Dunbar unsigned complete_column = CCAI->complete_column; 59077af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 59177af1c56SDaniel Dunbar unsigned num_unsaved_files = CCAI->num_unsaved_files; 59277af1c56SDaniel Dunbar unsigned options = CCAI->options; 59377af1c56SDaniel Dunbar CCAI->result = 0; 59477af1c56SDaniel Dunbar 5958e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 5968e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 5978e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 5988e984da8SDouglas Gregor #endif 5998e984da8SDouglas Gregor #endif 6008e984da8SDouglas Gregor 6018e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 6028e984da8SDouglas Gregor 60391554285STed Kremenek ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData); 6048e984da8SDouglas Gregor if (!AST) 60577af1c56SDaniel Dunbar return; 6068e984da8SDouglas Gregor 607ca5b0539SDouglas Gregor ASTUnit::ConcurrencyCheck Check(*AST); 608ca5b0539SDouglas Gregor 6098e984da8SDouglas Gregor // Perform the remapping of source files. 6108e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 6118e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 6128e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 6138e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 6148e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 6158e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 6168e984da8SDouglas Gregor Buffer)); 6178e984da8SDouglas Gregor } 6188e984da8SDouglas Gregor 6198e984da8SDouglas Gregor if (EnableLogging) { 6208e984da8SDouglas Gregor // FIXME: Add logging. 6218e984da8SDouglas Gregor } 6228e984da8SDouglas Gregor 6238e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 624c30dcecbSAnders Carlsson AllocatedCXCodeCompleteResults *Results = 625c30dcecbSAnders Carlsson new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts()); 6268e984da8SDouglas Gregor Results->Results = 0; 6278e984da8SDouglas Gregor Results->NumResults = 0; 6288e984da8SDouglas Gregor 6298e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 630*63745d59SDouglas Gregor CaptureCompletionResults Capture(*Results, &TU); 6318e984da8SDouglas Gregor 6328e984da8SDouglas Gregor // Perform completion. 6338e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 634b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 635b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 636b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 637b68bc59aSDouglas Gregor Capture, 6385e14d39aSTed Kremenek *Results->Diag, Results->LangOpts, *Results->SourceMgr, 6395e14d39aSTed Kremenek *Results->FileMgr, Results->Diagnostics, 640b97b666cSDouglas Gregor Results->TemporaryBuffers); 6418e984da8SDouglas Gregor 642162b712dSDouglas Gregor // Keep a reference to the allocator used for cached global completions, so 643162b712dSDouglas Gregor // that we can be sure that the memory used by our code completion strings 644162b712dSDouglas Gregor // doesn't get freed due to subsequent reparses (while the code completion 645162b712dSDouglas Gregor // results are still active). 646162b712dSDouglas Gregor Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator(); 647162b712dSDouglas Gregor 6488e984da8SDouglas Gregor 6498e984da8SDouglas Gregor 6508e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6518e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6528e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 6538e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 6548e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 6558e984da8SDouglas Gregor 6568e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 6578e984da8SDouglas Gregor const char *lang = 0; 6588e984da8SDouglas Gregor bool usesPCH = false; 6598e984da8SDouglas Gregor 6608e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 6618e984da8SDouglas Gregor I != E; ++I) { 6628e984da8SDouglas Gregor if (*I == 0) 6638e984da8SDouglas Gregor continue; 6648e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 6658e984da8SDouglas Gregor if (I + 1 != E) { 6668e984da8SDouglas Gregor lang = *(++I); 6678e984da8SDouglas Gregor continue; 6688e984da8SDouglas Gregor } 6698e984da8SDouglas Gregor } 6708e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 6718e984da8SDouglas Gregor if (I+1 != E) { 6728e984da8SDouglas Gregor const char *arg = *(++I); 6738e984da8SDouglas Gregor llvm::SmallString<512> pchName; 6748e984da8SDouglas Gregor { 6758e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 6768e984da8SDouglas Gregor os << arg << ".pth"; 6778e984da8SDouglas Gregor } 6788e984da8SDouglas Gregor pchName.push_back('\0'); 6798e984da8SDouglas Gregor struct stat stat_results; 6808e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 6818e984da8SDouglas Gregor usesPCH = true; 6828e984da8SDouglas Gregor continue; 6838e984da8SDouglas Gregor } 6848e984da8SDouglas Gregor } 6858e984da8SDouglas Gregor } 6868e984da8SDouglas Gregor 6878e984da8SDouglas Gregor os << "{ "; 6888e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 6898e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 6908e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 6918e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 6928e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 6938e984da8SDouglas Gregor const char *name = getlogin(); 6948e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 6958e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 6968e984da8SDouglas Gregor os << " }"; 6978e984da8SDouglas Gregor 6988e984da8SDouglas Gregor llvm::StringRef res = os.str(); 6998e984da8SDouglas Gregor if (res.size() > 0) { 7008e984da8SDouglas Gregor do { 7018e984da8SDouglas Gregor // Setup the UDP socket. 7028e984da8SDouglas Gregor struct sockaddr_in servaddr; 7038e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 7048e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 7058e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 7068e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 7078e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 7088e984da8SDouglas Gregor break; 7098e984da8SDouglas Gregor 7108e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 7118e984da8SDouglas Gregor if (sockfd < 0) 7128e984da8SDouglas Gregor break; 7138e984da8SDouglas Gregor 7148e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 7158e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 7168e984da8SDouglas Gregor close(sockfd); 7178e984da8SDouglas Gregor } 7188e984da8SDouglas Gregor while (false); 7198e984da8SDouglas Gregor } 7208e984da8SDouglas Gregor #endif 7218e984da8SDouglas Gregor #endif 72277af1c56SDaniel Dunbar CCAI->result = Results; 72377af1c56SDaniel Dunbar } 72477af1c56SDaniel Dunbar CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 72577af1c56SDaniel Dunbar const char *complete_filename, 72677af1c56SDaniel Dunbar unsigned complete_line, 72777af1c56SDaniel Dunbar unsigned complete_column, 72877af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 72977af1c56SDaniel Dunbar unsigned num_unsaved_files, 73077af1c56SDaniel Dunbar unsigned options) { 73177af1c56SDaniel Dunbar CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 73277af1c56SDaniel Dunbar complete_column, unsaved_files, num_unsaved_files, 73377af1c56SDaniel Dunbar options, 0 }; 73477af1c56SDaniel Dunbar llvm::CrashRecoveryContext CRC; 73577af1c56SDaniel Dunbar 736b7383e6dSDaniel Dunbar if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { 73777af1c56SDaniel Dunbar fprintf(stderr, "libclang: crash detected in code completion\n"); 73891554285STed Kremenek static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); 73977af1c56SDaniel Dunbar return 0; 740af44c781SDouglas Gregor } else if (getenv("LIBCLANG_RESOURCE_USAGE")) 741af44c781SDouglas Gregor PrintLibclangResourceUsage(TU); 74277af1c56SDaniel Dunbar 74377af1c56SDaniel Dunbar return CCAI.result; 7448e984da8SDouglas Gregor } 7458e984da8SDouglas Gregor 746b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 747b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 748b68bc59aSDouglas Gregor } 749b68bc59aSDouglas Gregor 750d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 751d5a2a073SDaniel Dunbar if (!ResultsIn) 752d5a2a073SDaniel Dunbar return; 753d5a2a073SDaniel Dunbar 754d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 755d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 756d5a2a073SDaniel Dunbar delete Results; 757d5a2a073SDaniel Dunbar } 758d5a2a073SDaniel Dunbar 759d5a2a073SDaniel Dunbar unsigned 760d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 761d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 762d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 763d5a2a073SDaniel Dunbar if (!Results) 764d5a2a073SDaniel Dunbar return 0; 765d5a2a073SDaniel Dunbar 766d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 767d5a2a073SDaniel Dunbar } 768d5a2a073SDaniel Dunbar 769d5a2a073SDaniel Dunbar CXDiagnostic 770d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 771d5a2a073SDaniel Dunbar unsigned Index) { 772d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 773d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 774d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 775d5a2a073SDaniel Dunbar return 0; 776d5a2a073SDaniel Dunbar 777d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 778d5a2a073SDaniel Dunbar } 779d5a2a073SDaniel Dunbar 7802132584dSDouglas Gregor unsigned long long 7812132584dSDouglas Gregor clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) { 7822132584dSDouglas Gregor AllocatedCXCodeCompleteResults *Results 7832132584dSDouglas Gregor = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 7842132584dSDouglas Gregor if (!Results) 7852132584dSDouglas Gregor return 0; 7862132584dSDouglas Gregor 7872132584dSDouglas Gregor return Results->Contexts; 7882132584dSDouglas Gregor } 789d5a2a073SDaniel Dunbar 790*63745d59SDouglas Gregor enum CXCursorKind clang_codeCompleteGetContainerKind( 791*63745d59SDouglas Gregor CXCodeCompleteResults *ResultsIn, 792*63745d59SDouglas Gregor unsigned *IsIncomplete) { 793*63745d59SDouglas Gregor AllocatedCXCodeCompleteResults *Results = 794*63745d59SDouglas Gregor static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); 795*63745d59SDouglas Gregor if (!Results) 796*63745d59SDouglas Gregor return CXCursor_InvalidCode; 797*63745d59SDouglas Gregor 798*63745d59SDouglas Gregor if (IsIncomplete != NULL) { 799*63745d59SDouglas Gregor *IsIncomplete = Results->ContainerIsIncomplete; 800*63745d59SDouglas Gregor } 801*63745d59SDouglas Gregor 802*63745d59SDouglas Gregor return Results->ContainerKind; 803*63745d59SDouglas Gregor } 804*63745d59SDouglas Gregor 805*63745d59SDouglas Gregor CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) { 806*63745d59SDouglas Gregor AllocatedCXCodeCompleteResults *Results = 807*63745d59SDouglas Gregor static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); 808*63745d59SDouglas Gregor if (!Results) 809*63745d59SDouglas Gregor return createCXString(""); 810*63745d59SDouglas Gregor 811*63745d59SDouglas Gregor return createCXString(clang_getCString(Results->ContainerUSR)); 812*63745d59SDouglas Gregor } 813*63745d59SDouglas Gregor 814d5a2a073SDaniel Dunbar } // end extern "C" 81549f67ce4SDouglas Gregor 8168e3e8743SDouglas Gregor /// \brief Simple utility function that appends a \p New string to the given 8178e3e8743SDouglas Gregor /// \p Old string, using the \p Buffer for storage. 8188e3e8743SDouglas Gregor /// 8198e3e8743SDouglas Gregor /// \param Old The string to which we are appending. This parameter will be 8208e3e8743SDouglas Gregor /// updated to reflect the complete string. 8218e3e8743SDouglas Gregor /// 8228e3e8743SDouglas Gregor /// 8238e3e8743SDouglas Gregor /// \param New The string to append to \p Old. 8248e3e8743SDouglas Gregor /// 8258e3e8743SDouglas Gregor /// \param Buffer A buffer that stores the actual, concatenated string. It will 8268e3e8743SDouglas Gregor /// be used if the old string is already-non-empty. 8278e3e8743SDouglas Gregor static void AppendToString(llvm::StringRef &Old, llvm::StringRef New, 8288e3e8743SDouglas Gregor llvm::SmallString<256> &Buffer) { 8298e3e8743SDouglas Gregor if (Old.empty()) { 8308e3e8743SDouglas Gregor Old = New; 8318e3e8743SDouglas Gregor return; 8328e3e8743SDouglas Gregor } 8338e3e8743SDouglas Gregor 8348e3e8743SDouglas Gregor if (Buffer.empty()) 8358e3e8743SDouglas Gregor Buffer.append(Old.begin(), Old.end()); 8368e3e8743SDouglas Gregor Buffer.append(New.begin(), New.end()); 8378e3e8743SDouglas Gregor Old = Buffer.str(); 8388e3e8743SDouglas Gregor } 8398e3e8743SDouglas Gregor 8408e3e8743SDouglas Gregor /// \brief Get the typed-text blocks from the given code-completion string 8418e3e8743SDouglas Gregor /// and return them as a single string. 8428e3e8743SDouglas Gregor /// 8438e3e8743SDouglas Gregor /// \param String The code-completion string whose typed-text blocks will be 8448e3e8743SDouglas Gregor /// concatenated. 8458e3e8743SDouglas Gregor /// 8468e3e8743SDouglas Gregor /// \param Buffer A buffer used for storage of the completed name. 8478e3e8743SDouglas Gregor static llvm::StringRef GetTypedName(CodeCompletionString *String, 8488e3e8743SDouglas Gregor llvm::SmallString<256> &Buffer) { 8498e3e8743SDouglas Gregor llvm::StringRef Result; 8508e3e8743SDouglas Gregor for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); 8518e3e8743SDouglas Gregor C != CEnd; ++C) { 8528e3e8743SDouglas Gregor if (C->Kind == CodeCompletionString::CK_TypedText) 8538e3e8743SDouglas Gregor AppendToString(Result, C->Text, Buffer); 8548e3e8743SDouglas Gregor } 8558e3e8743SDouglas Gregor 8568e3e8743SDouglas Gregor return Result; 8578e3e8743SDouglas Gregor } 8588e3e8743SDouglas Gregor 85949f67ce4SDouglas Gregor namespace { 86049f67ce4SDouglas Gregor struct OrderCompletionResults { 86149f67ce4SDouglas Gregor bool operator()(const CXCompletionResult &XR, 86249f67ce4SDouglas Gregor const CXCompletionResult &YR) const { 863b278aafbSDouglas Gregor CodeCompletionString *X 864b278aafbSDouglas Gregor = (CodeCompletionString *)XR.CompletionString; 865b278aafbSDouglas Gregor CodeCompletionString *Y 866b278aafbSDouglas Gregor = (CodeCompletionString *)YR.CompletionString; 86749f67ce4SDouglas Gregor 8688e3e8743SDouglas Gregor llvm::SmallString<256> XBuffer; 8698e3e8743SDouglas Gregor llvm::StringRef XText = GetTypedName(X, XBuffer); 8708e3e8743SDouglas Gregor llvm::SmallString<256> YBuffer; 8718e3e8743SDouglas Gregor llvm::StringRef YText = GetTypedName(Y, YBuffer); 87249f67ce4SDouglas Gregor 8738e3e8743SDouglas Gregor if (XText.empty() || YText.empty()) 8748e3e8743SDouglas Gregor return !XText.empty(); 8758e3e8743SDouglas Gregor 8768e3e8743SDouglas Gregor int result = XText.compare_lower(YText); 87749f67ce4SDouglas Gregor if (result < 0) 87849f67ce4SDouglas Gregor return true; 87949f67ce4SDouglas Gregor if (result > 0) 88049f67ce4SDouglas Gregor return false; 88149f67ce4SDouglas Gregor 8828e3e8743SDouglas Gregor result = XText.compare(YText); 883c7a7d92aSDouglas Gregor return result < 0; 88449f67ce4SDouglas Gregor } 88549f67ce4SDouglas Gregor }; 88649f67ce4SDouglas Gregor } 88749f67ce4SDouglas Gregor 88849f67ce4SDouglas Gregor extern "C" { 88949f67ce4SDouglas Gregor void clang_sortCodeCompletionResults(CXCompletionResult *Results, 89049f67ce4SDouglas Gregor unsigned NumResults) { 89149f67ce4SDouglas Gregor std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); 89249f67ce4SDouglas Gregor } 89349f67ce4SDouglas Gregor } 894