2009-09-21 20:56:56 +04:00
|
|
|
//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CodeCompleteConsumer class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2009-09-18 21:54:00 +04:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-09-18 19:37:17 +04:00
|
|
|
#include "clang/Parse/Scope.h"
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
2009-09-19 02:15:54 +04:00
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-19 02:15:54 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion string implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-09-23 03:15:58 +04:00
|
|
|
CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
|
|
|
|
: Kind(Kind), Text(0)
|
|
|
|
{
|
|
|
|
assert((Kind == CK_Text || Kind == CK_Placeholder || Kind == CK_Informative)
|
|
|
|
&& "Invalid text chunk kind");
|
2009-09-19 02:15:54 +04:00
|
|
|
char *New = new char [std::strlen(Text) + 1];
|
|
|
|
std::strcpy(New, Text);
|
2009-09-23 03:15:58 +04:00
|
|
|
this->Text = New;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeCompletionString::Chunk
|
|
|
|
CodeCompletionString::Chunk::CreateText(const char *Text) {
|
|
|
|
return Chunk(CK_Text, Text);
|
2009-09-19 02:15:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeCompletionString::Chunk
|
|
|
|
CodeCompletionString::Chunk::CreateOptional(
|
|
|
|
std::auto_ptr<CodeCompletionString> Optional) {
|
|
|
|
Chunk Result;
|
|
|
|
Result.Kind = CK_Optional;
|
|
|
|
Result.Optional = Optional.release();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeCompletionString::Chunk
|
|
|
|
CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
|
2009-09-23 03:15:58 +04:00
|
|
|
return Chunk(CK_Placeholder, Placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeCompletionString::Chunk
|
|
|
|
CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
|
|
|
|
return Chunk(CK_Informative, Informative);
|
2009-09-19 02:15:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CodeCompletionString::Chunk::Destroy() {
|
|
|
|
switch (Kind) {
|
2009-09-23 03:15:58 +04:00
|
|
|
case CK_Optional:
|
|
|
|
delete Optional;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_Text:
|
|
|
|
case CK_Placeholder:
|
|
|
|
case CK_Informative:
|
|
|
|
delete [] Text;
|
|
|
|
break;
|
2009-09-19 02:15:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeCompletionString::~CodeCompletionString() {
|
|
|
|
std::for_each(Chunks.begin(), Chunks.end(),
|
|
|
|
std::mem_fun_ref(&Chunk::Destroy));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CodeCompletionString::getAsString() const {
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
|
|
|
|
|
|
|
for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
|
|
|
|
switch (C->Kind) {
|
|
|
|
case CK_Text: OS << C->Text; break;
|
|
|
|
case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
|
2009-09-23 03:15:58 +04:00
|
|
|
case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
|
|
|
|
case CK_Informative: OS << "[#" << C->Text << "#]"; break;
|
2009-09-19 02:15:54 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-29 19:13:39 +04:00
|
|
|
OS.flush();
|
2009-09-19 02:15:54 +04:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-09-23 04:16:58 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion overload candidate implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
FunctionDecl *
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::getFunction() const {
|
|
|
|
if (getKind() == CK_Function)
|
|
|
|
return Function;
|
|
|
|
else if (getKind() == CK_FunctionTemplate)
|
|
|
|
return FunctionTemplate->getTemplatedDecl();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionType *
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
|
|
|
|
switch (Kind) {
|
|
|
|
case CK_Function:
|
|
|
|
return Function->getType()->getAs<FunctionType>();
|
|
|
|
|
|
|
|
case CK_FunctionTemplate:
|
|
|
|
return FunctionTemplate->getTemplatedDecl()->getType()
|
|
|
|
->getAs<FunctionType>();
|
|
|
|
|
|
|
|
case CK_FunctionType:
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-19 02:15:54 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion consumer implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-21 20:56:56 +04:00
|
|
|
CodeCompleteConsumer::~CodeCompleteConsumer() { }
|
2009-09-19 02:15:54 +04:00
|
|
|
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
void
|
|
|
|
PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results,
|
|
|
|
unsigned NumResults) {
|
|
|
|
// Print the results.
|
|
|
|
for (unsigned I = 0; I != NumResults; ++I) {
|
2009-10-10 02:16:47 +04:00
|
|
|
OS << "COMPLETION: ";
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
switch (Results[I].Kind) {
|
|
|
|
case Result::RK_Declaration:
|
|
|
|
OS << Results[I].Declaration->getNameAsString() << " : "
|
|
|
|
<< Results[I].Rank;
|
|
|
|
if (Results[I].Hidden)
|
|
|
|
OS << " (Hidden)";
|
2009-09-21 20:56:56 +04:00
|
|
|
if (CodeCompletionString *CCS
|
|
|
|
= Results[I].CreateCodeCompletionString(SemaRef)) {
|
2009-09-19 02:15:54 +04:00
|
|
|
OS << " : " << CCS->getAsString();
|
|
|
|
delete CCS;
|
|
|
|
}
|
|
|
|
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
OS << '\n';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Result::RK_Keyword:
|
|
|
|
OS << Results[I].Keyword << " : " << Results[I].Rank << '\n';
|
|
|
|
break;
|
2009-10-30 19:50:04 +03:00
|
|
|
|
|
|
|
case Result::RK_Macro: {
|
|
|
|
OS << Results[I].Macro->getName() << " : " << Results[I].Rank;
|
|
|
|
if (CodeCompletionString *CCS
|
|
|
|
= Results[I].CreateCodeCompletionString(SemaRef)) {
|
|
|
|
OS << " : " << CCS->getAsString();
|
|
|
|
delete CCS;
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
break;
|
|
|
|
}
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we've printed the code-completion results, suppress remaining
|
|
|
|
// diagnostics.
|
|
|
|
// FIXME: Move this somewhere else!
|
2009-09-21 20:56:56 +04:00
|
|
|
SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82166 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-18 01:32:03 +04:00
|
|
|
}
|
2009-09-23 04:16:58 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
PrintingCodeCompleteConsumer::ProcessOverloadCandidates(unsigned CurrentArg,
|
|
|
|
OverloadCandidate *Candidates,
|
|
|
|
unsigned NumCandidates) {
|
|
|
|
for (unsigned I = 0; I != NumCandidates; ++I) {
|
2009-09-23 04:34:09 +04:00
|
|
|
if (CodeCompletionString *CCS
|
|
|
|
= Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
|
2009-10-10 02:16:47 +04:00
|
|
|
OS << "OVERLOAD: " << CCS->getAsString() << "\n";
|
2009-09-23 04:34:09 +04:00
|
|
|
delete CCS;
|
2009-09-23 04:16:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we've printed the code-completion results, suppress remaining
|
|
|
|
// diagnostics.
|
|
|
|
// FIXME: Move this somewhere else!
|
|
|
|
SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
|
|
|
|
}
|