TranslationUnit

public class TranslationUnit

Undocumented

  • Creates a TranslationUnit by parsing the file at the specified path, passing optional command line arguments and options to clang.

    Throws

    ClangError if the translation unit could not be created successfully.

    Declaration

    Swift

    public init(filename: String,
                index: Index = Index(),
                commandLineArgs args: [String] = [],
                options: TranslationUnitOptions = []) throws

    Parameters

    filename

    The path you’re going to parse

    index

    The index (optional, will use a default index if not provided)

    args

    Optional command-line arguments to pass to clang

    options

    Options for how to handle the parsed file

  • Retrieve the cursor that represents the given translation unit. The translation unit cursor can be used to start traversing the various declarations within the given translation unit.

    Declaration

    Swift

    public var cursor: Cursor
  • Visits each of the children of this translation unit, calling the provided callback for each child.

    Note

    The returned value of this callback defines what the next item visited will be. See ChildVisitResult for a list of possible results.

    Declaration

    Swift

    public func visitChildren(_ perCursorCallback: (Cursor) -> ChildVisitResult)

    Parameters

    perCursorCallback

    The callback to call with each child in the receiver.

  • Get the original translation unit source file name.

    Declaration

    Swift

    public var spelling: String
  • Tokenizes the source code described by the given range into raw lexical tokens.

    Declaration

    Swift

    public func tokens(in range: SourceRange) -> [Token]

    Parameters

    range

    the source range in which text should be tokenized. All of the tokens produced by tokenization will fall within this source range.

    Return Value

    All tokens that fall within the provided source range in this translation unit.

  • Annotate the given set of tokens by providing cursors for each token that can be mapped to a specific entity within the abstract syntax tree. This token-annotation routine is equivalent to invoking cursor(at:) for the source locations of each of the tokens. The cursors provided are filtered, so that only those cursors that have a direct correspondence to the token are accepted. For example, given a function call f(x), cursor(at:) would provide the following cursors:

    • when the cursor is over the f, a DeclRefExpr cursor referring to f.
    • when the cursor is over the ( or the ), a CallExpr referring to f.
    • when the cursor is over the x, a DeclRefExpr cursor referring to x.

    Only the first and last of these cursors will occur within the annotate, since the tokens f and “x’ directly refer to a function and a variable, respectively, but the parentheses are just a small part of the full syntax of the function call expression, which is not provided as an annotation.

    Declaration

    Swift

    public func annotate(tokens: [Token]) -> [Cursor]

    Parameters

    tokens

    The set of tokens to annotate

    Return Value

    The cursors corresponding to each token provided