Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1 | //===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===// |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 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 CodeCompleteConsumer class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #include "clang/Sema/CodeCompleteConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang-c/Index.h" |
Douglas Gregor | 56c2dbc | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/Sema.h" |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include <algorithm> |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 26 | #include <cstring> |
| 27 | #include <functional> |
Douglas Gregor | ab6ccb5 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 28 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 32 | // Code completion context implementation |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | bool CodeCompletionContext::wantConstructorResults() const { |
| 36 | switch (Kind) { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 37 | case CCC_Recovery: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 38 | case CCC_Statement: |
| 39 | case CCC_Expression: |
| 40 | case CCC_ObjCMessageReceiver: |
| 41 | case CCC_ParenthesizedExpression: |
| 42 | return true; |
| 43 | |
| 44 | case CCC_TopLevel: |
| 45 | case CCC_ObjCInterface: |
| 46 | case CCC_ObjCImplementation: |
| 47 | case CCC_ObjCIvarList: |
| 48 | case CCC_ClassStructUnion: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 49 | case CCC_DotMemberAccess: |
| 50 | case CCC_ArrowMemberAccess: |
| 51 | case CCC_ObjCPropertyAccess: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 52 | case CCC_EnumTag: |
| 53 | case CCC_UnionTag: |
| 54 | case CCC_ClassOrStructTag: |
| 55 | case CCC_ObjCProtocolName: |
| 56 | case CCC_Namespace: |
| 57 | case CCC_Type: |
| 58 | case CCC_Name: |
| 59 | case CCC_PotentiallyQualifiedName: |
| 60 | case CCC_MacroName: |
| 61 | case CCC_MacroNameUse: |
| 62 | case CCC_PreprocessorExpression: |
| 63 | case CCC_PreprocessorDirective: |
| 64 | case CCC_NaturalLanguage: |
| 65 | case CCC_SelectorName: |
| 66 | case CCC_TypeQualifiers: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 67 | case CCC_Other: |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 68 | case CCC_OtherWithMacros: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 69 | case CCC_ObjCInstanceMessage: |
| 70 | case CCC_ObjCClassMessage: |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 71 | case CCC_ObjCInterfaceName: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 72 | case CCC_ObjCCategoryName: |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 73 | return false; |
| 74 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 75 | |
| 76 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ilya Biryukov | 27d8258 | 2018-02-19 12:35:33 +0000 | [diff] [blame^] | 79 | StringRef clang::getCompletionKindString(enum CodeCompletionContext::Kind Kind) { |
| 80 | using CCKind = enum CodeCompletionContext::Kind; |
| 81 | switch (Kind) { |
| 82 | case CCKind::CCC_Other: |
| 83 | return "Other"; |
| 84 | case CCKind::CCC_OtherWithMacros: |
| 85 | return "OtherWithMacros"; |
| 86 | case CCKind::CCC_TopLevel: |
| 87 | return "TopLevel"; |
| 88 | case CCKind::CCC_ObjCInterface: |
| 89 | return "ObjCInterface"; |
| 90 | case CCKind::CCC_ObjCImplementation: |
| 91 | return "ObjCImplementation"; |
| 92 | case CCKind::CCC_ObjCIvarList: |
| 93 | return "ObjCIvarList"; |
| 94 | case CCKind::CCC_ClassStructUnion: |
| 95 | return "ClassStructUnion"; |
| 96 | case CCKind::CCC_Statement: |
| 97 | return "Statement"; |
| 98 | case CCKind::CCC_Expression: |
| 99 | return "Expression"; |
| 100 | case CCKind::CCC_ObjCMessageReceiver: |
| 101 | return "ObjCMessageReceiver"; |
| 102 | case CCKind::CCC_DotMemberAccess: |
| 103 | return "DotMemberAccess"; |
| 104 | case CCKind::CCC_ArrowMemberAccess: |
| 105 | return "ArrowMemberAccess"; |
| 106 | case CCKind::CCC_ObjCPropertyAccess: |
| 107 | return "ObjCPropertyAccess"; |
| 108 | case CCKind::CCC_EnumTag: |
| 109 | return "EnumTag"; |
| 110 | case CCKind::CCC_UnionTag: |
| 111 | return "UnionTag"; |
| 112 | case CCKind::CCC_ClassOrStructTag: |
| 113 | return "ClassOrStructTag"; |
| 114 | case CCKind::CCC_ObjCProtocolName: |
| 115 | return "ObjCProtocolName"; |
| 116 | case CCKind::CCC_Namespace: |
| 117 | return "Namespace"; |
| 118 | case CCKind::CCC_Type: |
| 119 | return "Type"; |
| 120 | case CCKind::CCC_Name: |
| 121 | return "Name"; |
| 122 | case CCKind::CCC_PotentiallyQualifiedName: |
| 123 | return "PotentiallyQualifiedName"; |
| 124 | case CCKind::CCC_MacroName: |
| 125 | return "MacroName"; |
| 126 | case CCKind::CCC_MacroNameUse: |
| 127 | return "MacroNameUse"; |
| 128 | case CCKind::CCC_PreprocessorExpression: |
| 129 | return "PreprocessorExpression"; |
| 130 | case CCKind::CCC_PreprocessorDirective: |
| 131 | return "PreprocessorDirective"; |
| 132 | case CCKind::CCC_NaturalLanguage: |
| 133 | return "NaturalLanguage"; |
| 134 | case CCKind::CCC_SelectorName: |
| 135 | return "SelectorName"; |
| 136 | case CCKind::CCC_TypeQualifiers: |
| 137 | return "TypeQualifiers"; |
| 138 | case CCKind::CCC_ParenthesizedExpression: |
| 139 | return "ParenthesizedExpression"; |
| 140 | case CCKind::CCC_ObjCInstanceMessage: |
| 141 | return "ObjCInstanceMessage"; |
| 142 | case CCKind::CCC_ObjCClassMessage: |
| 143 | return "ObjCClassMessage"; |
| 144 | case CCKind::CCC_ObjCInterfaceName: |
| 145 | return "ObjCInterfaceName"; |
| 146 | case CCKind::CCC_ObjCCategoryName: |
| 147 | return "ObjCCategoryName"; |
| 148 | case CCKind::CCC_Recovery: |
| 149 | return "Recovery"; |
| 150 | } |
| 151 | llvm_unreachable("Invalid CodeCompletionContext::Kind!"); |
| 152 | } |
| 153 | |
Douglas Gregor | 0212fd7 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 154 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 155 | // Code completion string implementation |
| 156 | //===----------------------------------------------------------------------===// |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 157 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
Daniel Dunbar | b0a1942 | 2009-11-12 18:40:12 +0000 | [diff] [blame] | 158 | : Kind(Kind), Text("") |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 159 | { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 160 | switch (Kind) { |
| 161 | case CK_TypedText: |
| 162 | case CK_Text: |
| 163 | case CK_Placeholder: |
| 164 | case CK_Informative: |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 165 | case CK_ResultType: |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 166 | case CK_CurrentParameter: |
| 167 | this->Text = Text; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 168 | break; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 169 | |
| 170 | case CK_Optional: |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 171 | llvm_unreachable("Optional strings cannot be created from text"); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 172 | |
| 173 | case CK_LeftParen: |
| 174 | this->Text = "("; |
| 175 | break; |
| 176 | |
| 177 | case CK_RightParen: |
| 178 | this->Text = ")"; |
| 179 | break; |
| 180 | |
| 181 | case CK_LeftBracket: |
| 182 | this->Text = "["; |
| 183 | break; |
| 184 | |
| 185 | case CK_RightBracket: |
| 186 | this->Text = "]"; |
| 187 | break; |
| 188 | |
| 189 | case CK_LeftBrace: |
| 190 | this->Text = "{"; |
| 191 | break; |
| 192 | |
| 193 | case CK_RightBrace: |
| 194 | this->Text = "}"; |
| 195 | break; |
| 196 | |
| 197 | case CK_LeftAngle: |
| 198 | this->Text = "<"; |
| 199 | break; |
| 200 | |
| 201 | case CK_RightAngle: |
| 202 | this->Text = ">"; |
| 203 | break; |
| 204 | |
| 205 | case CK_Comma: |
| 206 | this->Text = ", "; |
| 207 | break; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 208 | |
| 209 | case CK_Colon: |
Douglas Gregor | 636a61e | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 210 | this->Text = ":"; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | case CK_SemiColon: |
| 214 | this->Text = ";"; |
| 215 | break; |
| 216 | |
| 217 | case CK_Equal: |
| 218 | this->Text = " = "; |
| 219 | break; |
| 220 | |
| 221 | case CK_HorizontalSpace: |
| 222 | this->Text = " "; |
| 223 | break; |
| 224 | |
| 225 | case CK_VerticalSpace: |
| 226 | this->Text = "\n"; |
| 227 | break; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 228 | } |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 232 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 233 | return Chunk(CK_Text, Text); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 237 | CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 238 | Chunk Result; |
| 239 | Result.Kind = CK_Optional; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 240 | Result.Optional = Optional; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 241 | return Result; |
| 242 | } |
| 243 | |
| 244 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 245 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 246 | return Chunk(CK_Placeholder, Placeholder); |
| 247 | } |
| 248 | |
| 249 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 250 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 251 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 254 | CodeCompletionString::Chunk |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 255 | CodeCompletionString::Chunk::CreateResultType(const char *ResultType) { |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 256 | return Chunk(CK_ResultType, ResultType); |
| 257 | } |
| 258 | |
| 259 | CodeCompletionString::Chunk |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 260 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 261 | const char *CurrentParameter) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 262 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 263 | } |
| 264 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 265 | CodeCompletionString::CodeCompletionString(const Chunk *Chunks, |
| 266 | unsigned NumChunks, |
| 267 | unsigned Priority, |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 268 | CXAvailabilityKind Availability, |
| 269 | const char **Annotations, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 270 | unsigned NumAnnotations, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 271 | StringRef ParentName, |
| 272 | const char *BriefComment) |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 273 | : NumChunks(NumChunks), NumAnnotations(NumAnnotations), |
Argyrios Kyrtzidis | 9ae3956 | 2012-09-26 16:39:56 +0000 | [diff] [blame] | 274 | Priority(Priority), Availability(Availability), |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 275 | ParentName(ParentName), BriefComment(BriefComment) |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 276 | { |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 277 | assert(NumChunks <= 0xffff); |
| 278 | assert(NumAnnotations <= 0xffff); |
| 279 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 280 | Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1); |
| 281 | for (unsigned I = 0; I != NumChunks; ++I) |
| 282 | StoredChunks[I] = Chunks[I]; |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 283 | |
| 284 | const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks); |
| 285 | for (unsigned I = 0; I != NumAnnotations; ++I) |
| 286 | StoredAnnotations[I] = Annotations[I]; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 289 | unsigned CodeCompletionString::getAnnotationCount() const { |
| 290 | return NumAnnotations; |
| 291 | } |
| 292 | |
| 293 | const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const { |
| 294 | if (AnnotationNr < NumAnnotations) |
| 295 | return reinterpret_cast<const char * const*>(end())[AnnotationNr]; |
| 296 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 297 | return nullptr; |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 301 | std::string CodeCompletionString::getAsString() const { |
| 302 | std::string Result; |
| 303 | llvm::raw_string_ostream OS(Result); |
| 304 | |
| 305 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
| 306 | switch (C->Kind) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 307 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 5bf5269 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 308 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 309 | |
| 310 | case CK_Informative: |
| 311 | case CK_ResultType: |
| 312 | OS << "[#" << C->Text << "#]"; |
| 313 | break; |
| 314 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 315 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 316 | default: OS << C->Text; break; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
Dan Gohman | 4888f1a | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 319 | return OS.str(); |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 322 | const char *CodeCompletionString::getTypedText() const { |
| 323 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) |
| 324 | if (C->Kind == CK_TypedText) |
| 325 | return C->Text; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 326 | |
| 327 | return nullptr; |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 330 | const char *CodeCompletionAllocator::CopyString(const Twine &String) { |
| 331 | SmallString<128> Data; |
| 332 | StringRef Ref = String.toStringRef(Data); |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 333 | // FIXME: It would be more efficient to teach Twine to tell us its size and |
| 334 | // then add a routine there to fill in an allocated char* with the contents |
| 335 | // of the string. |
Yaron Keren | 1ee89fc | 2015-03-17 09:51:17 +0000 | [diff] [blame] | 336 | char *Mem = (char *)Allocate(Ref.size() + 1, 1); |
| 337 | std::copy(Ref.begin(), Ref.end(), Mem); |
| 338 | Mem[Ref.size()] = 0; |
| 339 | return Mem; |
Douglas Gregor | 669a25a | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 342 | StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) { |
| 343 | const NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 344 | if (!ND) |
| 345 | return StringRef(); |
| 346 | |
| 347 | // Check whether we've already cached the parent name. |
| 348 | StringRef &CachedParentName = ParentNames[DC]; |
| 349 | if (!CachedParentName.empty()) |
| 350 | return CachedParentName; |
| 351 | |
| 352 | // If we already processed this DeclContext and assigned empty to it, the |
| 353 | // data pointer will be non-null. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 354 | if (CachedParentName.data() != nullptr) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 355 | return StringRef(); |
| 356 | |
| 357 | // Find the interesting names. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 358 | SmallVector<const DeclContext *, 2> Contexts; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 359 | while (DC && !DC->isFunctionOrMethod()) { |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 360 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 361 | if (ND->getIdentifier()) |
| 362 | Contexts.push_back(DC); |
| 363 | } |
| 364 | |
| 365 | DC = DC->getParent(); |
| 366 | } |
| 367 | |
| 368 | { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 369 | SmallString<128> S; |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 370 | llvm::raw_svector_ostream OS(S); |
| 371 | bool First = true; |
| 372 | for (unsigned I = Contexts.size(); I != 0; --I) { |
| 373 | if (First) |
| 374 | First = false; |
| 375 | else { |
| 376 | OS << "::"; |
| 377 | } |
| 378 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 379 | const DeclContext *CurDC = Contexts[I-1]; |
| 380 | if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC)) |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 381 | CurDC = CatImpl->getCategoryDecl(); |
| 382 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 383 | if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) { |
| 384 | const ObjCInterfaceDecl *Interface = Cat->getClassInterface(); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 385 | if (!Interface) { |
| 386 | // Assign an empty StringRef but with non-null data to distinguish |
| 387 | // between empty because we didn't process the DeclContext yet. |
Reid Kleckner | d16cebe | 2016-02-10 19:09:15 +0000 | [diff] [blame] | 388 | CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 389 | return StringRef(); |
| 390 | } |
| 391 | |
| 392 | OS << Interface->getName() << '(' << Cat->getName() << ')'; |
| 393 | } else { |
| 394 | OS << cast<NamedDecl>(CurDC)->getName(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | CachedParentName = AllocatorRef->CopyString(OS.str()); |
| 399 | } |
| 400 | |
| 401 | return CachedParentName; |
| 402 | } |
| 403 | |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 404 | CodeCompletionString *CodeCompletionBuilder::TakeString() { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 405 | void *Mem = getAllocator().Allocate( |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 406 | sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() + |
| 407 | sizeof(const char *) * Annotations.size(), |
| 408 | alignof(CodeCompletionString)); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 409 | CodeCompletionString *Result |
| 410 | = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(), |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 411 | Priority, Availability, |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 412 | Annotations.data(), Annotations.size(), |
Argyrios Kyrtzidis | 9ae3956 | 2012-09-26 16:39:56 +0000 | [diff] [blame] | 413 | ParentName, BriefComment); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 414 | Chunks.clear(); |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 415 | return Result; |
| 416 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 417 | |
Benjamin Kramer | db534a4 | 2012-03-26 16:57:36 +0000 | [diff] [blame] | 418 | void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) { |
| 419 | Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text)); |
| 420 | } |
| 421 | |
| 422 | void CodeCompletionBuilder::AddTextChunk(const char *Text) { |
| 423 | Chunks.push_back(Chunk::CreateText(Text)); |
| 424 | } |
| 425 | |
| 426 | void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) { |
| 427 | Chunks.push_back(Chunk::CreateOptional(Optional)); |
| 428 | } |
| 429 | |
| 430 | void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) { |
| 431 | Chunks.push_back(Chunk::CreatePlaceholder(Placeholder)); |
| 432 | } |
| 433 | |
| 434 | void CodeCompletionBuilder::AddInformativeChunk(const char *Text) { |
| 435 | Chunks.push_back(Chunk::CreateInformative(Text)); |
| 436 | } |
| 437 | |
| 438 | void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) { |
| 439 | Chunks.push_back(Chunk::CreateResultType(ResultType)); |
| 440 | } |
| 441 | |
| 442 | void |
| 443 | CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) { |
| 444 | Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter)); |
| 445 | } |
| 446 | |
| 447 | void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK, |
| 448 | const char *Text) { |
| 449 | Chunks.push_back(Chunk(CK, Text)); |
| 450 | } |
| 451 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 452 | void CodeCompletionBuilder::addParentContext(const DeclContext *DC) { |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 453 | if (DC->isTranslationUnit()) { |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (DC->isFunctionOrMethod()) |
| 458 | return; |
| 459 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 460 | const NamedDecl *ND = dyn_cast<NamedDecl>(DC); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 461 | if (!ND) |
| 462 | return; |
| 463 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 464 | ParentName = getCodeCompletionTUInfo().getParentName(DC); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 467 | void CodeCompletionBuilder::addBriefComment(StringRef Comment) { |
| 468 | BriefComment = Allocator.CopyString(Comment); |
| 469 | } |
| 470 | |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 471 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 472 | // Code completion overload candidate implementation |
| 473 | //===----------------------------------------------------------------------===// |
| 474 | FunctionDecl * |
| 475 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 476 | if (getKind() == CK_Function) |
| 477 | return Function; |
| 478 | else if (getKind() == CK_FunctionTemplate) |
| 479 | return FunctionTemplate->getTemplatedDecl(); |
| 480 | else |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 481 | return nullptr; |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | const FunctionType * |
| 485 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 486 | switch (Kind) { |
| 487 | case CK_Function: |
| 488 | return Function->getType()->getAs<FunctionType>(); |
| 489 | |
| 490 | case CK_FunctionTemplate: |
| 491 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 492 | ->getAs<FunctionType>(); |
| 493 | |
| 494 | case CK_FunctionType: |
| 495 | return Type; |
| 496 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 497 | |
| 498 | llvm_unreachable("Invalid CandidateKind!"); |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | //===----------------------------------------------------------------------===// |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 502 | // Code completion consumer implementation |
| 503 | //===----------------------------------------------------------------------===// |
| 504 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 505 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 506 | |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 507 | bool PrintingCodeCompleteConsumer::isResultFilteredOut(StringRef Filter, |
| 508 | CodeCompletionResult Result) { |
| 509 | switch (Result.Kind) { |
| 510 | case CodeCompletionResult::RK_Declaration: { |
| 511 | return !(Result.Declaration->getIdentifier() && |
| 512 | Result.Declaration->getIdentifier()->getName().startswith(Filter)); |
| 513 | } |
| 514 | case CodeCompletionResult::RK_Keyword: { |
| 515 | return !StringRef(Result.Keyword).startswith(Filter); |
| 516 | } |
| 517 | case CodeCompletionResult::RK_Macro: { |
| 518 | return !Result.Macro->getName().startswith(Filter); |
| 519 | } |
| 520 | case CodeCompletionResult::RK_Pattern: { |
| 521 | return !StringRef(Result.Pattern->getAsString()).startswith(Filter); |
| 522 | } |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 523 | } |
Simon Pilgrim | e71a1f9 | 2016-07-27 16:41:56 +0000 | [diff] [blame] | 524 | llvm_unreachable("Unknown code completion result Kind."); |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 527 | void |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 528 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | 00c37ef | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 529 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 530 | CodeCompletionResult *Results, |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 531 | unsigned NumResults) { |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 532 | std::stable_sort(Results, Results + NumResults); |
| 533 | |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 534 | StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter(); |
| 535 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 536 | // Print the results. |
| 537 | for (unsigned I = 0; I != NumResults; ++I) { |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 538 | if(!Filter.empty() && isResultFilteredOut(Filter, Results[I])) |
| 539 | continue; |
Douglas Gregor | 58acf32 | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 540 | OS << "COMPLETION: "; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 541 | switch (Results[I].Kind) { |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 542 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 543 | OS << *Results[I].Declaration; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 544 | if (Results[I].Hidden) |
| 545 | OS << " (Hidden)"; |
Douglas Gregor | 3545ff4 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 546 | if (CodeCompletionString *CCS |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 547 | = Results[I].CreateCodeCompletionString(SemaRef, Context, |
| 548 | getAllocator(), |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 549 | CCTUInfo, |
| 550 | includeBriefComments())) { |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 551 | OS << " : " << CCS->getAsString(); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 552 | if (const char *BriefComment = CCS->getBriefComment()) |
| 553 | OS << " : " << BriefComment; |
Douglas Gregor | fedc328 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 556 | OS << '\n'; |
| 557 | break; |
| 558 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 559 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 560 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 561 | break; |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 562 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 563 | case CodeCompletionResult::RK_Macro: { |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 564 | OS << Results[I].Macro->getName(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 565 | if (CodeCompletionString *CCS |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 566 | = Results[I].CreateCodeCompletionString(SemaRef, Context, |
| 567 | getAllocator(), |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 568 | CCTUInfo, |
| 569 | includeBriefComments())) { |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 570 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | f329c7c | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 571 | } |
| 572 | OS << '\n'; |
| 573 | break; |
| 574 | } |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 575 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 576 | case CodeCompletionResult::RK_Pattern: { |
Douglas Gregor | 52ce62f | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 577 | OS << "Pattern : " |
Douglas Gregor | 45f83ee | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 578 | << Results[I].Pattern->getAsString() << '\n'; |
| 579 | break; |
| 580 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
Douglas Gregor | 2436e71 | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 583 | } |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 584 | |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 585 | // This function is used solely to preserve the former presentation of overloads |
| 586 | // by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString |
| 587 | // needs to be improved for printing the newer and more detailed overload |
| 588 | // chunks. |
| 589 | static std::string getOverloadAsString(const CodeCompletionString &CCS) { |
| 590 | std::string Result; |
| 591 | llvm::raw_string_ostream OS(Result); |
| 592 | |
| 593 | for (auto &C : CCS) { |
| 594 | switch (C.Kind) { |
| 595 | case CodeCompletionString::CK_Informative: |
| 596 | case CodeCompletionString::CK_ResultType: |
| 597 | OS << "[#" << C.Text << "#]"; |
| 598 | break; |
| 599 | |
| 600 | case CodeCompletionString::CK_CurrentParameter: |
| 601 | OS << "<#" << C.Text << "#>"; |
| 602 | break; |
| 603 | |
| 604 | default: OS << C.Text; break; |
| 605 | } |
| 606 | } |
| 607 | return OS.str(); |
| 608 | } |
| 609 | |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 610 | void |
Daniel Dunbar | 242ea9a | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 611 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 612 | unsigned CurrentArg, |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 613 | OverloadCandidate *Candidates, |
| 614 | unsigned NumCandidates) { |
| 615 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | f0f5198 | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 616 | if (CodeCompletionString *CCS |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 617 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef, |
Francisco Lopes da Silva | 0c010cd | 2015-01-28 14:17:22 +0000 | [diff] [blame] | 618 | getAllocator(), CCTUInfo, |
| 619 | includeBriefComments())) { |
| 620 | OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n"; |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
Douglas Gregor | 05f477c | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 623 | } |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 625 | /// \brief Retrieve the effective availability of the given declaration. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 626 | static AvailabilityResult getDeclAvailability(const Decl *D) { |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 627 | AvailabilityResult AR = D->getAvailability(); |
| 628 | if (isa<EnumConstantDecl>(D)) |
| 629 | AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability()); |
| 630 | return AR; |
| 631 | } |
| 632 | |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 633 | void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 634 | switch (Kind) { |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 635 | case RK_Pattern: |
| 636 | if (!Declaration) { |
| 637 | // Do nothing: Patterns can come with cursor kinds! |
| 638 | break; |
| 639 | } |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 640 | LLVM_FALLTHROUGH; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 642 | case RK_Declaration: { |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 643 | // Set the availability based on attributes. |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 644 | switch (getDeclAvailability(Declaration)) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 645 | case AR_Available: |
| 646 | case AR_NotYetIntroduced: |
| 647 | Availability = CXAvailability_Available; |
| 648 | break; |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 650 | case AR_Deprecated: |
| 651 | Availability = CXAvailability_Deprecated; |
| 652 | break; |
| 653 | |
| 654 | case AR_Unavailable: |
| 655 | Availability = CXAvailability_NotAvailable; |
| 656 | break; |
| 657 | } |
| 658 | |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 659 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 660 | if (Function->isDeleted()) |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 661 | Availability = CXAvailability_NotAvailable; |
Douglas Gregor | 09c0eb1 | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 662 | |
| 663 | CursorKind = getCursorKindForDecl(Declaration); |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 664 | if (CursorKind == CXCursor_UnexposedDecl) { |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 665 | // FIXME: Forward declarations of Objective-C classes and protocols |
| 666 | // are not directly exposed, but we want code completion to treat them |
| 667 | // like a definition. |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 668 | if (isa<ObjCInterfaceDecl>(Declaration)) |
| 669 | CursorKind = CXCursor_ObjCInterfaceDecl; |
Douglas Gregor | f610267 | 2012-01-01 21:23:57 +0000 | [diff] [blame] | 670 | else if (isa<ObjCProtocolDecl>(Declaration)) |
| 671 | CursorKind = CXCursor_ObjCProtocolDecl; |
Douglas Gregor | deafd0b | 2011-12-27 22:43:10 +0000 | [diff] [blame] | 672 | else |
| 673 | CursorKind = CXCursor_NotImplemented; |
| 674 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 675 | break; |
Douglas Gregor | 7b31682 | 2012-03-17 06:39:06 +0000 | [diff] [blame] | 676 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 677 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 678 | case RK_Macro: |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 679 | case RK_Keyword: |
Benjamin Kramer | c25c0b9 | 2012-05-20 14:19:46 +0000 | [diff] [blame] | 680 | llvm_unreachable("Macro and keyword kinds are handled by the constructors"); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 681 | } |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 682 | |
| 683 | if (!Accessible) |
| 684 | Availability = CXAvailability_NotAccessible; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 685 | } |
Douglas Gregor | f09935f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 686 | |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 687 | /// \brief Retrieve the name that should be used to order a result. |
| 688 | /// |
| 689 | /// If the name needs to be constructed as a string, that string will be |
| 690 | /// saved into Saved and the returned StringRef will refer to it. |
Sam McCall | 1102a86 | 2017-11-15 09:15:06 +0000 | [diff] [blame] | 691 | StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const { |
| 692 | switch (Kind) { |
| 693 | case RK_Keyword: |
| 694 | return Keyword; |
| 695 | case RK_Pattern: |
| 696 | return Pattern->getTypedText(); |
| 697 | case RK_Macro: |
| 698 | return Macro->getName(); |
| 699 | case RK_Declaration: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 700 | // Handle declarations below. |
| 701 | break; |
| 702 | } |
| 703 | |
Sam McCall | 1102a86 | 2017-11-15 09:15:06 +0000 | [diff] [blame] | 704 | DeclarationName Name = Declaration->getDeclName(); |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 705 | |
| 706 | // If the name is a simple identifier (by far the common case), or a |
| 707 | // zero-argument selector, just return a reference to that identifier. |
| 708 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 709 | return Id->getName(); |
| 710 | if (Name.isObjCZeroArgSelector()) |
| 711 | if (IdentifierInfo *Id |
| 712 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 713 | return Id->getName(); |
| 714 | |
| 715 | Saved = Name.getAsString(); |
| 716 | return Saved; |
| 717 | } |
| 718 | |
| 719 | bool clang::operator<(const CodeCompletionResult &X, |
| 720 | const CodeCompletionResult &Y) { |
| 721 | std::string XSaved, YSaved; |
Sam McCall | 1102a86 | 2017-11-15 09:15:06 +0000 | [diff] [blame] | 722 | StringRef XStr = X.getOrderedName(XSaved); |
| 723 | StringRef YStr = Y.getOrderedName(YSaved); |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 724 | int cmp = XStr.compare_lower(YStr); |
| 725 | if (cmp) |
| 726 | return cmp < 0; |
| 727 | |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 728 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 729 | cmp = XStr.compare(YStr); |
| 730 | if (cmp) |
| 731 | return cmp < 0; |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 732 | |
| 733 | return false; |
| 734 | } |