2019-03-27 02:07:55 +01:00
|
|
|
#!/usr/bin/python
|
2018-06-21 20:29:00 +02:00
|
|
|
# Copyright 2018 The Tor Project, Inc. See LICENSE file for licensing info.
|
2018-06-21 20:02:11 +02:00
|
|
|
|
2018-07-09 22:02:31 +02:00
|
|
|
"""This script looks through all the directories for files matching *.c or
|
|
|
|
*.h, and checks their #include directives to make sure that only "permitted"
|
|
|
|
headers are included.
|
|
|
|
|
|
|
|
Any #include directives with angle brackets (like #include <stdio.h>) are
|
|
|
|
ignored -- only directives with quotes (like #include "foo.h") are
|
|
|
|
considered.
|
|
|
|
|
|
|
|
To decide what includes are permitted, this script looks at a .may_include
|
|
|
|
file in each directory. This file contains empty lines, #-prefixed
|
|
|
|
comments, filenames (like "lib/foo/bar.h") and file globs (like lib/*/*.h)
|
|
|
|
for files that are permitted.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2018-07-09 21:46:39 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-06-21 20:02:11 +02:00
|
|
|
import fnmatch
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
2018-07-09 22:02:31 +02:00
|
|
|
# Global: Have there been any errors?
|
2018-06-21 20:02:11 +02:00
|
|
|
trouble = False
|
|
|
|
|
2018-08-28 22:25:15 +02:00
|
|
|
if sys.version_info[0] <= 2:
|
|
|
|
def open_file(fname):
|
|
|
|
return open(fname, 'r')
|
|
|
|
else:
|
|
|
|
def open_file(fname):
|
|
|
|
return open(fname, 'r', encoding='utf-8')
|
|
|
|
|
2018-11-07 16:30:46 +01:00
|
|
|
def warn(msg):
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
2018-06-21 20:02:11 +02:00
|
|
|
def err(msg):
|
2018-07-09 22:02:31 +02:00
|
|
|
""" Declare that an error has happened, and remember that there has
|
|
|
|
been an error. """
|
2018-06-21 20:02:11 +02:00
|
|
|
global trouble
|
|
|
|
trouble = True
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
|
|
|
|
def fname_is_c(fname):
|
2018-07-09 22:02:31 +02:00
|
|
|
""" Return true iff 'fname' is the name of a file that we should
|
|
|
|
search for possibly disallowed #include directives. """
|
2018-06-21 20:02:11 +02:00
|
|
|
return fname.endswith(".h") or fname.endswith(".c")
|
|
|
|
|
|
|
|
INCLUDE_PATTERN = re.compile(r'\s*#\s*include\s+"([^"]*)"')
|
|
|
|
RULES_FNAME = ".may_include"
|
|
|
|
|
2018-11-07 16:30:46 +01:00
|
|
|
ALLOWED_PATTERNS = [
|
|
|
|
re.compile(r'^.*\*\.(h|inc)$'),
|
|
|
|
re.compile(r'^.*/.*\.h$'),
|
|
|
|
re.compile(r'^ext/.*\.c$'),
|
|
|
|
re.compile(r'^orconfig.h$'),
|
|
|
|
re.compile(r'^micro-revision.i$'),
|
|
|
|
]
|
|
|
|
|
|
|
|
def pattern_is_normal(s):
|
|
|
|
for p in ALLOWED_PATTERNS:
|
|
|
|
if p.match(s):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-06-21 20:02:11 +02:00
|
|
|
class Rules(object):
|
2018-07-09 22:02:31 +02:00
|
|
|
""" A 'Rules' object is the parsed version of a .may_include file. """
|
2018-07-02 00:14:28 +02:00
|
|
|
def __init__(self, dirpath):
|
|
|
|
self.dirpath = dirpath
|
2018-11-07 16:30:46 +01:00
|
|
|
if dirpath.startswith("src/"):
|
|
|
|
self.incpath = dirpath[4:]
|
|
|
|
else:
|
|
|
|
self.incpath = dirpath
|
2018-06-21 20:02:11 +02:00
|
|
|
self.patterns = []
|
2018-07-02 00:14:28 +02:00
|
|
|
self.usedPatterns = set()
|
2018-06-21 20:02:11 +02:00
|
|
|
|
|
|
|
def addPattern(self, pattern):
|
2018-11-07 16:30:46 +01:00
|
|
|
if not pattern_is_normal(pattern):
|
|
|
|
warn("Unusual pattern {} in {}".format(pattern, self.dirpath))
|
2018-06-21 20:02:11 +02:00
|
|
|
self.patterns.append(pattern)
|
|
|
|
|
|
|
|
def includeOk(self, path):
|
|
|
|
for pattern in self.patterns:
|
|
|
|
if fnmatch.fnmatchcase(path, pattern):
|
2018-07-02 00:14:28 +02:00
|
|
|
self.usedPatterns.add(pattern)
|
2018-06-21 20:02:11 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def applyToLines(self, lines, context=""):
|
|
|
|
lineno = 0
|
|
|
|
for line in lines:
|
|
|
|
lineno += 1
|
|
|
|
m = INCLUDE_PATTERN.match(line)
|
|
|
|
if m:
|
|
|
|
include = m.group(1)
|
|
|
|
if not self.includeOk(include):
|
|
|
|
err("Forbidden include of {} on line {}{}".format(
|
|
|
|
include, lineno, context))
|
|
|
|
|
|
|
|
def applyToFile(self, fname):
|
2018-08-28 22:25:15 +02:00
|
|
|
with open_file(fname) as f:
|
2018-06-21 20:02:11 +02:00
|
|
|
#print(fname)
|
|
|
|
self.applyToLines(iter(f), " of {}".format(fname))
|
|
|
|
|
2018-07-02 00:14:28 +02:00
|
|
|
def noteUnusedRules(self):
|
|
|
|
for p in self.patterns:
|
|
|
|
if p not in self.usedPatterns:
|
|
|
|
print("Pattern {} in {} was never used.".format(p, self.dirpath))
|
|
|
|
|
2018-11-07 16:30:46 +01:00
|
|
|
def getAllowedDirectories(self):
|
|
|
|
allowed = []
|
|
|
|
for p in self.patterns:
|
|
|
|
m = re.match(r'^(.*)/\*\.(h|inc)$', p)
|
|
|
|
if m:
|
|
|
|
allowed.append(m.group(1))
|
|
|
|
continue
|
|
|
|
m = re.match(r'^(.*)/[^/]*$', p)
|
|
|
|
if m:
|
|
|
|
allowed.append(m.group(1))
|
|
|
|
continue
|
|
|
|
|
|
|
|
return allowed
|
|
|
|
|
2018-06-21 20:02:11 +02:00
|
|
|
def load_include_rules(fname):
|
2018-07-09 22:02:31 +02:00
|
|
|
""" Read a rules file from 'fname', and return it as a Rules object. """
|
2018-07-02 00:14:28 +02:00
|
|
|
result = Rules(os.path.split(fname)[0])
|
2018-08-28 22:25:15 +02:00
|
|
|
with open_file(fname) as f:
|
2018-06-21 20:02:11 +02:00
|
|
|
for line in f:
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("#") or not line:
|
|
|
|
continue
|
|
|
|
result.addPattern(line)
|
|
|
|
return result
|
|
|
|
|
2018-07-02 00:14:28 +02:00
|
|
|
list_unused = False
|
2018-11-07 16:59:23 +01:00
|
|
|
log_sorted_levels = False
|
2018-07-02 00:14:28 +02:00
|
|
|
|
2018-11-07 16:30:46 +01:00
|
|
|
uses_dirs = { }
|
|
|
|
|
2018-06-21 20:02:11 +02:00
|
|
|
for dirpath, dirnames, fnames in os.walk("src"):
|
|
|
|
if ".may_include" in fnames:
|
|
|
|
rules = load_include_rules(os.path.join(dirpath, RULES_FNAME))
|
2018-06-26 18:04:24 +02:00
|
|
|
for fname in fnames:
|
|
|
|
if fname_is_c(fname):
|
|
|
|
rules.applyToFile(os.path.join(dirpath,fname))
|
2018-07-02 00:14:28 +02:00
|
|
|
if list_unused:
|
|
|
|
rules.noteUnusedRules()
|
2018-06-21 20:02:11 +02:00
|
|
|
|
2018-11-07 16:30:46 +01:00
|
|
|
uses_dirs[rules.incpath] = rules.getAllowedDirectories()
|
|
|
|
|
2018-06-21 20:02:11 +02:00
|
|
|
if trouble:
|
|
|
|
err(
|
2018-07-09 21:47:46 +02:00
|
|
|
"""To change which includes are allowed in a C file, edit the {}
|
|
|
|
files in its enclosing directory.""".format(RULES_FNAME))
|
2018-06-21 20:29:00 +02:00
|
|
|
sys.exit(1)
|
2018-11-07 16:30:46 +01:00
|
|
|
|
|
|
|
all_levels = []
|
|
|
|
|
|
|
|
n = 0
|
|
|
|
while uses_dirs:
|
|
|
|
n += 0
|
|
|
|
cur_level = []
|
|
|
|
for k in list(uses_dirs):
|
|
|
|
uses_dirs[k] = [ d for d in uses_dirs[k]
|
|
|
|
if (d in uses_dirs and d != k)]
|
|
|
|
if uses_dirs[k] == []:
|
|
|
|
cur_level.append(k)
|
|
|
|
for k in cur_level:
|
|
|
|
del uses_dirs[k]
|
|
|
|
n += 1
|
2018-11-07 16:59:23 +01:00
|
|
|
if cur_level and log_sorted_levels:
|
2018-11-07 16:30:46 +01:00
|
|
|
print(n, cur_level)
|
|
|
|
if n > 100:
|
|
|
|
break
|
|
|
|
|
|
|
|
if uses_dirs:
|
2018-11-07 16:59:23 +01:00
|
|
|
print("There are circular .may_include dependencies in here somewhere:",
|
|
|
|
uses_dirs)
|
2018-11-07 16:30:46 +01:00
|
|
|
sys.exit(1)
|