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" |
Douglas Gregor | 75b7128 | 2009-09-18 17:54:00 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 15 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "Sema.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include <algorithm> |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 22 | #include <cstring> |
| 23 | #include <functional> |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Code completion string implementation |
| 28 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 29 | CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text) |
| 30 | : Kind(Kind), Text(0) |
| 31 | { |
| 32 | assert((Kind == CK_Text || Kind == CK_Placeholder || Kind == CK_Informative) |
| 33 | && "Invalid text chunk kind"); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 34 | char *New = new char [std::strlen(Text) + 1]; |
| 35 | std::strcpy(New, Text); |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 36 | this->Text = New; |
| 37 | } |
| 38 | |
| 39 | CodeCompletionString::Chunk |
| 40 | CodeCompletionString::Chunk::CreateText(const char *Text) { |
| 41 | return Chunk(CK_Text, Text); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | CodeCompletionString::Chunk |
| 45 | CodeCompletionString::Chunk::CreateOptional( |
| 46 | std::auto_ptr<CodeCompletionString> Optional) { |
| 47 | Chunk Result; |
| 48 | Result.Kind = CK_Optional; |
| 49 | Result.Optional = Optional.release(); |
| 50 | return Result; |
| 51 | } |
| 52 | |
| 53 | CodeCompletionString::Chunk |
| 54 | CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 55 | return Chunk(CK_Placeholder, Placeholder); |
| 56 | } |
| 57 | |
| 58 | CodeCompletionString::Chunk |
| 59 | CodeCompletionString::Chunk::CreateInformative(const char *Informative) { |
| 60 | return Chunk(CK_Informative, Informative); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
| 64 | CodeCompletionString::Chunk::Destroy() { |
| 65 | switch (Kind) { |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 66 | case CK_Optional: |
| 67 | delete Optional; |
| 68 | break; |
| 69 | |
| 70 | case CK_Text: |
| 71 | case CK_Placeholder: |
| 72 | case CK_Informative: |
| 73 | delete [] Text; |
| 74 | break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | CodeCompletionString::~CodeCompletionString() { |
| 79 | std::for_each(Chunks.begin(), Chunks.end(), |
| 80 | std::mem_fun_ref(&Chunk::Destroy)); |
| 81 | } |
| 82 | |
| 83 | std::string CodeCompletionString::getAsString() const { |
| 84 | std::string Result; |
| 85 | llvm::raw_string_ostream OS(Result); |
| 86 | |
| 87 | for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) { |
| 88 | switch (C->Kind) { |
| 89 | case CK_Text: OS << C->Text; break; |
| 90 | case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break; |
Douglas Gregor | 0563c26 | 2009-09-22 23:15:58 +0000 | [diff] [blame] | 91 | case CK_Placeholder: OS << "<#" << C->Text << "#>"; break; |
| 92 | case CK_Informative: OS << "[#" << C->Text << "#]"; break; |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
Douglas Gregor | e2b7eea | 2009-09-29 15:13:39 +0000 | [diff] [blame^] | 95 | OS.flush(); |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 96 | return Result; |
| 97 | } |
| 98 | |
| 99 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 100 | // Code completion overload candidate implementation |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | FunctionDecl * |
| 103 | CodeCompleteConsumer::OverloadCandidate::getFunction() const { |
| 104 | if (getKind() == CK_Function) |
| 105 | return Function; |
| 106 | else if (getKind() == CK_FunctionTemplate) |
| 107 | return FunctionTemplate->getTemplatedDecl(); |
| 108 | else |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | const FunctionType * |
| 113 | CodeCompleteConsumer::OverloadCandidate::getFunctionType() const { |
| 114 | switch (Kind) { |
| 115 | case CK_Function: |
| 116 | return Function->getType()->getAs<FunctionType>(); |
| 117 | |
| 118 | case CK_FunctionTemplate: |
| 119 | return FunctionTemplate->getTemplatedDecl()->getType() |
| 120 | ->getAs<FunctionType>(); |
| 121 | |
| 122 | case CK_FunctionType: |
| 123 | return Type; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 130 | // Code completion consumer implementation |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 133 | CodeCompleteConsumer::~CodeCompleteConsumer() { } |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 134 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 135 | void |
| 136 | PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results, |
| 137 | unsigned NumResults) { |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 138 | // Print the results. |
| 139 | for (unsigned I = 0; I != NumResults; ++I) { |
| 140 | switch (Results[I].Kind) { |
| 141 | case Result::RK_Declaration: |
| 142 | OS << Results[I].Declaration->getNameAsString() << " : " |
| 143 | << Results[I].Rank; |
| 144 | if (Results[I].Hidden) |
| 145 | OS << " (Hidden)"; |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 146 | if (CodeCompletionString *CCS |
| 147 | = Results[I].CreateCodeCompletionString(SemaRef)) { |
Douglas Gregor | e6e0361 | 2009-09-18 22:15:54 +0000 | [diff] [blame] | 148 | OS << " : " << CCS->getAsString(); |
| 149 | delete CCS; |
| 150 | } |
| 151 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 152 | OS << '\n'; |
| 153 | break; |
| 154 | |
| 155 | case Result::RK_Keyword: |
| 156 | OS << Results[I].Keyword << " : " << Results[I].Rank << '\n'; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Once we've printed the code-completion results, suppress remaining |
| 162 | // diagnostics. |
| 163 | // FIXME: Move this somewhere else! |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 164 | SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics(); |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 165 | } |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 166 | |
| 167 | void |
| 168 | PrintingCodeCompleteConsumer::ProcessOverloadCandidates(unsigned CurrentArg, |
| 169 | OverloadCandidate *Candidates, |
| 170 | unsigned NumCandidates) { |
| 171 | for (unsigned I = 0; I != NumCandidates; ++I) { |
Douglas Gregor | 86d802e | 2009-09-23 00:34:09 +0000 | [diff] [blame] | 172 | if (CodeCompletionString *CCS |
| 173 | = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) { |
| 174 | OS << CCS->getAsString() << "\n"; |
| 175 | delete CCS; |
Douglas Gregor | 0594438 | 2009-09-23 00:16:58 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | // Once we've printed the code-completion results, suppress remaining |
| 180 | // diagnostics. |
| 181 | // FIXME: Move this somewhere else! |
| 182 | SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics(); |
| 183 | } |