2021-02-21 04:53:09 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-05-04 18:14:48 +00:00
|
|
|
# Script to check that headers are in a consistent order
|
2021-02-21 04:53:09 +00:00
|
|
|
# Canonical header order is defined in scripts/HeaderOrder.txt
|
|
|
|
# Requires Python 3.6 or later
|
2019-05-04 18:14:48 +00:00
|
|
|
|
2021-02-21 04:53:09 +00:00
|
|
|
import pathlib
|
2019-05-04 18:14:48 +00:00
|
|
|
|
2021-02-21 04:53:09 +00:00
|
|
|
patterns = [
|
|
|
|
"include/*.h",
|
|
|
|
"src/*.cxx",
|
|
|
|
"lexlib/*.cxx",
|
|
|
|
"lexers/*.cxx",
|
|
|
|
"win32/*.cxx",
|
|
|
|
"gtk/*.cxx",
|
|
|
|
"cocoa/*.mm",
|
|
|
|
"cocoa/*.h",
|
|
|
|
"test/unit/*.cxx",
|
|
|
|
"lexilla/src/*.cxx",
|
|
|
|
"lexilla/test/*.cxx",
|
|
|
|
]
|
2019-05-04 18:14:48 +00:00
|
|
|
|
|
|
|
def IsHeader(x):
|
2021-02-21 04:53:09 +00:00
|
|
|
return x.strip().startswith("#") and \
|
|
|
|
("include" in x or "import" in x) and \
|
|
|
|
"dllimport" not in x
|
|
|
|
|
|
|
|
def HeaderFromIncludeLine(s):
|
|
|
|
#\s*#\s*(include|import)\s+\S+\s*
|
|
|
|
return s.strip()[1:].strip()[7:].strip()
|
2019-05-04 18:14:48 +00:00
|
|
|
|
|
|
|
def ExtractHeaders(filename):
|
2021-02-21 04:53:09 +00:00
|
|
|
with filename.open(encoding="cp437") as infile:
|
|
|
|
return [HeaderFromIncludeLine(l) for l in infile if IsHeader(l)]
|
|
|
|
|
|
|
|
def ExcludeName(name):
|
|
|
|
# LexCaml adds system headers in #if to be an external lexer
|
|
|
|
# moc_ files are generated by Qt and follow its rules
|
|
|
|
return "LexCaml" in name or "moc_" in name
|
|
|
|
|
|
|
|
def SortLike(incs, order):
|
|
|
|
return sorted(incs, key = lambda i: order.index(i))
|
2019-05-04 18:14:48 +00:00
|
|
|
|
|
|
|
def CheckFiles(root):
|
|
|
|
# Find all the lexer source code files
|
2021-02-21 04:53:09 +00:00
|
|
|
filePaths = []
|
|
|
|
for p in patterns:
|
|
|
|
filePaths += root.glob(p)
|
2019-05-04 18:14:48 +00:00
|
|
|
# The Qt platform code interleaves system and Scintilla headers
|
2021-02-21 04:53:09 +00:00
|
|
|
#~ filePaths += root.glob("qt/ScintillaEditBase/*.cpp")
|
|
|
|
#~ filePaths += root.glob("qt/ScintillaEdit/*.cpp")
|
2019-05-04 18:14:48 +00:00
|
|
|
#~ print(filePaths)
|
2021-02-21 04:53:09 +00:00
|
|
|
scriptDirectory = root / "scripts"
|
|
|
|
headerOrderFile = scriptDirectory / "HeaderOrder.txt"
|
|
|
|
headerOrder = ExtractHeaders(headerOrderFile)
|
|
|
|
originalOrder = headerOrder[:]
|
|
|
|
orderedPaths = [p for p in sorted(filePaths) if not ExcludeName(str(p))]
|
|
|
|
allIncs = set()
|
|
|
|
for f in orderedPaths:
|
|
|
|
print(" File ", f.relative_to(root))
|
|
|
|
incs = ExtractHeaders(f)
|
2019-05-04 18:14:48 +00:00
|
|
|
#~ print("\n".join(incs))
|
2021-02-21 04:53:09 +00:00
|
|
|
news = set(incs) - set(headerOrder)
|
|
|
|
allIncs = allIncs.union(set(incs))
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
m = 0
|
|
|
|
i = 0
|
2021-02-21 04:53:09 +00:00
|
|
|
# Detect headers not in header order list and insert at OK position
|
|
|
|
needs = []
|
2019-05-04 18:14:48 +00:00
|
|
|
while i < len(incs):
|
2021-02-21 04:53:09 +00:00
|
|
|
if m == len(headerOrder):
|
|
|
|
#~ print("**** extend", incs[i:])
|
|
|
|
headerOrder.extend(incs[i:])
|
|
|
|
needs.extend(incs[i:])
|
2019-05-04 18:14:48 +00:00
|
|
|
break
|
2021-02-21 04:53:09 +00:00
|
|
|
if headerOrder[m] == incs[i]:
|
|
|
|
#~ print("equal", headerOrder[m])
|
2019-05-04 18:14:48 +00:00
|
|
|
i += 1
|
|
|
|
m += 1
|
|
|
|
else:
|
2021-02-21 04:53:09 +00:00
|
|
|
if headerOrder[m] not in incs:
|
|
|
|
#~ print("skip", headerOrder[m])
|
2019-05-04 18:14:48 +00:00
|
|
|
m += 1
|
2021-02-21 04:53:09 +00:00
|
|
|
elif incs[i] not in headerOrder:
|
|
|
|
#~ print(str(f) + ":1: Add master", incs[i])
|
|
|
|
headerOrder.insert(m, incs[i])
|
|
|
|
needs.append(incs[i])
|
2019-05-04 18:14:48 +00:00
|
|
|
i += 1
|
|
|
|
m += 1
|
|
|
|
else:
|
|
|
|
i += 1
|
2021-02-21 04:53:09 +00:00
|
|
|
if needs:
|
|
|
|
print(f"{f}:1: needs these headers:")
|
|
|
|
for header in needs:
|
|
|
|
print("#include " + header)
|
|
|
|
|
|
|
|
# Detect out of order
|
|
|
|
ordered = SortLike(incs, headerOrder)
|
|
|
|
if incs != ordered:
|
|
|
|
print(f"{f}:1: is out of order")
|
|
|
|
fOrdered = pathlib.Path(str(f) + ".ordered")
|
|
|
|
with fOrdered.open("w") as headerOut:
|
|
|
|
for header in ordered:
|
|
|
|
headerOut.write("#include " + header + "\n")
|
|
|
|
print(f"{fOrdered}:1: is ordered")
|
|
|
|
|
|
|
|
if headerOrder != originalOrder:
|
|
|
|
newIncludes = set(headerOrder) - set(originalOrder)
|
|
|
|
headerOrderNew = scriptDirectory / "NewOrder.txt"
|
|
|
|
print(f"{headerOrderFile}:1: changed to {headerOrderNew}")
|
|
|
|
print(f" Added {', '.join(newIncludes)}.")
|
|
|
|
with headerOrderNew.open("w") as headerOut:
|
|
|
|
for header in headerOrder:
|
|
|
|
headerOut.write("#include " + header + "\n")
|
|
|
|
|
|
|
|
unused = sorted(set(headerOrder) - allIncs)
|
|
|
|
if unused:
|
|
|
|
print("In HeaderOrder.txt but not used")
|
|
|
|
print("\n".join(unused))
|
2019-05-04 18:14:48 +00:00
|
|
|
|
2021-02-21 04:53:09 +00:00
|
|
|
CheckFiles(pathlib.Path(__file__).resolve().parent.parent)
|