Esempio n. 1
0
    def rewrite_do_stmt(self, rewriter, index, tokens, text):
        toks = list(tokens[index].cursor.get_tokens())

        if (toks[1].kind == TokenKind.PUNCTUATION) and (toks[1].spelling
                                                        == '{'):
            # Do statement is already using braces
            return None

        # Find while statement at the end
        end_index = None
        for i in range(len(toks)):
            tok = toks[i]
            if (tok.kind == TokenKind.PUNCTUATION) and (tok.spelling == ';'):
                end_index = i
                break

        if end_index is None:
            return None

        toks = toks[:end_index + 1]

        origindent = get_line_indent(toks[0], text)
        indent = get_configured_indent(rewriter.config)

        newtext = toks[0].spelling
        newtext += "\n" + origindent + "{"
        newtext += "\n" + (origindent + indent) + original_text_from_tokens(
            toks[1:], text)
        newtext += "\n" + origindent + "}"

        ret = CodeChunkReplacement(index, toks[0].extent.start.offset,
                                   toks[-1].extent.end.offset, newtext)

        return ret
Esempio n. 2
0
    def _code_block_after_conditional(self, rewriter, index, tokens, text):
        # Find the closing paren. of conditional statement
        start_index = find_last_matching_rparen(tokens)

        if start_index is None:
            return None

        if tokens[start_index].kind == TokenKind.PUNCTUATION:
            if tokens[start_index].spelling == '{':
                # Statement is already using braces.
                return None

        end_index = find_next_toplevel_semicolon_index(tokens) + 1
        if end_index is None:
            return None

        tokens = tokens[:end_index]
        origindent = get_line_indent(tokens[0], text)
        indent = get_configured_indent(rewriter.config)

        newtext = original_text_from_tokens(tokens[:start_index], text)
        newtext += "\n" + origindent + "{"
        newtext += "\n" + (origindent + indent) + original_text_from_tokens(
            tokens[start_index:], text)
        newtext += "\n" + origindent + "}"

        ret = CodeChunkReplacement(index, tokens[0].extent.start.offset,
                                   tokens[-1].extent.end.offset, newtext)

        return ret
Esempio n. 3
0
    def rewrite_if_stmt(self, rewriter, index, tokens, text):
        # Check if there are else-if clauses but a missing else clause
        has_else = False
        has_elseif = False

        for i in range(len(tokens)):
            t = tokens[i]
            if t.kind == TokenKind.KEYWORD:
                if t.spelling == 'else':
                    if ((i < (len(tokens) - 1))
                            and (tokens[i + 1].kind == TokenKind.KEYWORD)
                            and (tokens[i + 1].spelling == 'if')):
                        has_elseif = True
                    else:
                        has_else = True

        # If statement already has an 'else' clause, or if statement
        # has no else-if clause, no rewrite needed.
        if has_else or (not has_elseif):
            return None

        # No else clause, we need to add one.
        origindent = get_line_indent(tokens[0], text)
        indent = get_configured_indent(rewriter.config)

        newtext = original_text_from_tokens(tokens, text)
        newtext += "\n" + origindent + "else"
        newtext += "\n" + origindent + "{"
        newtext += "\n" + origindent + indent + ";"
        newtext += "\n" + origindent + "}"

        ret = CodeChunkReplacement(index, tokens[0].extent.start.offset,
                                   tokens[-1].extent.end.offset, newtext)

        return ret
Esempio n. 4
0
    def rewrite_func_impl(self, paramnames, rewriter, index, tokens, text):
        # Find opening brace
        lbrace_index = None
        for i in range(len(tokens)):
            tok = tokens[i]
            if (tok.kind == TokenKind.PUNCTUATION) and (tok.spelling == '{'):
                lbrace_index = i
                break

        if lbrace_index is None:
            return None

        # Now, count no. of references of each param within the function body
        refs = {n: 0 for n in paramnames}
        for i in range(lbrace_index, len(tokens), 1):
            tok = tokens[i]
            if tok.kind == TokenKind.IDENTIFIER:
                if tok.spelling in refs:
                    refs[tok.spelling] += 1

        not_used = []
        for n in refs:
            if refs[n] == 0:
                not_used.append(n)

        if not not_used:
            # All params are referenced
            return None

        # Get indent from current first line of function body
        indent = get_line_indent(tokens[lbrace_index + 1], text)
        bodyempty = (indent == '')

        newtext = ""

        if bodyempty:
            indent = get_configured_indent(rewriter.config)
            newtext += indent

        newtext += ("\n" + indent).join(["(void) %s;" % n for n in not_used])

        if not bodyempty:
            newtext += "\n"

        ret = CodeChunkReplacement(
            index, tokens[lbrace_index + 1].extent.start.offset,
            tokens[lbrace_index].extent.end.offset, newtext)

        return ret
Esempio n. 5
0
    def check_rewrite_ifelse_stmt(self, rewriter, index, tokens, text):
        tok = tokens[index]
        if tok.kind != TokenKind.KEYWORD:
            return None

        if tok.spelling == 'if':
            return self._code_block_after_conditional(rewriter, index,
                                                      tokens[index:], text)
        elif tok.spelling == 'else':
            if index < (len(tokens) - 1):
                if ((tokens[index + 1].kind == TokenKind.KEYWORD)
                        and (tokens[index + 1].spelling == 'if')):
                    return self._code_block_after_conditional(
                        rewriter, index, tokens[index:], text)

                elif ((tokens[index + 1].kind == TokenKind.PUNCTUATION)
                      and (tokens[index + 1].spelling == '{')):
                    # Statement is already using braces
                    return None

                else:
                    toks = tokens[index:]
                    end_index = find_next_toplevel_semicolon_index(toks) + 1
                    if end_index is None:
                        return None

                    toks = toks[:end_index]

                    origindent = get_line_indent(toks[0], text)
                    indent = get_configured_indent(rewriter.config)

                    newtext = toks[0].spelling
                    newtext += "\n" + origindent + "{"
                    newtext += ("\n" + (origindent + indent) +
                                original_text_from_tokens(toks[1:], text))
                    newtext += "\n" + origindent + "}"

                    ret = CodeChunkReplacement(index,
                                               toks[0].extent.start.offset,
                                               toks[-1].extent.end.offset,
                                               newtext)
                    return ret

        return None