예제 #1
0
    def apply(self, line, state):
        result = OperationResult(line, False)
        # strings = [] # string start and end positions as StringPos instances

        trimmed = stripComments(line)
        output = line
        expanded_line = line

        # find the directive
        dirsearch = regex['directive'].search(trimmed)
        if dirsearch:
            directive = dirsearch.group(1)
            identifier = dirsearch.group(2)

            origline = output
            output = commentLine(line)

            if directive == "#define":
                macro = Macro(dirsearch, trimmed)
                if macro != None:
                    state.macros[macro.name] = macro
            elif directive == "#undef":
                temp_macro = Macro(dirsearch, trimmed)
                if temp_macro.name in state.macros:
                    del state.macros[temp_macro.name]
                else:
                    warnings.add(
                        state.row, "Trying to undefine a nonexistent macro " +
                        temp_macro.name)
            elif directive == "#pragma":
                if state.args.verbose: print("pragma: " + identifier)
                state.args = handlePragma(identifier, state.args)
            elif directive == "#include":
                if state.args.filedir:
                    self.handleInclude(state, trimmed, state.args.filedir)
            else:
                # we'll leave #ifdef, #ifndef and #else to the other operators
                output = origline

        else:
            if state.args.nomacros:
                return result

            visited = Stack()

            #for name in self.macros:
            output = expandAll(line, state.macros, visited, state)

        result.line = output
        return result
예제 #2
0
def expandAll(string, macros, visited, state):
    macrokeys = getMacroKeys(macros, state)
    expanded = string
    line = string
    strings = []

    for name in macrokeys:
        m = macros[name]

        if m.flag:
            continue

        if visited.contains(name):
            warnings.add(state.row, "Skipping recursive macro call " + name)
            continue

        try_match = True
        while (try_match):
            try_match = False
            strings = scanForStrings(expanded)
            namematch = m.nameregex.finditer(expanded)

            for n in namematch:
                if checkIfInsideString(n.start('name'), strings):
                    continue

                if checkIfInsideComment(line, n.start('name')):
                    continue

                if state.args.verbose: infos.add(state.row, "\tfound " + name)

                visited.push(name)
                length = getInvocationLength(n, m)

                subs = m.expand(n)
                visited.pop()

                if not subs:
                    errors.add(state.row, "Invalid macro call " + name)
                    return expanded

                final = expandAll(subs.string, macros, visited, state)
                expanded = strInsert(expanded, subs.start, subs.end, final)

                if state.args.verbose == True:
                    infos.add(state.row, "Expand " + name + ": " + final)
                try_match = True
                break

    return expanded
예제 #3
0
def expandAll(string, macros, visited, state):
	macrokeys = getMacroKeys(macros, state)
	expanded = string
	line = string
	strings = []
	
	for name in macrokeys:
		m = macros[name]

		if m.flag:
			continue
			
		if visited.contains(name):
			warnings.add(state.row, "Skipping recursive macro call " + name)
			continue

		try_match = True
		while (try_match):
			try_match = False
			strings = scanForStrings(expanded)
			namematch = m.nameregex.finditer(expanded)

			for n in namematch:
				if checkIfInsideString(n.start('name'), strings):
					continue

				if checkIfInsideComment(line, n.start('name')):
					continue

				if state.args.verbose: infos.add(state.row, "\tfound " + name)

				visited.push(name)
				length = getInvocationLength(n, m)
				
				subs = m.expand(n)
				visited.pop()
				
				if not subs:
					errors.add(state.row, "Invalid macro call " + name)
					return expanded

				final = expandAll(subs.string, macros, visited, state)
				expanded = strInsert(expanded, subs.start, subs.end, final)
				
				if state.args.verbose == True: infos.add(state.row, "Expand " + name + ": " + final)
				try_match = True
				break
				
	return expanded
예제 #4
0
	def apply(self, line, state):
		result = OperationResult(line, False)
		# strings = [] # string start and end positions as StringPos instances

		trimmed = stripComments(line)
		output = line
		expanded_line = line

		# find the directive
		dirsearch = regex['directive'].search(trimmed)
		if dirsearch:
			directive = dirsearch.group(1)
			identifier = dirsearch.group(2)

			origline = output
			output = commentLine(line)

			if directive == "#define":
				macro = Macro(dirsearch, trimmed)
				if macro != None:
					state.macros[macro.name] = macro
			elif directive == "#undef":
				temp_macro = Macro(dirsearch, trimmed)
				if temp_macro.name in state.macros:
					del state.macros[temp_macro.name]
				else:
					warnings.add(state.row, "Trying to undefine a nonexistent macro " + temp_macro.name)					
			elif directive == "#pragma":
				if state.args.verbose: print("pragma: " + identifier)
				state.args = handlePragma(identifier, state.args)
			elif directive == "#include":
				if state.args.filedir:
					self.handleInclude(state, trimmed, state.args.filedir)
			else:
				# we'll leave #ifdef, #ifndef and #else to the other operators
				output = origline

		else:
			if state.args.nomacros:
				return result

			visited = Stack()

			#for name in self.macros:
			output = expandAll(line, state.macros, visited, state)

		result.line = output
		return result