2018-02-20 02:16:28 +00:00
|
|
|
//===- CodeCompleteConsumer.cpp - Code Completion Interface ---------------===//
|
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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CodeCompleteConsumer class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-02-20 02:16:28 +00: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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "clang-c/Index.h"
|
2018-02-20 02:16:28 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclBase.h"
|
2010-08-24 07:21:54 +00:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-08-25 05:32:35 +00:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2018-02-20 02:16:28 +00:00
|
|
|
#include "clang/AST/DeclarationName.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2016-07-27 14:56:59 +00:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2018-08-30 13:08:03 +00:00
|
|
|
#include "clang/Sema/Sema.h"
|
2012-12-04 09:13:33 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2018-02-20 02:16:28 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2020-04-06 10:32:16 -07:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2018-02-20 02:16:28 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2011-02-17 00:22:45 +00:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2018-02-20 02:16:28 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-08-30 13:08:03 +00:00
|
|
|
#include "llvm/Support/FormatVariadic.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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
2018-02-20 02:16:28 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
2009-11-17 16:43:05 +00: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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
using namespace clang;
|
|
|
|
|
2010-09-21 16:06:22 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion context implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool CodeCompletionContext::wantConstructorResults() const {
|
2018-02-19 13:53:49 +00:00
|
|
|
switch (CCKind) {
|
2010-09-23 23:01:17 +00:00
|
|
|
case CCC_Recovery:
|
2010-09-21 16:06:22 +00:00
|
|
|
case CCC_Statement:
|
|
|
|
case CCC_Expression:
|
|
|
|
case CCC_ObjCMessageReceiver:
|
|
|
|
case CCC_ParenthesizedExpression:
|
2018-10-24 15:23:49 +00:00
|
|
|
case CCC_Symbol:
|
|
|
|
case CCC_SymbolOrNewName:
|
Reland "[clang-repl] support code completion at a REPL."
Original commit message:
"
This patch enabled code completion for ClangREPL. The feature was built upon
three existing Clang components: a list completer for LineEditor, a
CompletionConsumer from SemaCodeCompletion, and the ASTUnit::codeComplete method.
The first component serves as the main entry point of handling interactive inputs.
Because a completion point for a compiler instance has to be unchanged once it
is set, an incremental compiler instance is created for each code
completion. Such a compiler instance carries over AST context source from the
main interpreter compiler in order to obtain declarations or bindings from
previous input in the same REPL session.
The most important API codeComplete in Interpreter/CodeCompletion is a thin
wrapper that calls with ASTUnit::codeComplete with necessary arguments, such as
a code completion point and a ReplCompletionConsumer, which communicates
completion results from SemaCodeCompletion back to the list completer for the
REPL.
In addition, PCC_TopLevelOrExpression and CCC_TopLevelOrExpression` top levels
were added so that SemaCodeCompletion can treat top level statements like
expression statements at the REPL. For example,
clang-repl> int foo = 42;
clang-repl> f<tab>
From a parser's persective, the cursor is at a top level. If we used code
completion without any changes, PCC_Namespace would be supplied to
Sema::CodeCompleteOrdinaryName, and thus the completion results would not
include foo.
Currently, the way we use PCC_TopLevelOrExpression and
CCC_TopLevelOrExpression is no different from the way we use PCC_Statement
and CCC_Statement respectively.
Differential revision: https://reviews.llvm.org/D154382
"
The new patch also fixes clangd and several memory issues that the bots reported
and upload the missing files.
2023-08-28 20:05:35 +00:00
|
|
|
case CCC_TopLevelOrExpression:
|
2010-09-21 16:06:22 +00:00
|
|
|
return true;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-09-21 16:06:22 +00:00
|
|
|
case CCC_TopLevel:
|
|
|
|
case CCC_ObjCInterface:
|
|
|
|
case CCC_ObjCImplementation:
|
|
|
|
case CCC_ObjCIvarList:
|
|
|
|
case CCC_ClassStructUnion:
|
2011-07-07 16:03:39 +00:00
|
|
|
case CCC_DotMemberAccess:
|
|
|
|
case CCC_ArrowMemberAccess:
|
|
|
|
case CCC_ObjCPropertyAccess:
|
2010-09-21 16:06:22 +00:00
|
|
|
case CCC_EnumTag:
|
|
|
|
case CCC_UnionTag:
|
|
|
|
case CCC_ClassOrStructTag:
|
|
|
|
case CCC_ObjCProtocolName:
|
|
|
|
case CCC_Namespace:
|
|
|
|
case CCC_Type:
|
2018-10-24 15:23:49 +00:00
|
|
|
case CCC_NewName:
|
2010-09-21 16:06:22 +00:00
|
|
|
case CCC_MacroName:
|
|
|
|
case CCC_MacroNameUse:
|
|
|
|
case CCC_PreprocessorExpression:
|
|
|
|
case CCC_PreprocessorDirective:
|
|
|
|
case CCC_NaturalLanguage:
|
|
|
|
case CCC_SelectorName:
|
|
|
|
case CCC_TypeQualifiers:
|
2010-09-23 23:01:17 +00:00
|
|
|
case CCC_Other:
|
2011-02-18 23:30:37 +00:00
|
|
|
case CCC_OtherWithMacros:
|
2011-07-07 16:03:39 +00:00
|
|
|
case CCC_ObjCInstanceMessage:
|
|
|
|
case CCC_ObjCClassMessage:
|
2011-07-30 06:55:39 +00:00
|
|
|
case CCC_ObjCInterfaceName:
|
2011-07-07 16:03:39 +00:00
|
|
|
case CCC_ObjCCategoryName:
|
2018-09-18 08:40:41 +00:00
|
|
|
case CCC_IncludedFile:
|
2021-08-07 17:36:26 +02:00
|
|
|
case CCC_Attribute:
|
2023-06-27 14:16:13 -04:00
|
|
|
case CCC_ObjCClassForwardDecl:
|
2010-09-21 16:06:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-01-17 06:56:22 +00:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid CodeCompletionContext::Kind!");
|
2010-09-21 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 13:53:49 +00:00
|
|
|
StringRef clang::getCompletionKindString(CodeCompletionContext::Kind Kind) {
|
|
|
|
using CCKind = CodeCompletionContext::Kind;
|
2018-02-19 12:35:33 +00:00
|
|
|
switch (Kind) {
|
|
|
|
case CCKind::CCC_Other:
|
|
|
|
return "Other";
|
|
|
|
case CCKind::CCC_OtherWithMacros:
|
|
|
|
return "OtherWithMacros";
|
|
|
|
case CCKind::CCC_TopLevel:
|
|
|
|
return "TopLevel";
|
|
|
|
case CCKind::CCC_ObjCInterface:
|
|
|
|
return "ObjCInterface";
|
|
|
|
case CCKind::CCC_ObjCImplementation:
|
|
|
|
return "ObjCImplementation";
|
|
|
|
case CCKind::CCC_ObjCIvarList:
|
|
|
|
return "ObjCIvarList";
|
|
|
|
case CCKind::CCC_ClassStructUnion:
|
|
|
|
return "ClassStructUnion";
|
|
|
|
case CCKind::CCC_Statement:
|
|
|
|
return "Statement";
|
|
|
|
case CCKind::CCC_Expression:
|
|
|
|
return "Expression";
|
|
|
|
case CCKind::CCC_ObjCMessageReceiver:
|
|
|
|
return "ObjCMessageReceiver";
|
|
|
|
case CCKind::CCC_DotMemberAccess:
|
|
|
|
return "DotMemberAccess";
|
|
|
|
case CCKind::CCC_ArrowMemberAccess:
|
|
|
|
return "ArrowMemberAccess";
|
|
|
|
case CCKind::CCC_ObjCPropertyAccess:
|
|
|
|
return "ObjCPropertyAccess";
|
|
|
|
case CCKind::CCC_EnumTag:
|
|
|
|
return "EnumTag";
|
|
|
|
case CCKind::CCC_UnionTag:
|
|
|
|
return "UnionTag";
|
|
|
|
case CCKind::CCC_ClassOrStructTag:
|
|
|
|
return "ClassOrStructTag";
|
|
|
|
case CCKind::CCC_ObjCProtocolName:
|
|
|
|
return "ObjCProtocolName";
|
|
|
|
case CCKind::CCC_Namespace:
|
|
|
|
return "Namespace";
|
|
|
|
case CCKind::CCC_Type:
|
|
|
|
return "Type";
|
2018-10-24 15:23:49 +00:00
|
|
|
case CCKind::CCC_NewName:
|
|
|
|
return "NewName";
|
|
|
|
case CCKind::CCC_Symbol:
|
|
|
|
return "Symbol";
|
|
|
|
case CCKind::CCC_SymbolOrNewName:
|
|
|
|
return "SymbolOrNewName";
|
2018-02-19 12:35:33 +00:00
|
|
|
case CCKind::CCC_MacroName:
|
|
|
|
return "MacroName";
|
|
|
|
case CCKind::CCC_MacroNameUse:
|
|
|
|
return "MacroNameUse";
|
|
|
|
case CCKind::CCC_PreprocessorExpression:
|
|
|
|
return "PreprocessorExpression";
|
|
|
|
case CCKind::CCC_PreprocessorDirective:
|
|
|
|
return "PreprocessorDirective";
|
|
|
|
case CCKind::CCC_NaturalLanguage:
|
|
|
|
return "NaturalLanguage";
|
|
|
|
case CCKind::CCC_SelectorName:
|
|
|
|
return "SelectorName";
|
|
|
|
case CCKind::CCC_TypeQualifiers:
|
|
|
|
return "TypeQualifiers";
|
|
|
|
case CCKind::CCC_ParenthesizedExpression:
|
|
|
|
return "ParenthesizedExpression";
|
|
|
|
case CCKind::CCC_ObjCInstanceMessage:
|
|
|
|
return "ObjCInstanceMessage";
|
|
|
|
case CCKind::CCC_ObjCClassMessage:
|
|
|
|
return "ObjCClassMessage";
|
|
|
|
case CCKind::CCC_ObjCInterfaceName:
|
|
|
|
return "ObjCInterfaceName";
|
|
|
|
case CCKind::CCC_ObjCCategoryName:
|
|
|
|
return "ObjCCategoryName";
|
2018-09-18 08:40:41 +00:00
|
|
|
case CCKind::CCC_IncludedFile:
|
|
|
|
return "IncludedFile";
|
2021-08-07 17:36:26 +02:00
|
|
|
case CCKind::CCC_Attribute:
|
|
|
|
return "Attribute";
|
2018-02-19 12:35:33 +00:00
|
|
|
case CCKind::CCC_Recovery:
|
|
|
|
return "Recovery";
|
2023-06-27 14:16:13 -04:00
|
|
|
case CCKind::CCC_ObjCClassForwardDecl:
|
|
|
|
return "ObjCClassForwardDecl";
|
Reland "[clang-repl] support code completion at a REPL."
Original commit message:
"
This patch enabled code completion for ClangREPL. The feature was built upon
three existing Clang components: a list completer for LineEditor, a
CompletionConsumer from SemaCodeCompletion, and the ASTUnit::codeComplete method.
The first component serves as the main entry point of handling interactive inputs.
Because a completion point for a compiler instance has to be unchanged once it
is set, an incremental compiler instance is created for each code
completion. Such a compiler instance carries over AST context source from the
main interpreter compiler in order to obtain declarations or bindings from
previous input in the same REPL session.
The most important API codeComplete in Interpreter/CodeCompletion is a thin
wrapper that calls with ASTUnit::codeComplete with necessary arguments, such as
a code completion point and a ReplCompletionConsumer, which communicates
completion results from SemaCodeCompletion back to the list completer for the
REPL.
In addition, PCC_TopLevelOrExpression and CCC_TopLevelOrExpression` top levels
were added so that SemaCodeCompletion can treat top level statements like
expression statements at the REPL. For example,
clang-repl> int foo = 42;
clang-repl> f<tab>
From a parser's persective, the cursor is at a top level. If we used code
completion without any changes, PCC_Namespace would be supplied to
Sema::CodeCompleteOrdinaryName, and thus the completion results would not
include foo.
Currently, the way we use PCC_TopLevelOrExpression and
CCC_TopLevelOrExpression is no different from the way we use PCC_Statement
and CCC_Statement respectively.
Differential revision: https://reviews.llvm.org/D154382
"
The new patch also fixes clangd and several memory issues that the bots reported
and upload the missing files.
2023-08-28 20:05:35 +00:00
|
|
|
case CCKind::CCC_TopLevelOrExpression:
|
|
|
|
return "ReplTopLevel";
|
2018-02-19 12:35:33 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid CodeCompletionContext::Kind!");
|
|
|
|
}
|
|
|
|
|
2009-09-18 22:15:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion string implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-02-20 02:16:28 +00:00
|
|
|
|
2018-07-30 19:24:48 +00:00
|
|
|
CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
|
2018-02-20 02:16:28 +00:00
|
|
|
: Kind(Kind), Text("") {
|
2009-11-07 00:00:49 +00:00
|
|
|
switch (Kind) {
|
|
|
|
case CK_TypedText:
|
|
|
|
case CK_Text:
|
|
|
|
case CK_Placeholder:
|
|
|
|
case CK_Informative:
|
2009-12-18 18:53:37 +00:00
|
|
|
case CK_ResultType:
|
2011-02-01 19:23:04 +00:00
|
|
|
case CK_CurrentParameter:
|
|
|
|
this->Text = Text;
|
2009-11-07 00:00:49 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_Optional:
|
2009-12-12 05:05:38 +00:00
|
|
|
llvm_unreachable("Optional strings cannot be created from text");
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-11-07 00:00:49 +00:00
|
|
|
case CK_LeftParen:
|
|
|
|
this->Text = "(";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_RightParen:
|
|
|
|
this->Text = ")";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_LeftBracket:
|
|
|
|
this->Text = "[";
|
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-11-07 00:00:49 +00:00
|
|
|
case CK_RightBracket:
|
|
|
|
this->Text = "]";
|
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-11-07 00:00:49 +00:00
|
|
|
case CK_LeftBrace:
|
|
|
|
this->Text = "{";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_RightBrace:
|
|
|
|
this->Text = "}";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_LeftAngle:
|
|
|
|
this->Text = "<";
|
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-11-07 00:00:49 +00:00
|
|
|
case CK_RightAngle:
|
|
|
|
this->Text = ">";
|
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-11-07 00:00:49 +00:00
|
|
|
case CK_Comma:
|
|
|
|
this->Text = ", ";
|
|
|
|
break;
|
Improve code completion by introducing patterns for the various C and
C++ grammatical constructs that show up in top-level (namespace-level)
declarations, member declarations, template declarations, statements,
expressions, conditions, etc. For example, we now provide a pattern
for
static_cast<type>(expr)
when we can have an expression, or
using namespace identifier;
when we can have a using directive.
Also, improves the results of code completion at the beginning of a
top-level declaration. Previously, we would see value names (function
names, global variables, etc.); now we see types, namespace names,
etc., but no values.
llvm-svn: 93134
2010-01-10 23:08:15 +00:00
|
|
|
|
|
|
|
case CK_Colon:
|
2010-04-07 00:21:17 +00:00
|
|
|
this->Text = ":";
|
Improve code completion by introducing patterns for the various C and
C++ grammatical constructs that show up in top-level (namespace-level)
declarations, member declarations, template declarations, statements,
expressions, conditions, etc. For example, we now provide a pattern
for
static_cast<type>(expr)
when we can have an expression, or
using namespace identifier;
when we can have a using directive.
Also, improves the results of code completion at the beginning of a
top-level declaration. Previously, we would see value names (function
names, global variables, etc.); now we see types, namespace names,
etc., but no values.
llvm-svn: 93134
2010-01-10 23:08:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_SemiColon:
|
|
|
|
this->Text = ";";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_Equal:
|
|
|
|
this->Text = " = ";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_HorizontalSpace:
|
|
|
|
this->Text = " ";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CK_VerticalSpace:
|
|
|
|
this->Text = "\n";
|
|
|
|
break;
|
2009-11-07 00:00:49 +00:00
|
|
|
}
|
2009-09-22 23:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CodeCompletionString::Chunk
|
2011-02-01 19:23:04 +00:00
|
|
|
CodeCompletionString::Chunk::CreateText(const char *Text) {
|
2009-09-22 23:15:58 +00:00
|
|
|
return Chunk(CK_Text, Text);
|
2009-09-18 22:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 19:24:48 +00:00
|
|
|
CodeCompletionString::Chunk
|
2011-02-01 19:23:04 +00:00
|
|
|
CodeCompletionString::Chunk::CreateOptional(CodeCompletionString *Optional) {
|
2009-09-18 22:15:54 +00:00
|
|
|
Chunk Result;
|
|
|
|
Result.Kind = CK_Optional;
|
2011-02-01 19:23:04 +00:00
|
|
|
Result.Optional = Optional;
|
2009-09-18 22:15:54 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-07-30 19:24:48 +00:00
|
|
|
CodeCompletionString::Chunk
|
2011-02-01 19:23:04 +00:00
|
|
|
CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
|
2009-09-22 23:15:58 +00:00
|
|
|
return Chunk(CK_Placeholder, Placeholder);
|
|
|
|
}
|
|
|
|
|
2018-07-30 19:24:48 +00:00
|
|
|
CodeCompletionString::Chunk
|
2011-02-01 19:23:04 +00:00
|
|
|
CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
|
2009-09-22 23:15:58 +00:00
|
|
|
return Chunk(CK_Informative, Informative);
|
2009-09-18 22:15:54 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 19:24:48 +00:00
|
|
|
CodeCompletionString::Chunk
|
2011-02-01 19:23:04 +00:00
|
|
|
CodeCompletionString::Chunk::CreateResultType(const char *ResultType) {
|
2009-12-18 18:53:37 +00:00
|
|
|
return Chunk(CK_ResultType, ResultType);
|
|
|
|
}
|
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
CodeCompletionString::Chunk CodeCompletionString::Chunk::CreateCurrentParameter(
|
|
|
|
const char *CurrentParameter) {
|
2009-11-07 00:00:49 +00:00
|
|
|
return Chunk(CK_CurrentParameter, CurrentParameter);
|
|
|
|
}
|
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
CodeCompletionString::CodeCompletionString(
|
|
|
|
const Chunk *Chunks, unsigned NumChunks, unsigned Priority,
|
|
|
|
CXAvailabilityKind Availability, const char **Annotations,
|
|
|
|
unsigned NumAnnotations, StringRef ParentName, const char *BriefComment)
|
|
|
|
: NumChunks(NumChunks), NumAnnotations(NumAnnotations), Priority(Priority),
|
|
|
|
Availability(Availability), ParentName(ParentName),
|
|
|
|
BriefComment(BriefComment) {
|
2011-10-14 15:31:08 +00:00
|
|
|
assert(NumChunks <= 0xffff);
|
|
|
|
assert(NumAnnotations <= 0xffff);
|
|
|
|
|
2018-04-05 22:15:42 +00:00
|
|
|
Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
|
2011-02-01 19:23:04 +00:00
|
|
|
for (unsigned I = 0; I != NumChunks; ++I)
|
|
|
|
StoredChunks[I] = Chunks[I];
|
2011-10-14 15:31:08 +00:00
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
const char **StoredAnnotations =
|
|
|
|
reinterpret_cast<const char **>(StoredChunks + NumChunks);
|
2011-10-14 15:31:08 +00:00
|
|
|
for (unsigned I = 0; I != NumAnnotations; ++I)
|
|
|
|
StoredAnnotations[I] = Annotations[I];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned CodeCompletionString::getAnnotationCount() const {
|
|
|
|
return NumAnnotations;
|
2009-09-18 22:15:54 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 15:31:08 +00:00
|
|
|
const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
|
|
|
|
if (AnnotationNr < NumAnnotations)
|
2018-11-25 20:57:05 +00:00
|
|
|
return reinterpret_cast<const char *const *>(end())[AnnotationNr];
|
2011-10-14 15:31:08 +00:00
|
|
|
else
|
2014-05-26 06:22:03 +00:00
|
|
|
return nullptr;
|
2011-10-14 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 22:15:54 +00:00
|
|
|
std::string CodeCompletionString::getAsString() const {
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
for (const Chunk &C : *this) {
|
|
|
|
switch (C.Kind) {
|
|
|
|
case CK_Optional:
|
|
|
|
OS << "{#" << C.Optional->getAsString() << "#}";
|
|
|
|
break;
|
|
|
|
case CK_Placeholder:
|
|
|
|
OS << "<#" << C.Text << "#>";
|
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
case CK_Informative:
|
2009-12-18 18:53:37 +00:00
|
|
|
case CK_ResultType:
|
2018-11-25 20:57:05 +00:00
|
|
|
OS << "[#" << C.Text << "#]";
|
|
|
|
break;
|
|
|
|
case CK_CurrentParameter:
|
|
|
|
OS << "<#" << C.Text << "#>";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OS << C.Text;
|
2009-12-18 18:53:37 +00:00
|
|
|
break;
|
2009-09-18 22:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 15:02:58 -08:00
|
|
|
return Result;
|
2009-09-18 22:15:54 +00:00
|
|
|
}
|
|
|
|
|
2009-11-19 00:01:57 +00:00
|
|
|
const char *CodeCompletionString::getTypedText() const {
|
2018-11-25 20:57:05 +00:00
|
|
|
for (const Chunk &C : *this)
|
|
|
|
if (C.Kind == CK_TypedText)
|
|
|
|
return C.Text;
|
2014-05-26 06:22:03 +00:00
|
|
|
|
|
|
|
return nullptr;
|
2009-11-19 00:01:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 15:13:21 -04:00
|
|
|
std::string CodeCompletionString::getAllTypedText() const {
|
|
|
|
std::string Res;
|
|
|
|
for (const Chunk &C : *this)
|
|
|
|
if (C.Kind == CK_TypedText)
|
|
|
|
Res += C.Text;
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2015-03-17 09:51:17 +00:00
|
|
|
const char *CodeCompletionAllocator::CopyString(const Twine &String) {
|
|
|
|
SmallString<128> Data;
|
|
|
|
StringRef Ref = String.toStringRef(Data);
|
2011-02-17 00:22:45 +00:00
|
|
|
// FIXME: It would be more efficient to teach Twine to tell us its size and
|
|
|
|
// then add a routine there to fill in an allocated char* with the contents
|
|
|
|
// of the string.
|
2018-04-05 22:15:42 +00:00
|
|
|
char *Mem = (char *)Allocate(Ref.size() + 1, 1);
|
2015-03-17 09:51:17 +00:00
|
|
|
std::copy(Ref.begin(), Ref.end(), Mem);
|
|
|
|
Mem[Ref.size()] = 0;
|
|
|
|
return Mem;
|
2011-02-17 00:22:45 +00:00
|
|
|
}
|
|
|
|
|
2013-01-23 17:21:11 +00:00
|
|
|
StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
|
2020-11-15 12:56:32 +00:00
|
|
|
if (!isa<NamedDecl>(DC))
|
2018-02-20 02:16:28 +00:00
|
|
|
return {};
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-04-10 17:23:48 +00:00
|
|
|
// Check whether we've already cached the parent name.
|
|
|
|
StringRef &CachedParentName = ParentNames[DC];
|
|
|
|
if (!CachedParentName.empty())
|
|
|
|
return CachedParentName;
|
|
|
|
|
|
|
|
// If we already processed this DeclContext and assigned empty to it, the
|
|
|
|
// data pointer will be non-null.
|
2014-05-26 06:22:03 +00:00
|
|
|
if (CachedParentName.data() != nullptr)
|
2018-02-20 02:16:28 +00:00
|
|
|
return {};
|
2012-04-10 17:23:48 +00:00
|
|
|
|
|
|
|
// Find the interesting names.
|
2013-01-23 17:21:11 +00:00
|
|
|
SmallVector<const DeclContext *, 2> Contexts;
|
2012-04-10 17:23:48 +00:00
|
|
|
while (DC && !DC->isFunctionOrMethod()) {
|
2018-11-25 20:57:05 +00:00
|
|
|
if (const auto *ND = dyn_cast<NamedDecl>(DC)) {
|
2012-04-10 17:23:48 +00:00
|
|
|
if (ND->getIdentifier())
|
|
|
|
Contexts.push_back(DC);
|
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-04-10 17:23:48 +00:00
|
|
|
DC = DC->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-01-12 19:30:44 +00:00
|
|
|
SmallString<128> S;
|
2012-04-10 17:23:48 +00:00
|
|
|
llvm::raw_svector_ostream OS(S);
|
|
|
|
bool First = true;
|
2021-11-07 14:24:33 +01:00
|
|
|
for (const DeclContext *CurDC : llvm::reverse(Contexts)) {
|
2012-04-10 17:23:48 +00:00
|
|
|
if (First)
|
|
|
|
First = false;
|
|
|
|
else {
|
|
|
|
OS << "::";
|
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
if (const auto *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
|
2012-04-10 17:23:48 +00:00
|
|
|
CurDC = CatImpl->getCategoryDecl();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
if (const auto *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
|
2013-01-23 17:21:11 +00:00
|
|
|
const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
|
2012-04-10 17:23:48 +00:00
|
|
|
if (!Interface) {
|
|
|
|
// Assign an empty StringRef but with non-null data to distinguish
|
|
|
|
// between empty because we didn't process the DeclContext yet.
|
2016-02-10 19:09:15 +00:00
|
|
|
CachedParentName = StringRef((const char *)(uintptr_t)~0U, 0);
|
2018-02-20 02:16:28 +00:00
|
|
|
return {};
|
2012-04-10 17:23:48 +00:00
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-04-10 17:23:48 +00:00
|
|
|
OS << Interface->getName() << '(' << Cat->getName() << ')';
|
|
|
|
} else {
|
|
|
|
OS << cast<NamedDecl>(CurDC)->getName();
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-04-10 17:23:48 +00:00
|
|
|
CachedParentName = AllocatorRef->CopyString(OS.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return CachedParentName;
|
|
|
|
}
|
|
|
|
|
2011-02-01 19:23:04 +00:00
|
|
|
CodeCompletionString *CodeCompletionBuilder::TakeString() {
|
2012-04-10 17:23:48 +00:00
|
|
|
void *Mem = getAllocator().Allocate(
|
2016-10-20 14:27:22 +00:00
|
|
|
sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
|
|
|
|
sizeof(const char *) * Annotations.size(),
|
|
|
|
alignof(CodeCompletionString));
|
2018-11-25 20:57:05 +00:00
|
|
|
CodeCompletionString *Result = new (Mem) CodeCompletionString(
|
|
|
|
Chunks.data(), Chunks.size(), Priority, Availability, Annotations.data(),
|
|
|
|
Annotations.size(), ParentName, BriefComment);
|
2011-02-01 19:23:04 +00:00
|
|
|
Chunks.clear();
|
2009-11-19 00:01:57 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2009-11-07 00:00:49 +00:00
|
|
|
|
2012-03-26 16:57:36 +00:00
|
|
|
void CodeCompletionBuilder::AddTypedTextChunk(const char *Text) {
|
|
|
|
Chunks.push_back(Chunk(CodeCompletionString::CK_TypedText, Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompletionBuilder::AddTextChunk(const char *Text) {
|
|
|
|
Chunks.push_back(Chunk::CreateText(Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompletionBuilder::AddOptionalChunk(CodeCompletionString *Optional) {
|
|
|
|
Chunks.push_back(Chunk::CreateOptional(Optional));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompletionBuilder::AddPlaceholderChunk(const char *Placeholder) {
|
|
|
|
Chunks.push_back(Chunk::CreatePlaceholder(Placeholder));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompletionBuilder::AddInformativeChunk(const char *Text) {
|
|
|
|
Chunks.push_back(Chunk::CreateInformative(Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompletionBuilder::AddResultTypeChunk(const char *ResultType) {
|
|
|
|
Chunks.push_back(Chunk::CreateResultType(ResultType));
|
|
|
|
}
|
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
void CodeCompletionBuilder::AddCurrentParameterChunk(
|
|
|
|
const char *CurrentParameter) {
|
2012-03-26 16:57:36 +00:00
|
|
|
Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompletionBuilder::AddChunk(CodeCompletionString::ChunkKind CK,
|
|
|
|
const char *Text) {
|
|
|
|
Chunks.push_back(Chunk(CK, Text));
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:21:11 +00:00
|
|
|
void CodeCompletionBuilder::addParentContext(const DeclContext *DC) {
|
2018-02-20 02:16:28 +00:00
|
|
|
if (DC->isTranslationUnit())
|
2012-03-27 23:34:16 +00:00
|
|
|
return;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-03-27 23:34:16 +00:00
|
|
|
if (DC->isFunctionOrMethod())
|
|
|
|
return;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2020-11-15 12:56:32 +00:00
|
|
|
if (!isa<NamedDecl>(DC))
|
2012-03-27 23:34:16 +00:00
|
|
|
return;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-04-10 17:23:48 +00:00
|
|
|
ParentName = getCodeCompletionTUInfo().getParentName(DC);
|
2012-03-27 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 17:35:10 +00:00
|
|
|
void CodeCompletionBuilder::addBriefComment(StringRef Comment) {
|
|
|
|
BriefComment = Allocator.CopyString(Comment);
|
|
|
|
}
|
|
|
|
|
2009-09-23 00:16:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion overload candidate implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-11-25 20:57:05 +00:00
|
|
|
FunctionDecl *CodeCompleteConsumer::OverloadCandidate::getFunction() const {
|
2009-09-23 00:16:58 +00:00
|
|
|
if (getKind() == CK_Function)
|
|
|
|
return Function;
|
|
|
|
else if (getKind() == CK_FunctionTemplate)
|
|
|
|
return FunctionTemplate->getTemplatedDecl();
|
|
|
|
else
|
2014-05-26 06:22:03 +00:00
|
|
|
return nullptr;
|
2009-09-23 00:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const FunctionType *
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
|
|
|
|
switch (Kind) {
|
|
|
|
case CK_Function:
|
|
|
|
return Function->getType()->getAs<FunctionType>();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-09-23 00:16:58 +00:00
|
|
|
case CK_FunctionTemplate:
|
2018-11-25 20:57:05 +00:00
|
|
|
return FunctionTemplate->getTemplatedDecl()
|
|
|
|
->getType()
|
|
|
|
->getAs<FunctionType>();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2009-09-23 00:16:58 +00:00
|
|
|
case CK_FunctionType:
|
|
|
|
return Type;
|
2022-07-19 20:02:24 -04:00
|
|
|
case CK_FunctionProtoTypeLoc:
|
|
|
|
return ProtoTypeLoc.getTypePtr();
|
2021-12-29 04:16:47 +01:00
|
|
|
case CK_Template:
|
2021-12-28 03:58:13 +01:00
|
|
|
case CK_Aggregate:
|
2021-12-29 04:16:47 +01:00
|
|
|
return nullptr;
|
2009-09-23 00:16:58 +00:00
|
|
|
}
|
2012-01-17 06:56:22 +00:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid CandidateKind!");
|
2009-09-23 00:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 20:02:24 -04:00
|
|
|
const FunctionProtoTypeLoc
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::getFunctionProtoTypeLoc() const {
|
|
|
|
if (Kind == CK_FunctionProtoTypeLoc)
|
|
|
|
return ProtoTypeLoc;
|
|
|
|
return FunctionProtoTypeLoc();
|
|
|
|
}
|
|
|
|
|
2021-12-29 04:16:47 +01:00
|
|
|
unsigned CodeCompleteConsumer::OverloadCandidate::getNumParams() const {
|
|
|
|
if (Kind == CK_Template)
|
|
|
|
return Template->getTemplateParameters()->size();
|
2021-12-28 03:58:13 +01:00
|
|
|
|
|
|
|
if (Kind == CK_Aggregate) {
|
|
|
|
unsigned Count =
|
|
|
|
std::distance(AggregateType->field_begin(), AggregateType->field_end());
|
|
|
|
if (const auto *CRD = dyn_cast<CXXRecordDecl>(AggregateType))
|
|
|
|
Count += CRD->getNumBases();
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *FT = getFunctionType())
|
|
|
|
if (const auto *FPT = dyn_cast<FunctionProtoType>(FT))
|
|
|
|
return FPT->getNumParams();
|
|
|
|
|
2021-12-29 04:16:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-28 03:58:13 +01:00
|
|
|
QualType
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::getParamType(unsigned N) const {
|
|
|
|
if (Kind == CK_Aggregate) {
|
|
|
|
if (const auto *CRD = dyn_cast<CXXRecordDecl>(AggregateType)) {
|
|
|
|
if (N < CRD->getNumBases())
|
|
|
|
return std::next(CRD->bases_begin(), N)->getType();
|
|
|
|
N -= CRD->getNumBases();
|
|
|
|
}
|
|
|
|
for (const auto *Field : AggregateType->fields())
|
|
|
|
if (N-- == 0)
|
|
|
|
return Field->getType();
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Kind == CK_Template) {
|
|
|
|
TemplateParameterList *TPL = getTemplate()->getTemplateParameters();
|
|
|
|
if (N < TPL->size())
|
|
|
|
if (const auto *D = dyn_cast<NonTypeTemplateParmDecl>(TPL->getParam(N)))
|
|
|
|
return D->getType();
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *FT = getFunctionType())
|
|
|
|
if (const auto *FPT = dyn_cast<FunctionProtoType>(FT))
|
|
|
|
if (N < FPT->getNumParams())
|
|
|
|
return FPT->getParamType(N);
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
const NamedDecl *
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::getParamDecl(unsigned N) const {
|
|
|
|
if (Kind == CK_Aggregate) {
|
|
|
|
if (const auto *CRD = dyn_cast<CXXRecordDecl>(AggregateType)) {
|
|
|
|
if (N < CRD->getNumBases())
|
|
|
|
return std::next(CRD->bases_begin(), N)->getType()->getAsTagDecl();
|
|
|
|
N -= CRD->getNumBases();
|
|
|
|
}
|
|
|
|
for (const auto *Field : AggregateType->fields())
|
|
|
|
if (N-- == 0)
|
|
|
|
return Field;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Kind == CK_Template) {
|
|
|
|
TemplateParameterList *TPL = getTemplate()->getTemplateParameters();
|
|
|
|
if (N < TPL->size())
|
|
|
|
return TPL->getParam(N);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that if we only have a FunctionProtoType, we don't have param decls.
|
|
|
|
if (const auto *FD = getFunction()) {
|
|
|
|
if (N < FD->param_size())
|
|
|
|
return FD->getParamDecl(N);
|
2022-07-19 20:02:24 -04:00
|
|
|
} else if (Kind == CK_FunctionProtoTypeLoc) {
|
|
|
|
if (N < ProtoTypeLoc.getNumParams()) {
|
|
|
|
return ProtoTypeLoc.getParam(N);
|
|
|
|
}
|
2021-12-28 03:58:13 +01:00
|
|
|
}
|
2022-07-19 20:02:24 -04:00
|
|
|
|
2021-12-28 03:58:13 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2009-09-18 22:15:54 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Code completion consumer implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-02-20 02:16:28 +00:00
|
|
|
CodeCompleteConsumer::~CodeCompleteConsumer() = default;
|
2009-09-18 22:15:54 +00:00
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
bool PrintingCodeCompleteConsumer::isResultFilteredOut(
|
|
|
|
StringRef Filter, CodeCompletionResult Result) {
|
2016-07-27 14:56:59 +00:00
|
|
|
switch (Result.Kind) {
|
2018-02-20 02:16:28 +00:00
|
|
|
case CodeCompletionResult::RK_Declaration:
|
2023-12-13 08:54:13 -08:00
|
|
|
return !(
|
|
|
|
Result.Declaration->getIdentifier() &&
|
|
|
|
Result.Declaration->getIdentifier()->getName().starts_with(Filter));
|
2018-02-20 02:16:28 +00:00
|
|
|
case CodeCompletionResult::RK_Keyword:
|
2023-12-13 08:54:13 -08:00
|
|
|
return !StringRef(Result.Keyword).starts_with(Filter);
|
2018-02-20 02:16:28 +00:00
|
|
|
case CodeCompletionResult::RK_Macro:
|
2023-12-13 08:54:13 -08:00
|
|
|
return !Result.Macro->getName().starts_with(Filter);
|
2018-02-20 02:16:28 +00:00
|
|
|
case CodeCompletionResult::RK_Pattern:
|
2018-09-18 08:40:41 +00:00
|
|
|
return !(Result.Pattern->getTypedText() &&
|
2023-12-13 08:54:13 -08:00
|
|
|
StringRef(Result.Pattern->getTypedText()).starts_with(Filter));
|
2016-07-27 14:56:59 +00:00
|
|
|
}
|
2016-07-27 16:41:56 +00:00
|
|
|
llvm_unreachable("Unknown code completion result Kind.");
|
2016-07-27 14:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
void PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(
|
|
|
|
Sema &SemaRef, CodeCompletionContext Context, CodeCompletionResult *Results,
|
|
|
|
unsigned NumResults) {
|
2010-08-26 13:48:20 +00:00
|
|
|
std::stable_sort(Results, Results + NumResults);
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2018-12-13 15:36:32 +00:00
|
|
|
if (!Context.getPreferredType().isNull())
|
2022-04-20 22:09:03 +01:00
|
|
|
OS << "PREFERRED-TYPE: " << Context.getPreferredType() << '\n';
|
2016-07-27 14:56:59 +00:00
|
|
|
|
2018-12-13 15:36:32 +00:00
|
|
|
StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
|
|
|
|
// Print the completions.
|
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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
for (unsigned I = 0; I != NumResults; ++I) {
|
2018-11-25 20:57:05 +00:00
|
|
|
if (!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
|
2016-07-27 14:56:59 +00:00
|
|
|
continue;
|
2009-10-09 22:16:47 +00: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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
switch (Results[I].Kind) {
|
2010-08-25 06:19:51 +00:00
|
|
|
case CodeCompletionResult::RK_Declaration:
|
2011-10-14 18:45:37 +00:00
|
|
|
OS << *Results[I].Declaration;
|
2018-10-24 12:57:27 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> Tags;
|
|
|
|
if (Results[I].Hidden)
|
|
|
|
Tags.push_back("Hidden");
|
|
|
|
if (Results[I].InBaseClass)
|
|
|
|
Tags.push_back("InBase");
|
2018-12-03 13:29:17 +00:00
|
|
|
if (Results[I].Availability ==
|
|
|
|
CXAvailabilityKind::CXAvailability_NotAccessible)
|
|
|
|
Tags.push_back("Inaccessible");
|
2018-10-24 12:57:27 +00:00
|
|
|
if (!Tags.empty())
|
|
|
|
OS << " (" << llvm::join(Tags, ",") << ")";
|
|
|
|
}
|
|
|
|
if (CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(
|
|
|
|
SemaRef, Context, getAllocator(), CCTUInfo,
|
|
|
|
includeBriefComments())) {
|
2009-09-18 22:15:54 +00:00
|
|
|
OS << " : " << CCS->getAsString();
|
2012-07-02 17:35:10 +00:00
|
|
|
if (const char *BriefComment = CCS->getBriefComment())
|
|
|
|
OS << " : " << BriefComment;
|
2009-09-18 22:15:54 +00: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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-08-25 06:19:51 +00:00
|
|
|
case CodeCompletionResult::RK_Keyword:
|
2020-01-29 19:11:21 +01:00
|
|
|
OS << Results[I].Keyword;
|
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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2018-02-20 02:16:28 +00:00
|
|
|
case CodeCompletionResult::RK_Macro:
|
2010-01-13 23:24:38 +00:00
|
|
|
OS << Results[I].Macro->getName();
|
2018-11-25 20:57:05 +00:00
|
|
|
if (CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(
|
|
|
|
SemaRef, Context, getAllocator(), CCTUInfo,
|
|
|
|
includeBriefComments())) {
|
2009-10-30 16:50:04 +00:00
|
|
|
OS << " : " << CCS->getAsString();
|
|
|
|
}
|
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2018-02-20 02:16:28 +00:00
|
|
|
case CodeCompletionResult::RK_Pattern:
|
2020-01-29 19:11:21 +01:00
|
|
|
OS << "Pattern : " << Results[I].Pattern->getAsString();
|
2009-11-19 00:01:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-01-29 19:11:21 +01:00
|
|
|
for (const FixItHint &FixIt : Results[I].FixIts) {
|
|
|
|
const SourceLocation BLoc = FixIt.RemoveRange.getBegin();
|
|
|
|
const SourceLocation ELoc = FixIt.RemoveRange.getEnd();
|
|
|
|
|
|
|
|
SourceManager &SM = SemaRef.SourceMgr;
|
|
|
|
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
|
|
|
|
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
|
|
|
|
// Adjust for token ranges.
|
|
|
|
if (FixIt.RemoveRange.isTokenRange())
|
|
|
|
EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, SemaRef.LangOpts);
|
|
|
|
|
|
|
|
OS << " (requires fix-it:"
|
|
|
|
<< " {" << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
|
|
|
|
<< SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
|
|
|
|
<< SM.getLineNumber(EInfo.first, EInfo.second) << ':'
|
|
|
|
<< SM.getColumnNumber(EInfo.first, EInfo.second) << "}"
|
|
|
|
<< " to \"" << FixIt.CodeToInsert << "\")";
|
|
|
|
}
|
|
|
|
OS << '\n';
|
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".
llvm-svn: 82166
2009-09-17 21:32:03 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-23 00:16:58 +00:00
|
|
|
|
2015-01-28 14:17:22 +00:00
|
|
|
// This function is used solely to preserve the former presentation of overloads
|
|
|
|
// by "clang -cc1 -code-completion-at", since CodeCompletionString::getAsString
|
|
|
|
// needs to be improved for printing the newer and more detailed overload
|
|
|
|
// chunks.
|
|
|
|
static std::string getOverloadAsString(const CodeCompletionString &CCS) {
|
|
|
|
std::string Result;
|
|
|
|
llvm::raw_string_ostream OS(Result);
|
|
|
|
|
|
|
|
for (auto &C : CCS) {
|
|
|
|
switch (C.Kind) {
|
|
|
|
case CodeCompletionString::CK_Informative:
|
|
|
|
case CodeCompletionString::CK_ResultType:
|
|
|
|
OS << "[#" << C.Text << "#]";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CodeCompletionString::CK_CurrentParameter:
|
|
|
|
OS << "<#" << C.Text << "#>";
|
|
|
|
break;
|
|
|
|
|
2018-09-10 13:46:28 +00:00
|
|
|
// FIXME: We can also print optional parameters of an overload.
|
|
|
|
case CodeCompletionString::CK_Optional:
|
|
|
|
break;
|
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
default:
|
|
|
|
OS << C.Text;
|
|
|
|
break;
|
2015-01-28 14:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 15:02:58 -08:00
|
|
|
return Result;
|
2015-01-28 14:17:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 13:08:03 +00:00
|
|
|
void PrintingCodeCompleteConsumer::ProcessOverloadCandidates(
|
|
|
|
Sema &SemaRef, unsigned CurrentArg, OverloadCandidate *Candidates,
|
2021-12-27 20:42:11 +01:00
|
|
|
unsigned NumCandidates, SourceLocation OpenParLoc, bool Braced) {
|
2018-08-30 13:08:03 +00:00
|
|
|
OS << "OPENING_PAREN_LOC: ";
|
|
|
|
OpenParLoc.print(OS, SemaRef.getSourceManager());
|
|
|
|
OS << "\n";
|
|
|
|
|
2009-09-23 00:16:58 +00:00
|
|
|
for (unsigned I = 0; I != NumCandidates; ++I) {
|
2018-08-30 13:08:03 +00:00
|
|
|
if (CodeCompletionString *CCS = Candidates[I].CreateSignatureString(
|
|
|
|
CurrentArg, SemaRef, getAllocator(), CCTUInfo,
|
2021-12-27 20:42:11 +01:00
|
|
|
includeBriefComments(), Braced)) {
|
2015-01-28 14:17:22 +00:00
|
|
|
OS << "OVERLOAD: " << getOverloadAsString(*CCS) << "\n";
|
2009-09-23 00:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-07 00:00:49 +00:00
|
|
|
|
2018-05-09 01:00:01 +00:00
|
|
|
/// Retrieve the effective availability of the given declaration.
|
2013-01-23 17:21:11 +00:00
|
|
|
static AvailabilityResult getDeclAvailability(const Decl *D) {
|
2012-03-17 06:39:06 +00:00
|
|
|
AvailabilityResult AR = D->getAvailability();
|
|
|
|
if (isa<EnumConstantDecl>(D))
|
|
|
|
AR = std::max(AR, cast<Decl>(D->getDeclContext())->getAvailability());
|
|
|
|
return AR;
|
|
|
|
}
|
|
|
|
|
2011-10-06 07:27:49 +00:00
|
|
|
void CodeCompletionResult::computeCursorKindAndAvailability(bool Accessible) {
|
2010-08-13 22:48:40 +00:00
|
|
|
switch (Kind) {
|
2012-03-27 23:34:16 +00:00
|
|
|
case RK_Pattern:
|
|
|
|
if (!Declaration) {
|
|
|
|
// Do nothing: Patterns can come with cursor kinds!
|
|
|
|
break;
|
|
|
|
}
|
2022-08-08 09:12:46 -07:00
|
|
|
[[fallthrough]];
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2012-03-17 06:39:06 +00:00
|
|
|
case RK_Declaration: {
|
2010-08-23 23:00:57 +00:00
|
|
|
// Set the availability based on attributes.
|
2012-03-17 06:39:06 +00:00
|
|
|
switch (getDeclAvailability(Declaration)) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 00:50:03 +00:00
|
|
|
case AR_Available:
|
|
|
|
case AR_NotYetIntroduced:
|
2018-07-30 19:24:48 +00:00
|
|
|
Availability = CXAvailability_Available;
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 00:50:03 +00:00
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 00:50:03 +00:00
|
|
|
case AR_Deprecated:
|
2010-08-23 23:00:57 +00:00
|
|
|
Availability = CXAvailability_Deprecated;
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 00:50:03 +00:00
|
|
|
break;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 00:50:03 +00:00
|
|
|
case AR_Unavailable:
|
|
|
|
Availability = CXAvailability_NotAvailable;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-25 20:57:05 +00:00
|
|
|
if (const auto *Function = dyn_cast<FunctionDecl>(Declaration))
|
2010-09-03 23:30:36 +00:00
|
|
|
if (Function->isDeleted())
|
2010-08-23 23:00:57 +00:00
|
|
|
Availability = CXAvailability_NotAvailable;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-09-03 23:30:36 +00:00
|
|
|
CursorKind = getCursorKindForDecl(Declaration);
|
2011-12-27 22:43:10 +00:00
|
|
|
if (CursorKind == CXCursor_UnexposedDecl) {
|
2018-07-30 19:24:48 +00:00
|
|
|
// FIXME: Forward declarations of Objective-C classes and protocols
|
|
|
|
// are not directly exposed, but we want code completion to treat them
|
2012-01-01 21:23:57 +00:00
|
|
|
// like a definition.
|
2011-12-27 22:43:10 +00:00
|
|
|
if (isa<ObjCInterfaceDecl>(Declaration))
|
|
|
|
CursorKind = CXCursor_ObjCInterfaceDecl;
|
2012-01-01 21:23:57 +00:00
|
|
|
else if (isa<ObjCProtocolDecl>(Declaration))
|
|
|
|
CursorKind = CXCursor_ObjCProtocolDecl;
|
2011-12-27 22:43:10 +00:00
|
|
|
else
|
|
|
|
CursorKind = CXCursor_NotImplemented;
|
|
|
|
}
|
2010-08-13 22:48:40 +00:00
|
|
|
break;
|
2012-03-17 06:39:06 +00:00
|
|
|
}
|
2010-08-13 22:48:40 +00:00
|
|
|
|
2010-08-25 06:19:51 +00:00
|
|
|
case RK_Macro:
|
|
|
|
case RK_Keyword:
|
2012-05-20 14:19:46 +00:00
|
|
|
llvm_unreachable("Macro and keyword kinds are handled by the constructors");
|
2010-08-04 16:47:14 +00:00
|
|
|
}
|
2011-10-06 07:27:49 +00:00
|
|
|
|
|
|
|
if (!Accessible)
|
|
|
|
Availability = CXAvailability_NotAccessible;
|
2010-08-04 16:47:14 +00:00
|
|
|
}
|
2009-12-01 05:55:20 +00:00
|
|
|
|
2018-05-09 01:00:01 +00:00
|
|
|
/// Retrieve the name that should be used to order a result.
|
2010-08-25 18:41:16 +00:00
|
|
|
///
|
|
|
|
/// If the name needs to be constructed as a string, that string will be
|
|
|
|
/// saved into Saved and the returned StringRef will refer to it.
|
2017-11-15 09:15:06 +00:00
|
|
|
StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const {
|
|
|
|
switch (Kind) {
|
2018-11-25 20:57:05 +00:00
|
|
|
case RK_Keyword:
|
|
|
|
return Keyword;
|
|
|
|
case RK_Pattern:
|
|
|
|
return Pattern->getTypedText();
|
|
|
|
case RK_Macro:
|
|
|
|
return Macro->getName();
|
|
|
|
case RK_Declaration:
|
|
|
|
// Handle declarations below.
|
|
|
|
break;
|
2010-08-25 18:41:16 +00:00
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2017-11-15 09:15:06 +00:00
|
|
|
DeclarationName Name = Declaration->getDeclName();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-08-25 18:41:16 +00:00
|
|
|
// If the name is a simple identifier (by far the common case), or a
|
|
|
|
// zero-argument selector, just return a reference to that identifier.
|
|
|
|
if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
|
|
|
|
return Id->getName();
|
|
|
|
if (Name.isObjCZeroArgSelector())
|
2018-11-25 20:57:05 +00:00
|
|
|
if (IdentifierInfo *Id = Name.getObjCSelector().getIdentifierInfoForSlot(0))
|
2010-08-25 18:41:16 +00:00
|
|
|
return Id->getName();
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-08-25 18:41:16 +00:00
|
|
|
Saved = Name.getAsString();
|
|
|
|
return Saved;
|
|
|
|
}
|
2018-07-30 19:24:48 +00:00
|
|
|
|
|
|
|
bool clang::operator<(const CodeCompletionResult &X,
|
2010-08-25 18:41:16 +00:00
|
|
|
const CodeCompletionResult &Y) {
|
|
|
|
std::string XSaved, YSaved;
|
2017-11-15 09:15:06 +00:00
|
|
|
StringRef XStr = X.getOrderedName(XSaved);
|
|
|
|
StringRef YStr = Y.getOrderedName(YSaved);
|
2021-06-23 14:52:36 +03:00
|
|
|
int cmp = XStr.compare_insensitive(YStr);
|
2010-08-25 18:41:16 +00:00
|
|
|
if (cmp)
|
|
|
|
return cmp < 0;
|
2018-07-30 19:24:48 +00:00
|
|
|
|
2010-08-26 13:48:20 +00:00
|
|
|
// If case-insensitive comparison fails, try case-sensitive comparison.
|
2018-11-25 20:57:05 +00:00
|
|
|
return XStr.compare(YStr) < 0;
|
2010-08-25 18:41:16 +00:00
|
|
|
}
|