parseTU: Parse a C/C++ source file & traverse the parse tree

View source: R/parseTU.R

parseTUR Documentation

Parse a C/C++ source file \& traverse the parse tree

Description

These collection of functions are related to both parsing C or C++ source code and then iterating over the resulting parse tree and extracting information about the contents.

Usage

parseTU(src, visitor = simpleVisitor, idx = createIndex(),
          tu = createTU(src, idx = idx, ...), ..., clone = FALSE)
createTU(src, includes = character(), idx = createIndex(verbose = verbose),
         args = character(), verbose = getOption("ShowParserDiagnostics", TRUE),
         options = 0)
visitCursor(tu, fun, clone = TRUE, data = NULL, ...)
getFile(tu, file_name)
defaultReparseOptions(TU)
isFileMultipleIncludeGuarded(tu, file)

Arguments

src

the fully-qualified name of the source code file

visitor, fun

a function that is called when iterating over the parse tree. This must return one of the CXChildVisit enumeration values.

idx

a CXIndex object used to create the translation unit. This is rarely specified.

includes

a character vector specifying the paths to directories in which the parser can look for \#include'd header files

args

a character vector of flags or arguments for the parser. These can include pre-processor definitions, e.g -DHAVE_CONFIG_H, and also arguments that clang understands, e.g. -fparse-all-comments and -ferror-limit=1000. The arguments need to be separate elements of the vector and should not be combined into a single string. The possible arguments are described in the clang user manual.

verbose

used when creating the index and controls whether diagnostic messages are written to the console.

file

a CXFile object representing a file

tu, TU

a translation unit object. This is rarely specified by the caller.

...

additional parameters passed to createTU.

clone

a logical value controlling whether to clone the CXCursor objects passed in each call to the visitor function. This is TRUE by default to be safe. However, if the caller knows that the visitor function will not store a cursor it is passed for later use, or that it will clone that cursor itself, it is more efficient to use clone = FALSE.

options

a collection of options that is a vector or bitwise OR'ed together. The values come from the CXTranslationUnit_FlagsValues bitwise enumeration vector or the variables corresponding to the individual elements of this enumeration. These include DetailedPreprocessingRecord, Incomplete, PrecompiledPreamble, CacheCompletionResults, ForSerialization, CXXChainedPCH, SkipFunctionBodies, IncludeBriefCommentsInCodeCompletion.

file_name

the name of the file for which we want a CXFile object

data

used in visitCursor when the fun is actually (a pointer to) a C routine. The value of data in this case is either an external pointer or treated directly as an R object. As we visit each cursor in the tree, we call the C routine, passing it the cursor and also this C value.

Value

An integer value indicating whether the parsing was successful (0) or not (non-zero).

Author(s)

Duncan Temple Lang

References

libclang http://clang.llvm.org/doxygen/group__CINDEX.html

See Also

visitTU createIndex

Examples

col = genFunctionCollector()
parseTU(system.file("exampleCode", "register.c", package = "RCIndex"),
         col$update)

names(col$funcs())


  # Illustrate using a compiled (C) routine as the visitor 'function'.
ff = system.file("exampleCode", "funPointer.c", package = "RCIndex")
routine = getNativeSymbolInfo("R_TestVisitor")$address
visitTU(ff, routine)


# Parse a C++ file.
f = system.file('exampleCode', 'simpleClass.cpp', package = 'RCIndex')
createTU(f, args = "-xc++")


# Get the pre-processor cursors
f = system.file("exampleCode", "macros.c", package = "RCIndex")
parseTU(f, options = RCIndex:::CXTranslationUnit_DetailedPreprocessingRecord)


omegahat/RClangSimple documentation built on April 25, 2024, 4:18 a.m.