blob: f490a2b5235a8334b980aed9ed59b9a78953bcf0 [file] [log] [blame]
Douglas Gregor3545ff42009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor2436e712009-09-17 21:32:03 +00002//
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 Gregor56c2dbc2009-09-18 17:54:00 +000014#include "clang/AST/DeclCXX.h"
Douglas Gregorf45b0cf2009-09-18 15:37:17 +000015#include "clang/Parse/Scope.h"
Douglas Gregor2436e712009-09-17 21:32:03 +000016#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 Gregorfedc3282009-09-18 22:15:54 +000022#include <cstring>
23#include <functional>
Douglas Gregor2436e712009-09-17 21:32:03 +000024using namespace clang;
25
Douglas Gregorfedc3282009-09-18 22:15:54 +000026//===----------------------------------------------------------------------===//
27// Code completion string implementation
28//===----------------------------------------------------------------------===//
Douglas Gregor5bf52692009-09-22 23:15:58 +000029CodeCompletionString::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 Gregorfedc3282009-09-18 22:15:54 +000034 char *New = new char [std::strlen(Text) + 1];
35 std::strcpy(New, Text);
Douglas Gregor5bf52692009-09-22 23:15:58 +000036 this->Text = New;
37}
38
39CodeCompletionString::Chunk
40CodeCompletionString::Chunk::CreateText(const char *Text) {
41 return Chunk(CK_Text, Text);
Douglas Gregorfedc3282009-09-18 22:15:54 +000042}
43
44CodeCompletionString::Chunk
45CodeCompletionString::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
53CodeCompletionString::Chunk
54CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor5bf52692009-09-22 23:15:58 +000055 return Chunk(CK_Placeholder, Placeholder);
56}
57
58CodeCompletionString::Chunk
59CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
60 return Chunk(CK_Informative, Informative);
Douglas Gregorfedc3282009-09-18 22:15:54 +000061}
62
63void
64CodeCompletionString::Chunk::Destroy() {
65 switch (Kind) {
Douglas Gregor5bf52692009-09-22 23:15:58 +000066 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 Gregorfedc3282009-09-18 22:15:54 +000075 }
76}
77
78CodeCompletionString::~CodeCompletionString() {
79 std::for_each(Chunks.begin(), Chunks.end(),
80 std::mem_fun_ref(&Chunk::Destroy));
81}
82
83std::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 Gregor5bf52692009-09-22 23:15:58 +000091 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
92 case CK_Informative: OS << "[#" << C->Text << "#]"; break;
Douglas Gregorfedc3282009-09-18 22:15:54 +000093 }
94 }
95
96 return Result;
97}
98
99//===----------------------------------------------------------------------===//
100// Code completion consumer implementation
101//===----------------------------------------------------------------------===//
102
Douglas Gregor3545ff42009-09-21 16:56:56 +0000103CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregorfedc3282009-09-18 22:15:54 +0000104
Douglas Gregor2436e712009-09-17 21:32:03 +0000105void
106PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results,
107 unsigned NumResults) {
Douglas Gregor2436e712009-09-17 21:32:03 +0000108 // Print the results.
109 for (unsigned I = 0; I != NumResults; ++I) {
110 switch (Results[I].Kind) {
111 case Result::RK_Declaration:
112 OS << Results[I].Declaration->getNameAsString() << " : "
113 << Results[I].Rank;
114 if (Results[I].Hidden)
115 OS << " (Hidden)";
Douglas Gregor3545ff42009-09-21 16:56:56 +0000116 if (CodeCompletionString *CCS
117 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregorfedc3282009-09-18 22:15:54 +0000118 OS << " : " << CCS->getAsString();
119 delete CCS;
120 }
121
Douglas Gregor2436e712009-09-17 21:32:03 +0000122 OS << '\n';
123 break;
124
125 case Result::RK_Keyword:
126 OS << Results[I].Keyword << " : " << Results[I].Rank << '\n';
127 break;
128 }
129 }
130
131 // Once we've printed the code-completion results, suppress remaining
132 // diagnostics.
133 // FIXME: Move this somewhere else!
Douglas Gregor3545ff42009-09-21 16:56:56 +0000134 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
Douglas Gregor2436e712009-09-17 21:32:03 +0000135}