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" |
Ted Kremenek | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 16 | #include "CXTranslationUnit.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 17 | #include "CXString.h" |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 18 | #include "CXCursor.h" |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame] | 19 | #include "CXString.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 20 | #include "CIndexDiagnostic.h" |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 21 | #include "clang/AST/Type.h" |
| 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
| 25 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/ASTUnit.h" |
Benjamin Kramer | b846deb | 2010-04-12 19:45:50 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendDiagnostic.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 29 | #include "clang/Sema/CodeCompleteConsumer.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Atomic.h" |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CrashRecoveryContext.h" |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Timer.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Program.h" |
Douglas Gregor | 3d398aa | 2010-07-26 16:29:14 +0000 | [diff] [blame] | 38 | #include <cstdlib> |
| 39 | #include <cstdio> |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | da7af32 | 2010-04-15 01:02:28 +0000 | [diff] [blame] | 42 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 43 | #include "clang/Basic/Version.h" |
Ted Kremenek | da7af32 | 2010-04-15 01:02:28 +0000 | [diff] [blame] | 44 | #include <arpa/inet.h> |
| 45 | #include <sys/socket.h> |
| 46 | #include <sys/types.h> |
| 47 | #include <unistd.h> |
| 48 | #endif |
| 49 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 50 | using namespace clang; |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 51 | using namespace clang::cxstring; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 52 | |
| 53 | extern "C" { |
| 54 | |
| 55 | enum CXCompletionChunkKind |
| 56 | clang_getCompletionChunkKind(CXCompletionString completion_string, |
| 57 | unsigned chunk_number) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 58 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 59 | if (!CCStr || chunk_number >= CCStr->size()) |
| 60 | return CXCompletionChunk_Text; |
| 61 | |
| 62 | switch ((*CCStr)[chunk_number].Kind) { |
| 63 | case CodeCompletionString::CK_TypedText: |
| 64 | return CXCompletionChunk_TypedText; |
| 65 | case CodeCompletionString::CK_Text: |
| 66 | return CXCompletionChunk_Text; |
| 67 | case CodeCompletionString::CK_Optional: |
| 68 | return CXCompletionChunk_Optional; |
| 69 | case CodeCompletionString::CK_Placeholder: |
| 70 | return CXCompletionChunk_Placeholder; |
| 71 | case CodeCompletionString::CK_Informative: |
| 72 | return CXCompletionChunk_Informative; |
| 73 | case CodeCompletionString::CK_ResultType: |
| 74 | return CXCompletionChunk_ResultType; |
| 75 | case CodeCompletionString::CK_CurrentParameter: |
| 76 | return CXCompletionChunk_CurrentParameter; |
| 77 | case CodeCompletionString::CK_LeftParen: |
| 78 | return CXCompletionChunk_LeftParen; |
| 79 | case CodeCompletionString::CK_RightParen: |
| 80 | return CXCompletionChunk_RightParen; |
| 81 | case CodeCompletionString::CK_LeftBracket: |
| 82 | return CXCompletionChunk_LeftBracket; |
| 83 | case CodeCompletionString::CK_RightBracket: |
| 84 | return CXCompletionChunk_RightBracket; |
| 85 | case CodeCompletionString::CK_LeftBrace: |
| 86 | return CXCompletionChunk_LeftBrace; |
| 87 | case CodeCompletionString::CK_RightBrace: |
| 88 | return CXCompletionChunk_RightBrace; |
| 89 | case CodeCompletionString::CK_LeftAngle: |
| 90 | return CXCompletionChunk_LeftAngle; |
| 91 | case CodeCompletionString::CK_RightAngle: |
| 92 | return CXCompletionChunk_RightAngle; |
| 93 | case CodeCompletionString::CK_Comma: |
| 94 | return CXCompletionChunk_Comma; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 95 | case CodeCompletionString::CK_Colon: |
| 96 | return CXCompletionChunk_Colon; |
| 97 | case CodeCompletionString::CK_SemiColon: |
| 98 | return CXCompletionChunk_SemiColon; |
| 99 | case CodeCompletionString::CK_Equal: |
| 100 | return CXCompletionChunk_Equal; |
| 101 | case CodeCompletionString::CK_HorizontalSpace: |
| 102 | return CXCompletionChunk_HorizontalSpace; |
| 103 | case CodeCompletionString::CK_VerticalSpace: |
| 104 | return CXCompletionChunk_VerticalSpace; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 105 | } |
| 106 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 107 | llvm_unreachable("Invalid CompletionKind!"); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 110 | CXString clang_getCompletionChunkText(CXCompletionString completion_string, |
| 111 | unsigned chunk_number) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 112 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 113 | if (!CCStr || chunk_number >= CCStr->size()) |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 114 | return createCXString((const char*)0); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 115 | |
| 116 | switch ((*CCStr)[chunk_number].Kind) { |
| 117 | case CodeCompletionString::CK_TypedText: |
| 118 | case CodeCompletionString::CK_Text: |
| 119 | case CodeCompletionString::CK_Placeholder: |
| 120 | case CodeCompletionString::CK_CurrentParameter: |
| 121 | case CodeCompletionString::CK_Informative: |
| 122 | case CodeCompletionString::CK_LeftParen: |
| 123 | case CodeCompletionString::CK_RightParen: |
| 124 | case CodeCompletionString::CK_LeftBracket: |
| 125 | case CodeCompletionString::CK_RightBracket: |
| 126 | case CodeCompletionString::CK_LeftBrace: |
| 127 | case CodeCompletionString::CK_RightBrace: |
| 128 | case CodeCompletionString::CK_LeftAngle: |
| 129 | case CodeCompletionString::CK_RightAngle: |
| 130 | case CodeCompletionString::CK_Comma: |
| 131 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 132 | case CodeCompletionString::CK_Colon: |
| 133 | case CodeCompletionString::CK_SemiColon: |
| 134 | case CodeCompletionString::CK_Equal: |
| 135 | case CodeCompletionString::CK_HorizontalSpace: |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 136 | case CodeCompletionString::CK_VerticalSpace: |
Douglas Gregor | 5a9c0bc | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 137 | return createCXString((*CCStr)[chunk_number].Text, false); |
Douglas Gregor | 21c241f | 2010-05-25 06:14:46 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 139 | case CodeCompletionString::CK_Optional: |
| 140 | // Note: treated as an empty text block. |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 141 | return createCXString(""); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 142 | } |
| 143 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 144 | llvm_unreachable("Invalid CodeCompletionString Kind!"); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 148 | CXCompletionString |
| 149 | clang_getCompletionChunkCompletionString(CXCompletionString completion_string, |
| 150 | unsigned chunk_number) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 151 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 152 | if (!CCStr || chunk_number >= CCStr->size()) |
| 153 | return 0; |
| 154 | |
| 155 | switch ((*CCStr)[chunk_number].Kind) { |
| 156 | case CodeCompletionString::CK_TypedText: |
| 157 | case CodeCompletionString::CK_Text: |
| 158 | case CodeCompletionString::CK_Placeholder: |
| 159 | case CodeCompletionString::CK_CurrentParameter: |
| 160 | case CodeCompletionString::CK_Informative: |
| 161 | case CodeCompletionString::CK_LeftParen: |
| 162 | case CodeCompletionString::CK_RightParen: |
| 163 | case CodeCompletionString::CK_LeftBracket: |
| 164 | case CodeCompletionString::CK_RightBracket: |
| 165 | case CodeCompletionString::CK_LeftBrace: |
| 166 | case CodeCompletionString::CK_RightBrace: |
| 167 | case CodeCompletionString::CK_LeftAngle: |
| 168 | case CodeCompletionString::CK_RightAngle: |
| 169 | case CodeCompletionString::CK_Comma: |
| 170 | case CodeCompletionString::CK_ResultType: |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 171 | case CodeCompletionString::CK_Colon: |
| 172 | case CodeCompletionString::CK_SemiColon: |
| 173 | case CodeCompletionString::CK_Equal: |
| 174 | case CodeCompletionString::CK_HorizontalSpace: |
| 175 | case CodeCompletionString::CK_VerticalSpace: |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | |
| 178 | case CodeCompletionString::CK_Optional: |
| 179 | // Note: treated as an empty text block. |
| 180 | return (*CCStr)[chunk_number].Optional; |
| 181 | } |
| 182 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 183 | llvm_unreachable("Invalid CompletionKind!"); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 187 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 188 | return CCStr? CCStr->size() : 0; |
| 189 | } |
| 190 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 191 | unsigned clang_getCompletionPriority(CXCompletionString completion_string) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 192 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
Bill Wendling | a2ace58 | 2010-05-27 18:35:05 +0000 | [diff] [blame] | 193 | return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 196 | enum CXAvailabilityKind |
| 197 | clang_getCompletionAvailability(CXCompletionString completion_string) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 198 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 199 | return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability()) |
| 200 | : CXAvailability_Available; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 203 | unsigned clang_getCompletionNumAnnotations(CXCompletionString completion_string) |
| 204 | { |
| 205 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 206 | return CCStr ? CCStr->getAnnotationCount() : 0; |
| 207 | } |
| 208 | |
| 209 | CXString clang_getCompletionAnnotation(CXCompletionString completion_string, |
| 210 | unsigned annotation_number) { |
| 211 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 212 | return CCStr ? createCXString(CCStr->getAnnotation(annotation_number)) |
| 213 | : createCXString((const char *) 0); |
| 214 | } |
| 215 | |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 216 | CXString |
| 217 | clang_getCompletionParent(CXCompletionString completion_string, |
| 218 | CXCursorKind *kind) { |
| 219 | if (kind) |
| 220 | *kind = CXCursor_NotImplemented; |
| 221 | |
| 222 | CodeCompletionString *CCStr = (CodeCompletionString *)completion_string; |
| 223 | if (!CCStr) |
| 224 | return createCXString((const char *)0); |
| 225 | |
| 226 | if (kind) |
| 227 | *kind = CCStr->getParentContextKind(); |
| 228 | return createCXString(CCStr->getParentContextName(), /*DupString=*/false); |
| 229 | } |
| 230 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 231 | /// \brief The CXCodeCompleteResults structure we allocate internally; |
| 232 | /// the client only sees the initial CXCodeCompleteResults structure. |
| 233 | struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 234 | AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 235 | ~AllocatedCXCodeCompleteResults(); |
| 236 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 237 | /// \brief Diagnostics produced while performing code completion. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 238 | SmallVector<StoredDiagnostic, 8> Diagnostics; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 239 | |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 240 | /// \brief Diag object |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 241 | IntrusiveRefCntPtr<DiagnosticsEngine> Diag; |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 242 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 243 | /// \brief Language options used to adjust source locations. |
Daniel Dunbar | 35b8440 | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 244 | LangOptions LangOpts; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 245 | |
| 246 | FileSystemOptions FileSystemOpts; |
| 247 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 248 | /// \brief File manager, used for diagnostics. |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 249 | IntrusiveRefCntPtr<FileManager> FileMgr; |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 250 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 251 | /// \brief Source manager, used for diagnostics. |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 252 | IntrusiveRefCntPtr<SourceManager> SourceMgr; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 253 | |
| 254 | /// \brief Temporary files that should be removed once we have finished |
| 255 | /// with the code-completion results. |
| 256 | std::vector<llvm::sys::Path> TemporaryFiles; |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 258 | /// \brief Temporary buffers that will be deleted once we have finished with |
| 259 | /// the code-completion results. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 260 | SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 262 | /// \brief Allocator used to store globally cached code-completion results. |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 263 | IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 264 | CachedCompletionAllocator; |
| 265 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 266 | /// \brief Allocator used to store code completion results. |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 267 | IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator> |
| 268 | CodeCompletionAllocator; |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 269 | |
| 270 | /// \brief Context under which completion occurred. |
| 271 | enum clang::CodeCompletionContext::Kind ContextKind; |
| 272 | |
| 273 | /// \brief A bitfield representing the acceptable completions for the |
| 274 | /// current context. |
| 275 | unsigned long long Contexts; |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 276 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 277 | /// \brief The kind of the container for the current context for completions. |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 278 | enum CXCursorKind ContainerKind; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 279 | /// \brief The USR of the container for the current context for completions. |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 280 | CXString ContainerUSR; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 281 | /// \brief a boolean value indicating whether there is complete information |
| 282 | /// about the container |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 283 | unsigned ContainerIsIncomplete; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 284 | |
| 285 | /// \brief A string containing the Objective-C selector entered thus far for a |
| 286 | /// message send. |
| 287 | std::string Selector; |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 290 | /// \brief Tracks the number of code-completion result objects that are |
| 291 | /// currently active. |
| 292 | /// |
| 293 | /// Used for debugging purposes only. |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 294 | static llvm::sys::cas_flag CodeCompletionResultObjects; |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 295 | |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 296 | AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( |
| 297 | const FileSystemOptions& FileSystemOpts) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 298 | : CXCodeCompleteResults(), |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 299 | Diag(new DiagnosticsEngine( |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 300 | IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))), |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 301 | FileSystemOpts(FileSystemOpts), |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 302 | FileMgr(new FileManager(FileSystemOpts)), |
Douglas Gregor | b77d197 | 2011-10-10 23:37:54 +0000 | [diff] [blame] | 303 | SourceMgr(new SourceManager(*Diag, *FileMgr)), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 304 | CodeCompletionAllocator(new clang::GlobalCodeCompletionAllocator), |
Douglas Gregor | b77d197 | 2011-10-10 23:37:54 +0000 | [diff] [blame] | 305 | Contexts(CXCompletionContext_Unknown), |
| 306 | ContainerKind(CXCursor_InvalidCode), |
| 307 | ContainerUSR(createCXString("")), |
| 308 | ContainerIsIncomplete(1) |
| 309 | { |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 310 | if (getenv("LIBCLANG_OBJTRACKING")) { |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 311 | llvm::sys::AtomicIncrement(&CodeCompletionResultObjects); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 312 | fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects); |
| 313 | } |
| 314 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 315 | |
| 316 | AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 317 | delete [] Results; |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 319 | clang_disposeString(ContainerUSR); |
| 320 | |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 321 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 322 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 323 | for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) |
| 324 | delete TemporaryBuffers[I]; |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 325 | |
| 326 | if (getenv("LIBCLANG_OBJTRACKING")) { |
Douglas Gregor | 1fd9e0d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 327 | llvm::sys::AtomicDecrement(&CodeCompletionResultObjects); |
Douglas Gregor | e3c60a7 | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 328 | fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects); |
| 329 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 332 | } // end extern "C" |
| 333 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 334 | static unsigned long long getContextsForContextKind( |
| 335 | enum CodeCompletionContext::Kind kind, |
| 336 | Sema &S) { |
| 337 | unsigned long long contexts = 0; |
| 338 | switch (kind) { |
| 339 | case CodeCompletionContext::CCC_OtherWithMacros: { |
| 340 | //We can allow macros here, but we don't know what else is permissible |
| 341 | //So we'll say the only thing permissible are macros |
| 342 | contexts = CXCompletionContext_MacroName; |
| 343 | break; |
| 344 | } |
| 345 | case CodeCompletionContext::CCC_TopLevel: |
| 346 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 347 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 348 | case CodeCompletionContext::CCC_Type: { |
| 349 | contexts = CXCompletionContext_AnyType | |
| 350 | CXCompletionContext_ObjCInterface; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 351 | if (S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 352 | contexts |= CXCompletionContext_EnumTag | |
| 353 | CXCompletionContext_UnionTag | |
| 354 | CXCompletionContext_StructTag | |
| 355 | CXCompletionContext_ClassTag | |
| 356 | CXCompletionContext_NestedNameSpecifier; |
| 357 | } |
| 358 | break; |
| 359 | } |
| 360 | case CodeCompletionContext::CCC_Statement: { |
| 361 | contexts = CXCompletionContext_AnyType | |
| 362 | CXCompletionContext_ObjCInterface | |
| 363 | CXCompletionContext_AnyValue; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 364 | if (S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 365 | contexts |= CXCompletionContext_EnumTag | |
| 366 | CXCompletionContext_UnionTag | |
| 367 | CXCompletionContext_StructTag | |
| 368 | CXCompletionContext_ClassTag | |
| 369 | CXCompletionContext_NestedNameSpecifier; |
| 370 | } |
| 371 | break; |
| 372 | } |
| 373 | case CodeCompletionContext::CCC_Expression: { |
| 374 | contexts = CXCompletionContext_AnyValue; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 375 | if (S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 376 | contexts |= CXCompletionContext_AnyType | |
| 377 | CXCompletionContext_ObjCInterface | |
| 378 | CXCompletionContext_EnumTag | |
| 379 | CXCompletionContext_UnionTag | |
| 380 | CXCompletionContext_StructTag | |
| 381 | CXCompletionContext_ClassTag | |
| 382 | CXCompletionContext_NestedNameSpecifier; |
| 383 | } |
| 384 | break; |
| 385 | } |
| 386 | case CodeCompletionContext::CCC_ObjCMessageReceiver: { |
| 387 | contexts = CXCompletionContext_ObjCObjectValue | |
| 388 | CXCompletionContext_ObjCSelectorValue | |
| 389 | CXCompletionContext_ObjCInterface; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 390 | if (S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 391 | contexts |= CXCompletionContext_CXXClassTypeValue | |
| 392 | CXCompletionContext_AnyType | |
| 393 | CXCompletionContext_EnumTag | |
| 394 | CXCompletionContext_UnionTag | |
| 395 | CXCompletionContext_StructTag | |
| 396 | CXCompletionContext_ClassTag | |
| 397 | CXCompletionContext_NestedNameSpecifier; |
| 398 | } |
| 399 | break; |
| 400 | } |
| 401 | case CodeCompletionContext::CCC_DotMemberAccess: { |
| 402 | contexts = CXCompletionContext_DotMemberAccess; |
| 403 | break; |
| 404 | } |
| 405 | case CodeCompletionContext::CCC_ArrowMemberAccess: { |
| 406 | contexts = CXCompletionContext_ArrowMemberAccess; |
| 407 | break; |
| 408 | } |
| 409 | case CodeCompletionContext::CCC_ObjCPropertyAccess: { |
| 410 | contexts = CXCompletionContext_ObjCPropertyAccess; |
| 411 | break; |
| 412 | } |
| 413 | case CodeCompletionContext::CCC_EnumTag: { |
| 414 | contexts = CXCompletionContext_EnumTag | |
| 415 | CXCompletionContext_NestedNameSpecifier; |
| 416 | break; |
| 417 | } |
| 418 | case CodeCompletionContext::CCC_UnionTag: { |
| 419 | contexts = CXCompletionContext_UnionTag | |
| 420 | CXCompletionContext_NestedNameSpecifier; |
| 421 | break; |
| 422 | } |
| 423 | case CodeCompletionContext::CCC_ClassOrStructTag: { |
| 424 | contexts = CXCompletionContext_StructTag | |
| 425 | CXCompletionContext_ClassTag | |
| 426 | CXCompletionContext_NestedNameSpecifier; |
| 427 | break; |
| 428 | } |
| 429 | case CodeCompletionContext::CCC_ObjCProtocolName: { |
| 430 | contexts = CXCompletionContext_ObjCProtocol; |
| 431 | break; |
| 432 | } |
| 433 | case CodeCompletionContext::CCC_Namespace: { |
| 434 | contexts = CXCompletionContext_Namespace; |
| 435 | break; |
| 436 | } |
| 437 | case CodeCompletionContext::CCC_PotentiallyQualifiedName: { |
| 438 | contexts = CXCompletionContext_NestedNameSpecifier; |
| 439 | break; |
| 440 | } |
| 441 | case CodeCompletionContext::CCC_MacroNameUse: { |
| 442 | contexts = CXCompletionContext_MacroName; |
| 443 | break; |
| 444 | } |
| 445 | case CodeCompletionContext::CCC_NaturalLanguage: { |
| 446 | contexts = CXCompletionContext_NaturalLanguage; |
| 447 | break; |
| 448 | } |
| 449 | case CodeCompletionContext::CCC_SelectorName: { |
| 450 | contexts = CXCompletionContext_ObjCSelectorName; |
| 451 | break; |
| 452 | } |
| 453 | case CodeCompletionContext::CCC_ParenthesizedExpression: { |
| 454 | contexts = CXCompletionContext_AnyType | |
| 455 | CXCompletionContext_ObjCInterface | |
| 456 | CXCompletionContext_AnyValue; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 457 | if (S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 458 | contexts |= CXCompletionContext_EnumTag | |
| 459 | CXCompletionContext_UnionTag | |
| 460 | CXCompletionContext_StructTag | |
| 461 | CXCompletionContext_ClassTag | |
| 462 | CXCompletionContext_NestedNameSpecifier; |
| 463 | } |
| 464 | break; |
| 465 | } |
| 466 | case CodeCompletionContext::CCC_ObjCInstanceMessage: { |
| 467 | contexts = CXCompletionContext_ObjCInstanceMessage; |
| 468 | break; |
| 469 | } |
| 470 | case CodeCompletionContext::CCC_ObjCClassMessage: { |
| 471 | contexts = CXCompletionContext_ObjCClassMessage; |
| 472 | break; |
| 473 | } |
Douglas Gregor | 0f91c8c | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 474 | case CodeCompletionContext::CCC_ObjCInterfaceName: { |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 475 | contexts = CXCompletionContext_ObjCInterface; |
| 476 | break; |
| 477 | } |
| 478 | case CodeCompletionContext::CCC_ObjCCategoryName: { |
| 479 | contexts = CXCompletionContext_ObjCCategory; |
| 480 | break; |
| 481 | } |
| 482 | case CodeCompletionContext::CCC_Other: |
| 483 | case CodeCompletionContext::CCC_ObjCInterface: |
| 484 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 485 | case CodeCompletionContext::CCC_Name: |
| 486 | case CodeCompletionContext::CCC_MacroName: |
| 487 | case CodeCompletionContext::CCC_PreprocessorExpression: |
| 488 | case CodeCompletionContext::CCC_PreprocessorDirective: |
| 489 | case CodeCompletionContext::CCC_TypeQualifiers: { |
| 490 | //Only Clang results should be accepted, so we'll set all of the other |
| 491 | //context bits to 0 (i.e. the empty set) |
| 492 | contexts = CXCompletionContext_Unexposed; |
| 493 | break; |
| 494 | } |
| 495 | case CodeCompletionContext::CCC_Recovery: { |
| 496 | //We don't know what the current context is, so we'll return unknown |
| 497 | //This is the equivalent of setting all of the other context bits |
| 498 | contexts = CXCompletionContext_Unknown; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | return contexts; |
| 503 | } |
| 504 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 505 | namespace { |
| 506 | class CaptureCompletionResults : public CodeCompleteConsumer { |
| 507 | AllocatedCXCodeCompleteResults &AllocatedResults; |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 508 | CodeCompletionTUInfo CCTUInfo; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 509 | SmallVector<CXCompletionResult, 16> StoredResults; |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 510 | CXTranslationUnit *TU; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 511 | public: |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 512 | CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results, |
| 513 | CXTranslationUnit *TranslationUnit) |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 514 | : CodeCompleteConsumer(true, false, true, false), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 515 | AllocatedResults(Results), CCTUInfo(Results.CodeCompletionAllocator), |
| 516 | TU(TranslationUnit) { } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 517 | ~CaptureCompletionResults() { Finish(); } |
| 518 | |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 519 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 520 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 521 | CodeCompletionResult *Results, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 522 | unsigned NumResults) { |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 523 | StoredResults.reserve(StoredResults.size() + NumResults); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 524 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 525 | CodeCompletionString *StoredCompletion |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 526 | = Results[I].CreateCodeCompletionString(S, getAllocator(), |
| 527 | getCodeCompletionTUInfo()); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 528 | |
| 529 | CXCompletionResult R; |
| 530 | R.CursorKind = Results[I].CursorKind; |
| 531 | R.CompletionString = StoredCompletion; |
| 532 | StoredResults.push_back(R); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 533 | } |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 535 | enum CodeCompletionContext::Kind contextKind = Context.getKind(); |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 536 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 537 | AllocatedResults.ContextKind = contextKind; |
| 538 | AllocatedResults.Contexts = getContextsForContextKind(contextKind, S); |
| 539 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 540 | AllocatedResults.Selector = ""; |
| 541 | if (Context.getNumSelIdents() > 0) { |
| 542 | for (unsigned i = 0; i < Context.getNumSelIdents(); i++) { |
| 543 | IdentifierInfo *selIdent = Context.getSelIdents()[i]; |
| 544 | if (selIdent != NULL) { |
| 545 | StringRef selectorString = Context.getSelIdents()[i]->getName(); |
Benjamin Kramer | a0651c5 | 2011-07-26 16:59:25 +0000 | [diff] [blame] | 546 | AllocatedResults.Selector += selectorString; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 547 | } |
| 548 | AllocatedResults.Selector += ":"; |
| 549 | } |
| 550 | } |
| 551 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 552 | QualType baseType = Context.getBaseType(); |
| 553 | NamedDecl *D = NULL; |
| 554 | |
| 555 | if (!baseType.isNull()) { |
| 556 | // Get the declaration for a class/struct/union/enum type |
| 557 | if (const TagType *Tag = baseType->getAs<TagType>()) |
| 558 | D = Tag->getDecl(); |
| 559 | // Get the @interface declaration for a (possibly-qualified) Objective-C |
| 560 | // object pointer type, e.g., NSString* |
| 561 | else if (const ObjCObjectPointerType *ObjPtr = |
| 562 | baseType->getAs<ObjCObjectPointerType>()) |
| 563 | D = ObjPtr->getInterfaceDecl(); |
| 564 | // Get the @interface declaration for an Objective-C object type |
| 565 | else if (const ObjCObjectType *Obj = baseType->getAs<ObjCObjectType>()) |
| 566 | D = Obj->getInterface(); |
| 567 | // Get the class for a C++ injected-class-name |
| 568 | else if (const InjectedClassNameType *Injected = |
| 569 | baseType->getAs<InjectedClassNameType>()) |
| 570 | D = Injected->getDecl(); |
| 571 | } |
| 572 | |
| 573 | if (D != NULL) { |
| 574 | CXCursor cursor = cxcursor::MakeCXCursor(D, *TU); |
| 575 | |
| 576 | CXCursorKind cursorKind = clang_getCursorKind(cursor); |
| 577 | CXString cursorUSR = clang_getCursorUSR(cursor); |
| 578 | |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame] | 579 | // Normally, clients of CXString shouldn't care whether or not |
| 580 | // a CXString is managed by a pool or by explicitly malloc'ed memory. |
| 581 | // However, there are cases when AllocatedResults outlives the |
| 582 | // CXTranslationUnit. This is a workaround that failure mode. |
| 583 | if (cxstring::isManagedByPool(cursorUSR)) { |
| 584 | CXString heapStr = |
| 585 | cxstring::createCXString(clang_getCString(cursorUSR), true); |
| 586 | clang_disposeString(cursorUSR); |
| 587 | cursorUSR = heapStr; |
| 588 | } |
| 589 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 590 | AllocatedResults.ContainerKind = cursorKind; |
| 591 | AllocatedResults.ContainerUSR = cursorUSR; |
Ted Kremenek | baf82b0 | 2011-08-17 22:19:53 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 593 | const Type *type = baseType.getTypePtrOrNull(); |
| 594 | if (type != NULL) { |
| 595 | AllocatedResults.ContainerIsIncomplete = type->isIncompleteType(); |
| 596 | } |
| 597 | else { |
| 598 | AllocatedResults.ContainerIsIncomplete = 1; |
| 599 | } |
| 600 | } |
| 601 | else { |
| 602 | AllocatedResults.ContainerKind = CXCursor_InvalidCode; |
| 603 | AllocatedResults.ContainerUSR = createCXString(""); |
| 604 | AllocatedResults.ContainerIsIncomplete = 1; |
| 605 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 606 | } |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 608 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 609 | OverloadCandidate *Candidates, |
| 610 | unsigned NumCandidates) { |
| 611 | StoredResults.reserve(StoredResults.size() + NumCandidates); |
| 612 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 613 | CodeCompletionString *StoredCompletion |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 614 | = Candidates[I].CreateSignatureString(CurrentArg, S, getAllocator(), |
| 615 | getCodeCompletionTUInfo()); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 616 | |
| 617 | CXCompletionResult R; |
| 618 | R.CursorKind = CXCursor_NotImplemented; |
| 619 | R.CompletionString = StoredCompletion; |
| 620 | StoredResults.push_back(R); |
| 621 | } |
| 622 | } |
| 623 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 624 | virtual CodeCompletionAllocator &getAllocator() { |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 625 | return *AllocatedResults.CodeCompletionAllocator; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 626 | } |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame^] | 627 | |
| 628 | virtual CodeCompletionTUInfo &getCodeCompletionTUInfo() { return CCTUInfo; } |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 630 | private: |
| 631 | void Finish() { |
| 632 | AllocatedResults.Results = new CXCompletionResult [StoredResults.size()]; |
| 633 | AllocatedResults.NumResults = StoredResults.size(); |
| 634 | std::memcpy(AllocatedResults.Results, StoredResults.data(), |
| 635 | StoredResults.size() * sizeof(CXCompletionResult)); |
| 636 | StoredResults.clear(); |
| 637 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 638 | }; |
| 639 | } |
| 640 | |
| 641 | extern "C" { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 642 | struct CodeCompleteAtInfo { |
| 643 | CXTranslationUnit TU; |
| 644 | const char *complete_filename; |
| 645 | unsigned complete_line; |
| 646 | unsigned complete_column; |
| 647 | struct CXUnsavedFile *unsaved_files; |
| 648 | unsigned num_unsaved_files; |
| 649 | unsigned options; |
| 650 | CXCodeCompleteResults *result; |
| 651 | }; |
| 652 | void clang_codeCompleteAt_Impl(void *UserData) { |
| 653 | CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData); |
| 654 | CXTranslationUnit TU = CCAI->TU; |
| 655 | const char *complete_filename = CCAI->complete_filename; |
| 656 | unsigned complete_line = CCAI->complete_line; |
| 657 | unsigned complete_column = CCAI->complete_column; |
| 658 | struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files; |
| 659 | unsigned num_unsaved_files = CCAI->num_unsaved_files; |
| 660 | unsigned options = CCAI->options; |
| 661 | CCAI->result = 0; |
| 662 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 663 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 664 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 665 | const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime(); |
| 666 | #endif |
| 667 | #endif |
| 668 | |
| 669 | bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0; |
| 670 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 671 | ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 672 | if (!AST) |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 673 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 674 | |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 675 | CIndexer *CXXIdx = (CIndexer*)TU->CIdx; |
| 676 | if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing)) |
Argyrios Kyrtzidis | 81b5ac3 | 2012-03-28 02:49:54 +0000 | [diff] [blame] | 677 | setThreadBackgroundPriority(); |
Argyrios Kyrtzidis | fdc1795 | 2012-03-28 02:18:05 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | 593b0c1 | 2010-09-23 18:47:53 +0000 | [diff] [blame] | 679 | ASTUnit::ConcurrencyCheck Check(*AST); |
| 680 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 681 | // Perform the remapping of source files. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 682 | SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 683 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 684 | StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 685 | const llvm::MemoryBuffer *Buffer |
| 686 | = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename); |
| 687 | RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename, |
| 688 | Buffer)); |
| 689 | } |
| 690 | |
| 691 | if (EnableLogging) { |
| 692 | // FIXME: Add logging. |
| 693 | } |
| 694 | |
| 695 | // Parse the resulting source file to find code-completion results. |
Anders Carlsson | 0d8d7e6 | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 696 | AllocatedCXCodeCompleteResults *Results = |
| 697 | new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts()); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 698 | Results->Results = 0; |
| 699 | Results->NumResults = 0; |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 700 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 701 | // Create a code-completion consumer to capture the results. |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 702 | CaptureCompletionResults Capture(*Results, &TU); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 703 | |
| 704 | // Perform completion. |
| 705 | AST->CodeComplete(complete_filename, complete_line, complete_column, |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 706 | RemappedFiles.data(), RemappedFiles.size(), |
| 707 | (options & CXCodeComplete_IncludeMacros), |
| 708 | (options & CXCodeComplete_IncludeCodePatterns), |
| 709 | Capture, |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 710 | *Results->Diag, Results->LangOpts, *Results->SourceMgr, |
| 711 | *Results->FileMgr, Results->Diagnostics, |
Douglas Gregor | 2283d79 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 712 | Results->TemporaryBuffers); |
Douglas Gregor | 48601b3 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 713 | |
| 714 | // Keep a reference to the allocator used for cached global completions, so |
| 715 | // that we can be sure that the memory used by our code completion strings |
| 716 | // doesn't get freed due to subsequent reparses (while the code completion |
| 717 | // results are still active). |
| 718 | Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | |
| 722 | #ifdef UDP_CODE_COMPLETION_LOGGER |
| 723 | #ifdef UDP_CODE_COMPLETION_LOGGER_PORT |
| 724 | const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime(); |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 725 | SmallString<256> LogResult; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 726 | llvm::raw_svector_ostream os(LogResult); |
| 727 | |
| 728 | // Figure out the language and whether or not it uses PCH. |
| 729 | const char *lang = 0; |
| 730 | bool usesPCH = false; |
| 731 | |
| 732 | for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end(); |
| 733 | I != E; ++I) { |
| 734 | if (*I == 0) |
| 735 | continue; |
| 736 | if (strcmp(*I, "-x") == 0) { |
| 737 | if (I + 1 != E) { |
| 738 | lang = *(++I); |
| 739 | continue; |
| 740 | } |
| 741 | } |
| 742 | else if (strcmp(*I, "-include") == 0) { |
| 743 | if (I+1 != E) { |
| 744 | const char *arg = *(++I); |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 745 | SmallString<512> pchName; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 746 | { |
| 747 | llvm::raw_svector_ostream os(pchName); |
| 748 | os << arg << ".pth"; |
| 749 | } |
| 750 | pchName.push_back('\0'); |
| 751 | struct stat stat_results; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 752 | if (stat(pchName.str().c_str(), &stat_results) == 0) |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 753 | usesPCH = true; |
| 754 | continue; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | os << "{ "; |
| 760 | os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime()); |
| 761 | os << ", \"numRes\": " << Results->NumResults; |
| 762 | os << ", \"diags\": " << Results->Diagnostics.size(); |
| 763 | os << ", \"pch\": " << (usesPCH ? "true" : "false"); |
| 764 | os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"'; |
| 765 | const char *name = getlogin(); |
| 766 | os << ", \"user\": \"" << (name ? name : "unknown") << '"'; |
| 767 | os << ", \"clangVer\": \"" << getClangFullVersion() << '"'; |
| 768 | os << " }"; |
| 769 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 770 | StringRef res = os.str(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 771 | if (res.size() > 0) { |
| 772 | do { |
| 773 | // Setup the UDP socket. |
| 774 | struct sockaddr_in servaddr; |
| 775 | bzero(&servaddr, sizeof(servaddr)); |
| 776 | servaddr.sin_family = AF_INET; |
| 777 | servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT); |
| 778 | if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER, |
| 779 | &servaddr.sin_addr) <= 0) |
| 780 | break; |
| 781 | |
| 782 | int sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 783 | if (sockfd < 0) |
| 784 | break; |
| 785 | |
| 786 | sendto(sockfd, res.data(), res.size(), 0, |
| 787 | (struct sockaddr *)&servaddr, sizeof(servaddr)); |
| 788 | close(sockfd); |
| 789 | } |
| 790 | while (false); |
| 791 | } |
| 792 | #endif |
| 793 | #endif |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 794 | CCAI->result = Results; |
| 795 | } |
| 796 | CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU, |
| 797 | const char *complete_filename, |
| 798 | unsigned complete_line, |
| 799 | unsigned complete_column, |
| 800 | struct CXUnsavedFile *unsaved_files, |
| 801 | unsigned num_unsaved_files, |
| 802 | unsigned options) { |
| 803 | CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line, |
| 804 | complete_column, unsaved_files, num_unsaved_files, |
| 805 | options, 0 }; |
| 806 | llvm::CrashRecoveryContext CRC; |
| 807 | |
Daniel Dunbar | bf44c3b | 2010-11-05 07:19:31 +0000 | [diff] [blame] | 808 | if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) { |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 809 | fprintf(stderr, "libclang: crash detected in code completion\n"); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 810 | static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true); |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 811 | return 0; |
Douglas Gregor | 6df7873 | 2011-05-05 20:27:22 +0000 | [diff] [blame] | 812 | } else if (getenv("LIBCLANG_RESOURCE_USAGE")) |
| 813 | PrintLibclangResourceUsage(TU); |
Daniel Dunbar | b1fd345 | 2010-08-19 23:44:10 +0000 | [diff] [blame] | 814 | |
| 815 | return CCAI.result; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 818 | unsigned clang_defaultCodeCompleteOptions(void) { |
| 819 | return CXCodeComplete_IncludeMacros; |
| 820 | } |
| 821 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 822 | void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) { |
| 823 | if (!ResultsIn) |
| 824 | return; |
| 825 | |
| 826 | AllocatedCXCodeCompleteResults *Results |
| 827 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 828 | delete Results; |
| 829 | } |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 831 | unsigned |
| 832 | clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) { |
| 833 | AllocatedCXCodeCompleteResults *Results |
| 834 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 835 | if (!Results) |
| 836 | return 0; |
| 837 | |
| 838 | return Results->Diagnostics.size(); |
| 839 | } |
| 840 | |
| 841 | CXDiagnostic |
| 842 | clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn, |
| 843 | unsigned Index) { |
| 844 | AllocatedCXCodeCompleteResults *Results |
| 845 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 846 | if (!Results || Index >= Results->Diagnostics.size()) |
| 847 | return 0; |
| 848 | |
| 849 | return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts); |
| 850 | } |
| 851 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 852 | unsigned long long |
| 853 | clang_codeCompleteGetContexts(CXCodeCompleteResults *ResultsIn) { |
| 854 | AllocatedCXCodeCompleteResults *Results |
| 855 | = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn); |
| 856 | if (!Results) |
| 857 | return 0; |
| 858 | |
| 859 | return Results->Contexts; |
| 860 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 862 | enum CXCursorKind clang_codeCompleteGetContainerKind( |
| 863 | CXCodeCompleteResults *ResultsIn, |
| 864 | unsigned *IsIncomplete) { |
| 865 | AllocatedCXCodeCompleteResults *Results = |
| 866 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
| 867 | if (!Results) |
| 868 | return CXCursor_InvalidCode; |
| 869 | |
| 870 | if (IsIncomplete != NULL) { |
| 871 | *IsIncomplete = Results->ContainerIsIncomplete; |
| 872 | } |
| 873 | |
| 874 | return Results->ContainerKind; |
| 875 | } |
| 876 | |
| 877 | CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) { |
| 878 | AllocatedCXCodeCompleteResults *Results = |
| 879 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
| 880 | if (!Results) |
| 881 | return createCXString(""); |
| 882 | |
| 883 | return createCXString(clang_getCString(Results->ContainerUSR)); |
| 884 | } |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 885 | |
| 886 | |
| 887 | CXString clang_codeCompleteGetObjCSelector(CXCodeCompleteResults *ResultsIn) { |
| 888 | AllocatedCXCodeCompleteResults *Results = |
| 889 | static_cast<AllocatedCXCodeCompleteResults *>(ResultsIn); |
| 890 | if (!Results) |
| 891 | return createCXString(""); |
| 892 | |
| 893 | return createCXString(Results->Selector); |
| 894 | } |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 895 | |
Ted Kremenek | ab18893 | 2010-01-05 19:32:54 +0000 | [diff] [blame] | 896 | } // end extern "C" |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 898 | /// \brief Simple utility function that appends a \p New string to the given |
| 899 | /// \p Old string, using the \p Buffer for storage. |
| 900 | /// |
| 901 | /// \param Old The string to which we are appending. This parameter will be |
| 902 | /// updated to reflect the complete string. |
| 903 | /// |
| 904 | /// |
| 905 | /// \param New The string to append to \p Old. |
| 906 | /// |
| 907 | /// \param Buffer A buffer that stores the actual, concatenated string. It will |
| 908 | /// be used if the old string is already-non-empty. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 909 | static void AppendToString(StringRef &Old, StringRef New, |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 910 | SmallString<256> &Buffer) { |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 911 | if (Old.empty()) { |
| 912 | Old = New; |
| 913 | return; |
| 914 | } |
| 915 | |
| 916 | if (Buffer.empty()) |
| 917 | Buffer.append(Old.begin(), Old.end()); |
| 918 | Buffer.append(New.begin(), New.end()); |
| 919 | Old = Buffer.str(); |
| 920 | } |
| 921 | |
| 922 | /// \brief Get the typed-text blocks from the given code-completion string |
| 923 | /// and return them as a single string. |
| 924 | /// |
| 925 | /// \param String The code-completion string whose typed-text blocks will be |
| 926 | /// concatenated. |
| 927 | /// |
| 928 | /// \param Buffer A buffer used for storage of the completed name. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 929 | static StringRef GetTypedName(CodeCompletionString *String, |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 930 | SmallString<256> &Buffer) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 931 | StringRef Result; |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 932 | for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end(); |
| 933 | C != CEnd; ++C) { |
| 934 | if (C->Kind == CodeCompletionString::CK_TypedText) |
| 935 | AppendToString(Result, C->Text, Buffer); |
| 936 | } |
| 937 | |
| 938 | return Result; |
| 939 | } |
| 940 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 941 | namespace { |
| 942 | struct OrderCompletionResults { |
| 943 | bool operator()(const CXCompletionResult &XR, |
| 944 | const CXCompletionResult &YR) const { |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 945 | CodeCompletionString *X |
| 946 | = (CodeCompletionString *)XR.CompletionString; |
| 947 | CodeCompletionString *Y |
| 948 | = (CodeCompletionString *)YR.CompletionString; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 949 | |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 950 | SmallString<256> XBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 951 | StringRef XText = GetTypedName(X, XBuffer); |
Dylan Noblesmith | 36d5927 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 952 | SmallString<256> YBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 953 | StringRef YText = GetTypedName(Y, YBuffer); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 955 | if (XText.empty() || YText.empty()) |
| 956 | return !XText.empty(); |
| 957 | |
| 958 | int result = XText.compare_lower(YText); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 959 | if (result < 0) |
| 960 | return true; |
| 961 | if (result > 0) |
| 962 | return false; |
| 963 | |
Douglas Gregor | c7b7b7a | 2010-10-18 21:05:04 +0000 | [diff] [blame] | 964 | result = XText.compare(YText); |
Douglas Gregor | 1aad340 | 2010-09-10 23:05:54 +0000 | [diff] [blame] | 965 | return result < 0; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 966 | } |
| 967 | }; |
| 968 | } |
| 969 | |
| 970 | extern "C" { |
| 971 | void clang_sortCodeCompletionResults(CXCompletionResult *Results, |
| 972 | unsigned NumResults) { |
| 973 | std::stable_sort(Results, Results + NumResults, OrderCompletionResults()); |
| 974 | } |
Douglas Gregor | 458433d | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 975 | } |