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