clang API Documentation

Classes | Typedefs | Enumerations | Functions
Translation unit manipulation
libclang: C Interface to Clang
Collaboration diagram for Translation unit manipulation:

Classes

struct  CXTUResourceUsageEntry
struct  CXTUResourceUsage
 The memory usage of a CXTranslationUnit, broken into categories. More...

Typedefs

typedef struct
CXTUResourceUsageEntry 
CXTUResourceUsageEntry
typedef struct CXTUResourceUsage CXTUResourceUsage
 The memory usage of a CXTranslationUnit, broken into categories.

Enumerations

enum  CXTranslationUnit_Flags {
  CXTranslationUnit_None = 0x0, CXTranslationUnit_DetailedPreprocessingRecord = 0x01, CXTranslationUnit_Incomplete = 0x02, CXTranslationUnit_PrecompiledPreamble = 0x04,
  CXTranslationUnit_CacheCompletionResults = 0x08, CXTranslationUnit_ForSerialization = 0x10, CXTranslationUnit_CXXChainedPCH = 0x20, CXTranslationUnit_SkipFunctionBodies = 0x40,
  CXTranslationUnit_IncludeBriefCommentsInCodeCompletion = 0x80
}
 Flags that control the creation of translation units. More...
enum  CXSaveTranslationUnit_Flags { CXSaveTranslationUnit_None = 0x0 }
 Flags that control how translation units are saved. More...
enum  CXSaveError { CXSaveError_None = 0, CXSaveError_Unknown = 1, CXSaveError_TranslationErrors = 2, CXSaveError_InvalidTU = 3 }
 Describes the kind of error that occurred (if any) in a call to clang_saveTranslationUnit(). More...
enum  CXReparse_Flags { CXReparse_None = 0x0 }
 Flags that control the reparsing of translation units. More...
enum  CXTUResourceUsageKind {
  CXTUResourceUsage_AST = 1, CXTUResourceUsage_Identifiers = 2, CXTUResourceUsage_Selectors = 3, CXTUResourceUsage_GlobalCompletionResults = 4,
  CXTUResourceUsage_SourceManagerContentCache = 5, CXTUResourceUsage_AST_SideTables = 6, CXTUResourceUsage_SourceManager_Membuffer_Malloc = 7, CXTUResourceUsage_SourceManager_Membuffer_MMap = 8,
  CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc = 9, CXTUResourceUsage_ExternalASTSource_Membuffer_MMap = 10, CXTUResourceUsage_Preprocessor = 11, CXTUResourceUsage_PreprocessingRecord = 12,
  CXTUResourceUsage_SourceManager_DataStructures = 13, CXTUResourceUsage_Preprocessor_HeaderSearch = 14, CXTUResourceUsage_MEMORY_IN_BYTES_BEGIN = CXTUResourceUsage_AST, CXTUResourceUsage_MEMORY_IN_BYTES_END,
  CXTUResourceUsage_First = CXTUResourceUsage_AST, CXTUResourceUsage_Last = CXTUResourceUsage_Preprocessor_HeaderSearch
}
 Categorizes how memory is being used by a translation unit. More...

Functions

CINDEX_LINKAGE CXString clang_getTranslationUnitSpelling (CXTranslationUnit CTUnit)
 Get the original translation unit source file name.
CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile (CXIndex CIdx, const char *source_filename, int num_clang_command_line_args, const char *const *clang_command_line_args, unsigned num_unsaved_files, struct CXUnsavedFile *unsaved_files)
 Return the CXTranslationUnit for a given source file and the provided command line arguments one would pass to the compiler.
CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit (CXIndex CIdx, const char *ast_filename)
 Same as clang_createTranslationUnit2, but returns the CXTranslationUnit instead of an error code. In case of an error this routine returns a NULL CXTranslationUnit, without further detailed error codes.
CINDEX_LINKAGE enum CXErrorCode clang_createTranslationUnit2 (CXIndex CIdx, const char *ast_filename, CXTranslationUnit *out_TU)
 Create a translation unit from an AST file (-emit-ast).
CINDEX_LINKAGE unsigned clang_defaultEditingTranslationUnitOptions (void)
 Returns the set of flags that is suitable for parsing a translation unit that is being edited.
CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit (CXIndex CIdx, const char *source_filename, const char *const *command_line_args, int num_command_line_args, struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, unsigned options)
 Same as clang_parseTranslationUnit2, but returns the CXTranslationUnit instead of an error code. In case of an error this routine returns a NULL CXTranslationUnit, without further detailed error codes.
CINDEX_LINKAGE enum CXErrorCode clang_parseTranslationUnit2 (CXIndex CIdx, const char *source_filename, const char *const *command_line_args, int num_command_line_args, struct CXUnsavedFile *unsaved_files, unsigned num_unsaved_files, unsigned options, CXTranslationUnit *out_TU)
 Parse the given source file and the translation unit corresponding to that file.
CINDEX_LINKAGE unsigned clang_defaultSaveOptions (CXTranslationUnit TU)
 Returns the set of flags that is suitable for saving a translation unit.
CINDEX_LINKAGE int clang_saveTranslationUnit (CXTranslationUnit TU, const char *FileName, unsigned options)
 Saves a translation unit into a serialized representation of that translation unit on disk.
CINDEX_LINKAGE void clang_disposeTranslationUnit (CXTranslationUnit)
 Destroy the specified CXTranslationUnit object.
CINDEX_LINKAGE unsigned clang_defaultReparseOptions (CXTranslationUnit TU)
 Returns the set of flags that is suitable for reparsing a translation unit.
CINDEX_LINKAGE int clang_reparseTranslationUnit (CXTranslationUnit TU, unsigned num_unsaved_files, struct CXUnsavedFile *unsaved_files, unsigned options)
 Reparse the source files that produced this translation unit.
CINDEX_LINKAGE const char * clang_getTUResourceUsageName (enum CXTUResourceUsageKind kind)
 Returns the human-readable null-terminated C string that represents the name of the memory category. This string should never be freed.
CINDEX_LINKAGE CXTUResourceUsage clang_getCXTUResourceUsage (CXTranslationUnit TU)
 Return the memory usage of a translation unit. This object should be released with clang_disposeCXTUResourceUsage().
CINDEX_LINKAGE void clang_disposeCXTUResourceUsage (CXTUResourceUsage usage)

Detailed Description

The routines in this group provide the ability to create and destroy translation units from files, either by parsing the contents of the files or by reading in a serialized representation of a translation unit.


Typedef Documentation

The memory usage of a CXTranslationUnit, broken into categories.


Enumeration Type Documentation

Flags that control the reparsing of translation units.

The enumerators in this enumeration type are meant to be bitwise ORed together to specify which options should be used when reparsing the translation unit.

Enumerator:
CXReparse_None 

Used to indicate that no special reparsing options are needed.

Definition at line 1390 of file Index.h.

Describes the kind of error that occurred (if any) in a call to clang_saveTranslationUnit().

Enumerator:
CXSaveError_None 

Indicates that no error occurred while saving a translation unit.

CXSaveError_Unknown 

Indicates that an unknown error occurred while attempting to save the file.

This error typically indicates that file I/O failed when attempting to write the file.

CXSaveError_TranslationErrors 

Indicates that errors during translation prevented this attempt to save the translation unit.

Errors that prevent the translation unit from being saved can be extracted using clang_getNumDiagnostics() and clang_getDiagnostic().

CXSaveError_InvalidTU 

Indicates that the translation unit to be saved was somehow invalid (e.g., NULL).

Definition at line 1320 of file Index.h.

Flags that control how translation units are saved.

The enumerators in this enumeration type are meant to be bitwise ORed together to specify which options should be used when saving the translation unit.

Enumerator:
CXSaveTranslationUnit_None 

Used to indicate that no special saving options are needed.

Definition at line 1298 of file Index.h.

Flags that control the creation of translation units.

The enumerators in this enumeration type are meant to be bitwise ORed together to specify which options should be used when constructing the translation unit.

Enumerator:
CXTranslationUnit_None 

Used to indicate that no special translation-unit options are needed.

CXTranslationUnit_DetailedPreprocessingRecord 

Used to indicate that the parser should construct a "detailed" preprocessing record, including all macro definitions and instantiations.

Constructing a detailed preprocessing record requires more memory and time to parse, since the information contained in the record is usually not retained. However, it can be useful for applications that require more detailed information about the behavior of the preprocessor.

CXTranslationUnit_Incomplete 

Used to indicate that the translation unit is incomplete.

When a translation unit is considered "incomplete", semantic analysis that is typically performed at the end of the translation unit will be suppressed. For example, this suppresses the completion of tentative declarations in C and of instantiation of implicitly-instantiation function templates in C++. This option is typically used when parsing a header with the intent of producing a precompiled header.

CXTranslationUnit_PrecompiledPreamble 

Used to indicate that the translation unit should be built with an implicit precompiled header for the preamble.

An implicit precompiled header is used as an optimization when a particular translation unit is likely to be reparsed many times when the sources aren't changing that often. In this case, an implicit precompiled header will be built containing all of the initial includes at the top of the main file (what we refer to as the "preamble" of the file). In subsequent parses, if the preamble or the files in it have not changed, clang_reparseTranslationUnit() will re-use the implicit precompiled header to improve parsing performance.

CXTranslationUnit_CacheCompletionResults 

Used to indicate that the translation unit should cache some code-completion results with each reparse of the source file.

Caching of code-completion results is a performance optimization that introduces some overhead to reparsing but improves the performance of code-completion operations.

CXTranslationUnit_ForSerialization 

Used to indicate that the translation unit will be serialized with clang_saveTranslationUnit.

This option is typically used when parsing a header with the intent of producing a precompiled header.

CXTranslationUnit_CXXChainedPCH 

DEPRECATED: Enabled chained precompiled preambles in C++.

Note: this is a *temporary* option that is available only while we are testing C++ precompiled preamble support. It is deprecated.

CXTranslationUnit_SkipFunctionBodies 

Used to indicate that function/method bodies should be skipped while parsing.

This option can be used to search for declarations/definitions while ignoring the usages.

CXTranslationUnit_IncludeBriefCommentsInCodeCompletion 

Used to indicate that brief documentation comments should be included into the set of code completions returned from this translation unit.

Definition at line 1116 of file Index.h.

Categorizes how memory is being used by a translation unit.

Enumerator:
CXTUResourceUsage_AST 
CXTUResourceUsage_Identifiers 
CXTUResourceUsage_Selectors 
CXTUResourceUsage_GlobalCompletionResults 
CXTUResourceUsage_SourceManagerContentCache 
CXTUResourceUsage_AST_SideTables 
CXTUResourceUsage_SourceManager_Membuffer_Malloc 
CXTUResourceUsage_SourceManager_Membuffer_MMap 
CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc 
CXTUResourceUsage_ExternalASTSource_Membuffer_MMap 
CXTUResourceUsage_Preprocessor 
CXTUResourceUsage_PreprocessingRecord 
CXTUResourceUsage_SourceManager_DataStructures 
CXTUResourceUsage_Preprocessor_HeaderSearch 
CXTUResourceUsage_MEMORY_IN_BYTES_BEGIN 
CXTUResourceUsage_MEMORY_IN_BYTES_END 
CXTUResourceUsage_First 
CXTUResourceUsage_Last 

Definition at line 1456 of file Index.h.


Function Documentation

CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit ( CXIndex  CIdx,
const char *  ast_filename 
)

Same as clang_createTranslationUnit2, but returns the CXTranslationUnit instead of an error code. In case of an error this routine returns a NULL CXTranslationUnit, without further detailed error codes.

CINDEX_LINKAGE enum CXErrorCode clang_createTranslationUnit2 ( CXIndex  CIdx,
const char *  ast_filename,
CXTranslationUnit out_TU 
)

Create a translation unit from an AST file (-emit-ast).

Parameters:
[out]out_TUA non-NULL pointer to store the created CXTranslationUnit.
Returns:
Zero on success, otherwise returns an error code.
CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile ( CXIndex  CIdx,
const char *  source_filename,
int  num_clang_command_line_args,
const char *const *  clang_command_line_args,
unsigned  num_unsaved_files,
struct CXUnsavedFile unsaved_files 
)

Return the CXTranslationUnit for a given source file and the provided command line arguments one would pass to the compiler.

Note: The 'source_filename' argument is optional. If the caller provides a NULL pointer, the name of the source file is expected to reside in the specified command line arguments.

Note: When encountered in 'clang_command_line_args', the following options are ignored:

'-c' '-emit-ast' '-fsyntax-only' '-o <output file>' (both '-o' and '<output file>' are ignored)

Parameters:
CIdxThe index object with which the translation unit will be associated.
source_filenameThe name of the source file to load, or NULL if the source file is included in clang_command_line_args.
num_clang_command_line_argsThe number of command-line arguments in clang_command_line_args.
clang_command_line_argsThe command-line arguments that would be passed to the clang executable if it were being invoked out-of-process. These command-line options will be parsed and will affect how the translation unit is parsed. Note that the following options are ignored: '-c', '-emit-ast', '-fsyntax-only' (which is the default), and '-o <output file>'.
num_unsaved_filesthe number of unsaved file entries in unsaved_files.
unsaved_filesthe files that have not yet been saved to disk but may be required for 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.

Returns the set of flags that is suitable for parsing a translation unit that is being edited.

The set of flags returned provide options for clang_parseTranslationUnit() to indicate that the translation unit is likely to be reparsed many times, either explicitly (via clang_reparseTranslationUnit()) or implicitly (e.g., by code completion (clang_codeCompletionAt())). The returned flag set contains an unspecified set of optimizations (e.g., the precompiled preamble) geared toward improving the performance of these routines. The set of optimizations enabled may change from one version to the next.

Returns the set of flags that is suitable for reparsing a translation unit.

The set of flags returned provide options for clang_reparseTranslationUnit() by default. The returned flag set contains an unspecified set of optimizations geared toward common uses of reparsing. The set of optimizations enabled may change from one version to the next.

Returns the set of flags that is suitable for saving a translation unit.

The set of flags returned provide options for clang_saveTranslationUnit() by default. The returned flag set contains an unspecified set of options that save translation units with the most commonly-requested data.

Destroy the specified CXTranslationUnit object.

Return the memory usage of a translation unit. This object should be released with clang_disposeCXTUResourceUsage().

Get the original translation unit source file name.

Returns the human-readable null-terminated C string that represents the name of the memory category. This string should never be freed.

CINDEX_LINKAGE CXTranslationUnit clang_parseTranslationUnit ( CXIndex  CIdx,
const char *  source_filename,
const char *const *  command_line_args,
int  num_command_line_args,
struct CXUnsavedFile unsaved_files,
unsigned  num_unsaved_files,
unsigned  options 
)

Same as clang_parseTranslationUnit2, but returns the CXTranslationUnit instead of an error code. In case of an error this routine returns a NULL CXTranslationUnit, without further detailed error codes.

CINDEX_LINKAGE enum CXErrorCode clang_parseTranslationUnit2 ( CXIndex  CIdx,
const char *  source_filename,
const char *const *  command_line_args,
int  num_command_line_args,
struct CXUnsavedFile unsaved_files,
unsigned  num_unsaved_files,
unsigned  options,
CXTranslationUnit out_TU 
)

Parse the given source file and the translation unit corresponding to that file.

This routine is the main entry point for the Clang C API, providing the ability to parse a source file into a translation unit that can then be queried by other functions in the API. This routine accepts a set of command-line arguments so that the compilation can be configured in the same way that the compiler is configured on the command line.

Parameters:
CIdxThe index object with which the translation unit will be associated.
source_filenameThe name of the source file to load, or NULL if the source file is included in command_line_args.
command_line_argsThe command-line arguments that would be passed to the clang executable if it were being invoked out-of-process. These command-line options will be parsed and will affect how the translation unit is parsed. Note that the following options are ignored: '-c', '-emit-ast', '-fsyntax-only' (which is the default), and '-o <output file>'.
num_command_line_argsThe number of command-line arguments in command_line_args.
unsaved_filesthe files that have not yet been saved to disk but may be required for parsing, 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.
optionsA bitmask of options that affects how the translation unit is managed but not its compilation. This should be a bitwise OR of the CXTranslationUnit_XXX flags.
[out]out_TUA non-NULL pointer to store the created CXTranslationUnit, describing the parsed code and containing any diagnostics produced by the compiler.
Returns:
Zero on success, otherwise returns an error code.
CINDEX_LINKAGE int clang_reparseTranslationUnit ( CXTranslationUnit  TU,
unsigned  num_unsaved_files,
struct CXUnsavedFile unsaved_files,
unsigned  options 
)

Reparse the source files that produced this translation unit.

This routine can be used to re-parse the source files that originally created the given translation unit, for example because those source files have changed (either on disk or as passed via unsaved_files). The source code will be reparsed with the same command-line options as it was originally parsed.

Reparsing a translation unit invalidates all cursors and source locations that refer into that translation unit. This makes reparsing a translation unit semantically equivalent to destroying the translation unit and then creating a new translation unit with the same command-line arguments. However, it may be more efficient to reparse a translation unit using this routine.

Parameters:
TUThe translation unit whose contents will be re-parsed. The translation unit must originally have been built with clang_createTranslationUnitFromSourceFile().
num_unsaved_filesThe number of unsaved file entries in unsaved_files.
unsaved_filesThe files that have not yet been saved to disk but may be required for parsing, 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.
optionsA bitset of options composed of the flags in CXReparse_Flags. The function clang_defaultReparseOptions() produces a default set of options recommended for most uses, based on the translation unit.
Returns:
0 if the sources could be reparsed. A non-zero error code will be returned if reparsing was impossible, such that the translation unit is invalid. In such cases, the only valid call for TU is clang_disposeTranslationUnit(TU). The error codes returned by this routine are described by the CXErrorCode enum.
CINDEX_LINKAGE int clang_saveTranslationUnit ( CXTranslationUnit  TU,
const char *  FileName,
unsigned  options 
)

Saves a translation unit into a serialized representation of that translation unit on disk.

Any translation unit that was parsed without error can be saved into a file. The translation unit can then be deserialized into a new CXTranslationUnit with clang_createTranslationUnit() or, if it is an incomplete translation unit that corresponds to a header, used as a precompiled header when parsing other translation units.

Parameters:
TUThe translation unit to save.
FileNameThe file to which the translation unit will be saved.
optionsA bitmask of options that affects how the translation unit is saved. This should be a bitwise OR of the CXSaveTranslationUnit_XXX flags.
Returns:
A value that will match one of the enumerators of the CXSaveError enumeration. Zero (CXSaveError_None) indicates that the translation unit was saved successfully, while a non-zero value indicates that a problem occurred.