Scarab  v2.9.0
Project 8 C++ Utility Library
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
TranslationUnit Class Reference
Inheritance diagram for TranslationUnit:
Inheritance graph

Public Member Functions

def from_source (cls, filename, args=None, unsaved_files=None, options=0, index=None)
 
def from_ast_file (cls, filename, index=None)
 
def __init__ (self, ptr, index)
 
def __del__ (self)
 
def cursor (self)
 
def spelling (self)
 
def get_includes (self)
 
def get_file (self, filename)
 
def get_location (self, filename, position)
 
def get_extent (self, filename, locations)
 
def diagnostics (self)
 
def reparse (self, unsaved_files=None, options=0)
 
def save (self, filename)
 
def codeComplete (self, path, line, column, unsaved_files=None, include_macros=False, include_code_patterns=False, include_brief_comments=False)
 
def get_tokens (self, locations=None, extent=None)
 
- Public Member Functions inherited from ClangObject
def __init__ (self, obj)
 
def from_param (self)
 

Public Attributes

 index
 
 tu
 
- Public Attributes inherited from ClangObject
 obj
 

Static Public Attributes

int PARSE_NONE = 0
 
int PARSE_DETAILED_PROCESSING_RECORD = 1
 
int PARSE_INCOMPLETE = 2
 
int PARSE_PRECOMPILED_PREAMBLE = 4
 
int PARSE_CACHE_COMPLETION_RESULTS = 8
 
int PARSE_SKIP_FUNCTION_BODIES = 64
 
int PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION = 128
 

Detailed Description

Represents a source code translation unit.

This is one of the main types in the API. Any time you wish to interact
with Clang's representation of a source file, you typically start with a
translation unit.

Definition at line 2421 of file cindex.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  ptr,
  index 
)
Create a TranslationUnit instance.

TranslationUnits should be created using one of the from_* @classmethod
functions above. __init__ is only called internally.

Definition at line 2562 of file cindex.py.

◆ __del__()

def __del__ (   self)

Definition at line 2572 of file cindex.py.

Member Function Documentation

◆ codeComplete()

def codeComplete (   self,
  path,
  line,
  column,
  unsaved_files = None,
  include_macros = False,
  include_code_patterns = False,
  include_brief_comments = False 
)
Code complete in this translation unit.

In-memory contents for files can be provided by passing a list of pairs
as unsaved_files, the first items should be the filenames to be mapped
and the second should be the contents to be substituted for the
file. The contents may be passed as strings or file objects.

Definition at line 2739 of file cindex.py.

◆ cursor()

def cursor (   self)
Retrieve the cursor that represents the given translation unit.

Definition at line 2576 of file cindex.py.

◆ diagnostics()

def diagnostics (   self)
Return an iterable (and indexable) object containing the diagnostics.

Definition at line 2667 of file cindex.py.

◆ from_ast_file()

def from_ast_file (   cls,
  filename,
  index = None 
)
Create a TranslationUnit instance from a saved AST file.

A previously-saved AST file (provided with -emit-ast or
TranslationUnit.save()) is loaded from the filename specified.

If the file cannot be loaded, a TranslationUnitLoadError will be
raised.

index is optional and is the Index instance to use. If not provided,
a default Index will be created.

Definition at line 2541 of file cindex.py.

◆ from_source()

def from_source (   cls,
  filename,
  args = None,
  unsaved_files = None,
  options = 0,
  index = None 
)
Create a TranslationUnit by parsing source.

This is capable of processing source code both from files on the
filesystem as well as in-memory contents.

Command-line arguments that would be passed to clang are specified as
a list via args. These can be used to specify include paths, warnings,
etc. e.g. ["-Wall", "-I/path/to/include"].

In-memory file content can be provided via unsaved_files. This is an
iterable of 2-tuples. The first element is the str filename. The
second element defines the content. Content can be provided as str
source code or as file objects (anything with a read() method). If
a file object is being used, content will be read until EOF and the
read cursor will not be reset to its original position.

options is a bitwise or of TranslationUnit.PARSE_XXX flags which will
control parsing behavior.

index is an Index instance to utilize. If not provided, a new Index
will be created for this TranslationUnit.

To parse source from the filesystem, the filename of the file to parse
is specified by the filename argument. Or, filename could be None and
the args list would contain the filename(s) to parse.

To parse source from an in-memory buffer, set filename to the virtual
filename you wish to associate with this source (e.g. "test.c"). The
contents of that file are then provided in unsaved_files.

If an error occurs, a TranslationUnitLoadError is raised.

Please note that a TranslationUnit with parser errors may be returned.
It is the caller's responsibility to check tu.diagnostics for errors.

Also note that Clang infers the source language from the extension of
the input filename. If you pass in source code containing a C++ class
declaration with the filename "test.c" parsing will fail.

Definition at line 2462 of file cindex.py.

◆ get_extent()

def get_extent (   self,
  filename,
  locations 
)
Obtain a SourceRange from this translation unit.

The bounds of the SourceRange must ultimately be defined by a start and
end SourceLocation. For the locations argument, you can pass:

  - 2 SourceLocation instances in a 2-tuple or list.
  - 2 int file offsets via a 2-tuple or list.
  - 2 2-tuple or lists of (line, column) pairs in a 2-tuple or list.

e.g.

get_extent('foo.c', (5, 10))
get_extent('foo.c', ((1, 1), (1, 15)))

Definition at line 2626 of file cindex.py.

◆ get_file()

def get_file (   self,
  filename 
)
Obtain a File from this translation unit.

Definition at line 2605 of file cindex.py.

◆ get_includes()

def get_includes (   self)
Return an iterable sequence of FileInclusion objects that describe the
sequence of inclusions in a translation unit. The first object in
this sequence is always the input file. Note that this method will not
recursively iterate over header files included through precompiled
headers.

Definition at line 2585 of file cindex.py.

◆ get_location()

def get_location (   self,
  filename,
  position 
)
Obtain a SourceLocation for a file in this translation unit.

The position can be specified by passing:

  - Integer file offset. Initial file offset is 0.
  - 2-tuple of (line number, column number). Initial file position is
    (0, 0)

Definition at line 2610 of file cindex.py.

◆ get_tokens()

def get_tokens (   self,
  locations = None,
  extent = None 
)
Obtain tokens in this translation unit.

This is a generator for Token instances. The caller specifies a range
of source code to obtain tokens for. The range can be specified as a
2-tuple of SourceLocation or as a SourceRange. If both are defined,
behavior is undefined.

Definition at line 2782 of file cindex.py.

◆ reparse()

def reparse (   self,
  unsaved_files = None,
  options = 0 
)
Reparse an already parsed translation unit.

In-memory contents for files can be provided by passing a list of pairs
as unsaved_files, the first items should be the filenames to be mapped
and the second should be the contents to be substituted for the
file. The contents may be passed as strings or file objects.

Definition at line 2686 of file cindex.py.

◆ save()

def save (   self,
  filename 
)
Saves the TranslationUnit to a file.

This is equivalent to passing -emit-ast to the clang frontend. The
saved file can be loaded back into a TranslationUnit. Or, if it
corresponds to a header, it can be used as a pre-compiled header file.

If an error occurs while saving, a TranslationUnitSaveError is raised.
If the error was TranslationUnitSaveError.ERROR_INVALID_TU, this means
the constructed TranslationUnit was not valid at time of save. In this
case, the reason(s) why should be available via
TranslationUnit.diagnostics().

filename -- The path to save the translation unit to.

Definition at line 2715 of file cindex.py.

◆ spelling()

def spelling (   self)
Get the original translation unit source file name.

Definition at line 2581 of file cindex.py.

Member Data Documentation

◆ index

index

Definition at line 2569 of file cindex.py.

◆ PARSE_CACHE_COMPLETION_RESULTS

int PARSE_CACHE_COMPLETION_RESULTS = 8
static

Definition at line 2448 of file cindex.py.

◆ PARSE_DETAILED_PROCESSING_RECORD

int PARSE_DETAILED_PROCESSING_RECORD = 1
static

Definition at line 2434 of file cindex.py.

◆ PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION

int PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION = 128
static

Definition at line 2458 of file cindex.py.

◆ PARSE_INCOMPLETE

int PARSE_INCOMPLETE = 2
static

Definition at line 2438 of file cindex.py.

◆ PARSE_NONE

int PARSE_NONE = 0
static

Definition at line 2430 of file cindex.py.

◆ PARSE_PRECOMPILED_PREAMBLE

int PARSE_PRECOMPILED_PREAMBLE = 4
static

Definition at line 2444 of file cindex.py.

◆ PARSE_SKIP_FUNCTION_BODIES

int PARSE_SKIP_FUNCTION_BODIES = 64
static

Definition at line 2454 of file cindex.py.

◆ tu

tu

Definition at line 2673 of file cindex.py.


The documentation for this class was generated from the following file: