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" 23d5a2a073SDaniel Dunbar #include "llvm/ADT/StringExtras.h" 24d5a2a073SDaniel Dunbar #include "llvm/Support/MemoryBuffer.h" 25d5a2a073SDaniel Dunbar #include "llvm/System/Program.h" 26d6009ffbSDouglas Gregor #include <cstdlib> 27d6009ffbSDouglas Gregor #include <cstdio> 28d5a2a073SDaniel Dunbar 29d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 30d5a2a073SDaniel Dunbar #include "clang/Basic/Version.h" 31d5a2a073SDaniel Dunbar #include "llvm/ADT/SmallString.h" 32d5a2a073SDaniel Dunbar #include "llvm/Support/Timer.h" 33d5a2a073SDaniel Dunbar #include "llvm/Support/raw_ostream.h" 34d5a2a073SDaniel Dunbar #include <arpa/inet.h> 35d5a2a073SDaniel Dunbar #include <sys/socket.h> 36d5a2a073SDaniel Dunbar #include <sys/types.h> 37d5a2a073SDaniel Dunbar #include <unistd.h> 38d5a2a073SDaniel Dunbar #endif 39d5a2a073SDaniel Dunbar 40d5a2a073SDaniel Dunbar using namespace clang; 41d5a2a073SDaniel Dunbar using namespace clang::cxstring; 42d5a2a073SDaniel Dunbar 43a2db793fSDouglas Gregor namespace { 44a2db793fSDouglas Gregor /// \brief Stored representation of a completion string. 45a2db793fSDouglas Gregor /// 46a2db793fSDouglas Gregor /// This is the representation behind a CXCompletionString. 47a2db793fSDouglas Gregor class CXStoredCodeCompletionString : public CodeCompletionString { 48a2db793fSDouglas Gregor unsigned Priority; 49a2db793fSDouglas Gregor 50a2db793fSDouglas Gregor public: 51a2db793fSDouglas Gregor CXStoredCodeCompletionString(unsigned Priority) : Priority(Priority) { } 52a2db793fSDouglas Gregor 53a2db793fSDouglas Gregor unsigned getPriority() const { return Priority; } 54a2db793fSDouglas Gregor }; 55a2db793fSDouglas Gregor } 56a2db793fSDouglas Gregor 57d5a2a073SDaniel Dunbar extern "C" { 58d5a2a073SDaniel Dunbar 59d5a2a073SDaniel Dunbar enum CXCompletionChunkKind 60d5a2a073SDaniel Dunbar clang_getCompletionChunkKind(CXCompletionString completion_string, 61d5a2a073SDaniel Dunbar unsigned chunk_number) { 62a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 63a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 64d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 65d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 66d5a2a073SDaniel Dunbar 67d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 68d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 69d5a2a073SDaniel Dunbar return CXCompletionChunk_TypedText; 70d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 71d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 72d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 73d5a2a073SDaniel Dunbar return CXCompletionChunk_Optional; 74d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 75d5a2a073SDaniel Dunbar return CXCompletionChunk_Placeholder; 76d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 77d5a2a073SDaniel Dunbar return CXCompletionChunk_Informative; 78d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 79d5a2a073SDaniel Dunbar return CXCompletionChunk_ResultType; 80d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 81d5a2a073SDaniel Dunbar return CXCompletionChunk_CurrentParameter; 82d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 83d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftParen; 84d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 85d5a2a073SDaniel Dunbar return CXCompletionChunk_RightParen; 86d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 87d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBracket; 88d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 89d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBracket; 90d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 91d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftBrace; 92d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 93d5a2a073SDaniel Dunbar return CXCompletionChunk_RightBrace; 94d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 95d5a2a073SDaniel Dunbar return CXCompletionChunk_LeftAngle; 96d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 97d5a2a073SDaniel Dunbar return CXCompletionChunk_RightAngle; 98d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 99d5a2a073SDaniel Dunbar return CXCompletionChunk_Comma; 100d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 101d5a2a073SDaniel Dunbar return CXCompletionChunk_Colon; 102d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 103d5a2a073SDaniel Dunbar return CXCompletionChunk_SemiColon; 104d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 105d5a2a073SDaniel Dunbar return CXCompletionChunk_Equal; 106d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 107d5a2a073SDaniel Dunbar return CXCompletionChunk_HorizontalSpace; 108d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 109d5a2a073SDaniel Dunbar return CXCompletionChunk_VerticalSpace; 110d5a2a073SDaniel Dunbar } 111d5a2a073SDaniel Dunbar 112d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 113d5a2a073SDaniel Dunbar return CXCompletionChunk_Text; 114d5a2a073SDaniel Dunbar } 115d5a2a073SDaniel Dunbar 116d5a2a073SDaniel Dunbar CXString clang_getCompletionChunkText(CXCompletionString completion_string, 117d5a2a073SDaniel Dunbar unsigned chunk_number) { 118a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 119a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 120d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 121d5a2a073SDaniel Dunbar return createCXString(0); 122d5a2a073SDaniel Dunbar 123d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 124d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 125d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 126d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 127d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 128d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 129d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 130d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 131d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 132d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 133d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 134d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 135d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 136d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 137d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 138d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 139d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 140d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 141d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 142d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 143d5a2a073SDaniel Dunbar return createCXString((*CCStr)[chunk_number].Text, false); 144d5a2a073SDaniel Dunbar 14509737ee8SDouglas Gregor case CodeCompletionString::CK_VerticalSpace: 14609737ee8SDouglas Gregor // FIXME: Temporary hack until we figure out how to handle vertical space. 14709737ee8SDouglas Gregor return createCXString(" "); 14809737ee8SDouglas Gregor 149d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 150d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 151d5a2a073SDaniel Dunbar return createCXString(""); 152d5a2a073SDaniel Dunbar } 153d5a2a073SDaniel Dunbar 154d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 155d5a2a073SDaniel Dunbar return createCXString(0); 156d5a2a073SDaniel Dunbar } 157d5a2a073SDaniel Dunbar 158d5a2a073SDaniel Dunbar 159d5a2a073SDaniel Dunbar CXCompletionString 160d5a2a073SDaniel Dunbar clang_getCompletionChunkCompletionString(CXCompletionString completion_string, 161d5a2a073SDaniel Dunbar unsigned chunk_number) { 162a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 163a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 164d5a2a073SDaniel Dunbar if (!CCStr || chunk_number >= CCStr->size()) 165d5a2a073SDaniel Dunbar return 0; 166d5a2a073SDaniel Dunbar 167d5a2a073SDaniel Dunbar switch ((*CCStr)[chunk_number].Kind) { 168d5a2a073SDaniel Dunbar case CodeCompletionString::CK_TypedText: 169d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Text: 170d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Placeholder: 171d5a2a073SDaniel Dunbar case CodeCompletionString::CK_CurrentParameter: 172d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Informative: 173d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftParen: 174d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightParen: 175d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBracket: 176d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBracket: 177d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftBrace: 178d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightBrace: 179d5a2a073SDaniel Dunbar case CodeCompletionString::CK_LeftAngle: 180d5a2a073SDaniel Dunbar case CodeCompletionString::CK_RightAngle: 181d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Comma: 182d5a2a073SDaniel Dunbar case CodeCompletionString::CK_ResultType: 183d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Colon: 184d5a2a073SDaniel Dunbar case CodeCompletionString::CK_SemiColon: 185d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Equal: 186d5a2a073SDaniel Dunbar case CodeCompletionString::CK_HorizontalSpace: 187d5a2a073SDaniel Dunbar case CodeCompletionString::CK_VerticalSpace: 188d5a2a073SDaniel Dunbar return 0; 189d5a2a073SDaniel Dunbar 190d5a2a073SDaniel Dunbar case CodeCompletionString::CK_Optional: 191d5a2a073SDaniel Dunbar // Note: treated as an empty text block. 192d5a2a073SDaniel Dunbar return (*CCStr)[chunk_number].Optional; 193d5a2a073SDaniel Dunbar } 194d5a2a073SDaniel Dunbar 195d5a2a073SDaniel Dunbar // Should be unreachable, but let's be careful. 196d5a2a073SDaniel Dunbar return 0; 197d5a2a073SDaniel Dunbar } 198d5a2a073SDaniel Dunbar 199d5a2a073SDaniel Dunbar unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { 200a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 201a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 202d5a2a073SDaniel Dunbar return CCStr? CCStr->size() : 0; 203d5a2a073SDaniel Dunbar } 204d5a2a073SDaniel Dunbar 205a2db793fSDouglas Gregor unsigned clang_getCompletionPriority(CXCompletionString completion_string) { 206a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 207a2db793fSDouglas Gregor = (CXStoredCodeCompletionString *)completion_string; 20847bb3e2bSBill Wendling return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); 209a2db793fSDouglas Gregor } 210a2db793fSDouglas Gregor 211d5a2a073SDaniel Dunbar static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, 212d5a2a073SDaniel Dunbar unsigned &Value) { 213d5a2a073SDaniel Dunbar if (Memory + sizeof(unsigned) > MemoryEnd) 214d5a2a073SDaniel Dunbar return true; 215d5a2a073SDaniel Dunbar 216d5a2a073SDaniel Dunbar memmove(&Value, Memory, sizeof(unsigned)); 217d5a2a073SDaniel Dunbar Memory += sizeof(unsigned); 218d5a2a073SDaniel Dunbar return false; 219d5a2a073SDaniel Dunbar } 220d5a2a073SDaniel Dunbar 221d5a2a073SDaniel Dunbar /// \brief The CXCodeCompleteResults structure we allocate internally; 222d5a2a073SDaniel Dunbar /// the client only sees the initial CXCodeCompleteResults structure. 223d5a2a073SDaniel Dunbar struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { 224d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults(); 225d5a2a073SDaniel Dunbar ~AllocatedCXCodeCompleteResults(); 226d5a2a073SDaniel Dunbar 227d5a2a073SDaniel Dunbar /// \brief Diagnostics produced while performing code completion. 228d5a2a073SDaniel Dunbar llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; 229d5a2a073SDaniel Dunbar 230d5a2a073SDaniel Dunbar /// \brief Diag object 2318e984da8SDouglas Gregor llvm::IntrusiveRefCntPtr<Diagnostic> Diag; 232d5a2a073SDaniel Dunbar 233d5a2a073SDaniel Dunbar /// \brief Language options used to adjust source locations. 234d5a2a073SDaniel Dunbar LangOptions LangOpts; 235d5a2a073SDaniel Dunbar 236d5a2a073SDaniel Dunbar /// \brief Source manager, used for diagnostics. 237d5a2a073SDaniel Dunbar SourceManager SourceMgr; 238d5a2a073SDaniel Dunbar 239d5a2a073SDaniel Dunbar /// \brief File manager, used for diagnostics. 240d5a2a073SDaniel Dunbar FileManager FileMgr; 241d5a2a073SDaniel Dunbar 242d5a2a073SDaniel Dunbar /// \brief Temporary files that should be removed once we have finished 243d5a2a073SDaniel Dunbar /// with the code-completion results. 244d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 245d8a5dba9SDouglas Gregor 246d8a5dba9SDouglas Gregor /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results. 247d8a5dba9SDouglas Gregor llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; 248d5a2a073SDaniel Dunbar }; 249d5a2a073SDaniel Dunbar 250d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 251d8a5dba9SDouglas Gregor : CXCodeCompleteResults(), Diag(new Diagnostic), SourceMgr(*Diag) { } 252d5a2a073SDaniel Dunbar 253d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { 254d5a2a073SDaniel Dunbar for (unsigned I = 0, N = NumResults; I != N; ++I) 255a2db793fSDouglas Gregor delete (CXStoredCodeCompletionString *)Results[I].CompletionString; 256d5a2a073SDaniel Dunbar delete [] Results; 257d5a2a073SDaniel Dunbar 258d5a2a073SDaniel Dunbar for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) 259d5a2a073SDaniel Dunbar TemporaryFiles[I].eraseFromDisk(); 260d8a5dba9SDouglas Gregor for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) 261d8a5dba9SDouglas Gregor delete TemporaryBuffers[I]; 262d5a2a073SDaniel Dunbar } 263d5a2a073SDaniel Dunbar 264d5a2a073SDaniel Dunbar CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, 265d5a2a073SDaniel Dunbar const char *source_filename, 266d5a2a073SDaniel Dunbar int num_command_line_args, 267d5a2a073SDaniel Dunbar const char **command_line_args, 268d5a2a073SDaniel Dunbar unsigned num_unsaved_files, 269d5a2a073SDaniel Dunbar struct CXUnsavedFile *unsaved_files, 270d5a2a073SDaniel Dunbar const char *complete_filename, 271d5a2a073SDaniel Dunbar unsigned complete_line, 272d5a2a073SDaniel Dunbar unsigned complete_column) { 273d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 274d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 275d5a2a073SDaniel Dunbar const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 276d5a2a073SDaniel Dunbar #endif 277d5a2a073SDaniel Dunbar #endif 278d5a2a073SDaniel Dunbar 279d6009ffbSDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 280d6009ffbSDouglas Gregor 281d5a2a073SDaniel Dunbar // The indexer, which is mainly used to determine where diagnostics go. 282d5a2a073SDaniel Dunbar CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); 283d5a2a073SDaniel Dunbar 284d5a2a073SDaniel Dunbar // Configure the diagnostics. 285d5a2a073SDaniel Dunbar DiagnosticOptions DiagOpts; 286d5a2a073SDaniel Dunbar llvm::IntrusiveRefCntPtr<Diagnostic> Diags; 287d5a2a073SDaniel Dunbar Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); 288d5a2a073SDaniel Dunbar 289d5a2a073SDaniel Dunbar // The set of temporary files that we've built. 290d5a2a073SDaniel Dunbar std::vector<llvm::sys::Path> TemporaryFiles; 291d5a2a073SDaniel Dunbar 292d5a2a073SDaniel Dunbar // Build up the arguments for invoking 'clang'. 293d5a2a073SDaniel Dunbar std::vector<const char *> argv; 294d5a2a073SDaniel Dunbar 295d5a2a073SDaniel Dunbar // First add the complete path to the 'clang' executable. 296d5a2a073SDaniel Dunbar llvm::sys::Path ClangPath = CXXIdx->getClangPath(); 297d5a2a073SDaniel Dunbar argv.push_back(ClangPath.c_str()); 298d5a2a073SDaniel Dunbar 299df000da8SDaniel Dunbar // Always use Clang C++ support. 300df000da8SDaniel Dunbar argv.push_back("-ccc-clang-cxx"); 301df000da8SDaniel Dunbar 302d5a2a073SDaniel Dunbar // Add the '-fsyntax-only' argument so that we only perform a basic 303d5a2a073SDaniel Dunbar // syntax check of the code. 304d5a2a073SDaniel Dunbar argv.push_back("-fsyntax-only"); 305d5a2a073SDaniel Dunbar 306d5a2a073SDaniel Dunbar // Add the appropriate '-code-completion-at=file:line:column' argument 307d5a2a073SDaniel Dunbar // to perform code completion, with an "-Xclang" preceding it. 308d5a2a073SDaniel Dunbar std::string code_complete_at; 309d5a2a073SDaniel Dunbar code_complete_at += complete_filename; 310d5a2a073SDaniel Dunbar code_complete_at += ":"; 311d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_line); 312d5a2a073SDaniel Dunbar code_complete_at += ":"; 313d5a2a073SDaniel Dunbar code_complete_at += llvm::utostr(complete_column); 314d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 315d5a2a073SDaniel Dunbar argv.push_back("-code-completion-at"); 316d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 317d5a2a073SDaniel Dunbar argv.push_back(code_complete_at.c_str()); 318d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 319d5a2a073SDaniel Dunbar argv.push_back("-no-code-completion-debug-printer"); 320d5a2a073SDaniel Dunbar argv.push_back("-Xclang"); 321d5a2a073SDaniel Dunbar argv.push_back("-code-completion-macros"); 322d5a2a073SDaniel Dunbar argv.push_back("-fdiagnostics-binary"); 323d5a2a073SDaniel Dunbar 324d5a2a073SDaniel Dunbar // Remap any unsaved files to temporary files. 325d5a2a073SDaniel Dunbar std::vector<std::string> RemapArgs; 326d5a2a073SDaniel Dunbar if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) 327d5a2a073SDaniel Dunbar return 0; 328d5a2a073SDaniel Dunbar 329d5a2a073SDaniel Dunbar // The pointers into the elements of RemapArgs are stable because we 330d5a2a073SDaniel Dunbar // won't be adding anything to RemapArgs after this point. 331d5a2a073SDaniel Dunbar for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) 332d5a2a073SDaniel Dunbar argv.push_back(RemapArgs[i].c_str()); 333d5a2a073SDaniel Dunbar 334d5a2a073SDaniel Dunbar // Add the source file name (FIXME: later, we'll want to build temporary 335d5a2a073SDaniel Dunbar // file from the buffer, or just feed the source text via standard input). 336d5a2a073SDaniel Dunbar if (source_filename) 337d5a2a073SDaniel Dunbar argv.push_back(source_filename); 338d5a2a073SDaniel Dunbar 339d5a2a073SDaniel Dunbar // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. 340d5a2a073SDaniel Dunbar for (int i = 0; i < num_command_line_args; ++i) 341d5a2a073SDaniel Dunbar if (const char *arg = command_line_args[i]) { 342d5a2a073SDaniel Dunbar if (strcmp(arg, "-o") == 0) { 343d5a2a073SDaniel Dunbar ++i; // Also skip the matching argument. 344d5a2a073SDaniel Dunbar continue; 345d5a2a073SDaniel Dunbar } 346d5a2a073SDaniel Dunbar if (strcmp(arg, "-emit-ast") == 0 || 347d5a2a073SDaniel Dunbar strcmp(arg, "-c") == 0 || 348d5a2a073SDaniel Dunbar strcmp(arg, "-fsyntax-only") == 0) { 349d5a2a073SDaniel Dunbar continue; 350d5a2a073SDaniel Dunbar } 351d5a2a073SDaniel Dunbar 352d5a2a073SDaniel Dunbar // Keep the argument. 353d5a2a073SDaniel Dunbar argv.push_back(arg); 354d5a2a073SDaniel Dunbar } 355d5a2a073SDaniel Dunbar 356d6009ffbSDouglas Gregor if (EnableLogging) { 357d6009ffbSDouglas Gregor std::string Log = ClangPath.str(); 358d6009ffbSDouglas Gregor for (unsigned I = 0, N = argv.size(); I != N; ++I) { 359d6009ffbSDouglas Gregor Log += ' '; 360d6009ffbSDouglas Gregor Log += argv[I]; 361d6009ffbSDouglas Gregor } 362d6009ffbSDouglas Gregor fprintf(stderr, "libclang (Code Completion): %s\n", Log.c_str()); 363d6009ffbSDouglas Gregor } 364d6009ffbSDouglas Gregor 365d5a2a073SDaniel Dunbar // Add the null terminator. 366d5a2a073SDaniel Dunbar argv.push_back(NULL); 367d5a2a073SDaniel Dunbar 368d5a2a073SDaniel Dunbar // Generate a temporary name for the code-completion results file. 369d5a2a073SDaniel Dunbar char tmpFile[L_tmpnam]; 370d5a2a073SDaniel Dunbar char *tmpFileName = tmpnam(tmpFile); 371d5a2a073SDaniel Dunbar llvm::sys::Path ResultsFile(tmpFileName); 372d5a2a073SDaniel Dunbar TemporaryFiles.push_back(ResultsFile); 373d5a2a073SDaniel Dunbar 374d5a2a073SDaniel Dunbar // Generate a temporary name for the diagnostics file. 375d5a2a073SDaniel Dunbar char tmpFileResults[L_tmpnam]; 376d5a2a073SDaniel Dunbar char *tmpResultsFileName = tmpnam(tmpFileResults); 377d5a2a073SDaniel Dunbar llvm::sys::Path DiagnosticsFile(tmpResultsFileName); 378d5a2a073SDaniel Dunbar TemporaryFiles.push_back(DiagnosticsFile); 379d5a2a073SDaniel Dunbar 380d6009ffbSDouglas Gregor 381d6009ffbSDouglas Gregor 382d5a2a073SDaniel Dunbar // Invoke 'clang'. 383d5a2a073SDaniel Dunbar llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null 384d5a2a073SDaniel Dunbar // on Unix or NUL (Windows). 385d5a2a073SDaniel Dunbar std::string ErrMsg; 386d5a2a073SDaniel Dunbar const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, 387d5a2a073SDaniel Dunbar &DiagnosticsFile, 0 }; 388d5a2a073SDaniel Dunbar llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, 389d5a2a073SDaniel Dunbar /* redirects */ &Redirects[0], 390d5a2a073SDaniel Dunbar /* secondsToWait */ 0, 391d5a2a073SDaniel Dunbar /* memoryLimits */ 0, &ErrMsg); 392d5a2a073SDaniel Dunbar 393d5a2a073SDaniel Dunbar if (!ErrMsg.empty()) { 394d5a2a073SDaniel Dunbar std::string AllArgs; 395d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 396d5a2a073SDaniel Dunbar I != E; ++I) { 397d5a2a073SDaniel Dunbar AllArgs += ' '; 398d5a2a073SDaniel Dunbar if (*I) 399d5a2a073SDaniel Dunbar AllArgs += *I; 400d5a2a073SDaniel Dunbar } 401d5a2a073SDaniel Dunbar 402d5a2a073SDaniel Dunbar Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; 403d5a2a073SDaniel Dunbar } 404d5a2a073SDaniel Dunbar 405d5a2a073SDaniel Dunbar // Parse the resulting source file to find code-completion results. 406d5a2a073SDaniel Dunbar using llvm::MemoryBuffer; 407d5a2a073SDaniel Dunbar using llvm::StringRef; 408d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 409d5a2a073SDaniel Dunbar Results->Results = 0; 410d5a2a073SDaniel Dunbar Results->NumResults = 0; 411d5a2a073SDaniel Dunbar // FIXME: Set Results->LangOpts! 412d5a2a073SDaniel Dunbar if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { 413d5a2a073SDaniel Dunbar llvm::SmallVector<CXCompletionResult, 4> CompletionResults; 414d5a2a073SDaniel Dunbar StringRef Buffer = F->getBuffer(); 415d5a2a073SDaniel Dunbar for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); 416d5a2a073SDaniel Dunbar Str < StrEnd;) { 417d5a2a073SDaniel Dunbar unsigned KindValue; 418d5a2a073SDaniel Dunbar if (ReadUnsigned(Str, StrEnd, KindValue)) 419d5a2a073SDaniel Dunbar break; 420d5a2a073SDaniel Dunbar 421a2db793fSDouglas Gregor unsigned Priority; 422a2db793fSDouglas Gregor if (ReadUnsigned(Str, StrEnd, Priority)) 423a2db793fSDouglas Gregor break; 424a2db793fSDouglas Gregor 425a2db793fSDouglas Gregor CXStoredCodeCompletionString *CCStr 426a2db793fSDouglas Gregor = new CXStoredCodeCompletionString(Priority); 427a2db793fSDouglas Gregor if (!CCStr->Deserialize(Str, StrEnd)) { 428a2db793fSDouglas Gregor delete CCStr; 429d5a2a073SDaniel Dunbar continue; 430a2db793fSDouglas Gregor } 431d5a2a073SDaniel Dunbar 432d5a2a073SDaniel Dunbar if (!CCStr->empty()) { 433d5a2a073SDaniel Dunbar // Vend the code-completion result to the caller. 434d5a2a073SDaniel Dunbar CXCompletionResult Result; 435d5a2a073SDaniel Dunbar Result.CursorKind = (CXCursorKind)KindValue; 436d5a2a073SDaniel Dunbar Result.CompletionString = CCStr; 437d5a2a073SDaniel Dunbar CompletionResults.push_back(Result); 438d5a2a073SDaniel Dunbar } 439d5a2a073SDaniel Dunbar }; 440d5a2a073SDaniel Dunbar 441d5a2a073SDaniel Dunbar // Allocate the results. 442d5a2a073SDaniel Dunbar Results->Results = new CXCompletionResult [CompletionResults.size()]; 443d5a2a073SDaniel Dunbar Results->NumResults = CompletionResults.size(); 444d5a2a073SDaniel Dunbar memcpy(Results->Results, CompletionResults.data(), 445d5a2a073SDaniel Dunbar CompletionResults.size() * sizeof(CXCompletionResult)); 446d8a5dba9SDouglas Gregor Results->TemporaryBuffers.push_back(F); 447d5a2a073SDaniel Dunbar } 448d5a2a073SDaniel Dunbar 449d5a2a073SDaniel Dunbar LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, 450d5a2a073SDaniel Dunbar Results->FileMgr, Results->SourceMgr, 451d5a2a073SDaniel Dunbar Results->Diagnostics); 452d5a2a073SDaniel Dunbar 453d5a2a073SDaniel Dunbar // Make sure we delete temporary files when the code-completion results are 454d5a2a073SDaniel Dunbar // destroyed. 455d5a2a073SDaniel Dunbar Results->TemporaryFiles.swap(TemporaryFiles); 456d5a2a073SDaniel Dunbar 457d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER 458d5a2a073SDaniel Dunbar #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 459d5a2a073SDaniel Dunbar const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 460d5a2a073SDaniel Dunbar llvm::SmallString<256> LogResult; 461d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(LogResult); 462d5a2a073SDaniel Dunbar 463d5a2a073SDaniel Dunbar // Figure out the language and whether or not it uses PCH. 464d5a2a073SDaniel Dunbar const char *lang = 0; 465d5a2a073SDaniel Dunbar bool usesPCH = false; 466d5a2a073SDaniel Dunbar 467d5a2a073SDaniel Dunbar for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 468d5a2a073SDaniel Dunbar I != E; ++I) { 469d5a2a073SDaniel Dunbar if (*I == 0) 470d5a2a073SDaniel Dunbar continue; 471d5a2a073SDaniel Dunbar if (strcmp(*I, "-x") == 0) { 472d5a2a073SDaniel Dunbar if (I + 1 != E) { 473d5a2a073SDaniel Dunbar lang = *(++I); 474d5a2a073SDaniel Dunbar continue; 475d5a2a073SDaniel Dunbar } 476d5a2a073SDaniel Dunbar } 477d5a2a073SDaniel Dunbar else if (strcmp(*I, "-include") == 0) { 478d5a2a073SDaniel Dunbar if (I+1 != E) { 479d5a2a073SDaniel Dunbar const char *arg = *(++I); 480d5a2a073SDaniel Dunbar llvm::SmallString<512> pchName; 481d5a2a073SDaniel Dunbar { 482d5a2a073SDaniel Dunbar llvm::raw_svector_ostream os(pchName); 483d5a2a073SDaniel Dunbar os << arg << ".pth"; 484d5a2a073SDaniel Dunbar } 485d5a2a073SDaniel Dunbar pchName.push_back('\0'); 486d5a2a073SDaniel Dunbar struct stat stat_results; 487d5a2a073SDaniel Dunbar if (stat(pchName.data(), &stat_results) == 0) 488d5a2a073SDaniel Dunbar usesPCH = true; 489d5a2a073SDaniel Dunbar continue; 490d5a2a073SDaniel Dunbar } 491d5a2a073SDaniel Dunbar } 492d5a2a073SDaniel Dunbar } 493d5a2a073SDaniel Dunbar 494d5a2a073SDaniel Dunbar os << "{ "; 495d5a2a073SDaniel Dunbar os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 496d5a2a073SDaniel Dunbar os << ", \"numRes\": " << Results->NumResults; 497d5a2a073SDaniel Dunbar os << ", \"diags\": " << Results->Diagnostics.size(); 498d5a2a073SDaniel Dunbar os << ", \"pch\": " << (usesPCH ? "true" : "false"); 499d5a2a073SDaniel Dunbar os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 500d5a2a073SDaniel Dunbar const char *name = getlogin(); 501d5a2a073SDaniel Dunbar os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 502d5a2a073SDaniel Dunbar os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 503d5a2a073SDaniel Dunbar os << " }"; 504d5a2a073SDaniel Dunbar 505d5a2a073SDaniel Dunbar llvm::StringRef res = os.str(); 506d5a2a073SDaniel Dunbar if (res.size() > 0) { 507d5a2a073SDaniel Dunbar do { 508d5a2a073SDaniel Dunbar // Setup the UDP socket. 509d5a2a073SDaniel Dunbar struct sockaddr_in servaddr; 510d5a2a073SDaniel Dunbar bzero(&servaddr, sizeof(servaddr)); 511d5a2a073SDaniel Dunbar servaddr.sin_family = AF_INET; 512d5a2a073SDaniel Dunbar servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 513d5a2a073SDaniel Dunbar if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 514d5a2a073SDaniel Dunbar &servaddr.sin_addr) <= 0) 515d5a2a073SDaniel Dunbar break; 516d5a2a073SDaniel Dunbar 517d5a2a073SDaniel Dunbar int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 518d5a2a073SDaniel Dunbar if (sockfd < 0) 519d5a2a073SDaniel Dunbar break; 520d5a2a073SDaniel Dunbar 521d5a2a073SDaniel Dunbar sendto(sockfd, res.data(), res.size(), 0, 522d5a2a073SDaniel Dunbar (struct sockaddr *)&servaddr, sizeof(servaddr)); 523d5a2a073SDaniel Dunbar close(sockfd); 524d5a2a073SDaniel Dunbar } 525d5a2a073SDaniel Dunbar while (false); 526d5a2a073SDaniel Dunbar } 527d5a2a073SDaniel Dunbar #endif 528d5a2a073SDaniel Dunbar #endif 529d5a2a073SDaniel Dunbar return Results; 530d5a2a073SDaniel Dunbar } 531d5a2a073SDaniel Dunbar 5328e984da8SDouglas Gregor } // end extern "C" 5338e984da8SDouglas Gregor 5348e984da8SDouglas Gregor namespace clang { 5358e984da8SDouglas Gregor // FIXME: defined in CodeCompleteConsumer.cpp, but should be a 5368e984da8SDouglas Gregor // static function here. 5378e984da8SDouglas Gregor CXCursorKind 5388e984da8SDouglas Gregor getCursorKindForCompletionResult(const CodeCompleteConsumer::Result &R); 5398e984da8SDouglas Gregor } 5408e984da8SDouglas Gregor 5418e984da8SDouglas Gregor 5428e984da8SDouglas Gregor namespace { 5438e984da8SDouglas Gregor class CaptureCompletionResults : public CodeCompleteConsumer { 5448e984da8SDouglas Gregor AllocatedCXCodeCompleteResults &AllocatedResults; 5458e984da8SDouglas Gregor 5468e984da8SDouglas Gregor public: 5478e984da8SDouglas Gregor explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results) 5488e984da8SDouglas Gregor : CodeCompleteConsumer(true, false, false), AllocatedResults(Results) { } 5498e984da8SDouglas Gregor 5508e984da8SDouglas Gregor virtual void ProcessCodeCompleteResults(Sema &S, Result *Results, 5518e984da8SDouglas Gregor unsigned NumResults) { 5528e984da8SDouglas Gregor AllocatedResults.Results = new CXCompletionResult [NumResults]; 5538e984da8SDouglas Gregor AllocatedResults.NumResults = NumResults; 5548e984da8SDouglas Gregor for (unsigned I = 0; I != NumResults; ++I) { 5558e984da8SDouglas Gregor CXStoredCodeCompletionString *StoredCompletion 5568e984da8SDouglas Gregor = new CXStoredCodeCompletionString(Results[I].Priority); 5578e984da8SDouglas Gregor (void)Results[I].CreateCodeCompletionString(S, StoredCompletion); 5588e984da8SDouglas Gregor AllocatedResults.Results[I].CursorKind 5598e984da8SDouglas Gregor = getCursorKindForCompletionResult(Results[I]); 5608e984da8SDouglas Gregor AllocatedResults.Results[I].CompletionString = StoredCompletion; 5618e984da8SDouglas Gregor } 5628e984da8SDouglas Gregor } 5638e984da8SDouglas Gregor }; 5648e984da8SDouglas Gregor } 5658e984da8SDouglas Gregor 5668e984da8SDouglas Gregor extern "C" { 5678e984da8SDouglas Gregor CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, 5688e984da8SDouglas Gregor const char *complete_filename, 5698e984da8SDouglas Gregor unsigned complete_line, 5708e984da8SDouglas Gregor unsigned complete_column, 5718e984da8SDouglas Gregor struct CXUnsavedFile *unsaved_files, 572*b68bc59aSDouglas Gregor unsigned num_unsaved_files, 573*b68bc59aSDouglas Gregor unsigned options) { 5748e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 5758e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 5768e984da8SDouglas Gregor const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); 5778e984da8SDouglas Gregor #endif 5788e984da8SDouglas Gregor #endif 5798e984da8SDouglas Gregor 5808e984da8SDouglas Gregor bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; 5818e984da8SDouglas Gregor 5828e984da8SDouglas Gregor ASTUnit *AST = static_cast<ASTUnit *>(TU); 5838e984da8SDouglas Gregor if (!AST) 5848e984da8SDouglas Gregor return 0; 5858e984da8SDouglas Gregor 5868e984da8SDouglas Gregor // Perform the remapping of source files. 5878e984da8SDouglas Gregor llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; 5888e984da8SDouglas Gregor for (unsigned I = 0; I != num_unsaved_files; ++I) { 5898e984da8SDouglas Gregor llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); 5908e984da8SDouglas Gregor const llvm::MemoryBuffer *Buffer 5918e984da8SDouglas Gregor = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); 5928e984da8SDouglas Gregor RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, 5938e984da8SDouglas Gregor Buffer)); 5948e984da8SDouglas Gregor } 5958e984da8SDouglas Gregor 5968e984da8SDouglas Gregor if (EnableLogging) { 5978e984da8SDouglas Gregor // FIXME: Add logging. 5988e984da8SDouglas Gregor } 5998e984da8SDouglas Gregor 6008e984da8SDouglas Gregor // Parse the resulting source file to find code-completion results. 6018e984da8SDouglas Gregor AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; 6028e984da8SDouglas Gregor Results->Results = 0; 6038e984da8SDouglas Gregor Results->NumResults = 0; 6048e984da8SDouglas Gregor 6058e984da8SDouglas Gregor // Create a code-completion consumer to capture the results. 6068e984da8SDouglas Gregor CaptureCompletionResults Capture(*Results); 6078e984da8SDouglas Gregor 608d8a5dba9SDouglas Gregor // Make sure that we free the temporary buffers when the 609d8a5dba9SDouglas Gregor // code-completion constructor is freed. 610d8a5dba9SDouglas Gregor for (unsigned I = 0, N = RemappedFiles.size(); I != N; ++I) 611d8a5dba9SDouglas Gregor Results->TemporaryBuffers.push_back(RemappedFiles[I].second); 612d8a5dba9SDouglas Gregor 6138e984da8SDouglas Gregor // Perform completion. 6148e984da8SDouglas Gregor AST->CodeComplete(complete_filename, complete_line, complete_column, 615*b68bc59aSDouglas Gregor RemappedFiles.data(), RemappedFiles.size(), 616*b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeMacros), 617*b68bc59aSDouglas Gregor (options & CXCodeComplete_IncludeCodePatterns), 618*b68bc59aSDouglas Gregor Capture, 6198e984da8SDouglas Gregor *Results->Diag, Results->LangOpts, Results->SourceMgr, 6208e984da8SDouglas Gregor Results->FileMgr, Results->Diagnostics); 6218e984da8SDouglas Gregor 6228e984da8SDouglas Gregor 6238e984da8SDouglas Gregor 6248e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER 6258e984da8SDouglas Gregor #ifdef UDP_CODE_COMPLETION_LOGGER_PORT 6268e984da8SDouglas Gregor const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); 6278e984da8SDouglas Gregor llvm::SmallString<256> LogResult; 6288e984da8SDouglas Gregor llvm::raw_svector_ostream os(LogResult); 6298e984da8SDouglas Gregor 6308e984da8SDouglas Gregor // Figure out the language and whether or not it uses PCH. 6318e984da8SDouglas Gregor const char *lang = 0; 6328e984da8SDouglas Gregor bool usesPCH = false; 6338e984da8SDouglas Gregor 6348e984da8SDouglas Gregor for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); 6358e984da8SDouglas Gregor I != E; ++I) { 6368e984da8SDouglas Gregor if (*I == 0) 6378e984da8SDouglas Gregor continue; 6388e984da8SDouglas Gregor if (strcmp(*I, "-x") == 0) { 6398e984da8SDouglas Gregor if (I + 1 != E) { 6408e984da8SDouglas Gregor lang = *(++I); 6418e984da8SDouglas Gregor continue; 6428e984da8SDouglas Gregor } 6438e984da8SDouglas Gregor } 6448e984da8SDouglas Gregor else if (strcmp(*I, "-include") == 0) { 6458e984da8SDouglas Gregor if (I+1 != E) { 6468e984da8SDouglas Gregor const char *arg = *(++I); 6478e984da8SDouglas Gregor llvm::SmallString<512> pchName; 6488e984da8SDouglas Gregor { 6498e984da8SDouglas Gregor llvm::raw_svector_ostream os(pchName); 6508e984da8SDouglas Gregor os << arg << ".pth"; 6518e984da8SDouglas Gregor } 6528e984da8SDouglas Gregor pchName.push_back('\0'); 6538e984da8SDouglas Gregor struct stat stat_results; 6548e984da8SDouglas Gregor if (stat(pchName.data(), &stat_results) == 0) 6558e984da8SDouglas Gregor usesPCH = true; 6568e984da8SDouglas Gregor continue; 6578e984da8SDouglas Gregor } 6588e984da8SDouglas Gregor } 6598e984da8SDouglas Gregor } 6608e984da8SDouglas Gregor 6618e984da8SDouglas Gregor os << "{ "; 6628e984da8SDouglas Gregor os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); 6638e984da8SDouglas Gregor os << ", \"numRes\": " << Results->NumResults; 6648e984da8SDouglas Gregor os << ", \"diags\": " << Results->Diagnostics.size(); 6658e984da8SDouglas Gregor os << ", \"pch\": " << (usesPCH ? "true" : "false"); 6668e984da8SDouglas Gregor os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; 6678e984da8SDouglas Gregor const char *name = getlogin(); 6688e984da8SDouglas Gregor os << ", \"user\": \"" << (name ? name : "unknown") << '"'; 6698e984da8SDouglas Gregor os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; 6708e984da8SDouglas Gregor os << " }"; 6718e984da8SDouglas Gregor 6728e984da8SDouglas Gregor llvm::StringRef res = os.str(); 6738e984da8SDouglas Gregor if (res.size() > 0) { 6748e984da8SDouglas Gregor do { 6758e984da8SDouglas Gregor // Setup the UDP socket. 6768e984da8SDouglas Gregor struct sockaddr_in servaddr; 6778e984da8SDouglas Gregor bzero(&servaddr, sizeof(servaddr)); 6788e984da8SDouglas Gregor servaddr.sin_family = AF_INET; 6798e984da8SDouglas Gregor servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); 6808e984da8SDouglas Gregor if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, 6818e984da8SDouglas Gregor &servaddr.sin_addr) <= 0) 6828e984da8SDouglas Gregor break; 6838e984da8SDouglas Gregor 6848e984da8SDouglas Gregor int sockfd = socket(AF_INET, SOCK_DGRAM, 0); 6858e984da8SDouglas Gregor if (sockfd < 0) 6868e984da8SDouglas Gregor break; 6878e984da8SDouglas Gregor 6888e984da8SDouglas Gregor sendto(sockfd, res.data(), res.size(), 0, 6898e984da8SDouglas Gregor (struct sockaddr *)&servaddr, sizeof(servaddr)); 6908e984da8SDouglas Gregor close(sockfd); 6918e984da8SDouglas Gregor } 6928e984da8SDouglas Gregor while (false); 6938e984da8SDouglas Gregor } 6948e984da8SDouglas Gregor #endif 6958e984da8SDouglas Gregor #endif 6968e984da8SDouglas Gregor return Results; 6978e984da8SDouglas Gregor } 6988e984da8SDouglas Gregor 699*b68bc59aSDouglas Gregor unsigned clang_defaultCodeCompleteOptions(void) { 700*b68bc59aSDouglas Gregor return CXCodeComplete_IncludeMacros; 701*b68bc59aSDouglas Gregor } 702*b68bc59aSDouglas Gregor 703d5a2a073SDaniel Dunbar void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { 704d5a2a073SDaniel Dunbar if (!ResultsIn) 705d5a2a073SDaniel Dunbar return; 706d5a2a073SDaniel Dunbar 707d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 708d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 709d5a2a073SDaniel Dunbar delete Results; 710d5a2a073SDaniel Dunbar } 711d5a2a073SDaniel Dunbar 712d5a2a073SDaniel Dunbar unsigned 713d5a2a073SDaniel Dunbar clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { 714d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 715d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 716d5a2a073SDaniel Dunbar if (!Results) 717d5a2a073SDaniel Dunbar return 0; 718d5a2a073SDaniel Dunbar 719d5a2a073SDaniel Dunbar return Results->Diagnostics.size(); 720d5a2a073SDaniel Dunbar } 721d5a2a073SDaniel Dunbar 722d5a2a073SDaniel Dunbar CXDiagnostic 723d5a2a073SDaniel Dunbar clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, 724d5a2a073SDaniel Dunbar unsigned Index) { 725d5a2a073SDaniel Dunbar AllocatedCXCodeCompleteResults *Results 726d5a2a073SDaniel Dunbar = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); 727d5a2a073SDaniel Dunbar if (!Results || Index >= Results->Diagnostics.size()) 728d5a2a073SDaniel Dunbar return 0; 729d5a2a073SDaniel Dunbar 730d5a2a073SDaniel Dunbar return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); 731d5a2a073SDaniel Dunbar } 732d5a2a073SDaniel Dunbar 733d5a2a073SDaniel Dunbar 734d5a2a073SDaniel Dunbar } // end extern "C" 735