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