wpiformat/wpiformat/includeorder.py
def header_sort(self, config_file, lines_list, file_name, start, end,
                    ifdef_level):
        """Recursively parses within #ifdef blocks for header includes and sorts
        them.

        Keyword arguments:
        config_file -- Config object
        lines_list -- list of file contents delimited by line separator
        file_name -- file name string
        start -- starting line for recursion step
        end -- ending line for recursion step
        ifdef_level -- current recursion depth of #ifdefs

        Returns tuple of the following:
          sorted output
          list of bools indicating header category presence within #ifdef block
          the index of the last line processed
          whether all header includes had header extension
        """
        output_list = []

        # Using sets here eliminates duplicate includes
        includes = [[], [], [], [], []]

        # List of bools indicating header category presence within #ifdef block
        includes_present = [False, False, False, False, False]

        ifdef_blocks = [[], [], [], [], []]

        i = start
        while i < end:
            if "#ifdef" in lines_list[i]:
                ifdef_count = 1
                for j in range(i + 1, end):
                    if "#ifdef" in lines_list[j]:
                        ifdef_count += 1
                    elif "#endif" in lines_list[j]:
                        ifdef_count -= 1
                    if ifdef_count == 0:
                        # Add #ifdef or #else line
                        ifdef = lines_list[i] + self.linesep

                        suboutput, inc_present, idx, valid_headers = self.header_sort(
                            config_file, lines_list, file_name, i + 1, j,
                            ifdef_level + 1)
                        i = j

                        # If header failed to classify, return failure
                        if not valid_headers:
                            return output_list, inc_present, i, False

                        if suboutput:
                            ifdef += self.linesep.join(suboutput) + self.linesep

                        # Add #endif line
                        ifdef += lines_list[j]

                        # #endif gets an extra line separator
                        if "#endif" in lines_list[j]:
                            ifdef += self.linesep

                        for k in range(len(includes_present)):
                            includes_present[k] |= inc_present[k]

                        # If there is only one type of include present
                        if sum(inc_present) == 1:
                            ifdef_idx = 0
                            for k in range(len(inc_present)):
                                if inc_present[k]:
                                    ifdef_idx = k
                                    break

                            # Only one type of include, so add it to sorts
                            ifdef_blocks[ifdef_idx].append(ifdef)
                        else:
                            # Treat #ifdef as barrier and flush includes
                            output_list.extend(self.write_headers(includes))
                            if output_list and not output_list[-1].endswith(
                                    self.linesep):
                                # Only add newline if content exists in
                                # output_list that doesn't end in a newline,
                                # which needs to be delimited from the #ifdef
                                output_list.append("")
                            includes = [[], [], [], [], []]

                            output_list.append(ifdef)
                        break
            elif "#include" in lines_list[i]:
                include_line = self.header_regex.search(lines_list[i])
                if not include_line:
                    # Write headers and #ifdef blocks if found
                    written = self.write_headers(includes, ifdef_blocks)
                    if written:
                        output_list.extend(written)
                    return output_list, includes_present, i, True

                if "NOLINT" in lines_list[i]:
                    # NOLINT is a barrier, so flush includes
                    written = self.write_headers(includes)
                    if written:
                        output_list.append(self.linesep.join(written))
                        includes = [[], [], [], [], []]

                    # NOLINT line will have newlines above and below, unless it
                    # is the first line being processed.
                    if output_list and output_list[-1] != "":
                        output_list.append("")
                    output_list.append(lines_list[i])
                    output_list.append("")

                    i += 1
                    continue

                # Insert header into appropriate list
                idx = self.classify_header(config_file, include_line, file_name)
                if idx != -1:
                    if include_line.group("comment"):
                        print("Warning: " + file_name + ": include '" +
                              include_line.group("name") + "' is commented out")
                    includes[idx].append(include_line)
                    includes_present[idx] = True
                else:
                    # If header failed to classify, return failure
                    return output_list, includes_present, i, False
            elif ifdef_level > 0 and lines_list[i] != "":
                # Non-preprocessor statements within a #ifdef block don't mark
                # the end of header include processing, but act as barrier for
                # sorting.

                # Dump currently collected header includes to file.
                # includes_present isn't reset as well here because that info is
                # still valid and necessary for sorting.
                written = self.write_headers(includes)
                if written:
                    output_list.append(self.linesep.join(written))
                    includes = [[], [], [], [], []]

                output_list.append(lines_list[i])
            elif lines_list[i].strip() != "":
                # Write headers and #ifdef blocks if found
                written = self.write_headers(includes, ifdef_blocks)
                if written:
                    output_list.extend(written)

                return output_list, includes_present, i, True
            i += 1

        # Write headers and #ifdef blocks if found
        written = self.write_headers(includes, ifdef_blocks)
        if written:
            output_list.extend(written)

        return output_list, includes_present, i, True
Recursively parses within #ifdef blocks for header includes and sorts
        them.

        Keyword arguments:
        config_file -- Config object
        lines_list -- list of file contents delimited by line separator
        file_name -- file name string
        start -- starting line for recursion step
        end -- ending line for recursion step
        ifdef_level -- current recursion depth of #ifdefs

        Returns tuple of the following:
          sorted output
          list of bools indicating header category presence within #ifdef block
          the index of the last line processed
          whether all header includes had header extension
        
Recursively parses within #ifdef blocks for header includes and sorts
them.



Returns tuple of the following:
sorted output
list of bools indicating header category presence within #ifdef block
the index of the last line processed
whether all header includes had header extension
Recursively parses within #ifdef blocks for header includes and sorts them . Returns tuple of the following : sorted output list of bools indicating header category presence within #ifdef block the index of the last line processed whether all header includes had header extension
def header_sort(self, config_file, lines_list, file_name, start, end,
                    ifdef_level):
        output_list = []
        includes = [[], [], [], [], []]
        includes_present = [False, False, False, False, False]
        ifdef_blocks = [[], [], [], [], []]
        i = start
        while i < end:
            if "#ifdef" in lines_list[i]:
                ifdef_count = 1
                for j in range(i + 1, end):
                    if "#ifdef" in lines_list[j]:
                        ifdef_count += 1
                    elif "#endif" in lines_list[j]:
                        ifdef_count -= 1
                    if ifdef_count == 0:
                        ifdef = lines_list[i] + self.linesep
                        suboutput, inc_present, idx, valid_headers = self.header_sort(
                            config_file, lines_list, file_name, i + 1, j,
                            ifdef_level + 1)
                        i = j
                        if not valid_headers:
                            return output_list, inc_present, i, False
                        if suboutput:
                            ifdef += self.linesep.join(suboutput) + self.linesep
                        ifdef += lines_list[j]
                        if "#endif" in lines_list[j]:
                            ifdef += self.linesep
                        for k in range(len(includes_present)):
                            includes_present[k] |= inc_present[k]
                        if sum(inc_present) == 1:
                            ifdef_idx = 0
                            for k in range(len(inc_present)):
                                if inc_present[k]:
                                    ifdef_idx = k
                                    break
                            ifdef_blocks[ifdef_idx].append(ifdef)
                        else:
                            output_list.extend(self.write_headers(includes))
                            if output_list and not output_list[-1].endswith(
                                    self.linesep):
                                output_list.append("")
                            includes = [[], [], [], [], []]
                            output_list.append(ifdef)
                        break
            elif "#include" in lines_list[i]:
                include_line = self.header_regex.search(lines_list[i])
                if not include_line:
                    written = self.write_headers(includes, ifdef_blocks)
                    if written:
                        output_list.extend(written)
                    return output_list, includes_present, i, True
                if "NOLINT" in lines_list[i]:
                    written = self.write_headers(includes)
                    if written:
                        output_list.append(self.linesep.join(written))
                        includes = [[], [], [], [], []]
                    if output_list and output_list[-1] != "":
                        output_list.append("")
                    output_list.append(lines_list[i])
                    output_list.append("")
                    i += 1
                    continue
                idx = self.classify_header(config_file, include_line, file_name)
                if idx != -1:
                    if include_line.group("comment"):
                        print("Warning: " + file_name + ": include '" +
                              include_line.group("name") + "' is commented out")
                    includes[idx].append(include_line)
                    includes_present[idx] = True
                else:
                    return output_list, includes_present, i, False
            elif ifdef_level > 0 and lines_list[i] != "":
                written = self.write_headers(includes)
                if written:
                    output_list.append(self.linesep.join(written))
                    includes = [[], [], [], [], []]
                output_list.append(lines_list[i])
            elif lines_list[i].strip() != "":
                written = self.write_headers(includes, ifdef_blocks)
                if written:
                    output_list.extend(written)
                return output_list, includes_present, i, True
            i += 1
        written = self.write_headers(includes, ifdef_blocks)
        if written:
            output_list.extend(written)
        return output_list, includes_present, i, True
def header_sort ( self , config_file , lines_list , file_name , start , end , ifdef_level ) : output_list = [ ] includes = [ [ ] , [ ] , [ ] , [ ] , [ ] ] includes_present = [ False , False , False , False , False ] ifdef_blocks = [ [ ] , [ ] , [ ] , [ ] , [ ] ] i = start while i < end : if "#ifdef" in lines_list [ i ] : ifdef_count = 1 for j in range ( i + 1 , end ) : if "#ifdef" in lines_list [ j ] : ifdef_count += 1 elif "#endif" in lines_list [ j ] : ifdef_count -= 1 if ifdef_count == 0 : ifdef = lines_list [ i ] + self . linesep suboutput , inc_present , idx , valid_headers = self . header_sort ( config_file , lines_list , file_name , i + 1 , j , ifdef_level + 1 ) i = j if not valid_headers : return output_list , inc_present , i , False if suboutput : ifdef += self . linesep . join ( suboutput ) + self . linesep ifdef += lines_list [ j ] if "#endif" in lines_list [ j ] : ifdef += self . linesep for k in range ( len ( includes_present ) ) : includes_present [ k ] |= inc_present [ k ] if sum ( inc_present ) == 1 : ifdef_idx = 0 for k in range ( len ( inc_present ) ) : if inc_present [ k ] : ifdef_idx = k break ifdef_blocks [ ifdef_idx ] . append ( ifdef ) else : output_list . extend ( self . write_headers ( includes ) ) if output_list and not output_list [ - 1 ] . endswith ( self . linesep ) : output_list . append ( "" ) includes = [ [ ] , [ ] , [ ] , [ ] , [ ] ] output_list . append ( ifdef ) break elif "#include" in lines_list [ i ] : include_line = self . header_regex . search ( lines_list [ i ] ) if not include_line : written = self . write_headers ( includes , ifdef_blocks ) if written : output_list . extend ( written ) return output_list , includes_present , i , True if "NOLINT" in lines_list [ i ] : written = self . write_headers ( includes ) if written : output_list . append ( self . linesep . join ( written ) ) includes = [ [ ] , [ ] , [ ] , [ ] , [ ] ] if output_list and output_list [ - 1 ] != "" : output_list . append ( "" ) output_list . append ( lines_list [ i ] ) output_list . append ( "" ) i += 1 continue idx = self . classify_header ( config_file , include_line , file_name ) if idx != - 1 : if include_line . group ( "comment" ) : print ( "Warning: " + file_name + ": include '" + include_line . group ( "name" ) + "' is commented out" ) includes [ idx ] . append ( include_line ) includes_present [ idx ] = True else : return output_list , includes_present , i , False elif ifdef_level > 0 and lines_list [ i ] != "" : written = self . write_headers ( includes ) if written : output_list . append ( self . linesep . join ( written ) ) includes = [ [ ] , [ ] , [ ] , [ ] , [ ] ] output_list . append ( lines_list [ i ] ) elif lines_list [ i ] . strip ( ) != "" : written = self . write_headers ( includes , ifdef_blocks ) if written : output_list . extend ( written ) return output_list , includes_present , i , True i += 1 written = self . write_headers ( includes , ifdef_blocks ) if written : output_list . extend ( written ) return output_list , includes_present , i , True
Recursively parses within #ifdef blocks for header includes and sorts
them.
Recursively parses within #ifdef blocks for header includes and sorts them .
"""Recursively parses within #ifdef blocks for header includes and sorts
        them.

        Keyword arguments:
        config_file -- Config object
        lines_list -- list of file contents delimited by line separator
        file_name -- file name string
        start -- starting line for recursion step
        end -- ending line for recursion step
        ifdef_level -- current recursion depth of #ifdefs

        Returns tuple of the following:
          sorted output
          list of bools indicating header category presence within #ifdef block
          the index of the last line processed
          whether all header includes had header extension
        """ # Using sets here eliminates duplicate includes # List of bools indicating header category presence within #ifdef block # Add #ifdef or #else line # If header failed to classify, return failure # Add #endif line # #endif gets an extra line separator # If there is only one type of include present # Only one type of include, so add it to sorts # Treat #ifdef as barrier and flush includes # Only add newline if content exists in # output_list that doesn't end in a newline, # which needs to be delimited from the #ifdef # Write headers and #ifdef blocks if found # NOLINT is a barrier, so flush includes # NOLINT line will have newlines above and below, unless it # is the first line being processed. # Insert header into appropriate list # If header failed to classify, return failure # Non-preprocessor statements within a #ifdef block don't mark # the end of header include processing, but act as barrier for # sorting. # Dump currently collected header includes to file. # includes_present isn't reset as well here because that info is # still valid and necessary for sorting. # Write headers and #ifdef blocks if found # Write headers and #ifdef blocks if found
self
config_file
lines_list
file_name
start
end
ifdef_level