clang API Documentation

Classes | Typedefs | Enumerations | Functions
Code completion
libclang: C Interface to Clang
Collaboration diagram for Code completion:

Classes

struct  CXCompletionResult
 A single result of code completion. More...
struct  CXCodeCompleteResults
 Contains the results of code-completion. More...

Typedefs

typedef void * CXCompletionString
 A semantic string that describes a code-completion result.

Enumerations

enum  CXCompletionChunkKind {
  CXCompletionChunk_Optional, CXCompletionChunk_TypedText, CXCompletionChunk_Text, CXCompletionChunk_Placeholder,
  CXCompletionChunk_Informative, CXCompletionChunk_CurrentParameter, CXCompletionChunk_LeftParen, CXCompletionChunk_RightParen,
  CXCompletionChunk_LeftBracket, CXCompletionChunk_RightBracket, CXCompletionChunk_LeftBrace, CXCompletionChunk_RightBrace,
  CXCompletionChunk_LeftAngle, CXCompletionChunk_RightAngle, CXCompletionChunk_Comma, CXCompletionChunk_ResultType,
  CXCompletionChunk_Colon, CXCompletionChunk_SemiColon, CXCompletionChunk_Equal, CXCompletionChunk_HorizontalSpace,
  CXCompletionChunk_VerticalSpace
}
 Describes a single piece of text within a code-completion string. More...
enum  CXCodeComplete_Flags { CXCodeComplete_IncludeMacros = 0x01, CXCodeComplete_IncludeCodePatterns = 0x02, CXCodeComplete_IncludeBriefComments = 0x04 }
 Flags that can be passed to clang_codeCompleteAt() to modify its behavior. More...
enum  CXCompletionContext {
  CXCompletionContext_Unexposed = 0, CXCompletionContext_AnyType = 1 << 0, CXCompletionContext_AnyValue = 1 << 1, CXCompletionContext_ObjCObjectValue = 1 << 2,
  CXCompletionContext_ObjCSelectorValue = 1 << 3, CXCompletionContext_CXXClassTypeValue = 1 << 4, CXCompletionContext_DotMemberAccess = 1 << 5, CXCompletionContext_ArrowMemberAccess = 1 << 6,
  CXCompletionContext_ObjCPropertyAccess = 1 << 7, CXCompletionContext_EnumTag = 1 << 8, CXCompletionContext_UnionTag = 1 << 9, CXCompletionContext_StructTag = 1 << 10,
  CXCompletionContext_ClassTag = 1 << 11, CXCompletionContext_Namespace = 1 << 12, CXCompletionContext_NestedNameSpecifier = 1 << 13, CXCompletionContext_ObjCInterface = 1 << 14,
  CXCompletionContext_ObjCProtocol = 1 << 15, CXCompletionContext_ObjCCategory = 1 << 16, CXCompletionContext_ObjCInstanceMessage = 1 << 17, CXCompletionContext_ObjCClassMessage = 1 << 18,
  CXCompletionContext_ObjCSelectorName = 1 << 19, CXCompletionContext_MacroName = 1 << 20, CXCompletionContext_NaturalLanguage = 1 << 21, CXCompletionContext_Unknown = ((1 << 22) - 1)
}
 Bits that represent the context under which completion is occurring. More...

Functions

CINDEX_LINKAGE enum
CXCompletionChunkKind 
clang_getCompletionChunkKind (CXCompletionString completion_string, unsigned chunk_number)
 Determine the kind of a particular chunk within a completion string.
CINDEX_LINKAGE CXString clang_getCompletionChunkText (CXCompletionString completion_string, unsigned chunk_number)
 Retrieve the text associated with a particular chunk within a completion string.
CINDEX_LINKAGE CXCompletionString clang_getCompletionChunkCompletionString (CXCompletionString completion_string, unsigned chunk_number)
 Retrieve the completion string associated with a particular chunk within a completion string.
CINDEX_LINKAGE unsigned clang_getNumCompletionChunks (CXCompletionString completion_string)
 Retrieve the number of chunks in the given code-completion string.
CINDEX_LINKAGE unsigned clang_getCompletionPriority (CXCompletionString completion_string)
 Determine the priority of this code completion.
CINDEX_LINKAGE enum
CXAvailabilityKind 
clang_getCompletionAvailability (CXCompletionString completion_string)
 Determine the availability of the entity that this code-completion string refers to.
CINDEX_LINKAGE unsigned clang_getCompletionNumAnnotations (CXCompletionString completion_string)
 Retrieve the number of annotations associated with the given completion string.
CINDEX_LINKAGE CXString clang_getCompletionAnnotation (CXCompletionString completion_string, unsigned annotation_number)
 Retrieve the annotation associated with the given completion string.
CINDEX_LINKAGE CXString clang_getCompletionParent (CXCompletionString completion_string, enum CXCursorKind *kind)
 Retrieve the parent context of the given completion string.
CINDEX_LINKAGE CXString clang_getCompletionBriefComment (CXCompletionString completion_string)
 Retrieve the brief documentation comment attached to the declaration that corresponds to the given completion string.
CINDEX_LINKAGE CXCompletionString clang_getCursorCompletionString (CXCursor cursor)
 Retrieve a completion string for an arbitrary declaration or macro definition cursor.
CINDEX_LINKAGE unsigned clang_defaultCodeCompleteOptions (void)
 Returns a default set of code-completion options that can be passed toclang_codeCompleteAt().
CINDEX_LINKAGE
CXCodeCompleteResults
clang_codeCompleteAt (CXTranslationUnit TU, const char *complete_filename, unsigned complete_line, unsigned complete_column, struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, unsigned options)
 Perform code completion at a given location in a translation unit.
CINDEX_LINKAGE void clang_sortCodeCompletionResults (CXCompletionResult *Results, unsigned NumResults)
 Sort the code-completion results in case-insensitive alphabetical order.
CINDEX_LINKAGE void clang_disposeCodeCompleteResults (CXCodeCompleteResults *Results)
 Free the given set of code-completion results.
CINDEX_LINKAGE unsigned clang_codeCompleteGetNumDiagnostics (CXCodeCompleteResults *Results)
 Determine the number of diagnostics produced prior to the location where code completion was performed.
CINDEX_LINKAGE CXDiagnostic clang_codeCompleteGetDiagnostic (CXCodeCompleteResults *Results, unsigned Index)
 Retrieve a diagnostic associated with the given code completion.
CINDEX_LINKAGE unsigned long long clang_codeCompleteGetContexts (CXCodeCompleteResults *Results)
 Determines what completions are appropriate for the context the given code completion.
CINDEX_LINKAGE enum CXCursorKind clang_codeCompleteGetContainerKind (CXCodeCompleteResults *Results, unsigned *IsIncomplete)
 Returns the cursor kind for the container for the current code completion context. The container is only guaranteed to be set for contexts where a container exists (i.e. member accesses or Objective-C message sends); if there is not a container, this function will return CXCursor_InvalidCode.
CINDEX_LINKAGE CXString clang_codeCompleteGetContainerUSR (CXCodeCompleteResults *Results)
 Returns the USR for the container for the current code completion context. If there is not a container for the current context, this function will return the empty string.
CINDEX_LINKAGE CXString clang_codeCompleteGetObjCSelector (CXCodeCompleteResults *Results)
 Returns the currently-entered selector for an Objective-C message send, formatted like "initWithFoo:bar:". Only guaranteed to return a non-empty string for CXCompletionContext_ObjCInstanceMessage and CXCompletionContext_ObjCClassMessage.

Detailed Description

Code completion involves taking an (incomplete) source file, along with knowledge of where the user is actively editing that file, and suggesting syntactically- and semantically-valid constructs that the user might want to use at that particular point in the source code. These data structures and routines provide support for code completion.


Typedef Documentation

typedef void* CXCompletionString

A semantic string that describes a code-completion result.

A semantic string that describes the formatting of a code-completion result as a single "template" of text that should be inserted into the source buffer when a particular code-completion result is selected. Each semantic string is made up of some number of "chunks", each of which contains some text along with a description of what that text means, e.g., the name of the entity being referenced, whether the text chunk is part of the template, or whether it is a "placeholder" that the user should replace with actual code,of a specific kind. See CXCompletionChunkKind for a description of the different kinds of chunks.

Definition at line 4224 of file Index.h.


Enumeration Type Documentation

Flags that can be passed to clang_codeCompleteAt() to modify its behavior.

The enumerators in this enumeration can be bitwise-OR'd together to provide multiple options to clang_codeCompleteAt().

Enumerator:
CXCodeComplete_IncludeMacros 

Whether to include macros within the set of code completions returned.

CXCodeComplete_IncludeCodePatterns 

Whether to include code patterns for language constructs within the set of code completions, e.g., for loops.

CXCodeComplete_IncludeBriefComments 

Whether to include brief documentation within the set of code completions returned.

Definition at line 4587 of file Index.h.

Describes a single piece of text within a code-completion string.

Each "chunk" within a code-completion string (CXCompletionString) is either a piece of text with a specific "kind" that describes how that text should be interpreted by the client or is another completion string.

Enumerator:
CXCompletionChunk_Optional 

A code-completion string that describes "optional" text that could be a part of the template (but is not required).

The Optional chunk is the only kind of chunk that has a code-completion string for its representation, which is accessible via clang_getCompletionChunkCompletionString(). The code-completion string describes an additional part of the template that is completely optional. For example, optional chunks can be used to describe the placeholders for arguments that match up with defaulted function parameters, e.g. given:

 void f(int x, float y = 3.14, double z = 2.71828);

The code-completion string for this function would contain:

  • a TypedText chunk for "f".
  • a LeftParen chunk for "(".
  • a Placeholder chunk for "int x"
  • an Optional chunk containing the remaining defaulted arguments, e.g.,
    • a Comma chunk for ","
    • a Placeholder chunk for "float y"
    • an Optional chunk containing the last defaulted argument:
      • a Comma chunk for ","
      • a Placeholder chunk for "double z"
  • a RightParen chunk for ")"

There are many ways to handle Optional chunks. Two simple approaches are:

  • Completely ignore optional chunks, in which case the template for the function "f" would only include the first parameter ("int x").
  • Fully expand all optional chunks, in which case the template for the function "f" would have all of the parameters.
CXCompletionChunk_TypedText 

Text that a user would be expected to type to get this code-completion result.

There will be exactly one "typed text" chunk in a semantic string, which will typically provide the spelling of a keyword or the name of a declaration that could be used at the current code point. Clients are expected to filter the code-completion results based on the text in this chunk.

CXCompletionChunk_Text 

Text that should be inserted as part of a code-completion result.

A "text" chunk represents text that is part of the template to be inserted into user code should this particular code-completion result be selected.

CXCompletionChunk_Placeholder 

Placeholder text that should be replaced by the user.

A "placeholder" chunk marks a place where the user should insert text into the code-completion template. For example, placeholders might mark the function parameters for a function declaration, to indicate that the user should provide arguments for each of those parameters. The actual text in a placeholder is a suggestion for the text to display before the user replaces the placeholder with real code.

CXCompletionChunk_Informative 

Informative text that should be displayed but never inserted as part of the template.

An "informative" chunk contains annotations that can be displayed to help the user decide whether a particular code-completion result is the right option, but which is not part of the actual template to be inserted by code completion.

CXCompletionChunk_CurrentParameter 

Text that describes the current parameter when code-completion is referring to function call, message send, or template specialization.

A "current parameter" chunk occurs when code-completion is providing information about a parameter corresponding to the argument at the code-completion point. For example, given a function

 int add(int x, int y);

and the source code add(, where the code-completion point is after the "(", the code-completion string will contain a "current parameter" chunk for "int x", indicating that the current argument will initialize that parameter. After typing further, to add(17, (where the code-completion point is after the ","), the code-completion string will contain a "current paremeter" chunk to "int y".

CXCompletionChunk_LeftParen 

A left parenthesis ('('), used to initiate a function call or signal the beginning of a function parameter list.

CXCompletionChunk_RightParen 

A right parenthesis (')'), used to finish a function call or signal the end of a function parameter list.

CXCompletionChunk_LeftBracket 

A left bracket ('[').

CXCompletionChunk_RightBracket 

A right bracket (']').

CXCompletionChunk_LeftBrace 

A left brace ('{').

CXCompletionChunk_RightBrace 

A right brace ('}').

CXCompletionChunk_LeftAngle 

A left angle bracket ('<').

CXCompletionChunk_RightAngle 

A right angle bracket ('>').

CXCompletionChunk_Comma 

A comma separator (',').

CXCompletionChunk_ResultType 

Text that specifies the result type of a given result.

This special kind of informative chunk is not meant to be inserted into the text buffer. Rather, it is meant to illustrate the type that an expression using the given completion string would have.

CXCompletionChunk_Colon 

A colon (':').

CXCompletionChunk_SemiColon 

A semicolon (';').

CXCompletionChunk_Equal 

An '=' sign.

CXCompletionChunk_HorizontalSpace 

Horizontal space (' ').

CXCompletionChunk_VerticalSpace 

Vertical space ('
'), after which it is generally a good idea to perform indentation.

Definition at line 4256 of file Index.h.

Bits that represent the context under which completion is occurring.

The enumerators in this enumeration may be bitwise-OR'd together if multiple contexts are occurring simultaneously.

Enumerator:
CXCompletionContext_Unexposed 

The context for completions is unexposed, as only Clang results should be included. (This is equivalent to having no context bits set.)

CXCompletionContext_AnyType 

Completions for any possible type should be included in the results.

CXCompletionContext_AnyValue 

Completions for any possible value (variables, function calls, etc.) should be included in the results.

CXCompletionContext_ObjCObjectValue 

Completions for values that resolve to an Objective-C object should be included in the results.

CXCompletionContext_ObjCSelectorValue 

Completions for values that resolve to an Objective-C selector should be included in the results.

CXCompletionContext_CXXClassTypeValue 

Completions for values that resolve to a C++ class type should be included in the results.

CXCompletionContext_DotMemberAccess 

Completions for fields of the member being accessed using the dot operator should be included in the results.

CXCompletionContext_ArrowMemberAccess 

Completions for fields of the member being accessed using the arrow operator should be included in the results.

CXCompletionContext_ObjCPropertyAccess 

Completions for properties of the Objective-C object being accessed using the dot operator should be included in the results.

CXCompletionContext_EnumTag 

Completions for enum tags should be included in the results.

CXCompletionContext_UnionTag 

Completions for union tags should be included in the results.

CXCompletionContext_StructTag 

Completions for struct tags should be included in the results.

CXCompletionContext_ClassTag 

Completions for C++ class names should be included in the results.

CXCompletionContext_Namespace 

Completions for C++ namespaces and namespace aliases should be included in the results.

CXCompletionContext_NestedNameSpecifier 

Completions for C++ nested name specifiers should be included in the results.

CXCompletionContext_ObjCInterface 

Completions for Objective-C interfaces (classes) should be included in the results.

CXCompletionContext_ObjCProtocol 

Completions for Objective-C protocols should be included in the results.

CXCompletionContext_ObjCCategory 

Completions for Objective-C categories should be included in the results.

CXCompletionContext_ObjCInstanceMessage 

Completions for Objective-C instance messages should be included in the results.

CXCompletionContext_ObjCClassMessage 

Completions for Objective-C class messages should be included in the results.

CXCompletionContext_ObjCSelectorName 

Completions for Objective-C selector names should be included in the results.

CXCompletionContext_MacroName 

Completions for preprocessor macro names should be included in the results.

CXCompletionContext_NaturalLanguage 

Natural language completions should be included in the results.

CXCompletionContext_Unknown 

The current context is unknown, so set all contexts.

Definition at line 4613 of file Index.h.


Function Documentation

CINDEX_LINKAGE CXCodeCompleteResults* clang_codeCompleteAt ( CXTranslationUnit  TU,
const char *  complete_filename,
unsigned  complete_line,
unsigned  complete_column,
struct CXUnsavedFile unsaved_files,
unsigned  num_unsaved_files,
unsigned  options 
)

Perform code completion at a given location in a translation unit.

This function performs code completion at a particular file, line, and column within source code, providing results that suggest potential code snippets based on the context of the completion. The basic model for code completion is that Clang will parse a complete source file, performing syntax checking up to the location where code-completion has been requested. At that point, a special code-completion token is passed to the parser, which recognizes this token and determines, based on the current location in the C/Objective-C/C++ grammar and the state of semantic analysis, what completions to provide. These completions are returned via a new CXCodeCompleteResults structure.

Code completion itself is meant to be triggered by the client when the user types punctuation characters or whitespace, at which point the code-completion location will coincide with the cursor. For example, if p is a pointer, code-completion might be triggered after the "-" and then after the ">" in p->. When the code-completion location is afer the ">", the completion results will provide, e.g., the members of the struct that "p" points to. The client is responsible for placing the cursor at the beginning of the token currently being typed, then filtering the results based on the contents of the token. For example, when code-completing for the expression p->get, the client should provide the location just after the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the client can filter the results based on the current token text ("get"), only showing those results that start with "get". The intent of this interface is to separate the relatively high-latency acquisition of code-completion results from the filtering of results on a per-character basis, which must have a lower latency.

Parameters:
TUThe translation unit in which code-completion should occur. The source files for this translation unit need not be completely up-to-date (and the contents of those source files may be overridden via unsaved_files). Cursors referring into the translation unit may be invalidated by this invocation.
complete_filenameThe name of the source file where code completion should be performed. This filename may be any file included in the translation unit.
complete_lineThe line at which code-completion should occur.
complete_columnThe column at which code-completion should occur. Note that the column should point just after the syntactic construct that initiated code completion, and not in the middle of a lexical token.
unsaved_filesthe Tiles that have not yet been saved to disk but may be required for parsing or code completion, including the contents of those files. The contents and name of these files (as specified by CXUnsavedFile) are copied when necessary, so the client only needs to guarantee their validity until the call to this function returns.
num_unsaved_filesThe number of unsaved file entries in unsaved_files.
optionsExtra options that control the behavior of code completion, expressed as a bitwise OR of the enumerators of the CXCodeComplete_Flags enumeration. The clang_defaultCodeCompleteOptions() function returns a default set of code-completion options.
Returns:
If successful, a new CXCodeCompleteResults structure containing code-completion results, which should eventually be freed with clang_disposeCodeCompleteResults(). If code completion fails, returns NULL.

Returns the cursor kind for the container for the current code completion context. The container is only guaranteed to be set for contexts where a container exists (i.e. member accesses or Objective-C message sends); if there is not a container, this function will return CXCursor_InvalidCode.

Parameters:
Resultsthe code completion results to query
IsIncompleteon return, this value will be false if Clang has complete information about the container. If Clang does not have complete information, this value will be true.
Returns:
the container kind, or CXCursor_InvalidCode if there is not a container

Returns the USR for the container for the current code completion context. If there is not a container for the current context, this function will return the empty string.

Parameters:
Resultsthe code completion results to query
Returns:
the USR for the container

Determines what completions are appropriate for the context the given code completion.

Parameters:
Resultsthe code completion results to query
Returns:
the kinds of completions that are appropriate for use along with the given code completion results.

Retrieve a diagnostic associated with the given code completion.

Parameters:
Resultsthe code completion results to query.
Indexthe zero-based diagnostic number to retrieve.
Returns:
the requested diagnostic. This diagnostic must be freed via a call to clang_disposeDiagnostic().

Determine the number of diagnostics produced prior to the location where code completion was performed.

Returns the currently-entered selector for an Objective-C message send, formatted like "initWithFoo:bar:". Only guaranteed to return a non-empty string for CXCompletionContext_ObjCInstanceMessage and CXCompletionContext_ObjCClassMessage.

Parameters:
Resultsthe code completion results to query
Returns:
the selector (or partial selector) that has been entered thus far for an Objective-C message send.

Returns a default set of code-completion options that can be passed toclang_codeCompleteAt().

Free the given set of code-completion results.

CINDEX_LINKAGE CXString clang_getCompletionAnnotation ( CXCompletionString  completion_string,
unsigned  annotation_number 
)

Retrieve the annotation associated with the given completion string.

Parameters:
completion_stringthe completion string to query.
annotation_numberthe 0-based index of the annotation of the completion string.
Returns:
annotation string associated with the completion at index annotation_number, or a NULL string if that annotation is not available.

Determine the availability of the entity that this code-completion string refers to.

Parameters:
completion_stringThe completion string to query.
Returns:
The availability of the completion string.

Retrieve the brief documentation comment attached to the declaration that corresponds to the given completion string.

Retrieve the completion string associated with a particular chunk within a completion string.

Parameters:
completion_stringthe completion string to query.
chunk_numberthe 0-based index of the chunk in the completion string.
Returns:
the completion string associated with the chunk at index chunk_number.

Determine the kind of a particular chunk within a completion string.

Parameters:
completion_stringthe completion string to query.
chunk_numberthe 0-based index of the chunk in the completion string.
Returns:
the kind of the chunk at the index chunk_number.

Retrieve the text associated with a particular chunk within a completion string.

Parameters:
completion_stringthe completion string to query.
chunk_numberthe 0-based index of the chunk in the completion string.
Returns:
the text associated with the chunk at index chunk_number.

Retrieve the number of annotations associated with the given completion string.

Parameters:
completion_stringthe completion string to query.
Returns:
the number of annotations associated with the given completion string.

Retrieve the parent context of the given completion string.

The parent context of a completion string is the semantic parent of the declaration (if any) that the code completion represents. For example, a code completion for an Objective-C method would have the method's class or protocol as its context.

Parameters:
completion_stringThe code completion string whose parent is being queried.
kindDEPRECATED: always set to CXCursor_NotImplemented if non-NULL.
Returns:
The name of the completion parent, e.g., "NSObject" if the completion string represents a method in the NSObject class.

Determine the priority of this code completion.

The priority of a code completion indicates how likely it is that this particular completion is the completion that the user will select. The priority is selected by various internal heuristics.

Parameters:
completion_stringThe completion string to query.
Returns:
The priority of this completion string. Smaller values indicate higher-priority (more likely) completions.

Retrieve a completion string for an arbitrary declaration or macro definition cursor.

Parameters:
cursorThe cursor to query.
Returns:
A non-context-sensitive completion string for declaration and macro definition cursors, or NULL for other kinds of cursors.

Retrieve the number of chunks in the given code-completion string.

Sort the code-completion results in case-insensitive alphabetical order.

Parameters:
ResultsThe set of results to sort.
NumResultsThe number of results in Results.