2019-04-09 11:52:21 -04:00
|
|
|
// Package ast parses the clang AST output into AST structures.
|
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2019-06-03 11:07:12 -04:00
|
|
|
"fmt"
|
2019-04-09 11:52:21 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-05-29 22:36:49 -04:00
|
|
|
"git.wow.st/gmp/nswrap/util"
|
2019-04-09 11:52:21 -04:00
|
|
|
)
|
|
|
|
|
2019-06-04 00:14:04 -04:00
|
|
|
var (
|
|
|
|
TrackPositions bool = false
|
2019-06-11 12:38:22 -04:00
|
|
|
Debug = false
|
2019-06-04 00:14:04 -04:00
|
|
|
)
|
2019-05-11 23:03:56 -04:00
|
|
|
|
2019-04-09 11:52:21 -04:00
|
|
|
// Node represents any node in the AST.
|
|
|
|
type Node interface {
|
|
|
|
Address() Address
|
|
|
|
Children() []Node
|
|
|
|
AddChild(node Node)
|
|
|
|
Position() Position
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address contains the memory address (originally outputted as a hexadecimal
|
|
|
|
// string) from the clang AST. The address are not predictable between run and
|
|
|
|
// are only useful for identifying nodes in a single AST.
|
|
|
|
//
|
|
|
|
// The Address is used like a primary key when storing the tree as a flat
|
|
|
|
// structure.
|
|
|
|
type Address uint64
|
|
|
|
|
|
|
|
// ParseAddress returns the integer representation of the hexadecimal address
|
|
|
|
// (like 0x7f8a1d8ccfd0). If the address cannot be parsed, 0 is returned.
|
|
|
|
func ParseAddress(address string) Address {
|
2019-05-11 23:03:56 -04:00
|
|
|
if !TrackPositions {
|
|
|
|
return 0
|
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
addr, _ := strconv.ParseUint(address, 0, 64)
|
|
|
|
|
|
|
|
return Address(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse takes the coloured output of the clang AST command and returns a root
|
|
|
|
// node for the AST.
|
|
|
|
func Parse(fullline string) Node {
|
|
|
|
line := fullline
|
|
|
|
|
|
|
|
// This is a special case. I'm not sure if it's a bug in the clang AST
|
|
|
|
// dumper. It should have children.
|
|
|
|
if line == "array filler" {
|
|
|
|
return parseArrayFiller(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(line, " ", 2)
|
|
|
|
nodeName := parts[0]
|
|
|
|
|
|
|
|
if nodeName == "super" || nodeName == "getter" { // special ObjC case
|
|
|
|
parts = strings.SplitN(parts[1], " ", 2)
|
|
|
|
nodeName += " " + parts[0]
|
|
|
|
}
|
|
|
|
// skip node name
|
|
|
|
if len(parts) > 1 {
|
|
|
|
line = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch nodeName {
|
|
|
|
case "AlignedAttr":
|
|
|
|
return parseAlignedAttr(line)
|
|
|
|
case "AllocSizeAttr":
|
|
|
|
return parseAllocSizeAttr(line)
|
|
|
|
case "AlwaysInlineAttr":
|
|
|
|
return parseAlwaysInlineAttr(line)
|
|
|
|
case "ArraySubscriptExpr":
|
|
|
|
return parseArraySubscriptExpr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ArcWeakrefUnavailableAttr":
|
|
|
|
return parseArcWeakrefUnavailableAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "AsmLabelAttr":
|
|
|
|
return parseAsmLabelAttr(line)
|
|
|
|
case "AttributedType":
|
|
|
|
return parseAttributedType(line)
|
|
|
|
case "AvailabilityAttr":
|
|
|
|
return parseAvailabilityAttr(line)
|
|
|
|
case "BinaryOperator":
|
|
|
|
return parseBinaryOperator(line)
|
|
|
|
case "BlockCommandComment":
|
|
|
|
return parseBlockCommandComment(line)
|
|
|
|
case "BlockPointerType":
|
|
|
|
return parseBlockPointerType(line)
|
|
|
|
case "BreakStmt":
|
|
|
|
return parseBreakStmt(line)
|
|
|
|
case "BuiltinType":
|
|
|
|
return parseBuiltinType(line)
|
|
|
|
case "CallExpr":
|
|
|
|
return parseCallExpr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ConvertVectorExpr":
|
|
|
|
return parseConvertVectorExpr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "CaseStmt":
|
|
|
|
return parseCaseStmt(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "CFAuditedTransferAttr":
|
|
|
|
return parseCFAuditedTransferAttr(line)
|
|
|
|
case "CFConsumedAttr":
|
|
|
|
return parseCFConsumedAttr(line)
|
|
|
|
case "CFReturnsRetainedAttr":
|
|
|
|
return parseCFReturnsRetainedAttr(line)
|
|
|
|
case "CFReturnsNotRetainedAttr":
|
|
|
|
return parseCFReturnsNotRetainedAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "CharacterLiteral":
|
|
|
|
return parseCharacterLiteral(line)
|
|
|
|
case "CompoundLiteralExpr":
|
|
|
|
return parseCompoundLiteralExpr(line)
|
|
|
|
case "CompoundStmt":
|
|
|
|
return parseCompoundStmt(line)
|
|
|
|
case "ConditionalOperator":
|
|
|
|
return parseConditionalOperator(line)
|
|
|
|
case "ConstAttr":
|
|
|
|
return parseConstAttr(line)
|
|
|
|
case "ConstantArrayType":
|
|
|
|
return parseConstantArrayType(line)
|
|
|
|
case "ContinueStmt":
|
|
|
|
return parseContinueStmt(line)
|
|
|
|
case "CompoundAssignOperator":
|
|
|
|
return parseCompoundAssignOperator(line)
|
|
|
|
case "CStyleCastExpr":
|
|
|
|
return parseCStyleCastExpr(line)
|
|
|
|
case "DeclRefExpr":
|
|
|
|
return parseDeclRefExpr(line)
|
|
|
|
case "DeclStmt":
|
|
|
|
return parseDeclStmt(line)
|
|
|
|
case "DefaultStmt":
|
|
|
|
return parseDefaultStmt(line)
|
|
|
|
case "DeprecatedAttr":
|
|
|
|
return parseDeprecatedAttr(line)
|
|
|
|
case "DisableTailCallsAttr":
|
|
|
|
return parseDisableTailCallsAttr(line)
|
|
|
|
case "DoStmt":
|
|
|
|
return parseDoStmt(line)
|
|
|
|
case "ElaboratedType":
|
|
|
|
return parseElaboratedType(line)
|
|
|
|
case "EmptyDecl":
|
|
|
|
return parseEmptyDecl(line)
|
|
|
|
case "Enum":
|
|
|
|
return parseEnum(line)
|
|
|
|
case "EnumConstantDecl":
|
|
|
|
return parseEnumConstantDecl(line)
|
|
|
|
case "EnumDecl":
|
|
|
|
return parseEnumDecl(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "EnumExtensibilityAttr":
|
|
|
|
return parseEnumExtensibilityAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "EnumType":
|
|
|
|
return parseEnumType(line)
|
|
|
|
case "Field":
|
|
|
|
return parseField(line)
|
|
|
|
case "FieldDecl":
|
|
|
|
return parseFieldDecl(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "FlagEnumAttr":
|
|
|
|
return parseFlagEnumAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "FloatingLiteral":
|
|
|
|
return parseFloatingLiteral(line)
|
|
|
|
case "FormatAttr":
|
|
|
|
return parseFormatAttr(line)
|
|
|
|
case "FormatArgAttr":
|
|
|
|
return parseFormatArgAttr(line)
|
|
|
|
case "FunctionDecl":
|
|
|
|
return parseFunctionDecl(line)
|
|
|
|
case "FullComment":
|
|
|
|
return parseFullComment(line)
|
|
|
|
case "FunctionProtoType":
|
|
|
|
return parseFunctionProtoType(line)
|
|
|
|
case "ForStmt":
|
|
|
|
return parseForStmt(line)
|
|
|
|
case "HTMLStartTagComment":
|
|
|
|
return parseHTMLStartTagComment(line)
|
|
|
|
case "HTMLEndTagComment":
|
|
|
|
return parseHTMLEndTagComment(line)
|
|
|
|
case "GCCAsmStmt":
|
|
|
|
return parseGCCAsmStmt(line)
|
|
|
|
case "GotoStmt":
|
|
|
|
return parseGotoStmt(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "IBActionAttr":
|
|
|
|
return parseIBActionAttr(line)
|
|
|
|
case "IBOutletAttr":
|
|
|
|
return parseIBOutletAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "IfStmt":
|
|
|
|
return parseIfStmt(line)
|
|
|
|
case "ImplicitCastExpr":
|
|
|
|
return parseImplicitCastExpr(line)
|
|
|
|
case "ImplicitValueInitExpr":
|
|
|
|
return parseImplicitValueInitExpr(line)
|
|
|
|
case "IncompleteArrayType":
|
|
|
|
return parseIncompleteArrayType(line)
|
|
|
|
case "IndirectFieldDecl":
|
|
|
|
return parseIndirectFieldDecl(line)
|
|
|
|
case "InitListExpr":
|
|
|
|
return parseInitListExpr(line)
|
|
|
|
case "InlineCommandComment":
|
|
|
|
return parseInlineCommandComment(line)
|
|
|
|
case "IntegerLiteral":
|
|
|
|
return parseIntegerLiteral(line)
|
|
|
|
case "LabelStmt":
|
|
|
|
return parseLabelStmt(line)
|
|
|
|
case "MallocAttr":
|
|
|
|
return parseMallocAttr(line)
|
|
|
|
case "MaxFieldAlignmentAttr":
|
|
|
|
return parseMaxFieldAlignmentAttr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "MayAliasAttr":
|
|
|
|
return parseMayAliasAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "MemberExpr":
|
|
|
|
return parseMemberExpr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "MinVectorWidthAttr":
|
|
|
|
return parseMinVectorWidthAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "ModeAttr":
|
|
|
|
return parseModeAttr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "NoDebugAttr":
|
|
|
|
return parseNoDebugAttr(line)
|
|
|
|
case "NoEscapeAttr":
|
|
|
|
return parseNoEscapeAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "NoInlineAttr":
|
|
|
|
return parseNoInlineAttr(line)
|
|
|
|
case "NoThrowAttr":
|
|
|
|
return parseNoThrowAttr(line)
|
|
|
|
case "NonNullAttr":
|
|
|
|
return parseNonNullAttr(line)
|
|
|
|
case "NotTailCalledAttr":
|
|
|
|
return parseNotTailCalledAttr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "NSConsumedAttr":
|
|
|
|
return parseNSConsumedAttr(line)
|
|
|
|
case "NSConsumesSelfAttr":
|
|
|
|
return parseNSConsumesSelfAttr(line)
|
|
|
|
case "NSErrorDomainAttr":
|
|
|
|
return parseNSErrorDomainAttr(line)
|
|
|
|
case "NSReturnsRetainedAttr":
|
|
|
|
return parseNSReturnsRetainedAttr(line)
|
|
|
|
case "ObjCBoolLiteralExpr":
|
|
|
|
return parseObjCBoolLiteralExpr(line)
|
|
|
|
case "ObjCBoxableAttr":
|
|
|
|
return parseObjCBoxableAttr(line)
|
|
|
|
case "ObjCBridgeAttr":
|
|
|
|
return parseObjCBridgeAttr(line)
|
|
|
|
case "ObjCBridgeRelatedAttr":
|
|
|
|
return parseObjCBridgeRelatedAttr(line)
|
|
|
|
case "ObjCBridgeMutableAttr":
|
|
|
|
return parseObjCBridgeMutableAttr(line)
|
2019-04-09 23:19:49 -04:00
|
|
|
case "ObjCCategoryDecl":
|
|
|
|
return parseObjCCategoryDecl(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ObjCDesignatedInitializerAttr":
|
|
|
|
return parseObjCDesignatedInitializerAttr(line)
|
|
|
|
case "ObjCExceptionAttr":
|
|
|
|
return parseObjCExceptionAttr(line)
|
|
|
|
case "ObjCExplicitProtocolImplAttr":
|
|
|
|
return parseObjCExplicitProtocolImplAttr(line)
|
|
|
|
case "ObjCIndependentClassAttr":
|
|
|
|
return parseObjCIndependentClassAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "ObjCInterface":
|
2019-06-11 12:38:22 -04:00
|
|
|
return parseObjCInterface(line, false)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "super ObjCInterface":
|
2019-06-11 12:38:22 -04:00
|
|
|
return parseObjCInterface(line, true)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "ObjCInterfaceDecl":
|
|
|
|
return parseObjCInterfaceDecl(line)
|
2019-04-11 11:46:24 -04:00
|
|
|
case "ObjCInterfaceType":
|
|
|
|
return parseObjCInterfaceType(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ObjCIvarDecl":
|
|
|
|
return parseObjCIvarDecl(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "getter ObjCMethod":
|
|
|
|
return parseObjCMethod(line)
|
|
|
|
case "ObjCMethod":
|
|
|
|
return parseObjCMethod(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ObjCMessageExpr":
|
|
|
|
return parseObjCMessageExpr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "ObjCMethodDecl":
|
|
|
|
return parseObjCMethodDecl(line)
|
|
|
|
case "ObjCObjectType":
|
|
|
|
return parseObjCObjectType(line)
|
|
|
|
case "ObjCObjectPointerType":
|
|
|
|
return parseObjCObjectPointerType(line)
|
|
|
|
case "ObjCProtocol":
|
|
|
|
return parseObjCProtocol(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ObjCReturnsInnerPointerAttr":
|
|
|
|
return parseObjCReturnsInnerPointerAttr(line)
|
|
|
|
case "ObjCRequiresSuperAttr":
|
|
|
|
return parseObjCRequiresSuperAttr(line)
|
2019-05-09 12:25:45 -04:00
|
|
|
case "ObjCProtocolDecl":
|
|
|
|
return parseObjCProtocolDecl(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ObjCRootClassAttr":
|
|
|
|
return parseObjCRootClassAttr(line)
|
2019-04-09 23:19:49 -04:00
|
|
|
case "ObjCPropertyDecl":
|
|
|
|
return parseObjCPropertyDecl(line)
|
2019-04-26 22:44:30 -04:00
|
|
|
case "ObjCTypeParamDecl":
|
|
|
|
return parseObjCTypeParamDecl(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "OffsetOfExpr":
|
|
|
|
return parseOffsetOfExpr(line)
|
|
|
|
case "PackedAttr":
|
|
|
|
return parsePackedAttr(line)
|
|
|
|
case "ParagraphComment":
|
|
|
|
return parseParagraphComment(line)
|
|
|
|
case "ParamCommandComment":
|
|
|
|
return parseParamCommandComment(line)
|
|
|
|
case "ParenExpr":
|
|
|
|
return parseParenExpr(line)
|
|
|
|
case "ParenType":
|
|
|
|
return parseParenType(line)
|
|
|
|
case "ParmVarDecl":
|
|
|
|
return parseParmVarDecl(line)
|
|
|
|
case "PointerType":
|
|
|
|
return parsePointerType(line)
|
|
|
|
case "DecayedType":
|
|
|
|
return parseDecayedType(line)
|
|
|
|
case "PredefinedExpr":
|
|
|
|
return parsePredefinedExpr(line)
|
|
|
|
case "PureAttr":
|
|
|
|
return parsePureAttr(line)
|
|
|
|
case "QualType":
|
|
|
|
return parseQualType(line)
|
|
|
|
case "Record":
|
|
|
|
return parseRecord(line)
|
|
|
|
case "RecordDecl":
|
|
|
|
return parseRecordDecl(line)
|
|
|
|
case "RecordType":
|
|
|
|
return parseRecordType(line)
|
|
|
|
case "RestrictAttr":
|
|
|
|
return parseRestrictAttr(line)
|
|
|
|
case "ReturnStmt":
|
|
|
|
return parseReturnStmt(line)
|
|
|
|
case "ReturnsTwiceAttr":
|
|
|
|
return parseReturnsTwiceAttr(line)
|
|
|
|
case "SentinelAttr":
|
|
|
|
return parseSentinelAttr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "ShuffleVectorExpr":
|
|
|
|
return parseShuffleVectorExpr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "StmtExpr":
|
|
|
|
return parseStmtExpr(line)
|
|
|
|
case "StringLiteral":
|
|
|
|
return parseStringLiteral(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "SwiftBridgedTypedefAttr":
|
|
|
|
return parseSwiftBridgedTypedefAttr(line)
|
|
|
|
case "SwiftErrorAttr":
|
|
|
|
return parseSwiftErrorAttr(line)
|
|
|
|
case "SwiftNameAttr":
|
|
|
|
return parseSwiftNameAttr(line)
|
|
|
|
case "SwiftNewtypeAttr":
|
|
|
|
return parseSwiftNewtypeAttr(line)
|
|
|
|
case "SwiftPrivateAttr":
|
|
|
|
return parseSwiftPrivateAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "SwitchStmt":
|
|
|
|
return parseSwitchStmt(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "TargetAttr":
|
|
|
|
return parseTargetAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "TextComment":
|
|
|
|
return parseTextComment(line)
|
|
|
|
case "TranslationUnitDecl":
|
|
|
|
return parseTranslationUnitDecl(line)
|
|
|
|
case "TransparentUnionAttr":
|
|
|
|
return parseTransparentUnionAttr(line)
|
|
|
|
case "Typedef":
|
|
|
|
return parseTypedef(line)
|
|
|
|
case "TypedefDecl":
|
|
|
|
return parseTypedefDecl(line)
|
|
|
|
case "TypedefType":
|
|
|
|
return parseTypedefType(line)
|
|
|
|
case "UnaryExprOrTypeTraitExpr":
|
|
|
|
return parseUnaryExprOrTypeTraitExpr(line)
|
|
|
|
case "UnaryOperator":
|
|
|
|
return parseUnaryOperator(line)
|
2019-04-11 11:46:24 -04:00
|
|
|
case "UnavailableAttr":
|
|
|
|
return parseUnavailableAttr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "UsedAttr":
|
|
|
|
return parseUsedAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "UnusedAttr":
|
|
|
|
return parseUnusedAttr(line)
|
|
|
|
case "VAArgExpr":
|
|
|
|
return parseVAArgExpr(line)
|
|
|
|
case "VarDecl":
|
|
|
|
return parseVarDecl(line)
|
|
|
|
case "VectorType":
|
|
|
|
return parseVectorType(line)
|
|
|
|
case "VerbatimBlockComment":
|
|
|
|
return parseVerbatimBlockComment(line)
|
|
|
|
case "VerbatimBlockLineComment":
|
|
|
|
return parseVerbatimBlockLineComment(line)
|
|
|
|
case "VerbatimLineComment":
|
|
|
|
return parseVerbatimLineComment(line)
|
|
|
|
case "VisibilityAttr":
|
|
|
|
return parseVisibilityAttr(line)
|
|
|
|
case "WarnUnusedResultAttr":
|
|
|
|
return parseWarnUnusedResultAttr(line)
|
|
|
|
case "WeakAttr":
|
|
|
|
return parseWeakAttr(line)
|
2019-06-03 11:07:12 -04:00
|
|
|
case "WeakImportAttr":
|
|
|
|
return parseWeakImportAttr(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "WhileStmt":
|
|
|
|
return parseWhileStmt(line)
|
2019-04-29 16:14:45 -04:00
|
|
|
case "...":
|
|
|
|
return parseVariadic(line)
|
2019-04-09 11:52:21 -04:00
|
|
|
case "NullStmt":
|
|
|
|
return nil
|
|
|
|
default:
|
2019-06-11 12:38:22 -04:00
|
|
|
return parseUnknown(nodeName, line)
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupsFromRegex(rx, line string) map[string]string {
|
|
|
|
// We remove tabs and newlines from the regex. This is purely cosmetic,
|
|
|
|
// as the regex input can be quite long and it's nice for the caller to
|
|
|
|
// be able to format it in a more readable way.
|
|
|
|
fullRegexp := "^(?P<address>[0-9a-fx]+) " +
|
|
|
|
strings.Replace(strings.Replace(rx, "\n", "", -1), "\t", "", -1)
|
|
|
|
rx = fullRegexp + "[\\s]*$"
|
|
|
|
|
|
|
|
re := util.GetRegex(rx)
|
|
|
|
match := re.FindStringSubmatch(line)
|
|
|
|
if len(match) == 0 {
|
2019-06-04 00:14:04 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("AST parser: could not match regexp with string. Regexp:\n" + rx + "\nOriginal line:\n" + line + "\n\n")
|
|
|
|
}
|
2019-06-03 11:07:12 -04:00
|
|
|
return nil
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[string]string)
|
|
|
|
for i, name := range re.SubexpNames() {
|
|
|
|
if i != 0 {
|
|
|
|
result[name] = match[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|