Ejemplo n.º 1
0
def count(string):
    string = strip_comments(string)
    lines = string.splitlines()
    # Remove lines containing only whitespace.
    lines = [l for l in lines if not l.isspace()]
    # Remove blank lines.
    lines = [l for l in lines if len(l) > 0]
    return len(lines)
Ejemplo n.º 2
0
def test_dashed_comment_line():
    code = """
            /*-----------------------------------------------------------*/
            int a;
            """
    expected = """
            
            int a;
            """
    stripped = strip_comments(code)
    assert (stripped == expected)
Ejemplo n.º 3
0
def test_multiline_block_comment():
    code = """
            /* See if the handle of the queue being unregistered in actually in the
            registry. */
            for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
            """
    expected = """
            
            for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
            """
    stripped = strip_comments(code)
    assert (stripped == expected)
Ejemplo n.º 4
0
def test_some_tricky_code():
    code = """
                /*-----------------------------------------------------------*/
            
            #if configQUEUE_REGISTRY_SIZE > 0

                static void vQueueUnregisterQueue( xQueueHandle xQueue )
                {
                unsigned portBASE_TYPE ux;

                    /* See if the handle of the queue being unregistered in actually in the
                    registry. */
                    for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
                    {
                        if( xQueueRegistry[ ux ].xHandle == xQueue )
                        {
                            /* Set the name to NULL to show that this slot if free again. */
                            xQueueRegistry[ ux ].pcQueueName = NULL;
                            break;
                        }
                    }
                }

            #endif
            """
    expected = """
                
            
            #if configQUEUE_REGISTRY_SIZE > 0

                static void vQueueUnregisterQueue( xQueueHandle xQueue )
                {
                unsigned portBASE_TYPE ux;

                    
                    for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
                    {
                        if( xQueueRegistry[ ux ].xHandle == xQueue )
                        {
                            
                            xQueueRegistry[ ux ].pcQueueName = NULL;
                            break;
                        }
                    }
                }

            #endif
            """

    stripped = strip_comments(code)
    assert (stripped == expected)
Ejemplo n.º 5
0
def calculate_complexity(code):
    original_code = code
    code = strip_comments(code)
    results = []
    function_matcher = re.compile(r'\s+(\w+)\s*\([\w\s,\*]*\)\s*{', re.MULTILINE)

    for m in function_matcher.finditer(code):
        name = m.group(1)
        if __is_a_function(name):
            start_of_function = m.end()
            # We just found the next function. Extract the body of the function.
            function_body = __extract_next_function_body(code[start_of_function:])
            # Find the line number.
            line_number = __find_line_number(m.group(), original_code)

            # Compute the complexity of this function.
            results.append(Function(name, __calculate_complexity_for_a_function(function_body), line_number))

    return results
Ejemplo n.º 6
0
def find_globals(code):
    original_code = code
    results = []
    # Remove all comments.
    code = strip_comments(code)
    # Remove #ifs, #elifs, #ifdefs
    code = __strip_preprocessor_directives(code)
    if "some_const_array" in code:
        print(code)
    # Remove anything between brackets.
    code = re.sub(r'{[^}]*}', '{}', code, flags=re.DOTALL)
    # Find all the globals.
    global_matcher = re.compile(r'([\w\t\f\v {}]+)\s+(\w+)[\d\[\]]*(?:;|\s*=)')
    for m in global_matcher.finditer(code):
        qualifiers = m.group(1)
        name = m.group(2)
        line_number = __get_line_number(m.group(), original_code)
        if ('static' not in qualifiers and 'typedef' not in qualifiers
                and 'extern' not in qualifiers and 'const' not in qualifiers):
            results.append(GlobalVariable(name, line_number))
    return results
Ejemplo n.º 7
0
def find_globals(code):
    original_code = code
    results = []
    # Remove all comments.
    code = strip_comments(code)
    # Remove #ifs, #elifs, #ifdefs
    code = __strip_preprocessor_directives(code)
    # Remove anything between brackets.
    code = re.sub(r'{.*}', '{}', code, flags=re.DOTALL)
    # Find all the globals.
    global_matcher = re.compile(r'([\w\t\f\v {}]+)\s+(\w+)[\d\[\]]*(?:;|\s*=)')
    for m in global_matcher.finditer(code):
        qualifiers = m.group(1)
        name = m.group(2)
        line_number = __get_line_number(m.group(), original_code)
        if ('static' not in qualifiers
                and 'typedef' not in qualifiers
                and 'extern' not in qualifiers
                and 'const' not in qualifiers):
            results.append(GlobalVariable(name, line_number))
    return results