Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 1 | //===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Clang-C Source Indexing library hooks for |
| 11 | // code completion. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CIndexer.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 16 | #include "CIndexDiagnostic.h" |
| 17 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 18 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/System/Program.h" |
| 22 | |
| 23 | using namespace clang; |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 24 | using namespace clang::cxstring; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 25 | |
| 26 | extern "C" { |
| 27 | |
| 28 | enum CXCompletionChunkKind |
| 29 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 30 | unsigned chunk_number) { |
| 31 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 32 | if (!CCStr || chunk_number >= CCStr->size()) |
| 33 | return CXCompletionChunk_Text; |
| 34 | |
| 35 | switch ((*CCStr)[chunk_number].Kind) { |
| 36 | case CodeCompletionString::CK_TypedText: |
| 37 | return CXCompletionChunk_TypedText; |
| 38 | case CodeCompletionString::CK_Text: |
| 39 | return CXCompletionChunk_Text; |
| 40 | case CodeCompletionString::CK_Optional: |
| 41 | return CXCompletionChunk_Optional; |
| 42 | case CodeCompletionString::CK_Placeholder: |
| 43 | return CXCompletionChunk_Placeholder; |
| 44 | case CodeCompletionString::CK_Informative: |
| 45 | return CXCompletionChunk_Informative; |
| 46 | case CodeCompletionString::CK_ResultType: |
| 47 | return CXCompletionChunk_ResultType; |
| 48 | case CodeCompletionString::CK_CurrentParameter: |
| 49 | return CXCompletionChunk_CurrentParameter; |
| 50 | case CodeCompletionString::CK_LeftParen: |
| 51 | return CXCompletionChunk_LeftParen; |
| 52 | case CodeCompletionString::CK_RightParen: |
| 53 | return CXCompletionChunk_RightParen; |
| 54 | case CodeCompletionString::CK_LeftBracket: |
| 55 | return CXCompletionChunk_LeftBracket; |
| 56 | case CodeCompletionString::CK_RightBracket: |
| 57 | return CXCompletionChunk_RightBracket; |
| 58 | case CodeCompletionString::CK_LeftBrace: |
| 59 | return CXCompletionChunk_LeftBrace; |
| 60 | case CodeCompletionString::CK_RightBrace: |
| 61 | return CXCompletionChunk_RightBrace; |
| 62 | case CodeCompletionString::CK_LeftAngle: |
| 63 | return CXCompletionChunk_LeftAngle; |
| 64 | case CodeCompletionString::CK_RightAngle: |
| 65 | return CXCompletionChunk_RightAngle; |
| 66 | case CodeCompletionString::CK_Comma: |
| 67 | return CXCompletionChunk_Comma; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 68 | case CodeCompletionString::CK_Colon: |
| 69 | return CXCompletionChunk_Colon; |
| 70 | case CodeCompletionString::CK_SemiColon: |
| 71 | return CXCompletionChunk_SemiColon; |
| 72 | case CodeCompletionString::CK_Equal: |
| 73 | return CXCompletionChunk_Equal; |
| 74 | case CodeCompletionString::CK_HorizontalSpace: |
| 75 | return CXCompletionChunk_HorizontalSpace; |
| 76 | case CodeCompletionString::CK_VerticalSpace: |
| 77 | return CXCompletionChunk_VerticalSpace; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Should be unreachable, but let's be careful. |
| 81 | return CXCompletionChunk_Text; |
| 82 | } |
| 83 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 84 | CXString clang_getCompletionChunkText(CXCompletionString completion_string, |
| 85 | unsigned chunk_number) { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 86 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 87 | if (!CCStr || chunk_number >= CCStr->size()) |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 88 | return createCXString(0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 89 | |
| 90 | switch ((*CCStr)[chunk_number].Kind) { |
| 91 | case CodeCompletionString::CK_TypedText: |
| 92 | case CodeCompletionString::CK_Text: |
| 93 | case CodeCompletionString::CK_Placeholder: |
| 94 | case CodeCompletionString::CK_CurrentParameter: |
| 95 | case CodeCompletionString::CK_Informative: |
| 96 | case CodeCompletionString::CK_LeftParen: |
| 97 | case CodeCompletionString::CK_RightParen: |
| 98 | case CodeCompletionString::CK_LeftBracket: |
| 99 | case CodeCompletionString::CK_RightBracket: |
| 100 | case CodeCompletionString::CK_LeftBrace: |
| 101 | case CodeCompletionString::CK_RightBrace: |
| 102 | case CodeCompletionString::CK_LeftAngle: |
| 103 | case CodeCompletionString::CK_RightAngle: |
| 104 | case CodeCompletionString::CK_Comma: |
| 105 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 106 | case CodeCompletionString::CK_Colon: |
| 107 | case CodeCompletionString::CK_SemiColon: |
| 108 | case CodeCompletionString::CK_Equal: |
| 109 | case CodeCompletionString::CK_HorizontalSpace: |
| 110 | case CodeCompletionString::CK_VerticalSpace: |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 111 | return createCXString((*CCStr)[chunk_number].Text, false); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 112 | |
| 113 | case CodeCompletionString::CK_Optional: |
| 114 | // Note: treated as an empty text block. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 115 | return createCXString(""); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Should be unreachable, but let's be careful. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 119 | return createCXString(0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 123 | CXCompletionString |
| 124 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 125 | unsigned chunk_number) { |
| 126 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 127 | if (!CCStr || chunk_number >= CCStr->size()) |
| 128 | return 0; |
| 129 | |
| 130 | switch ((*CCStr)[chunk_number].Kind) { |
| 131 | case CodeCompletionString::CK_TypedText: |
| 132 | case CodeCompletionString::CK_Text: |
| 133 | case CodeCompletionString::CK_Placeholder: |
| 134 | case CodeCompletionString::CK_CurrentParameter: |
| 135 | case CodeCompletionString::CK_Informative: |
| 136 | case CodeCompletionString::CK_LeftParen: |
| 137 | case CodeCompletionString::CK_RightParen: |
| 138 | case CodeCompletionString::CK_LeftBracket: |
| 139 | case CodeCompletionString::CK_RightBracket: |
| 140 | case CodeCompletionString::CK_LeftBrace: |
| 141 | case CodeCompletionString::CK_RightBrace: |
| 142 | case CodeCompletionString::CK_LeftAngle: |
| 143 | case CodeCompletionString::CK_RightAngle: |
| 144 | case CodeCompletionString::CK_Comma: |
| 145 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 146 | case CodeCompletionString::CK_Colon: |
| 147 | case CodeCompletionString::CK_SemiColon: |
| 148 | case CodeCompletionString::CK_Equal: |
| 149 | case CodeCompletionString::CK_HorizontalSpace: |
| 150 | case CodeCompletionString::CK_VerticalSpace: |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | |
| 153 | case CodeCompletionString::CK_Optional: |
| 154 | // Note: treated as an empty text block. |
| 155 | return (*CCStr)[chunk_number].Optional; |
| 156 | } |
| 157 | |
| 158 | // Should be unreachable, but let's be careful. |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
| 163 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 164 | return CCStr? CCStr->size() : 0; |
| 165 | } |
| 166 | |
| 167 | static bool ReadUnsigned(const char *&Memory, const char *MemoryEnd, |
| 168 | unsigned &Value) { |
| 169 | if (Memory + sizeof(unsigned) > MemoryEnd) |
| 170 | return true; |
| 171 | |
| 172 | memmove(&Value, Memory, sizeof(unsigned)); |
| 173 | Memory += sizeof(unsigned); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | /// \brief The CXCodeCompleteResults structure we allocate internally; |
| 178 | /// the client only sees the initial CXCodeCompleteResults structure. |
| 179 | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 180 | AllocatedCXCodeCompleteResults(); |
| 181 | ~AllocatedCXCodeCompleteResults(); |
| 182 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 183 | /// \brief The memory buffer from which we parsed the results. We |
| 184 | /// retain this buffer because the completion strings point into it. |
| 185 | llvm::MemoryBuffer *Buffer; |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 187 | /// \brief Diagnostics produced while performing code completion. |
| 188 | llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; |
| 189 | |
| 190 | /// \brief Language options used to adjust source locations. |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 191 | LangOptions LangOpts; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 192 | |
| 193 | /// \brief Source manager, used for diagnostics. |
| 194 | SourceManager SourceMgr; |
| 195 | |
| 196 | /// \brief File manager, used for diagnostics. |
| 197 | FileManager FileMgr; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 198 | |
| 199 | /// \brief Temporary files that should be removed once we have finished |
| 200 | /// with the code-completion results. |
| 201 | std::vector<llvm::sys::Path> TemporaryFiles; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 204 | AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() |
| 205 | : CXCodeCompleteResults(), Buffer(0) { } |
| 206 | |
| 207 | AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { |
| 208 | for (unsigned I = 0, N = NumResults; I != N; ++I) |
| 209 | delete (CodeCompletionString *)Results[I].CompletionString; |
| 210 | delete [] Results; |
| 211 | delete Buffer; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 212 | |
| 213 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 214 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 217 | CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, |
| 218 | const char *source_filename, |
| 219 | int num_command_line_args, |
| 220 | const char **command_line_args, |
| 221 | unsigned num_unsaved_files, |
| 222 | struct CXUnsavedFile *unsaved_files, |
| 223 | const char *complete_filename, |
| 224 | unsigned complete_line, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 225 | unsigned complete_column) { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 226 | // The indexer, which is mainly used to determine where diagnostics go. |
| 227 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 228 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 229 | // Configure the diagnostics. |
| 230 | DiagnosticOptions DiagOpts; |
| 231 | llvm::OwningPtr<Diagnostic> Diags; |
| 232 | Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0)); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 234 | // The set of temporary files that we've built. |
| 235 | std::vector<llvm::sys::Path> TemporaryFiles; |
| 236 | |
| 237 | // Build up the arguments for invoking 'clang'. |
| 238 | std::vector<const char *> argv; |
| 239 | |
| 240 | // First add the complete path to the 'clang' executable. |
| 241 | llvm::sys::Path ClangPath = CXXIdx->getClangPath(); |
| 242 | argv.push_back(ClangPath.c_str()); |
| 243 | |
| 244 | // Add the '-fsyntax-only' argument so that we only perform a basic |
| 245 | // syntax check of the code. |
| 246 | argv.push_back("-fsyntax-only"); |
| 247 | |
| 248 | // Add the appropriate '-code-completion-at=file:line:column' argument |
| 249 | // to perform code completion, with an "-Xclang" preceding it. |
| 250 | std::string code_complete_at; |
| 251 | code_complete_at += complete_filename; |
| 252 | code_complete_at += ":"; |
| 253 | code_complete_at += llvm::utostr(complete_line); |
| 254 | code_complete_at += ":"; |
| 255 | code_complete_at += llvm::utostr(complete_column); |
| 256 | argv.push_back("-Xclang"); |
| 257 | argv.push_back("-code-completion-at"); |
| 258 | argv.push_back("-Xclang"); |
| 259 | argv.push_back(code_complete_at.c_str()); |
| 260 | argv.push_back("-Xclang"); |
| 261 | argv.push_back("-no-code-completion-debug-printer"); |
| 262 | argv.push_back("-Xclang"); |
| 263 | argv.push_back("-code-completion-macros"); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 264 | argv.push_back("-fdiagnostics-binary"); |
| 265 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 266 | // Remap any unsaved files to temporary files. |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 267 | std::vector<std::string> RemapArgs; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 268 | if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) |
| 269 | return 0; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 270 | |
| 271 | // The pointers into the elements of RemapArgs are stable because we |
| 272 | // won't be adding anything to RemapArgs after this point. |
| 273 | for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) |
| 274 | argv.push_back(RemapArgs[i].c_str()); |
| 275 | |
| 276 | // Add the source file name (FIXME: later, we'll want to build temporary |
| 277 | // file from the buffer, or just feed the source text via standard input). |
| 278 | if (source_filename) |
| 279 | argv.push_back(source_filename); |
| 280 | |
| 281 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 282 | for (int i = 0; i < num_command_line_args; ++i) |
| 283 | if (const char *arg = command_line_args[i]) { |
| 284 | if (strcmp(arg, "-o") == 0) { |
| 285 | ++i; // Also skip the matching argument. |
| 286 | continue; |
| 287 | } |
| 288 | if (strcmp(arg, "-emit-ast") == 0 || |
| 289 | strcmp(arg, "-c") == 0 || |
| 290 | strcmp(arg, "-fsyntax-only") == 0) { |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | // Keep the argument. |
| 295 | argv.push_back(arg); |
| 296 | } |
| 297 | |
| 298 | // Add the null terminator. |
| 299 | argv.push_back(NULL); |
| 300 | |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 301 | // Generate a temporary name for the code-completion results file. |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame^] | 302 | char tmpFile[L_tmpnam]; |
| 303 | char *tmpFileName = tmpnam(tmpFile); |
| 304 | llvm::sys::Path ResultsFile(tmpFileName); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 305 | TemporaryFiles.push_back(ResultsFile); |
| 306 | |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 307 | // Generate a temporary name for the diagnostics file. |
Benjamin Kramer | c2a9816 | 2010-03-13 21:22:49 +0000 | [diff] [blame^] | 308 | char tmpFileResults[L_tmpnam]; |
| 309 | char *tmpResultsFileName = tmpnam(tmpFileResults); |
| 310 | llvm::sys::Path DiagnosticsFile(tmpResultsFileName); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 311 | TemporaryFiles.push_back(DiagnosticsFile); |
| 312 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 313 | // Invoke 'clang'. |
| 314 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 315 | // on Unix or NUL (Windows). |
| 316 | std::string ErrMsg; |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 317 | const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, |
| 318 | &DiagnosticsFile, 0 }; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 319 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 320 | /* redirects */ &Redirects[0], |
| 321 | /* secondsToWait */ 0, |
| 322 | /* memoryLimits */ 0, &ErrMsg); |
| 323 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 324 | if (!ErrMsg.empty()) { |
| 325 | std::string AllArgs; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 326 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 327 | I != E; ++I) { |
| 328 | AllArgs += ' '; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 329 | if (*I) |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 330 | AllArgs += *I; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 331 | } |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 332 | |
Daniel Dunbar | 32141c8 | 2010-02-23 20:23:45 +0000 | [diff] [blame] | 333 | Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // Parse the resulting source file to find code-completion results. |
| 337 | using llvm::MemoryBuffer; |
| 338 | using llvm::StringRef; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 339 | AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; |
| 340 | Results->Results = 0; |
| 341 | Results->NumResults = 0; |
| 342 | Results->Buffer = 0; |
| 343 | // FIXME: Set Results->LangOpts! |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 344 | if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { |
| 345 | llvm::SmallVector<CXCompletionResult, 4> CompletionResults; |
| 346 | StringRef Buffer = F->getBuffer(); |
| 347 | for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); |
| 348 | Str < StrEnd;) { |
| 349 | unsigned KindValue; |
| 350 | if (ReadUnsigned(Str, StrEnd, KindValue)) |
| 351 | break; |
| 352 | |
| 353 | CodeCompletionString *CCStr |
| 354 | = CodeCompletionString::Deserialize(Str, StrEnd); |
| 355 | if (!CCStr) |
| 356 | continue; |
| 357 | |
| 358 | if (!CCStr->empty()) { |
| 359 | // Vend the code-completion result to the caller. |
| 360 | CXCompletionResult Result; |
| 361 | Result.CursorKind = (CXCursorKind)KindValue; |
| 362 | Result.CompletionString = CCStr; |
| 363 | CompletionResults.push_back(Result); |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | // Allocate the results. |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 368 | Results->Results = new CXCompletionResult [CompletionResults.size()]; |
| 369 | Results->NumResults = CompletionResults.size(); |
| 370 | memcpy(Results->Results, CompletionResults.data(), |
| 371 | CompletionResults.size() * sizeof(CXCompletionResult)); |
| 372 | Results->Buffer = F; |
| 373 | } |
| 374 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 375 | LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, |
| 376 | Results->FileMgr, Results->SourceMgr, |
| 377 | Results->Diagnostics); |
| 378 | |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 379 | // Make sure we delete temporary files when the code-completion results are |
| 380 | // destroyed. |
| 381 | Results->TemporaryFiles.swap(TemporaryFiles); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 382 | |
| 383 | return Results; |
| 384 | } |
| 385 | |
| 386 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 387 | if (!ResultsIn) |
| 388 | return; |
| 389 | |
| 390 | AllocatedCXCodeCompleteResults *Results |
| 391 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 392 | delete Results; |
| 393 | } |
| 394 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 395 | unsigned |
| 396 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
| 397 | AllocatedCXCodeCompleteResults *Results |
| 398 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 399 | if (!Results) |
| 400 | return 0; |
| 401 | |
| 402 | return Results->Diagnostics.size(); |
| 403 | } |
| 404 | |
| 405 | CXDiagnostic |
| 406 | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
| 407 | unsigned Index) { |
| 408 | AllocatedCXCodeCompleteResults *Results |
| 409 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 410 | if (!Results || Index >= Results->Diagnostics.size()) |
| 411 | return 0; |
| 412 | |
| 413 | return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); |
| 414 | } |
| 415 | |
| 416 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 417 | } // end extern "C" |