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