def lookupOrder(member):
     order = orders.get(HashableCursor(member.cursor.get_definition()))
     if order is None:
         printerr('The following cursor:')
         printCursor(member.cursor, printerr)
         printerr('with definition:')
         printCursor(member.cursor.get_definition(), printerr)
         printerr('is not in my catalog of fields.')
         import sys
         sys.exit('Encountered an unexpected constructor init field')
     return order
def getSwitchRewrite(switchCursor, printerr):
    if switchCursor.kind != CursorKind.SWITCH_STMT:
        printerr('This cursor is a {} instead of a switch.',
                 switchCursor.kind)
        return None

    # Find the only compound statement in the switch.
    body = None
    for child in switchCursor.get_children():
        if child.kind == CursorKind.COMPOUND_STMT:
            if body is not None:
                printerr("This switch has two or more bodies. Here's the second one:")
                printCursor(child, printer=printerr)
                return None
            else:
                body = child

    if body is None:
        printerr("This switch doesn't have a body. That's legal, but I can't work with it.")
        return None

    # Find the last child of the body.
    children = list(body.get_children())
    if len(children) == 0:
        printerr("The body of this switch doesn't have any children. What the hell.")
        return None

    bodyTokens = list(body.get_tokens())
    assert len(bodyTokens) > 1
    offset = bodyTokens[-2].extent.end.offset

    def isCase(cursor):
        return cursor.kind == CursorKind.CASE_STMT
        
    lastChild = children[-1]
    cursorToBeBelow = firstWith(reversed(children), isCase) or lastChild
    indentation = repeatedString(' ', cursorToBeBelow.location.column - 1)

    # A rewrite is (offset to beginning, length of old text, new text)
    # Note: I don't append a newline since I assume that the last token 
    #       of the last child was probably followed by a newline.
    tabWidth = 4
    return (offset, 0, '\n{0}default:\n{0}{1}break;{2}'.format(
                           indentation, 
                           repeatedString(' ', tabWidth),
                           '' if args.noTodo else ' /* TODO? */'))
def eponymousToken(cursor):
    if not cursor.displayname:
        return None # Of course nothing will match

    matches = [token \
               for token in cursor.get_tokens() \
               if token.spelling == cursor.displayname]

    # "assert" might be too harsh.
    # assert len(matches) > 0
    if len(matches) == 0:
        printerr('WARNING The following cursor does not have an eponymous token:')
        printerr(cursor.location.file.name)
        printCursor(cursor, printerr)
        return None
                       # Take the last rather than the first, 
    return matches[-1] # in case the parameter shares its name with a type.