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