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