Skip to content

Instantly share code, notes, and snippets.

@d0k
Created February 11, 2014 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d0k/8939686 to your computer and use it in GitHub Desktop.
Save d0k/8939686 to your computer and use it in GitHub Desktop.
LLVM test linter
#!/usr/bin/env python
import os
import re
import sys
comments = [";", "//", "#"]
fileendings = re.compile("\.(ll|cpp|c)$")
prefixcheck = re.compile("-check-prefix[ =]([A-Za-z0-9_.\-]*)")
placeholders = re.compile("%[sStTp]")
class Parser:
def __init__(self, filename):
self.filename = filename
self.checkPrefixes = set()
self.continued = False
def run(self):
with open(filename) as f:
self.lineno = 0
for line in f.readlines():
commentlen = len(self._startswith(line, comments))
if commentlen == 0:
next
self.line = line
stripped = line[commentlen:].strip().upper()
if stripped.startswith("RUN"):
self.checkRunLine()
prefix = self._startswith(stripped, self.checkPrefixes)
if len(prefix) != 0:
self.checkCheckLine(prefix)
self.lineno += 1
def _startswith(self, line, strings):
for s in strings:
if line.startswith(s):
return s
return ""
def _warn(self, message):
print "%s:%d: %s"%(self.filename, self.lineno, message)
print " ", self.line
def checkRunLine(self):
stripped = self.line.strip()
if stripped.endswith("true") or stripped.endswith("false"):
return
if not "RUN:" in stripped:
self._warn("RUN line won't be run.")
if not self.continued and not placeholders.search(stripped):
self._warn("RUN line doesn't use placeholder.")
prefix = prefixcheck.search(stripped)
new = ""
if prefix:
new = prefix.group(1).upper()
else:
if "FileCheck" in stripped:
new = "CHECK"
if len(new) != 0:
for p in self.checkPrefixes:
if new.startswith(p):
self.checkPrefixes.remove(p)
break
self.checkPrefixes.add(new)
self.continued = stripped[-1] == "\\"
def checkCheckLine(self, prefix):
stripped = self.line.strip()
if "Check" in stripped:
return
if not re.search(prefix + "(:|-NOT:|-NEXT|-LABEL:|-DAG:)",
stripped.upper()):
self._warn("CHECK line doesn't check anything")
if __name__ == '__main__':
for path, dirs, files in os.walk(sys.argv[1]):
for filename in [os.path.abspath(os.path.join(path, filename)) for filename in files if fileendings.search(filename)]:
Parser(filename).run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment