public class ClangUtils extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.util.EnumSet<ELanguage> |
CLANG_ENABLED_LANGUAGES
Languages for which we enable parsing with clang.
|
Constructor and Description |
---|
ClangUtils() |
Modifier and Type | Method and Description |
---|---|
static java.util.Optional<CheckTextRegionLocation> |
createTextRegionLocation(eu.cqse.clang.CXCursor cursor)
Creates a
CheckTextRegionLocation for the extent of the given clang
cursor, if the given cursor is actually from the given file (and not some
included header). |
static void |
debugPrintDiagnostics(eu.cqse.clang.SWIGTYPE_p_CXTranslationUnitImpl translationUnit,
java.io.PrintStream out)
Prints the diagnostics information ("errors and warnings" in Clang) generated
during construction of the given translationUnit to the given
PrintStream . |
static void |
debugPrintTranslationUnitAST(eu.cqse.clang.SWIGTYPE_p_CXTranslationUnitImpl translationUnit)
Prints the AST of the given translation unit to System.out.
|
static java.util.List<eu.cqse.clang.CXCursor> |
findChildrenWithKindRecursively(eu.cqse.clang.CXCursor cursor,
eu.cqse.clang.CXCursorKind cursorKind)
Returns all children of the given cursor (recursive) that have the given
CXCursorKind . |
static java.util.Optional<java.lang.String> |
getCompleteNodeText(eu.cqse.clang.CXCursor cursor,
java.lang.String mainFileContent)
Returns the code from the start to the end of the given
CXCursor . |
static java.util.Optional<eu.cqse.clang.CXCursor> |
getCursorForStartEndOffset(int inclusiveStartOffset,
int exclusiveEndOffset,
eu.cqse.clang.CXCursor rootCursor)
Returns the cursor for a given start/end offset (if any found).
|
static java.util.Optional<eu.cqse.clang.CXCursor> |
getCursorForToken(IToken token,
eu.cqse.clang.CXCursor rootCursor)
Returns the cursor for a given token (if any found).
|
static java.util.List<eu.cqse.clang.CXCursor> |
getDirectChildren(eu.cqse.clang.CXCursor cursor)
Returns the direct children of the given cursor.
|
static java.util.Optional<java.lang.String> |
getOperatorText(eu.cqse.clang.CXCursor operatorCursor,
java.lang.String completeFileText)
This is a hack to get the text of a binary operator.
|
static boolean |
isBinaryOperatorOrCompoundAssignment(eu.cqse.clang.CXCursor cursor)
Returns true if the this is a
CXCursor for a
CXCursorKind.CXCursor_BinaryOperator (e.g. |
static eu.cqse.clang.CXType |
resolveTypedefs(eu.cqse.clang.CXType type)
Resolves the given type to its initial type definition.
|
public static final java.util.EnumSet<ELanguage> CLANG_ENABLED_LANGUAGES
public ClangUtils()
public static java.util.List<eu.cqse.clang.CXCursor> getDirectChildren(eu.cqse.clang.CXCursor cursor)
public static java.util.List<eu.cqse.clang.CXCursor> findChildrenWithKindRecursively(eu.cqse.clang.CXCursor cursor, eu.cqse.clang.CXCursorKind cursorKind)
CXCursorKind
.public static void debugPrintDiagnostics(eu.cqse.clang.SWIGTYPE_p_CXTranslationUnitImpl translationUnit, java.io.PrintStream out)
PrintStream
.
Don't delete this method even if it is unused since we need it for debugging
Clang.public static void debugPrintTranslationUnitAST(eu.cqse.clang.SWIGTYPE_p_CXTranslationUnitImpl translationUnit)
public static java.util.Optional<CheckTextRegionLocation> createTextRegionLocation(eu.cqse.clang.CXCursor cursor)
CheckTextRegionLocation
for the extent of the given clang
cursor, if the given cursor is actually from the given file (and not some
included header).public static java.util.Optional<java.lang.String> getCompleteNodeText(eu.cqse.clang.CXCursor cursor, java.lang.String mainFileContent)
CXCursor
.
This assumes that the cursors' TranslationUnit was created with the given
code (same
ECodeViewOption.ETextViewOption
text representation). Otherwise offsets will be wrong.
Line breaks have been removed from the returned string (to simplify using it
in user-visible messages).
Just getting the name of the current cursor works for simple cases (with
Clang.clang_getCString(Clang.clang_getCursorSpelling(cursor))
),
but if child is a more complex expression, this returns the empty string.
Therefore, we get the text from the entire region of the cursor here.public static eu.cqse.clang.CXType resolveTypedefs(eu.cqse.clang.CXType type)
X
in
typedef int A; typedef A B; typedef B X;
will be resolved to
int
.public static java.util.Optional<java.lang.String> getOperatorText(eu.cqse.clang.CXCursor operatorCursor, java.lang.String completeFileText)
operatorCursor
- Cursor of kind CXCursorKind.CXCursor_BinaryOperator
or
CXCursorKind.CXCursor_CompoundAssignOperator
completeFileText
- The text of the file used to create the current translation unit.
The operator text is extracted from this string based on the
offsets reported by clang.public static java.util.Optional<eu.cqse.clang.CXCursor> getCursorForToken(IToken token, eu.cqse.clang.CXCursor rootCursor)
public static java.util.Optional<eu.cqse.clang.CXCursor> getCursorForStartEndOffset(int inclusiveStartOffset, int exclusiveEndOffset, eu.cqse.clang.CXCursor rootCursor)
public static boolean isBinaryOperatorOrCompoundAssignment(eu.cqse.clang.CXCursor cursor)
CXCursor
for a
CXCursorKind.CXCursor_BinaryOperator
(e.g. +, -, = etc.) or a
CXCursorKind.CXCursor_CompoundAssignOperator
(e.g. +=, -= etc.).
Since both have a left hand- and right hand side with the operator in the
middle, these operands are handled together for convenience.