Created
August 6, 2014 22:19
-
-
Save ptrv/e74c8d7bedf9650f25f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import sys | |
CONFIG_NAME = ".clang_complete" | |
def readConfiguration(): | |
try: | |
f = open(CONFIG_NAME, "r") | |
except IOError: | |
return [] | |
result = [] | |
for line in f.readlines(): | |
strippedLine = line.strip() | |
if strippedLine: | |
result.append(strippedLine) | |
f.close() | |
return result | |
def writeConfiguration(lines): | |
f = open(CONFIG_NAME, "w") | |
f.writelines(lines) | |
f.close() | |
def parseArguments(arguments): | |
nextIsInclude = False | |
nextIsDefine = False | |
nextIsIncludeFile = False | |
includes = [] | |
defines = [] | |
include_file = [] | |
options = [] | |
for arg in arguments: | |
if nextIsInclude: | |
includes += [arg] | |
nextIsInclude = False | |
elif nextIsDefine: | |
defines += [arg] | |
nextIsDefine = False | |
elif nextIsIncludeFile: | |
include_file += [arg] | |
nextIsIncludeFile = False | |
elif arg == "-I": | |
nextIsInclude = True | |
elif arg == "-D": | |
nextIsDefine = True | |
elif arg[:2] == "-I": | |
includes += [arg[2:]] | |
elif arg[:2] == "-D": | |
defines += [arg[2:]] | |
elif arg == "-include": | |
nextIsIncludeFile = True | |
elif arg.startswith('-std='): | |
options.append(arg) | |
elif arg.startswith('-W'): | |
options.append(arg) | |
result = list(map(lambda x: "-I" + x, includes)) | |
result.extend(map(lambda x: "-D" + x, defines)) | |
result.extend(map(lambda x: "-include " + x, include_file)) | |
result.extend(options) | |
return result | |
def mergeLists(base, new): | |
result = list(base) | |
for newLine in new: | |
if newLine not in result: | |
result.append(newLine) | |
return result | |
############################################################ | |
with open(sys.argv[1]) as f: | |
for line in f: | |
configuration = readConfiguration() | |
args = parseArguments(line.split()) | |
result = mergeLists(configuration, args) | |
writeConfiguration(map(lambda x: x + "\n", result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment