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 { |
| 180 | /// \brief The memory buffer from which we parsed the results. We |
| 181 | /// retain this buffer because the completion strings point into it. |
| 182 | llvm::MemoryBuffer *Buffer; |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 184 | /// \brief Diagnostics produced while performing code completion. |
| 185 | llvm::SmallVector<StoredDiagnostic, 8> Diagnostics; |
| 186 | |
| 187 | /// \brief Language options used to adjust source locations. |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 188 | LangOptions LangOpts; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 189 | |
| 190 | /// \brief Source manager, used for diagnostics. |
| 191 | SourceManager SourceMgr; |
| 192 | |
| 193 | /// \brief File manager, used for diagnostics. |
| 194 | FileManager FileMgr; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx, |
| 198 | const char *source_filename, |
| 199 | int num_command_line_args, |
| 200 | const char **command_line_args, |
| 201 | unsigned num_unsaved_files, |
| 202 | struct CXUnsavedFile *unsaved_files, |
| 203 | const char *complete_filename, |
| 204 | unsigned complete_line, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 205 | unsigned complete_column) { |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 206 | // The indexer, which is mainly used to determine where diagnostics go. |
| 207 | CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx); |
| 208 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 209 | // Configure the diagnostics. |
| 210 | DiagnosticOptions DiagOpts; |
| 211 | llvm::OwningPtr<Diagnostic> Diags; |
| 212 | Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0)); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 214 | // The set of temporary files that we've built. |
| 215 | std::vector<llvm::sys::Path> TemporaryFiles; |
| 216 | |
| 217 | // Build up the arguments for invoking 'clang'. |
| 218 | std::vector<const char *> argv; |
| 219 | |
| 220 | // First add the complete path to the 'clang' executable. |
| 221 | llvm::sys::Path ClangPath = CXXIdx->getClangPath(); |
| 222 | argv.push_back(ClangPath.c_str()); |
| 223 | |
| 224 | // Add the '-fsyntax-only' argument so that we only perform a basic |
| 225 | // syntax check of the code. |
| 226 | argv.push_back("-fsyntax-only"); |
| 227 | |
| 228 | // Add the appropriate '-code-completion-at=file:line:column' argument |
| 229 | // to perform code completion, with an "-Xclang" preceding it. |
| 230 | std::string code_complete_at; |
| 231 | code_complete_at += complete_filename; |
| 232 | code_complete_at += ":"; |
| 233 | code_complete_at += llvm::utostr(complete_line); |
| 234 | code_complete_at += ":"; |
| 235 | code_complete_at += llvm::utostr(complete_column); |
| 236 | argv.push_back("-Xclang"); |
| 237 | argv.push_back("-code-completion-at"); |
| 238 | argv.push_back("-Xclang"); |
| 239 | argv.push_back(code_complete_at.c_str()); |
| 240 | argv.push_back("-Xclang"); |
| 241 | argv.push_back("-no-code-completion-debug-printer"); |
| 242 | argv.push_back("-Xclang"); |
| 243 | argv.push_back("-code-completion-macros"); |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 244 | argv.push_back("-fdiagnostics-binary"); |
| 245 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 246 | // Remap any unsaved files to temporary files. |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 247 | std::vector<std::string> RemapArgs; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 248 | if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles)) |
| 249 | return 0; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 250 | |
| 251 | // The pointers into the elements of RemapArgs are stable because we |
| 252 | // won't be adding anything to RemapArgs after this point. |
| 253 | for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i) |
| 254 | argv.push_back(RemapArgs[i].c_str()); |
| 255 | |
| 256 | // Add the source file name (FIXME: later, we'll want to build temporary |
| 257 | // file from the buffer, or just feed the source text via standard input). |
| 258 | if (source_filename) |
| 259 | argv.push_back(source_filename); |
| 260 | |
| 261 | // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'. |
| 262 | for (int i = 0; i < num_command_line_args; ++i) |
| 263 | if (const char *arg = command_line_args[i]) { |
| 264 | if (strcmp(arg, "-o") == 0) { |
| 265 | ++i; // Also skip the matching argument. |
| 266 | continue; |
| 267 | } |
| 268 | if (strcmp(arg, "-emit-ast") == 0 || |
| 269 | strcmp(arg, "-c") == 0 || |
| 270 | strcmp(arg, "-fsyntax-only") == 0) { |
| 271 | continue; |
| 272 | } |
| 273 | |
| 274 | // Keep the argument. |
| 275 | argv.push_back(arg); |
| 276 | } |
| 277 | |
| 278 | // Add the null terminator. |
| 279 | argv.push_back(NULL); |
| 280 | |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 281 | // Generate a temporary name for the code-completion results file. |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 282 | char tmpFile[L_tmpnam]; |
| 283 | char *tmpFileName = tmpnam(tmpFile); |
| 284 | llvm::sys::Path ResultsFile(tmpFileName); |
| 285 | TemporaryFiles.push_back(ResultsFile); |
| 286 | |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 287 | // Generate a temporary name for the diagnostics file. |
| 288 | char tmpFileResults[L_tmpnam]; |
| 289 | char *tmpResultsFileName = tmpnam(tmpFileResults); |
| 290 | llvm::sys::Path DiagnosticsFile(tmpResultsFileName); |
| 291 | TemporaryFiles.push_back(DiagnosticsFile); |
| 292 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 293 | // Invoke 'clang'. |
| 294 | llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null |
| 295 | // on Unix or NUL (Windows). |
| 296 | std::string ErrMsg; |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 297 | const llvm::sys::Path *Redirects[] = { &DevNull, &ResultsFile, |
| 298 | &DiagnosticsFile, 0 }; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 299 | llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL, |
| 300 | /* redirects */ &Redirects[0], |
| 301 | /* secondsToWait */ 0, |
| 302 | /* memoryLimits */ 0, &ErrMsg); |
| 303 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 304 | if (!ErrMsg.empty()) { |
| 305 | std::string AllArgs; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 306 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 307 | I != E; ++I) { |
| 308 | AllArgs += ' '; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 309 | if (*I) |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 310 | AllArgs += *I; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 311 | } |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 312 | |
| 313 | Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Parse the resulting source file to find code-completion results. |
| 317 | using llvm::MemoryBuffer; |
| 318 | using llvm::StringRef; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 319 | AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults; |
| 320 | Results->Results = 0; |
| 321 | Results->NumResults = 0; |
| 322 | Results->Buffer = 0; |
| 323 | // FIXME: Set Results->LangOpts! |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 324 | if (MemoryBuffer *F = MemoryBuffer::getFile(ResultsFile.c_str())) { |
| 325 | llvm::SmallVector<CXCompletionResult, 4> CompletionResults; |
| 326 | StringRef Buffer = F->getBuffer(); |
| 327 | for (const char *Str = Buffer.data(), *StrEnd = Str + Buffer.size(); |
| 328 | Str < StrEnd;) { |
| 329 | unsigned KindValue; |
| 330 | if (ReadUnsigned(Str, StrEnd, KindValue)) |
| 331 | break; |
| 332 | |
| 333 | CodeCompletionString *CCStr |
| 334 | = CodeCompletionString::Deserialize(Str, StrEnd); |
| 335 | if (!CCStr) |
| 336 | continue; |
| 337 | |
| 338 | if (!CCStr->empty()) { |
| 339 | // Vend the code-completion result to the caller. |
| 340 | CXCompletionResult Result; |
| 341 | Result.CursorKind = (CXCursorKind)KindValue; |
| 342 | Result.CompletionString = CCStr; |
| 343 | CompletionResults.push_back(Result); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | // Allocate the results. |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 348 | Results->Results = new CXCompletionResult [CompletionResults.size()]; |
| 349 | Results->NumResults = CompletionResults.size(); |
| 350 | memcpy(Results->Results, CompletionResults.data(), |
| 351 | CompletionResults.size() * sizeof(CXCompletionResult)); |
| 352 | Results->Buffer = F; |
| 353 | } |
| 354 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 355 | LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, |
| 356 | Results->FileMgr, Results->SourceMgr, |
| 357 | Results->Diagnostics); |
| 358 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 359 | for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) |
| 360 | TemporaryFiles[i].eraseFromDisk(); |
| 361 | |
| 362 | return Results; |
| 363 | } |
| 364 | |
| 365 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 366 | if (!ResultsIn) |
| 367 | return; |
| 368 | |
| 369 | AllocatedCXCodeCompleteResults *Results |
| 370 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 371 | |
| 372 | for (unsigned I = 0, N = Results->NumResults; I != N; ++I) |
| 373 | delete (CXCompletionString *)Results->Results[I].CompletionString; |
| 374 | delete [] Results->Results; |
| 375 | |
| 376 | Results->Results = 0; |
| 377 | Results->NumResults = 0; |
| 378 | delete Results->Buffer; |
| 379 | Results->Buffer = 0; |
| 380 | delete Results; |
| 381 | } |
| 382 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 383 | unsigned |
| 384 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
| 385 | AllocatedCXCodeCompleteResults *Results |
| 386 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 387 | if (!Results) |
| 388 | return 0; |
| 389 | |
| 390 | return Results->Diagnostics.size(); |
| 391 | } |
| 392 | |
| 393 | CXDiagnostic |
| 394 | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
| 395 | unsigned Index) { |
| 396 | AllocatedCXCodeCompleteResults *Results |
| 397 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 398 | if (!Results || Index >= Results->Diagnostics.size()) |
| 399 | return 0; |
| 400 | |
| 401 | return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); |
| 402 | } |
| 403 | |
| 404 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 405 | } // end extern "C" |