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" 18d5a2a073SDaniel Dunbar #include "CIndexDiagnostic.h" 19d5a2a073SDaniel Dunbar #include "clang/Basic/SourceManager.h" 20d5a2a073SDaniel Dunbar #include "clang/Basic/FileManager.h" 218e984da8SDouglas Gregor #include "clang/Frontend/ASTUnit.h" 22d5a2a073SDaniel Dunbar #include "clang/Frontend/CompilerInstance.h" 23d5a2a073SDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 24d5a2a073SDaniel Dunbar #include "clang/Sema/CodeCompleteConsumer.h" 25028d3e4dSDouglas Gregor #include "llvm/ADT/SmallString.h" 26d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 2777af1c56SDaniel Dunbar #include "llvm/Support/CrashRecoveryContext.h" 28d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 29028d3e4dSDouglas Gregor #include "llvm/Support/Timer.h" 30028d3e4dSDouglas Gregor #include "llvm/Support/raw_ostream.h" 31d5a2a073SDaniel Dunbar #include "llvm/System/Program.h" 32d6009ffbSDouglas Gregor #include <cstdlib> 33d6009ffbSDouglas Gregor #include <cstdio> 34d5a2a073SDaniel Dunbar 35028d3e4dSDouglas Gregor 36d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 37d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 38d5a2a073SDaniel Dunbar #include <arpa/inet.h> 39d5a2a073SDaniel Dunbar #include <sys/socket.h> 40d5a2a073SDaniel Dunbar #include <sys/types.h> 41d5a2a073SDaniel Dunbar #include <unistd.h> 42d5a2a073SDaniel Dunbar #endif 43d5a2a073SDaniel Dunbar 44d5a2a073SDaniel Dunbar using namespace clang; 45d5a2a073SDaniel Dunbar using namespace clang::cxstring; 46d5a2a073SDaniel Dunbar 47a2db793fSDouglas Gregor namespace { 48a2db793fSDouglas Gregor /// \brief Stored representation of a completion string. 49a2db793fSDouglas Gregor /// 50a2db793fSDouglas Gregor /// This is the representation behind a CXCompletionString. 51a2db793fSDouglas Gregor class CXStoredCodeCompletionString : public CodeCompletionString { 52a2db793fSDouglas Gregor unsigned Priority; 53f757a12dSDouglas Gregor CXAvailabilityKind Availability; 54a2db793fSDouglas Gregor 55a2db793fSDouglas Gregor public: 56f757a12dSDouglas Gregor CXStoredCodeCompletionString(unsigned Priority, 57f757a12dSDouglas Gregor CXAvailabilityKind Availability) 58f757a12dSDouglas Gregor : Priority(Priority), Availability(Availability) { } 59a2db793fSDouglas Gregor 60a2db793fSDouglas Gregor unsigned getPriority() const { return Priority; } 61f757a12dSDouglas Gregor CXAvailabilityKind getAvailability() const { return Availability; } 62a2db793fSDouglas Gregor }; 63a2db793fSDouglas Gregor } 64a2db793fSDouglas Gregor 65d5a2a073SDaniel Dunbar extern "C" { 66d5a2a073SDaniel Dunbar 67d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 68d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 69d5a2a073SDaniel Dunbar unsigned chunk_number) { 70a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 71a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 72d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 73d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 74d5a2a073SDaniel Dunbar 75d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 104d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 105d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 106d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 107d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 108d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 109d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 110d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 111d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 112d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 113d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 114d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 115d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 116d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 117d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 118d5a2a073SDaniel Dunbar } 119d5a2a073SDaniel Dunbar 120d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 121d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 122d5a2a073SDaniel Dunbar } 123d5a2a073SDaniel Dunbar 124d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 125d5a2a073SDaniel Dunbar unsigned chunk_number) { 126a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 127a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 128d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 12991554285STed Kremenek return createCXString((const char*)0); 130d5a2a073SDaniel Dunbar 131d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 136d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 137d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 138d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 140d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 141d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 142d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 143d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 144d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 145d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 146d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 147d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 148d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 149d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 150d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 15109737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 1528ed5b770SDouglas Gregor return createCXString((*CCStr)[chunk_number].Text, false); 15309737ee8SDouglas Gregor 154d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 155d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 156d5a2a073SDaniel Dunbar return createCXString(""); 157d5a2a073SDaniel Dunbar } 158d5a2a073SDaniel Dunbar 159d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 16091554285STed Kremenek return createCXString((const char*)0); 161d5a2a073SDaniel Dunbar } 162d5a2a073SDaniel Dunbar 163d5a2a073SDaniel Dunbar 164d5a2a073SDaniel Dunbar CXCompletionString 165d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 166d5a2a073SDaniel Dunbar unsigned chunk_number) { 167a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 168a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 169d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 170d5a2a073SDaniel Dunbar return 0; 171d5a2a073SDaniel Dunbar 172d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 173d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 177d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 178d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 180d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 181d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 182d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 183d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 184d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 185d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 186d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 187d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 188d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 189d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 190d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 191d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 192d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 193d5a2a073SDaniel Dunbar return 0; 194d5a2a073SDaniel Dunbar 195d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 196d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 197d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 198d5a2a073SDaniel Dunbar } 199d5a2a073SDaniel Dunbar 200d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 201d5a2a073SDaniel Dunbar return 0; 202d5a2a073SDaniel Dunbar } 203d5a2a073SDaniel Dunbar 204d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 205a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 206a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 207d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 208d5a2a073SDaniel Dunbar } 209d5a2a073SDaniel Dunbar 210a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 211a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 212a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 21347bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 214a2db793fSDouglas Gregor } 215a2db793fSDouglas Gregor 216f757a12dSDouglas Gregor enum CXAvailabilityKind 217f757a12dSDouglas Gregor clang_getCompletionAvailability(CXCompletionString completion_string) { 218f757a12dSDouglas Gregor CXStoredCodeCompletionString *CCStr 219f757a12dSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 220f757a12dSDouglas Gregor return CCStr? CCStr->getAvailability() : CXAvailability_Available; 221f757a12dSDouglas Gregor } 222f757a12dSDouglas Gregor 223d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 224d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 225d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 226d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults(); 227d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 228d5a2a073SDaniel Dunbar 229d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 230d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 231d5a2a073SDaniel Dunbar 232d5a2a073SDaniel Dunbar /// \brief Diag object 2338e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 234d5a2a073SDaniel Dunbar 235d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 236d5a2a073SDaniel Dunbar LangOptions LangOpts; 237d5a2a073SDaniel Dunbar 238d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 239d5a2a073SDaniel Dunbar FileManager FileMgr; 240d5a2a073SDaniel Dunbar 24171731d6bSArgyrios Kyrtzidis FileSystemOptions FileSystemOpts; 24271731d6bSArgyrios Kyrtzidis 24371731d6bSArgyrios Kyrtzidis /// \brief Source manager, used for diagnostics. 24471731d6bSArgyrios Kyrtzidis SourceManager SourceMgr; 24571731d6bSArgyrios Kyrtzidis 246d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 247d5a2a073SDaniel Dunbar /// with the code-completion results. 248d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 249d8a5dba9SDouglas Gregor 250d8a5dba9SDouglas Gregor /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. 251d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 252d5a2a073SDaniel Dunbar }; 253d5a2a073SDaniel Dunbar 25468dbaeadSDouglas Gregor /// \brief Tracks the number of code-completion result objects that are 25568dbaeadSDouglas Gregor /// currently active. 25668dbaeadSDouglas Gregor /// 25768dbaeadSDouglas Gregor /// Used for debugging purposes only. 25868dbaeadSDouglas Gregor static unsigned CodeCompletionResultObjects; 25968dbaeadSDouglas Gregor 260d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 261*d0040648SArgyrios Kyrtzidis : CXCodeCompleteResults(), 262*d0040648SArgyrios Kyrtzidis Diag(new Diagnostic( 263*d0040648SArgyrios Kyrtzidis llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))), 26468dbaeadSDouglas Gregor SourceMgr(*Diag, FileMgr, FileSystemOpts) { 26568dbaeadSDouglas Gregor if (getenv("LIBCLANG_OBJTRACKING")) { 26668dbaeadSDouglas Gregor ++CodeCompletionResultObjects; 26768dbaeadSDouglas Gregor fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects); 26868dbaeadSDouglas Gregor } 26968dbaeadSDouglas Gregor } 270d5a2a073SDaniel Dunbar 271d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 272d5a2a073SDaniel Dunbar for (unsigned I = 0, N = NumResults; I != N; ++I) 273a2db793fSDouglas Gregor delete (CXStoredCodeCompletionString *)Results[I].CompletionString; 274d5a2a073SDaniel Dunbar delete [] Results; 275d5a2a073SDaniel Dunbar 276d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 277d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 278d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 279d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 28068dbaeadSDouglas Gregor 28168dbaeadSDouglas Gregor if (getenv("LIBCLANG_OBJTRACKING")) { 28268dbaeadSDouglas Gregor --CodeCompletionResultObjects; 28368dbaeadSDouglas Gregor fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects); 28468dbaeadSDouglas Gregor } 285d5a2a073SDaniel Dunbar } 286d5a2a073SDaniel Dunbar 2878e984da8SDouglas Gregor } // end extern "C" 2888e984da8SDouglas Gregor 2898e984da8SDouglas Gregor namespace { 2908e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 2918e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 29236e3b5c7SDouglas Gregor llvm::SmallVector<CXCompletionResult, 16> StoredResults; 2938e984da8SDouglas Gregor 2948e984da8SDouglas Gregor public: 2958e984da8SDouglas Gregor explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 29639982197SDouglas Gregor : CodeCompleteConsumer(true, false, true, false), 29739982197SDouglas Gregor AllocatedResults(Results) { } 29836e3b5c7SDouglas Gregor ~CaptureCompletionResults() { Finish(); } 2998e984da8SDouglas Gregor 30000c37ef0SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, 30100c37ef0SDouglas Gregor CodeCompletionContext Context, 302276321a9SJohn McCall CodeCompletionResult *Results, 3038e984da8SDouglas Gregor unsigned NumResults) { 30436e3b5c7SDouglas Gregor StoredResults.reserve(StoredResults.size() + NumResults); 3058e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 3068e984da8SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 307f757a12dSDouglas Gregor = new CXStoredCodeCompletionString(Results[I].Priority, 308f757a12dSDouglas Gregor Results[I].Availability); 3098e984da8SDouglas Gregor (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); 31036e3b5c7SDouglas Gregor 31136e3b5c7SDouglas Gregor CXCompletionResult R; 31236e3b5c7SDouglas Gregor R.CursorKind = Results[I].CursorKind; 31336e3b5c7SDouglas Gregor R.CompletionString = StoredCompletion; 31436e3b5c7SDouglas Gregor StoredResults.push_back(R); 3158e984da8SDouglas Gregor } 3168e984da8SDouglas Gregor } 31749f67ce4SDouglas Gregor 31836e3b5c7SDouglas Gregor virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 31936e3b5c7SDouglas Gregor OverloadCandidate *Candidates, 32036e3b5c7SDouglas Gregor unsigned NumCandidates) { 32136e3b5c7SDouglas Gregor StoredResults.reserve(StoredResults.size() + NumCandidates); 32236e3b5c7SDouglas Gregor for (unsigned I = 0; I != NumCandidates; ++I) { 32336e3b5c7SDouglas Gregor // FIXME: Set priority, availability appropriately. 32436e3b5c7SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 32536e3b5c7SDouglas Gregor = new CXStoredCodeCompletionString(1, CXAvailability_Available); 32636e3b5c7SDouglas Gregor (void)Candidates[I].CreateSignatureString(CurrentArg, S, 32736e3b5c7SDouglas Gregor StoredCompletion); 32836e3b5c7SDouglas Gregor 32936e3b5c7SDouglas Gregor CXCompletionResult R; 33036e3b5c7SDouglas Gregor R.CursorKind = CXCursor_NotImplemented; 33136e3b5c7SDouglas Gregor R.CompletionString = StoredCompletion; 33236e3b5c7SDouglas Gregor StoredResults.push_back(R); 33336e3b5c7SDouglas Gregor } 33436e3b5c7SDouglas Gregor } 33536e3b5c7SDouglas Gregor 33636e3b5c7SDouglas Gregor private: 33736e3b5c7SDouglas Gregor void Finish() { 33836e3b5c7SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; 33936e3b5c7SDouglas Gregor AllocatedResults.NumResults = StoredResults.size(); 34036e3b5c7SDouglas Gregor std::memcpy(AllocatedResults.Results, StoredResults.data(), 34136e3b5c7SDouglas Gregor StoredResults.size() * sizeof(CXCompletionResult)); 34236e3b5c7SDouglas Gregor StoredResults.clear(); 34336e3b5c7SDouglas Gregor } 3448e984da8SDouglas Gregor }; 3458e984da8SDouglas Gregor } 3468e984da8SDouglas Gregor 3478e984da8SDouglas Gregor extern "C" { 34877af1c56SDaniel Dunbar struct CodeCompleteAtInfo { 34977af1c56SDaniel Dunbar CXTranslationUnit TU; 35077af1c56SDaniel Dunbar const char *complete_filename; 35177af1c56SDaniel Dunbar unsigned complete_line; 35277af1c56SDaniel Dunbar unsigned complete_column; 35377af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files; 35477af1c56SDaniel Dunbar unsigned num_unsaved_files; 35577af1c56SDaniel Dunbar unsigned options; 35677af1c56SDaniel Dunbar CXCodeCompleteResults *result; 35777af1c56SDaniel Dunbar }; 35877af1c56SDaniel Dunbar void clang_codeCompleteAt_Impl(void *UserData) { 35977af1c56SDaniel Dunbar CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 36077af1c56SDaniel Dunbar CXTranslationUnit TU = CCAI->TU; 36177af1c56SDaniel Dunbar const char *complete_filename = CCAI->complete_filename; 36277af1c56SDaniel Dunbar unsigned complete_line = CCAI->complete_line; 36377af1c56SDaniel Dunbar unsigned complete_column = CCAI->complete_column; 36477af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 36577af1c56SDaniel Dunbar unsigned num_unsaved_files = CCAI->num_unsaved_files; 36677af1c56SDaniel Dunbar unsigned options = CCAI->options; 36777af1c56SDaniel Dunbar CCAI->result = 0; 36877af1c56SDaniel Dunbar 3698e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 3708e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 3718e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 3728e984da8SDouglas Gregor #endif 3738e984da8SDouglas Gregor #endif 3748e984da8SDouglas Gregor 3758e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 3768e984da8SDouglas Gregor 37791554285STed Kremenek ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData); 3788e984da8SDouglas Gregor if (!AST) 37977af1c56SDaniel Dunbar return; 3808e984da8SDouglas Gregor 381ca5b0539SDouglas Gregor ASTUnit::ConcurrencyCheck Check(*AST); 382ca5b0539SDouglas Gregor 3838e984da8SDouglas Gregor // Perform the remapping of source files. 3848e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 3858e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 3868e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 3878e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 3888e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 3898e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 3908e984da8SDouglas Gregor Buffer)); 3918e984da8SDouglas Gregor } 3928e984da8SDouglas Gregor 3938e984da8SDouglas Gregor if (EnableLogging) { 3948e984da8SDouglas Gregor // FIXME: Add logging. 3958e984da8SDouglas Gregor } 3968e984da8SDouglas Gregor 3978e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 3988e984da8SDouglas Gregor AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 3998e984da8SDouglas Gregor Results->Results = 0; 4008e984da8SDouglas Gregor Results->NumResults = 0; 4018e984da8SDouglas Gregor 4028e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 4038e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 4048e984da8SDouglas Gregor 4058e984da8SDouglas Gregor // Perform completion. 4068e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 407b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 408b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 409b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 410b68bc59aSDouglas Gregor Capture, 4118e984da8SDouglas Gregor *Results->Diag, Results->LangOpts, Results->SourceMgr, 412b97b666cSDouglas Gregor Results->FileMgr, Results->Diagnostics, 413b97b666cSDouglas Gregor Results->TemporaryBuffers); 4148e984da8SDouglas Gregor 4158e984da8SDouglas Gregor 4168e984da8SDouglas Gregor 4178e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 4188e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 4198e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 4208e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 4218e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 4228e984da8SDouglas Gregor 4238e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 4248e984da8SDouglas Gregor const char *lang = 0; 4258e984da8SDouglas Gregor bool usesPCH = false; 4268e984da8SDouglas Gregor 4278e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 4288e984da8SDouglas Gregor I != E; ++I) { 4298e984da8SDouglas Gregor if (*I == 0) 4308e984da8SDouglas Gregor continue; 4318e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 4328e984da8SDouglas Gregor if (I + 1 != E) { 4338e984da8SDouglas Gregor lang = *(++I); 4348e984da8SDouglas Gregor continue; 4358e984da8SDouglas Gregor } 4368e984da8SDouglas Gregor } 4378e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 4388e984da8SDouglas Gregor if (I+1 != E) { 4398e984da8SDouglas Gregor const char *arg = *(++I); 4408e984da8SDouglas Gregor llvm::SmallString<512> pchName; 4418e984da8SDouglas Gregor { 4428e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 4438e984da8SDouglas Gregor os << arg << ".pth"; 4448e984da8SDouglas Gregor } 4458e984da8SDouglas Gregor pchName.push_back('\0'); 4468e984da8SDouglas Gregor struct stat stat_results; 4478e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 4488e984da8SDouglas Gregor usesPCH = true; 4498e984da8SDouglas Gregor continue; 4508e984da8SDouglas Gregor } 4518e984da8SDouglas Gregor } 4528e984da8SDouglas Gregor } 4538e984da8SDouglas Gregor 4548e984da8SDouglas Gregor os << "{ "; 4558e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 4568e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 4578e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 4588e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 4598e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 4608e984da8SDouglas Gregor const char *name = getlogin(); 4618e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 4628e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 4638e984da8SDouglas Gregor os << " }"; 4648e984da8SDouglas Gregor 4658e984da8SDouglas Gregor llvm::StringRef res = os.str(); 4668e984da8SDouglas Gregor if (res.size() > 0) { 4678e984da8SDouglas Gregor do { 4688e984da8SDouglas Gregor // Setup the UDP socket. 4698e984da8SDouglas Gregor struct sockaddr_in servaddr; 4708e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 4718e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 4728e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 4738e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 4748e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 4758e984da8SDouglas Gregor break; 4768e984da8SDouglas Gregor 4778e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 4788e984da8SDouglas Gregor if (sockfd < 0) 4798e984da8SDouglas Gregor break; 4808e984da8SDouglas Gregor 4818e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 4828e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 4838e984da8SDouglas Gregor close(sockfd); 4848e984da8SDouglas Gregor } 4858e984da8SDouglas Gregor while (false); 4868e984da8SDouglas Gregor } 4878e984da8SDouglas Gregor #endif 4888e984da8SDouglas Gregor #endif 48977af1c56SDaniel Dunbar CCAI->result = Results; 49077af1c56SDaniel Dunbar } 49177af1c56SDaniel Dunbar CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 49277af1c56SDaniel Dunbar const char *complete_filename, 49377af1c56SDaniel Dunbar unsigned complete_line, 49477af1c56SDaniel Dunbar unsigned complete_column, 49577af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 49677af1c56SDaniel Dunbar unsigned num_unsaved_files, 49777af1c56SDaniel Dunbar unsigned options) { 49877af1c56SDaniel Dunbar CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 49977af1c56SDaniel Dunbar complete_column, unsaved_files, num_unsaved_files, 50077af1c56SDaniel Dunbar options, 0 }; 50177af1c56SDaniel Dunbar llvm::CrashRecoveryContext CRC; 50277af1c56SDaniel Dunbar 503b7383e6dSDaniel Dunbar if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { 50477af1c56SDaniel Dunbar fprintf(stderr, "libclang: crash detected in code completion\n"); 50591554285STed Kremenek static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); 50677af1c56SDaniel Dunbar return 0; 50777af1c56SDaniel Dunbar } 50877af1c56SDaniel Dunbar 50977af1c56SDaniel Dunbar return CCAI.result; 5108e984da8SDouglas Gregor } 5118e984da8SDouglas Gregor 512b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 513b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 514b68bc59aSDouglas Gregor } 515b68bc59aSDouglas Gregor 516d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 517d5a2a073SDaniel Dunbar if (!ResultsIn) 518d5a2a073SDaniel Dunbar return; 519d5a2a073SDaniel Dunbar 520d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 521d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 522d5a2a073SDaniel Dunbar delete Results; 523d5a2a073SDaniel Dunbar } 524d5a2a073SDaniel Dunbar 525d5a2a073SDaniel Dunbar unsigned 526d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 527d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 528d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 529d5a2a073SDaniel Dunbar if (!Results) 530d5a2a073SDaniel Dunbar return 0; 531d5a2a073SDaniel Dunbar 532d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 533d5a2a073SDaniel Dunbar } 534d5a2a073SDaniel Dunbar 535d5a2a073SDaniel Dunbar CXDiagnostic 536d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 537d5a2a073SDaniel Dunbar unsigned Index) { 538d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 539d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 540d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 541d5a2a073SDaniel Dunbar return 0; 542d5a2a073SDaniel Dunbar 543d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 544d5a2a073SDaniel Dunbar } 545d5a2a073SDaniel Dunbar 546d5a2a073SDaniel Dunbar 547d5a2a073SDaniel Dunbar } // end extern "C" 54849f67ce4SDouglas Gregor 5498e3e8743SDouglas Gregor /// \brief Simple utility function that appends a \p New string to the given 5508e3e8743SDouglas Gregor /// \p Old string, using the \p Buffer for storage. 5518e3e8743SDouglas Gregor /// 5528e3e8743SDouglas Gregor /// \param Old The string to which we are appending. This parameter will be 5538e3e8743SDouglas Gregor /// updated to reflect the complete string. 5548e3e8743SDouglas Gregor /// 5558e3e8743SDouglas Gregor /// 5568e3e8743SDouglas Gregor /// \param New The string to append to \p Old. 5578e3e8743SDouglas Gregor /// 5588e3e8743SDouglas Gregor /// \param Buffer A buffer that stores the actual, concatenated string. It will 5598e3e8743SDouglas Gregor /// be used if the old string is already-non-empty. 5608e3e8743SDouglas Gregor static void AppendToString(llvm::StringRef &Old, llvm::StringRef New, 5618e3e8743SDouglas Gregor llvm::SmallString<256> &Buffer) { 5628e3e8743SDouglas Gregor if (Old.empty()) { 5638e3e8743SDouglas Gregor Old = New; 5648e3e8743SDouglas Gregor return; 5658e3e8743SDouglas Gregor } 5668e3e8743SDouglas Gregor 5678e3e8743SDouglas Gregor if (Buffer.empty()) 5688e3e8743SDouglas Gregor Buffer.append(Old.begin(), Old.end()); 5698e3e8743SDouglas Gregor Buffer.append(New.begin(), New.end()); 5708e3e8743SDouglas Gregor Old = Buffer.str(); 5718e3e8743SDouglas Gregor } 5728e3e8743SDouglas Gregor 5738e3e8743SDouglas Gregor /// \brief Get the typed-text blocks from the given code-completion string 5748e3e8743SDouglas Gregor /// and return them as a single string. 5758e3e8743SDouglas Gregor /// 5768e3e8743SDouglas Gregor /// \param String The code-completion string whose typed-text blocks will be 5778e3e8743SDouglas Gregor /// concatenated. 5788e3e8743SDouglas Gregor /// 5798e3e8743SDouglas Gregor /// \param Buffer A buffer used for storage of the completed name. 5808e3e8743SDouglas Gregor static llvm::StringRef GetTypedName(CodeCompletionString *String, 5818e3e8743SDouglas Gregor llvm::SmallString<256> &Buffer) { 5828e3e8743SDouglas Gregor llvm::StringRef Result; 5838e3e8743SDouglas Gregor for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); 5848e3e8743SDouglas Gregor C != CEnd; ++C) { 5858e3e8743SDouglas Gregor if (C->Kind == CodeCompletionString::CK_TypedText) 5868e3e8743SDouglas Gregor AppendToString(Result, C->Text, Buffer); 5878e3e8743SDouglas Gregor } 5888e3e8743SDouglas Gregor 5898e3e8743SDouglas Gregor return Result; 5908e3e8743SDouglas Gregor } 5918e3e8743SDouglas Gregor 59249f67ce4SDouglas Gregor namespace { 59349f67ce4SDouglas Gregor struct OrderCompletionResults { 59449f67ce4SDouglas Gregor bool operator()(const CXCompletionResult &XR, 59549f67ce4SDouglas Gregor const CXCompletionResult &YR) const { 59649f67ce4SDouglas Gregor CXStoredCodeCompletionString *X 59749f67ce4SDouglas Gregor = (CXStoredCodeCompletionString *)XR.CompletionString; 59849f67ce4SDouglas Gregor CXStoredCodeCompletionString *Y 59949f67ce4SDouglas Gregor = (CXStoredCodeCompletionString *)YR.CompletionString; 60049f67ce4SDouglas Gregor 6018e3e8743SDouglas Gregor llvm::SmallString<256> XBuffer; 6028e3e8743SDouglas Gregor llvm::StringRef XText = GetTypedName(X, XBuffer); 6038e3e8743SDouglas Gregor llvm::SmallString<256> YBuffer; 6048e3e8743SDouglas Gregor llvm::StringRef YText = GetTypedName(Y, YBuffer); 60549f67ce4SDouglas Gregor 6068e3e8743SDouglas Gregor if (XText.empty() || YText.empty()) 6078e3e8743SDouglas Gregor return !XText.empty(); 6088e3e8743SDouglas Gregor 6098e3e8743SDouglas Gregor int result = XText.compare_lower(YText); 61049f67ce4SDouglas Gregor if (result < 0) 61149f67ce4SDouglas Gregor return true; 61249f67ce4SDouglas Gregor if (result > 0) 61349f67ce4SDouglas Gregor return false; 61449f67ce4SDouglas Gregor 6158e3e8743SDouglas Gregor result = XText.compare(YText); 616c7a7d92aSDouglas Gregor return result < 0; 61749f67ce4SDouglas Gregor } 61849f67ce4SDouglas Gregor }; 61949f67ce4SDouglas Gregor } 62049f67ce4SDouglas Gregor 62149f67ce4SDouglas Gregor extern "C" { 62249f67ce4SDouglas Gregor void clang_sortCodeCompletionResults(CXCompletionResult *Results, 62349f67ce4SDouglas Gregor unsigned NumResults) { 62449f67ce4SDouglas Gregor std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); 62549f67ce4SDouglas Gregor } 62649f67ce4SDouglas Gregor } 627