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" 16d5a2a073SDaniel Dunbar #include "CIndexDiagnostic.h" 17d5a2a073SDaniel Dunbar #include "clang/Basic/SourceManager.h" 18d5a2a073SDaniel Dunbar #include "clang/Basic/FileManager.h" 198e984da8SDouglas Gregor #include "clang/Frontend/ASTUnit.h" 20d5a2a073SDaniel Dunbar #include "clang/Frontend/CompilerInstance.h" 21d5a2a073SDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 22d5a2a073SDaniel Dunbar #include "clang/Sema/CodeCompleteConsumer.h" 23028d3e4dSDouglas Gregor #include "llvm/ADT/SmallString.h" 24d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 2577af1c56SDaniel Dunbar #include "llvm/Support/CrashRecoveryContext.h" 26d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 27028d3e4dSDouglas Gregor #include "llvm/Support/Timer.h" 28028d3e4dSDouglas Gregor #include "llvm/Support/raw_ostream.h" 29d5a2a073SDaniel Dunbar #include "llvm/System/Program.h" 30d6009ffbSDouglas Gregor #include <cstdlib> 31d6009ffbSDouglas Gregor #include <cstdio> 32d5a2a073SDaniel Dunbar 33028d3e4dSDouglas Gregor 34d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 35d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 36d5a2a073SDaniel Dunbar #include <arpa/inet.h> 37d5a2a073SDaniel Dunbar #include <sys/socket.h> 38d5a2a073SDaniel Dunbar #include <sys/types.h> 39d5a2a073SDaniel Dunbar #include <unistd.h> 40d5a2a073SDaniel Dunbar #endif 41d5a2a073SDaniel Dunbar 42d5a2a073SDaniel Dunbar using namespace clang; 43d5a2a073SDaniel Dunbar using namespace clang::cxstring; 44d5a2a073SDaniel Dunbar 45a2db793fSDouglas Gregor namespace { 46a2db793fSDouglas Gregor /// \brief Stored representation of a completion string. 47a2db793fSDouglas Gregor /// 48a2db793fSDouglas Gregor /// This is the representation behind a CXCompletionString. 49a2db793fSDouglas Gregor class CXStoredCodeCompletionString : public CodeCompletionString { 50a2db793fSDouglas Gregor unsigned Priority; 51f757a12dSDouglas Gregor CXAvailabilityKind Availability; 52a2db793fSDouglas Gregor 53a2db793fSDouglas Gregor public: 54f757a12dSDouglas Gregor CXStoredCodeCompletionString(unsigned Priority, 55f757a12dSDouglas Gregor CXAvailabilityKind Availability) 56f757a12dSDouglas Gregor : Priority(Priority), Availability(Availability) { } 57a2db793fSDouglas Gregor 58a2db793fSDouglas Gregor unsigned getPriority() const { return Priority; } 59f757a12dSDouglas Gregor CXAvailabilityKind getAvailability() const { return Availability; } 60a2db793fSDouglas Gregor }; 61a2db793fSDouglas Gregor } 62a2db793fSDouglas Gregor 63d5a2a073SDaniel Dunbar extern "C" { 64d5a2a073SDaniel Dunbar 65d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 66d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 67d5a2a073SDaniel Dunbar unsigned chunk_number) { 68a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 69a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 70d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 71d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 72d5a2a073SDaniel Dunbar 73d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 104d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 105d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 106d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 107d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 108d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 109d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 110d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 111d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 112d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 113d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 114d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 115d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 116d5a2a073SDaniel Dunbar } 117d5a2a073SDaniel Dunbar 118d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 119d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 120d5a2a073SDaniel Dunbar } 121d5a2a073SDaniel Dunbar 122d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 123d5a2a073SDaniel Dunbar unsigned chunk_number) { 124a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 125a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 126d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 127d5a2a073SDaniel Dunbar return createCXString(0); 128d5a2a073SDaniel Dunbar 129d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 136d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 137d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 138d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 140d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 141d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 142d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 143d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 144d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 145d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 146d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 147d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 148d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 149d5a2a073SDaniel Dunbar return createCXString((*CCStr)[chunk_number].Text, false); 150d5a2a073SDaniel Dunbar 15109737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 15209737ee8SDouglas Gregor // FIXME: Temporary hack until we figure out how to handle vertical space. 15309737ee8SDouglas Gregor return createCXString(" "); 15409737ee8SDouglas Gregor 155d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 156d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 157d5a2a073SDaniel Dunbar return createCXString(""); 158d5a2a073SDaniel Dunbar } 159d5a2a073SDaniel Dunbar 160d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 161d5a2a073SDaniel Dunbar return createCXString(0); 162d5a2a073SDaniel Dunbar } 163d5a2a073SDaniel Dunbar 164d5a2a073SDaniel Dunbar 165d5a2a073SDaniel Dunbar CXCompletionString 166d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 167d5a2a073SDaniel Dunbar unsigned chunk_number) { 168a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 169a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 170d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 171d5a2a073SDaniel Dunbar return 0; 172d5a2a073SDaniel Dunbar 173d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 177d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 178d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 180d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 181d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 182d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 183d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 184d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 185d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 186d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 187d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 188d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 189d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 190d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 191d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 192d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 193d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 194d5a2a073SDaniel Dunbar return 0; 195d5a2a073SDaniel Dunbar 196d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 197d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 198d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 199d5a2a073SDaniel Dunbar } 200d5a2a073SDaniel Dunbar 201d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 202d5a2a073SDaniel Dunbar return 0; 203d5a2a073SDaniel Dunbar } 204d5a2a073SDaniel Dunbar 205d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 206a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 207a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 208d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 209d5a2a073SDaniel Dunbar } 210d5a2a073SDaniel Dunbar 211a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 212a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 213a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 21447bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 215a2db793fSDouglas Gregor } 216a2db793fSDouglas Gregor 217f757a12dSDouglas Gregor enum CXAvailabilityKind 218f757a12dSDouglas Gregor clang_getCompletionAvailability(CXCompletionString completion_string) { 219f757a12dSDouglas Gregor CXStoredCodeCompletionString *CCStr 220f757a12dSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 221f757a12dSDouglas Gregor return CCStr? CCStr->getAvailability() : CXAvailability_Available; 222f757a12dSDouglas Gregor } 223f757a12dSDouglas Gregor 224d5a2a073SDaniel Dunbar static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, 225d5a2a073SDaniel Dunbar unsigned &Value) { 226d5a2a073SDaniel Dunbar if (Memory + sizeof(unsigned) > MemoryEnd) 227d5a2a073SDaniel Dunbar return true; 228d5a2a073SDaniel Dunbar 229d5a2a073SDaniel Dunbar memmove(&Value, Memory, sizeof(unsigned)); 230d5a2a073SDaniel Dunbar Memory += sizeof(unsigned); 231d5a2a073SDaniel Dunbar return false; 232d5a2a073SDaniel Dunbar } 233d5a2a073SDaniel Dunbar 234d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 235d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 236d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 237d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults(); 238d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 239d5a2a073SDaniel Dunbar 240d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 241d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 242d5a2a073SDaniel Dunbar 243d5a2a073SDaniel Dunbar /// \brief Diag object 2448e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 245d5a2a073SDaniel Dunbar 246d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 247d5a2a073SDaniel Dunbar LangOptions LangOpts; 248d5a2a073SDaniel Dunbar 249d5a2a073SDaniel Dunbar /// \brief Source manager, used for diagnostics. 250d5a2a073SDaniel Dunbar SourceManager SourceMgr; 251d5a2a073SDaniel Dunbar 252d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 253d5a2a073SDaniel Dunbar FileManager FileMgr; 254d5a2a073SDaniel Dunbar 255d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 256d5a2a073SDaniel Dunbar /// with the code-completion results. 257d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 258d8a5dba9SDouglas Gregor 259d8a5dba9SDouglas Gregor /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. 260d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 261d5a2a073SDaniel Dunbar }; 262d5a2a073SDaniel Dunbar 263d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 264d8a5dba9SDouglas Gregor : CXCodeCompleteResults(), Diag(new Diagnostic), SourceMgr(*Diag) { } 265d5a2a073SDaniel Dunbar 266d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 267d5a2a073SDaniel Dunbar for (unsigned I = 0, N = NumResults; I != N; ++I) 268a2db793fSDouglas Gregor delete (CXStoredCodeCompletionString *)Results[I].CompletionString; 269d5a2a073SDaniel Dunbar delete [] Results; 270d5a2a073SDaniel Dunbar 271d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 272d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 273d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 274d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 275d5a2a073SDaniel Dunbar } 276d5a2a073SDaniel Dunbar 277d5a2a073SDaniel Dunbar CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, 278d5a2a073SDaniel Dunbar const char *source_filename, 279d5a2a073SDaniel Dunbar int num_command_line_args, 28057879fafSDouglas Gregor const char * const *command_line_args, 281d5a2a073SDaniel Dunbar unsigned num_unsaved_files, 282d5a2a073SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 283d5a2a073SDaniel Dunbar const char *complete_filename, 284d5a2a073SDaniel Dunbar unsigned complete_line, 285d5a2a073SDaniel Dunbar unsigned complete_column) { 286d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 287d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 288d5a2a073SDaniel Dunbar const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 289d5a2a073SDaniel Dunbar #endif 290d5a2a073SDaniel Dunbar #endif 291d5a2a073SDaniel Dunbar 292d6009ffbSDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 293d6009ffbSDouglas Gregor 294028d3e4dSDouglas Gregor llvm::OwningPtr<llvm::NamedRegionTimer> CCTimer; 295028d3e4dSDouglas Gregor if (getenv("LIBCLANG_TIMING")) { 296028d3e4dSDouglas Gregor llvm::SmallString<128> TimerName; 297028d3e4dSDouglas Gregor llvm::raw_svector_ostream TimerNameOut(TimerName); 2988ef4c807SDouglas Gregor TimerNameOut << "Code completion (out-of-process) @ " << complete_filename 2998ef4c807SDouglas Gregor << ":" << complete_line << ":" << complete_column; 300028d3e4dSDouglas Gregor CCTimer.reset(new llvm::NamedRegionTimer(TimerNameOut.str())); 301028d3e4dSDouglas Gregor } 302028d3e4dSDouglas Gregor 303d5a2a073SDaniel Dunbar // The indexer, which is mainly used to determine where diagnostics go. 304d5a2a073SDaniel Dunbar CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 305d5a2a073SDaniel Dunbar 306d5a2a073SDaniel Dunbar // Configure the diagnostics. 307d5a2a073SDaniel Dunbar DiagnosticOptions DiagOpts; 308d5a2a073SDaniel Dunbar llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 309d5a2a073SDaniel Dunbar Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 310d5a2a073SDaniel Dunbar 311d5a2a073SDaniel Dunbar // The set of temporary files that we've built. 312d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 313d5a2a073SDaniel Dunbar 314d5a2a073SDaniel Dunbar // Build up the arguments for invoking 'clang'. 315d5a2a073SDaniel Dunbar std::vector<const char *> argv; 316d5a2a073SDaniel Dunbar 317d5a2a073SDaniel Dunbar // First add the complete path to the 'clang' executable. 318d5a2a073SDaniel Dunbar llvm::sys::Path ClangPath = CXXIdx->getClangPath(); 319d5a2a073SDaniel Dunbar argv.push_back(ClangPath.c_str()); 320d5a2a073SDaniel Dunbar 321df000da8SDaniel Dunbar // Always use Clang C++ support. 322df000da8SDaniel Dunbar argv.push_back("-ccc-clang-cxx"); 323df000da8SDaniel Dunbar 324d5a2a073SDaniel Dunbar // Add the '-fsyntax-only' argument so that we only perform a basic 325d5a2a073SDaniel Dunbar // syntax check of the code. 326d5a2a073SDaniel Dunbar argv.push_back("-fsyntax-only"); 327d5a2a073SDaniel Dunbar 328d5a2a073SDaniel Dunbar // Add the appropriate '-code-completion-at=file:line:column' argument 329d5a2a073SDaniel Dunbar // to perform code completion, with an "-Xclang" preceding it. 330d5a2a073SDaniel Dunbar std::string code_complete_at; 331d5a2a073SDaniel Dunbar code_complete_at += complete_filename; 332d5a2a073SDaniel Dunbar code_complete_at += ":"; 333d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_line); 334d5a2a073SDaniel Dunbar code_complete_at += ":"; 335d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_column); 336d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 337d5a2a073SDaniel Dunbar argv.push_back("-code-completion-at"); 338d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 339d5a2a073SDaniel Dunbar argv.push_back(code_complete_at.c_str()); 340d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 341d5a2a073SDaniel Dunbar argv.push_back("-no-code-completion-debug-printer"); 342d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 343d5a2a073SDaniel Dunbar argv.push_back("-code-completion-macros"); 344d5a2a073SDaniel Dunbar argv.push_back("-fdiagnostics-binary"); 345d5a2a073SDaniel Dunbar 346d5a2a073SDaniel Dunbar // Remap any unsaved files to temporary files. 347d5a2a073SDaniel Dunbar std::vector<std::string> RemapArgs; 348d5a2a073SDaniel Dunbar if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) 349d5a2a073SDaniel Dunbar return 0; 350d5a2a073SDaniel Dunbar 351d5a2a073SDaniel Dunbar // The pointers into the elements of RemapArgs are stable because we 352d5a2a073SDaniel Dunbar // won't be adding anything to RemapArgs after this point. 353d5a2a073SDaniel Dunbar for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) 354d5a2a073SDaniel Dunbar argv.push_back(RemapArgs[i].c_str()); 355d5a2a073SDaniel Dunbar 356d5a2a073SDaniel Dunbar // Add the source file name (FIXME: later, we'll want to build temporary 357d5a2a073SDaniel Dunbar // file from the buffer, or just feed the source text via standard input). 358d5a2a073SDaniel Dunbar if (source_filename) 359d5a2a073SDaniel Dunbar argv.push_back(source_filename); 360d5a2a073SDaniel Dunbar 361d5a2a073SDaniel Dunbar // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. 362d5a2a073SDaniel Dunbar for (int i = 0; i < num_command_line_args; ++i) 363d5a2a073SDaniel Dunbar if (const char *arg = command_line_args[i]) { 364d5a2a073SDaniel Dunbar if (strcmp(arg, "-o") == 0) { 365d5a2a073SDaniel Dunbar ++i; // Also skip the matching argument. 366d5a2a073SDaniel Dunbar continue; 367d5a2a073SDaniel Dunbar } 368d5a2a073SDaniel Dunbar if (strcmp(arg, "-emit-ast") == 0 || 369d5a2a073SDaniel Dunbar strcmp(arg, "-c") == 0 || 370d5a2a073SDaniel Dunbar strcmp(arg, "-fsyntax-only") == 0) { 371d5a2a073SDaniel Dunbar continue; 372d5a2a073SDaniel Dunbar } 373d5a2a073SDaniel Dunbar 374d5a2a073SDaniel Dunbar // Keep the argument. 375d5a2a073SDaniel Dunbar argv.push_back(arg); 376d5a2a073SDaniel Dunbar } 377d5a2a073SDaniel Dunbar 378d6009ffbSDouglas Gregor if (EnableLogging) { 379d6009ffbSDouglas Gregor std::string Log = ClangPath.str(); 380d6009ffbSDouglas Gregor for (unsigned I = 0, N = argv.size(); I != N; ++I) { 381d6009ffbSDouglas Gregor Log += ' '; 382d6009ffbSDouglas Gregor Log += argv[I]; 383d6009ffbSDouglas Gregor } 384d6009ffbSDouglas Gregor fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str()); 385d6009ffbSDouglas Gregor } 386d6009ffbSDouglas Gregor 387d5a2a073SDaniel Dunbar // Add the null terminator. 388d5a2a073SDaniel Dunbar argv.push_back(NULL); 389d5a2a073SDaniel Dunbar 390d5a2a073SDaniel Dunbar // Generate a temporary name for the code-completion results file. 391d5a2a073SDaniel Dunbar char tmpFile[L_tmpnam]; 392d5a2a073SDaniel Dunbar char *tmpFileName = tmpnam(tmpFile); 393d5a2a073SDaniel Dunbar llvm::sys::Path ResultsFile(tmpFileName); 394d5a2a073SDaniel Dunbar TemporaryFiles.push_back(ResultsFile); 395d5a2a073SDaniel Dunbar 396d5a2a073SDaniel Dunbar // Generate a temporary name for the diagnostics file. 397d5a2a073SDaniel Dunbar char tmpFileResults[L_tmpnam]; 398d5a2a073SDaniel Dunbar char *tmpResultsFileName = tmpnam(tmpFileResults); 399d5a2a073SDaniel Dunbar llvm::sys::Path DiagnosticsFile(tmpResultsFileName); 400d5a2a073SDaniel Dunbar TemporaryFiles.push_back(DiagnosticsFile); 401d5a2a073SDaniel Dunbar 402d6009ffbSDouglas Gregor 403d6009ffbSDouglas Gregor 404d5a2a073SDaniel Dunbar // Invoke 'clang'. 405d5a2a073SDaniel Dunbar llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null 406d5a2a073SDaniel Dunbar // on Unix or NUL (Windows). 407d5a2a073SDaniel Dunbar std::string ErrMsg; 408d5a2a073SDaniel Dunbar const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, 409d5a2a073SDaniel Dunbar &DiagnosticsFile, 0 }; 410d5a2a073SDaniel Dunbar llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, 411d5a2a073SDaniel Dunbar /* redirects */ &Redirects[0], 412d5a2a073SDaniel Dunbar /* secondsToWait */ 0, 413d5a2a073SDaniel Dunbar /* memoryLimits */ 0, &ErrMsg); 414d5a2a073SDaniel Dunbar 415d5a2a073SDaniel Dunbar if (!ErrMsg.empty()) { 416d5a2a073SDaniel Dunbar std::string AllArgs; 417d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 418d5a2a073SDaniel Dunbar I != E; ++I) { 419d5a2a073SDaniel Dunbar AllArgs += ' '; 420d5a2a073SDaniel Dunbar if (*I) 421d5a2a073SDaniel Dunbar AllArgs += *I; 422d5a2a073SDaniel Dunbar } 423d5a2a073SDaniel Dunbar 424d5a2a073SDaniel Dunbar Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; 425d5a2a073SDaniel Dunbar } 426d5a2a073SDaniel Dunbar 427d5a2a073SDaniel Dunbar // Parse the resulting source file to find code-completion results. 428d5a2a073SDaniel Dunbar using llvm::MemoryBuffer; 429d5a2a073SDaniel Dunbar using llvm::StringRef; 430d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 431d5a2a073SDaniel Dunbar Results->Results = 0; 432d5a2a073SDaniel Dunbar Results->NumResults = 0; 433d5a2a073SDaniel Dunbar // FIXME: Set Results->LangOpts! 434d5a2a073SDaniel Dunbar if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { 435d5a2a073SDaniel Dunbar llvm::SmallVector<CXCompletionResult, 4> CompletionResults; 436d5a2a073SDaniel Dunbar StringRef Buffer = F->getBuffer(); 437d5a2a073SDaniel Dunbar for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); 438d5a2a073SDaniel Dunbar Str < StrEnd;) { 439d5a2a073SDaniel Dunbar unsigned KindValue; 440d5a2a073SDaniel Dunbar if (ReadUnsigned(Str, StrEnd, KindValue)) 441d5a2a073SDaniel Dunbar break; 442d5a2a073SDaniel Dunbar 443a2db793fSDouglas Gregor unsigned Priority; 444a2db793fSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Priority)) 445a2db793fSDouglas Gregor break; 446a2db793fSDouglas Gregor 447f757a12dSDouglas Gregor unsigned Availability; 448f757a12dSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Availability)) 449f757a12dSDouglas Gregor break; 450f757a12dSDouglas Gregor 451a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 452f757a12dSDouglas Gregor = new CXStoredCodeCompletionString(Priority, 453f757a12dSDouglas Gregor (CXAvailabilityKind)Availability); 454a2db793fSDouglas Gregor if (!CCStr->Deserialize(Str, StrEnd)) { 455a2db793fSDouglas Gregor delete CCStr; 456d5a2a073SDaniel Dunbar continue; 457a2db793fSDouglas Gregor } 458d5a2a073SDaniel Dunbar 459d5a2a073SDaniel Dunbar if (!CCStr->empty()) { 460d5a2a073SDaniel Dunbar // Vend the code-completion result to the caller. 461d5a2a073SDaniel Dunbar CXCompletionResult Result; 462d5a2a073SDaniel Dunbar Result.CursorKind = (CXCursorKind)KindValue; 463d5a2a073SDaniel Dunbar Result.CompletionString = CCStr; 464d5a2a073SDaniel Dunbar CompletionResults.push_back(Result); 465d5a2a073SDaniel Dunbar } 466d5a2a073SDaniel Dunbar }; 467d5a2a073SDaniel Dunbar 468d5a2a073SDaniel Dunbar // Allocate the results. 469d5a2a073SDaniel Dunbar Results->Results = new CXCompletionResult [CompletionResults.size()]; 470d5a2a073SDaniel Dunbar Results->NumResults = CompletionResults.size(); 471d5a2a073SDaniel Dunbar memcpy(Results->Results, CompletionResults.data(), 472d5a2a073SDaniel Dunbar CompletionResults.size() * sizeof(CXCompletionResult)); 473d8a5dba9SDouglas Gregor Results->TemporaryBuffers.push_back(F); 474d5a2a073SDaniel Dunbar } 475d5a2a073SDaniel Dunbar 476d5a2a073SDaniel Dunbar LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, 477d5a2a073SDaniel Dunbar Results->FileMgr, Results->SourceMgr, 478d5a2a073SDaniel Dunbar Results->Diagnostics); 479d5a2a073SDaniel Dunbar 480d5a2a073SDaniel Dunbar // Make sure we delete temporary files when the code-completion results are 481d5a2a073SDaniel Dunbar // destroyed. 482d5a2a073SDaniel Dunbar Results->TemporaryFiles.swap(TemporaryFiles); 483d5a2a073SDaniel Dunbar 484d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 485d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 486d5a2a073SDaniel Dunbar const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 487d5a2a073SDaniel Dunbar llvm::SmallString<256> LogResult; 488d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(LogResult); 489d5a2a073SDaniel Dunbar 490d5a2a073SDaniel Dunbar // Figure out the language and whether or not it uses PCH. 491d5a2a073SDaniel Dunbar const char *lang = 0; 492d5a2a073SDaniel Dunbar bool usesPCH = false; 493d5a2a073SDaniel Dunbar 494d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 495d5a2a073SDaniel Dunbar I != E; ++I) { 496d5a2a073SDaniel Dunbar if (*I == 0) 497d5a2a073SDaniel Dunbar continue; 498d5a2a073SDaniel Dunbar if (strcmp(*I, "-x") == 0) { 499d5a2a073SDaniel Dunbar if (I + 1 != E) { 500d5a2a073SDaniel Dunbar lang = *(++I); 501d5a2a073SDaniel Dunbar continue; 502d5a2a073SDaniel Dunbar } 503d5a2a073SDaniel Dunbar } 504d5a2a073SDaniel Dunbar else if (strcmp(*I, "-include") == 0) { 505d5a2a073SDaniel Dunbar if (I+1 != E) { 506d5a2a073SDaniel Dunbar const char *arg = *(++I); 507d5a2a073SDaniel Dunbar llvm::SmallString<512> pchName; 508d5a2a073SDaniel Dunbar { 509d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(pchName); 510d5a2a073SDaniel Dunbar os << arg << ".pth"; 511d5a2a073SDaniel Dunbar } 512d5a2a073SDaniel Dunbar pchName.push_back('\0'); 513d5a2a073SDaniel Dunbar struct stat stat_results; 514d5a2a073SDaniel Dunbar if (stat(pchName.data(), &stat_results) == 0) 515d5a2a073SDaniel Dunbar usesPCH = true; 516d5a2a073SDaniel Dunbar continue; 517d5a2a073SDaniel Dunbar } 518d5a2a073SDaniel Dunbar } 519d5a2a073SDaniel Dunbar } 520d5a2a073SDaniel Dunbar 521d5a2a073SDaniel Dunbar os << "{ "; 522d5a2a073SDaniel Dunbar os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 523d5a2a073SDaniel Dunbar os << ", \"numRes\": " << Results->NumResults; 524d5a2a073SDaniel Dunbar os << ", \"diags\": " << Results->Diagnostics.size(); 525d5a2a073SDaniel Dunbar os << ", \"pch\": " << (usesPCH ? "true" : "false"); 526d5a2a073SDaniel Dunbar os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 527d5a2a073SDaniel Dunbar const char *name = getlogin(); 528d5a2a073SDaniel Dunbar os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 529d5a2a073SDaniel Dunbar os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 530d5a2a073SDaniel Dunbar os << " }"; 531d5a2a073SDaniel Dunbar 532d5a2a073SDaniel Dunbar llvm::StringRef res = os.str(); 533d5a2a073SDaniel Dunbar if (res.size() > 0) { 534d5a2a073SDaniel Dunbar do { 535d5a2a073SDaniel Dunbar // Setup the UDP socket. 536d5a2a073SDaniel Dunbar struct sockaddr_in servaddr; 537d5a2a073SDaniel Dunbar bzero(&servaddr, sizeof(servaddr)); 538d5a2a073SDaniel Dunbar servaddr.sin_family = AF_INET; 539d5a2a073SDaniel Dunbar servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 540d5a2a073SDaniel Dunbar if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 541d5a2a073SDaniel Dunbar &servaddr.sin_addr) <= 0) 542d5a2a073SDaniel Dunbar break; 543d5a2a073SDaniel Dunbar 544d5a2a073SDaniel Dunbar int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 545d5a2a073SDaniel Dunbar if (sockfd < 0) 546d5a2a073SDaniel Dunbar break; 547d5a2a073SDaniel Dunbar 548d5a2a073SDaniel Dunbar sendto(sockfd, res.data(), res.size(), 0, 549d5a2a073SDaniel Dunbar (struct sockaddr *)&servaddr, sizeof(servaddr)); 550d5a2a073SDaniel Dunbar close(sockfd); 551d5a2a073SDaniel Dunbar } 552d5a2a073SDaniel Dunbar while (false); 553d5a2a073SDaniel Dunbar } 554d5a2a073SDaniel Dunbar #endif 555d5a2a073SDaniel Dunbar #endif 5569704c75bSDouglas Gregor clang_sortCodeCompletionResults(Results->Results, Results->NumResults); 557d5a2a073SDaniel Dunbar return Results; 558d5a2a073SDaniel Dunbar } 559d5a2a073SDaniel Dunbar 5608e984da8SDouglas Gregor } // end extern "C" 5618e984da8SDouglas Gregor 5628e984da8SDouglas Gregor namespace { 5638e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 5648e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 5658e984da8SDouglas Gregor 5668e984da8SDouglas Gregor public: 5678e984da8SDouglas Gregor explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 56839982197SDouglas Gregor : CodeCompleteConsumer(true, false, true, false), 56939982197SDouglas Gregor AllocatedResults(Results) { } 5708e984da8SDouglas Gregor 57100c37ef0SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, 57200c37ef0SDouglas Gregor CodeCompletionContext Context, 573276321a9SJohn McCall CodeCompletionResult *Results, 5748e984da8SDouglas Gregor unsigned NumResults) { 5758e984da8SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [NumResults]; 5768e984da8SDouglas Gregor AllocatedResults.NumResults = NumResults; 5778e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 5788e984da8SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 579f757a12dSDouglas Gregor = new CXStoredCodeCompletionString(Results[I].Priority, 580f757a12dSDouglas Gregor Results[I].Availability); 5818e984da8SDouglas Gregor (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); 582b14904c4SDouglas Gregor AllocatedResults.Results[I].CursorKind = Results[I].CursorKind; 5838e984da8SDouglas Gregor AllocatedResults.Results[I].CompletionString = StoredCompletion; 5848e984da8SDouglas Gregor } 5858e984da8SDouglas Gregor } 58649f67ce4SDouglas Gregor 58749f67ce4SDouglas Gregor // FIXME: Add ProcessOverloadCandidates? 5888e984da8SDouglas Gregor }; 5898e984da8SDouglas Gregor } 5908e984da8SDouglas Gregor 5918e984da8SDouglas Gregor extern "C" { 59277af1c56SDaniel Dunbar struct CodeCompleteAtInfo { 59377af1c56SDaniel Dunbar CXTranslationUnit TU; 59477af1c56SDaniel Dunbar const char *complete_filename; 59577af1c56SDaniel Dunbar unsigned complete_line; 59677af1c56SDaniel Dunbar unsigned complete_column; 59777af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files; 59877af1c56SDaniel Dunbar unsigned num_unsaved_files; 59977af1c56SDaniel Dunbar unsigned options; 60077af1c56SDaniel Dunbar CXCodeCompleteResults *result; 60177af1c56SDaniel Dunbar }; 60277af1c56SDaniel Dunbar void clang_codeCompleteAt_Impl(void *UserData) { 60377af1c56SDaniel Dunbar CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); 60477af1c56SDaniel Dunbar CXTranslationUnit TU = CCAI->TU; 60577af1c56SDaniel Dunbar const char *complete_filename = CCAI->complete_filename; 60677af1c56SDaniel Dunbar unsigned complete_line = CCAI->complete_line; 60777af1c56SDaniel Dunbar unsigned complete_column = CCAI->complete_column; 60877af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; 60977af1c56SDaniel Dunbar unsigned num_unsaved_files = CCAI->num_unsaved_files; 61077af1c56SDaniel Dunbar unsigned options = CCAI->options; 61177af1c56SDaniel Dunbar CCAI->result = 0; 61277af1c56SDaniel Dunbar 6138e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6148e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6158e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 6168e984da8SDouglas Gregor #endif 6178e984da8SDouglas Gregor #endif 6188e984da8SDouglas Gregor 6198e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 6208e984da8SDouglas Gregor 6218e984da8SDouglas Gregor ASTUnit *AST = static_cast<ASTUnit *>(TU); 6228e984da8SDouglas Gregor if (!AST) 62377af1c56SDaniel Dunbar return; 6248e984da8SDouglas Gregor 625*ca5b0539SDouglas Gregor ASTUnit::ConcurrencyCheck Check(*AST); 626*ca5b0539SDouglas Gregor 6278e984da8SDouglas Gregor // Perform the remapping of source files. 6288e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 6298e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 6308e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 6318e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 6328e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 6338e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 6348e984da8SDouglas Gregor Buffer)); 6358e984da8SDouglas Gregor } 6368e984da8SDouglas Gregor 6378e984da8SDouglas Gregor if (EnableLogging) { 6388e984da8SDouglas Gregor // FIXME: Add logging. 6398e984da8SDouglas Gregor } 6408e984da8SDouglas Gregor 6418e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 6428e984da8SDouglas Gregor AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 6438e984da8SDouglas Gregor Results->Results = 0; 6448e984da8SDouglas Gregor Results->NumResults = 0; 6458e984da8SDouglas Gregor 6468e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 6478e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 6488e984da8SDouglas Gregor 6498e984da8SDouglas Gregor // Perform completion. 6508e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 651b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 652b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 653b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 654b68bc59aSDouglas Gregor Capture, 6558e984da8SDouglas Gregor *Results->Diag, Results->LangOpts, Results->SourceMgr, 656b97b666cSDouglas Gregor Results->FileMgr, Results->Diagnostics, 657b97b666cSDouglas Gregor Results->TemporaryBuffers); 6588e984da8SDouglas Gregor 6598e984da8SDouglas Gregor 6608e984da8SDouglas Gregor 6618e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6628e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6638e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 6648e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 6658e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 6668e984da8SDouglas Gregor 6678e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 6688e984da8SDouglas Gregor const char *lang = 0; 6698e984da8SDouglas Gregor bool usesPCH = false; 6708e984da8SDouglas Gregor 6718e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 6728e984da8SDouglas Gregor I != E; ++I) { 6738e984da8SDouglas Gregor if (*I == 0) 6748e984da8SDouglas Gregor continue; 6758e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 6768e984da8SDouglas Gregor if (I + 1 != E) { 6778e984da8SDouglas Gregor lang = *(++I); 6788e984da8SDouglas Gregor continue; 6798e984da8SDouglas Gregor } 6808e984da8SDouglas Gregor } 6818e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 6828e984da8SDouglas Gregor if (I+1 != E) { 6838e984da8SDouglas Gregor const char *arg = *(++I); 6848e984da8SDouglas Gregor llvm::SmallString<512> pchName; 6858e984da8SDouglas Gregor { 6868e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 6878e984da8SDouglas Gregor os << arg << ".pth"; 6888e984da8SDouglas Gregor } 6898e984da8SDouglas Gregor pchName.push_back('\0'); 6908e984da8SDouglas Gregor struct stat stat_results; 6918e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 6928e984da8SDouglas Gregor usesPCH = true; 6938e984da8SDouglas Gregor continue; 6948e984da8SDouglas Gregor } 6958e984da8SDouglas Gregor } 6968e984da8SDouglas Gregor } 6978e984da8SDouglas Gregor 6988e984da8SDouglas Gregor os << "{ "; 6998e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 7008e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 7018e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 7028e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 7038e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 7048e984da8SDouglas Gregor const char *name = getlogin(); 7058e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 7068e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 7078e984da8SDouglas Gregor os << " }"; 7088e984da8SDouglas Gregor 7098e984da8SDouglas Gregor llvm::StringRef res = os.str(); 7108e984da8SDouglas Gregor if (res.size() > 0) { 7118e984da8SDouglas Gregor do { 7128e984da8SDouglas Gregor // Setup the UDP socket. 7138e984da8SDouglas Gregor struct sockaddr_in servaddr; 7148e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 7158e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 7168e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 7178e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 7188e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 7198e984da8SDouglas Gregor break; 7208e984da8SDouglas Gregor 7218e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 7228e984da8SDouglas Gregor if (sockfd < 0) 7238e984da8SDouglas Gregor break; 7248e984da8SDouglas Gregor 7258e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 7268e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 7278e984da8SDouglas Gregor close(sockfd); 7288e984da8SDouglas Gregor } 7298e984da8SDouglas Gregor while (false); 7308e984da8SDouglas Gregor } 7318e984da8SDouglas Gregor #endif 7328e984da8SDouglas Gregor #endif 73377af1c56SDaniel Dunbar CCAI->result = Results; 73477af1c56SDaniel Dunbar } 73577af1c56SDaniel Dunbar CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 73677af1c56SDaniel Dunbar const char *complete_filename, 73777af1c56SDaniel Dunbar unsigned complete_line, 73877af1c56SDaniel Dunbar unsigned complete_column, 73977af1c56SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 74077af1c56SDaniel Dunbar unsigned num_unsaved_files, 74177af1c56SDaniel Dunbar unsigned options) { 74277af1c56SDaniel Dunbar CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, 74377af1c56SDaniel Dunbar complete_column, unsaved_files, num_unsaved_files, 74477af1c56SDaniel Dunbar options, 0 }; 74577af1c56SDaniel Dunbar llvm::CrashRecoveryContext CRC; 74677af1c56SDaniel Dunbar 74777af1c56SDaniel Dunbar if (!CRC.RunSafely(clang_codeCompleteAt_Impl, &CCAI)) { 74877af1c56SDaniel Dunbar fprintf(stderr, "libclang: crash detected in code completion\n"); 74977af1c56SDaniel Dunbar static_cast<ASTUnit *>(TU)->setUnsafeToFree(true); 75077af1c56SDaniel Dunbar return 0; 75177af1c56SDaniel Dunbar } 75277af1c56SDaniel Dunbar 75377af1c56SDaniel Dunbar return CCAI.result; 7548e984da8SDouglas Gregor } 7558e984da8SDouglas Gregor 756b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 757b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 758b68bc59aSDouglas Gregor } 759b68bc59aSDouglas Gregor 760d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 761d5a2a073SDaniel Dunbar if (!ResultsIn) 762d5a2a073SDaniel Dunbar return; 763d5a2a073SDaniel Dunbar 764d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 765d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 766d5a2a073SDaniel Dunbar delete Results; 767d5a2a073SDaniel Dunbar } 768d5a2a073SDaniel Dunbar 769d5a2a073SDaniel Dunbar unsigned 770d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 771d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 772d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 773d5a2a073SDaniel Dunbar if (!Results) 774d5a2a073SDaniel Dunbar return 0; 775d5a2a073SDaniel Dunbar 776d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 777d5a2a073SDaniel Dunbar } 778d5a2a073SDaniel Dunbar 779d5a2a073SDaniel Dunbar CXDiagnostic 780d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 781d5a2a073SDaniel Dunbar unsigned Index) { 782d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 783d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 784d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 785d5a2a073SDaniel Dunbar return 0; 786d5a2a073SDaniel Dunbar 787d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 788d5a2a073SDaniel Dunbar } 789d5a2a073SDaniel Dunbar 790d5a2a073SDaniel Dunbar 791d5a2a073SDaniel Dunbar } // end extern "C" 79249f67ce4SDouglas Gregor 79349f67ce4SDouglas Gregor namespace { 79449f67ce4SDouglas Gregor struct OrderCompletionResults { 79549f67ce4SDouglas Gregor bool operator()(const CXCompletionResult &XR, 79649f67ce4SDouglas Gregor const CXCompletionResult &YR) const { 79749f67ce4SDouglas Gregor CXStoredCodeCompletionString *X 79849f67ce4SDouglas Gregor = (CXStoredCodeCompletionString *)XR.CompletionString; 79949f67ce4SDouglas Gregor CXStoredCodeCompletionString *Y 80049f67ce4SDouglas Gregor = (CXStoredCodeCompletionString *)YR.CompletionString; 80149f67ce4SDouglas Gregor 80249f67ce4SDouglas Gregor const char *XText = X->getTypedText(); 80349f67ce4SDouglas Gregor const char *YText = Y->getTypedText(); 80449f67ce4SDouglas Gregor if (!XText || !YText) 80549f67ce4SDouglas Gregor return XText != 0; 80649f67ce4SDouglas Gregor 80749f67ce4SDouglas Gregor int result = llvm::StringRef(XText).compare_lower(YText); 80849f67ce4SDouglas Gregor if (result < 0) 80949f67ce4SDouglas Gregor return true; 81049f67ce4SDouglas Gregor if (result > 0) 81149f67ce4SDouglas Gregor return false; 81249f67ce4SDouglas Gregor 81349f67ce4SDouglas Gregor result = llvm::StringRef(XText).compare(YText); 814c7a7d92aSDouglas Gregor return result < 0; 81549f67ce4SDouglas Gregor } 81649f67ce4SDouglas Gregor }; 81749f67ce4SDouglas Gregor } 81849f67ce4SDouglas Gregor 81949f67ce4SDouglas Gregor extern "C" { 82049f67ce4SDouglas Gregor void clang_sortCodeCompletionResults(CXCompletionResult *Results, 82149f67ce4SDouglas Gregor unsigned NumResults) { 82249f67ce4SDouglas Gregor std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); 82349f67ce4SDouglas Gregor } 82449f67ce4SDouglas Gregor } 825