Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1 | //===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===// |
Douglas Gregor | 81b747b | 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" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 75b7128 | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 20 | #include "clang-c/Index.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include <algorithm> |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 25 | #include <cstring> |
| 26 | #include <functional> |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 27 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 28 | using namespace clang; |
Douglas Gregor | 92eff46 | 2009-11-17 16:43:05 +0000 | [diff] [blame] | 29 | using llvm::StringRef; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 6f942b2 | 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 | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 37 | case CCC_Recovery: |
Douglas Gregor | 6f942b2 | 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: |
| 49 | case CCC_MemberAccess: |
| 50 | case CCC_EnumTag: |
| 51 | case CCC_UnionTag: |
| 52 | case CCC_ClassOrStructTag: |
| 53 | case CCC_ObjCProtocolName: |
| 54 | case CCC_Namespace: |
| 55 | case CCC_Type: |
| 56 | case CCC_Name: |
| 57 | case CCC_PotentiallyQualifiedName: |
| 58 | case CCC_MacroName: |
| 59 | case CCC_MacroNameUse: |
| 60 | case CCC_PreprocessorExpression: |
| 61 | case CCC_PreprocessorDirective: |
| 62 | case CCC_NaturalLanguage: |
| 63 | case CCC_SelectorName: |
| 64 | case CCC_TypeQualifiers: |
Douglas Gregor | 52779fb | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 65 | case CCC_Other: |
Douglas Gregor | 5c722c70 | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 66 | case CCC_OtherWithMacros: |
Douglas Gregor | 6f942b2 | 2010-09-21 16:06:22 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 74 | // Code completion string implementation |
| 75 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 76 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
Daniel Dunbar | ad5757f | 2009-11-12 18:40:12 +0000 | [diff] [blame] | 77 | : Kind(Kind), Text("") |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 78 | { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 79 | switch (Kind) { |
| 80 | case CK_TypedText: |
| 81 | case CK_Text: |
| 82 | case CK_Placeholder: |
| 83 | case CK_Informative: |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 84 | case CK_ResultType: |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 85 | case CK_CurrentParameter: |
| 86 | this->Text = Text; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 87 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 88 | |
| 89 | case CK_Optional: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 90 | llvm_unreachable("Optional strings cannot be created from text"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 91 | break; |
| 92 | |
| 93 | case CK_LeftParen: |
| 94 | this->Text = "("; |
| 95 | break; |
| 96 | |
| 97 | case CK_RightParen: |
| 98 | this->Text = ")"; |
| 99 | break; |
| 100 | |
| 101 | case CK_LeftBracket: |
| 102 | this->Text = "["; |
| 103 | break; |
| 104 | |
| 105 | case CK_RightBracket: |
| 106 | this->Text = "]"; |
| 107 | break; |
| 108 | |
| 109 | case CK_LeftBrace: |
| 110 | this->Text = "{"; |
| 111 | break; |
| 112 | |
| 113 | case CK_RightBrace: |
| 114 | this->Text = "}"; |
| 115 | break; |
| 116 | |
| 117 | case CK_LeftAngle: |
| 118 | this->Text = "<"; |
| 119 | break; |
| 120 | |
| 121 | case CK_RightAngle: |
| 122 | this->Text = ">"; |
| 123 | break; |
| 124 | |
| 125 | case CK_Comma: |
| 126 | this->Text = ", "; |
| 127 | break; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 128 | |
| 129 | case CK_Colon: |
Douglas Gregor | e8f5a17 | 2010-04-07 00:21:17 +0000 | [diff] [blame] | 130 | this->Text = ":"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | case CK_SemiColon: |
| 134 | this->Text = ";"; |
| 135 | break; |
| 136 | |
| 137 | case CK_Equal: |
| 138 | this->Text = " = "; |
| 139 | break; |
| 140 | |
| 141 | case CK_HorizontalSpace: |
| 142 | this->Text = " "; |
| 143 | break; |
| 144 | |
| 145 | case CK_VerticalSpace: |
| 146 | this->Text = "\n"; |
| 147 | break; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 148 | } |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 152 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 153 | return Chunk(CK_Text, Text); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 157 | CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 158 | Chunk Result; |
| 159 | Result.Kind = CK_Optional; |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 160 | Result.Optional = Optional; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 161 | return Result; |
| 162 | } |
| 163 | |
| 164 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 165 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 166 | return Chunk(CK_Placeholder, Placeholder); |
| 167 | } |
| 168 | |
| 169 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 170 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 171 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 174 | CodeCompletionString::Chunk |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 175 | CodeCompletionString::Chunk::CreateResultType(const char *ResultType) { |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 176 | return Chunk(CK_ResultType, ResultType); |
| 177 | } |
| 178 | |
| 179 | CodeCompletionString::Chunk |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 180 | CodeCompletionString::Chunk::CreateCurrentParameter( |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 181 | const char *CurrentParameter) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 182 | return Chunk(CK_CurrentParameter, CurrentParameter); |
| 183 | } |
| 184 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 185 | CodeCompletionString::CodeCompletionString(const Chunk *Chunks, |
| 186 | unsigned NumChunks, |
| 187 | unsigned Priority, |
| 188 | CXAvailabilityKind Availability) |
| 189 | : NumChunks(NumChunks), Priority(Priority), Availability(Availability) |
| 190 | { |
| 191 | Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1); |
| 192 | for (unsigned I = 0; I != NumChunks; ++I) |
| 193 | StoredChunks[I] = Chunks[I]; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | std::string CodeCompletionString::getAsString() const { |
| 197 | std::string Result; |
| 198 | llvm::raw_string_ostream OS(Result); |
| 199 | |
| 200 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
| 201 | switch (C->Kind) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 202 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 203 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 204 | |
| 205 | case CK_Informative: |
| 206 | case CK_ResultType: |
| 207 | OS << "[#" << C->Text << "#]"; |
| 208 | break; |
| 209 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 210 | case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break; |
| 211 | default: OS << C->Text; break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
Dan Gohman | 6bdeb40 | 2010-07-26 21:33:22 +0000 | [diff] [blame] | 214 | return OS.str(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 217 | const char *CodeCompletionString::getTypedText() const { |
| 218 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) |
| 219 | if (C->Kind == CK_TypedText) |
| 220 | return C->Text; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Douglas Gregor | dae6875 | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 225 | const char *CodeCompletionAllocator::CopyString(llvm::StringRef String) { |
| 226 | char *Mem = (char *)Allocate(String.size() + 1, 1); |
| 227 | std::copy(String.begin(), String.end(), Mem); |
| 228 | Mem[String.size()] = 0; |
| 229 | return Mem; |
| 230 | } |
| 231 | |
Douglas Gregor | 577cdfd | 2011-02-17 00:22:45 +0000 | [diff] [blame] | 232 | const char *CodeCompletionAllocator::CopyString(llvm::Twine String) { |
| 233 | // FIXME: It would be more efficient to teach Twine to tell us its size and |
| 234 | // then add a routine there to fill in an allocated char* with the contents |
| 235 | // of the string. |
| 236 | llvm::SmallString<128> Data; |
| 237 | return CopyString(String.toStringRef(Data)); |
| 238 | } |
| 239 | |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 240 | CodeCompletionString *CodeCompletionBuilder::TakeString() { |
| 241 | void *Mem = Allocator.Allocate( |
| 242 | sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size(), |
| 243 | llvm::alignOf<CodeCompletionString>()); |
| 244 | CodeCompletionString *Result |
| 245 | = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(), |
| 246 | Priority, Availability); |
| 247 | Chunks.clear(); |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 248 | return Result; |
| 249 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 250 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 251 | unsigned CodeCompletionResult::getPriorityFromDecl(NamedDecl *ND) { |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 252 | if (!ND) |
| 253 | return CCP_Unlikely; |
| 254 | |
| 255 | // Context-based decisions. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 256 | DeclContext *DC = ND->getDeclContext()->getRedeclContext(); |
Douglas Gregor | 97fe61c | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 257 | if (DC->isFunctionOrMethod() || isa<BlockDecl>(DC)) { |
| 258 | // _cmd is relatively rare |
| 259 | if (ImplicitParamDecl *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND)) |
| 260 | if (ImplicitParam->getIdentifier() && |
| 261 | ImplicitParam->getIdentifier()->isStr("_cmd")) |
| 262 | return CCP_ObjC_cmd; |
| 263 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 264 | return CCP_LocalDeclaration; |
Douglas Gregor | 97fe61c | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 265 | } |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 266 | if (DC->isRecord() || isa<ObjCContainerDecl>(DC)) |
| 267 | return CCP_MemberDeclaration; |
| 268 | |
| 269 | // Content-based decisions. |
| 270 | if (isa<EnumConstantDecl>(ND)) |
| 271 | return CCP_Constant; |
| 272 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) |
| 273 | return CCP_Type; |
Douglas Gregor | 97fe61c | 2010-09-18 15:16:27 +0000 | [diff] [blame] | 274 | |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 275 | return CCP_Declaration; |
| 276 | } |
| 277 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 278 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 279 | // Code completion overload candidate implementation |
| 280 | //===----------------------------------------------------------------------===// |
| 281 | FunctionDecl * |
| 282 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 283 | if (getKind() == CK_Function) |
| 284 | return Function; |
| 285 | else if (getKind() == CK_FunctionTemplate) |
| 286 | return FunctionTemplate->getTemplatedDecl(); |
| 287 | else |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | const FunctionType * |
| 292 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 293 | switch (Kind) { |
| 294 | case CK_Function: |
| 295 | return Function->getType()->getAs<FunctionType>(); |
| 296 | |
| 297 | case CK_FunctionTemplate: |
| 298 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 299 | ->getAs<FunctionType>(); |
| 300 | |
| 301 | case CK_FunctionType: |
| 302 | return Type; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 309 | // Code completion consumer implementation |
| 310 | //===----------------------------------------------------------------------===// |
| 311 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 312 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 313 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 314 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 315 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef, |
Douglas Gregor | e6b1bb6 | 2010-08-11 21:23:17 +0000 | [diff] [blame] | 316 | CodeCompletionContext Context, |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 317 | CodeCompletionResult *Results, |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 318 | unsigned NumResults) { |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 319 | std::stable_sort(Results, Results + NumResults); |
| 320 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 321 | // Print the results. |
| 322 | for (unsigned I = 0; I != NumResults; ++I) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 323 | OS << "COMPLETION: "; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 324 | switch (Results[I].Kind) { |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 325 | case CodeCompletionResult::RK_Declaration: |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 326 | OS << Results[I].Declaration; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 327 | if (Results[I].Hidden) |
| 328 | OS << " (Hidden)"; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 329 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 330 | = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 331 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 334 | OS << '\n'; |
| 335 | break; |
| 336 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 337 | case CodeCompletionResult::RK_Keyword: |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 338 | OS << Results[I].Keyword << '\n'; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 339 | break; |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 340 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 341 | case CodeCompletionResult::RK_Macro: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 342 | OS << Results[I].Macro->getName(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 343 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 344 | = Results[I].CreateCodeCompletionString(SemaRef, Allocator)) { |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 345 | OS << " : " << CCS->getAsString(); |
Douglas Gregor | 3f7c7f4 | 2009-10-30 16:50:04 +0000 | [diff] [blame] | 346 | } |
| 347 | OS << '\n'; |
| 348 | break; |
| 349 | } |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 350 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 351 | case CodeCompletionResult::RK_Pattern: { |
Douglas Gregor | ab0b4f1 | 2010-01-13 23:24:38 +0000 | [diff] [blame] | 352 | OS << "Pattern : " |
Douglas Gregor | 54f0161 | 2009-11-19 00:01:57 +0000 | [diff] [blame] | 353 | << Results[I].Pattern->getAsString() << '\n'; |
| 354 | break; |
| 355 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 358 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 359 | |
| 360 | void |
Daniel Dunbar | 3a2838d | 2009-11-13 08:58:20 +0000 | [diff] [blame] | 361 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(Sema &SemaRef, |
| 362 | unsigned CurrentArg, |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 363 | OverloadCandidate *Candidates, |
| 364 | unsigned NumCandidates) { |
| 365 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 366 | if (CodeCompletionString *CCS |
Douglas Gregor | 218937c | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 367 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef, |
| 368 | Allocator)) { |
Douglas Gregor | f52cede | 2009-10-09 22:16:47 +0000 | [diff] [blame] | 369 | OS << "OVERLOAD: " << CCS->getAsString() << "\n"; |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 372 | } |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 373 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 374 | void CodeCompletionResult::computeCursorKindAndAvailability() { |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 375 | switch (Kind) { |
| 376 | case RK_Declaration: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 377 | // Set the availability based on attributes. |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 378 | switch (Declaration->getAvailability()) { |
| 379 | case AR_Available: |
| 380 | case AR_NotYetIntroduced: |
| 381 | Availability = CXAvailability_Available; |
| 382 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 384 | case AR_Deprecated: |
| 385 | Availability = CXAvailability_Deprecated; |
| 386 | break; |
| 387 | |
| 388 | case AR_Unavailable: |
| 389 | Availability = CXAvailability_NotAvailable; |
| 390 | break; |
| 391 | } |
| 392 | |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 393 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration)) |
| 394 | if (Function->isDeleted()) |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 395 | Availability = CXAvailability_NotAvailable; |
Douglas Gregor | e8d7beb | 2010-09-03 23:30:36 +0000 | [diff] [blame] | 396 | |
| 397 | CursorKind = getCursorKindForDecl(Declaration); |
| 398 | if (CursorKind == CXCursor_UnexposedDecl) |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 399 | CursorKind = CXCursor_NotImplemented; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 400 | break; |
| 401 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 402 | case RK_Macro: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 403 | Availability = CXAvailability_Available; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 404 | CursorKind = CXCursor_MacroDefinition; |
| 405 | break; |
| 406 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 407 | case RK_Keyword: |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 408 | Availability = CXAvailability_Available; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 409 | CursorKind = CXCursor_NotImplemented; |
| 410 | break; |
| 411 | |
John McCall | 0a2c5e2 | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 412 | case RK_Pattern: |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 413 | // Do nothing: Patterns can come with cursor kinds! |
| 414 | break; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 418 | /// \brief Retrieve the name that should be used to order a result. |
| 419 | /// |
| 420 | /// If the name needs to be constructed as a string, that string will be |
| 421 | /// saved into Saved and the returned StringRef will refer to it. |
| 422 | static llvm::StringRef getOrderedName(const CodeCompletionResult &R, |
| 423 | std::string &Saved) { |
| 424 | switch (R.Kind) { |
| 425 | case CodeCompletionResult::RK_Keyword: |
| 426 | return R.Keyword; |
| 427 | |
| 428 | case CodeCompletionResult::RK_Pattern: |
| 429 | return R.Pattern->getTypedText(); |
| 430 | |
| 431 | case CodeCompletionResult::RK_Macro: |
| 432 | return R.Macro->getName(); |
| 433 | |
| 434 | case CodeCompletionResult::RK_Declaration: |
| 435 | // Handle declarations below. |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | DeclarationName Name = R.Declaration->getDeclName(); |
| 440 | |
| 441 | // If the name is a simple identifier (by far the common case), or a |
| 442 | // zero-argument selector, just return a reference to that identifier. |
| 443 | if (IdentifierInfo *Id = Name.getAsIdentifierInfo()) |
| 444 | return Id->getName(); |
| 445 | if (Name.isObjCZeroArgSelector()) |
| 446 | if (IdentifierInfo *Id |
| 447 | = Name.getObjCSelector().getIdentifierInfoForSlot(0)) |
| 448 | return Id->getName(); |
| 449 | |
| 450 | Saved = Name.getAsString(); |
| 451 | return Saved; |
| 452 | } |
| 453 | |
| 454 | bool clang::operator<(const CodeCompletionResult &X, |
| 455 | const CodeCompletionResult &Y) { |
| 456 | std::string XSaved, YSaved; |
| 457 | llvm::StringRef XStr = getOrderedName(X, XSaved); |
| 458 | llvm::StringRef YStr = getOrderedName(Y, YSaved); |
| 459 | int cmp = XStr.compare_lower(YStr); |
| 460 | if (cmp) |
| 461 | return cmp < 0; |
| 462 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 463 | // If case-insensitive comparison fails, try case-sensitive comparison. |
| 464 | cmp = XStr.compare(YStr); |
| 465 | if (cmp) |
| 466 | return cmp < 0; |
Douglas Gregor | 721f359 | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 467 | |
| 468 | return false; |
| 469 | } |