|
@@ -527,6 +527,7 @@ _error_suppressions = {}
|
|
# The root directory used for deriving header guard CPP variable.
|
|
# The root directory used for deriving header guard CPP variable.
|
|
# This is set by --root flag.
|
|
# This is set by --root flag.
|
|
_root = None
|
|
_root = None
|
|
|
|
+_root_debug = False
|
|
|
|
|
|
# The project root directory. Used for deriving header guard CPP variable.
|
|
# The project root directory. Used for deriving header guard CPP variable.
|
|
# This is set by --project_root flag. Must be an absolute path.
|
|
# This is set by --project_root flag. Must be an absolute path.
|
|
@@ -1735,6 +1736,32 @@ def GetIndentLevel(line):
|
|
return 0
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
+def PathSplitToList(path):
|
|
|
|
+ """Returns the path split into a list by the separator.
|
|
|
|
+
|
|
|
|
+ Args:
|
|
|
|
+ path: An absolute or relative path (e.g. '/a/b/c/' or '../a')
|
|
|
|
+
|
|
|
|
+ Returns:
|
|
|
|
+ A list of path components (e.g. ['a', 'b', 'c]).
|
|
|
|
+ """
|
|
|
|
+ lst = []
|
|
|
|
+ while True:
|
|
|
|
+ (head, tail) = os.path.split(path)
|
|
|
|
+ if head == path: # absolute paths end
|
|
|
|
+ lst.append(head)
|
|
|
|
+ break
|
|
|
|
+ if tail == path: # relative paths end
|
|
|
|
+ lst.append(tail)
|
|
|
|
+ break
|
|
|
|
+
|
|
|
|
+ path = head
|
|
|
|
+ lst.append(tail)
|
|
|
|
+
|
|
|
|
+ lst.reverse()
|
|
|
|
+ return lst
|
|
|
|
+
|
|
|
|
+
|
|
def GetHeaderGuardCPPVariable(filename):
|
|
def GetHeaderGuardCPPVariable(filename):
|
|
"""Returns the CPP variable that should be used as a header guard.
|
|
"""Returns the CPP variable that should be used as a header guard.
|
|
|
|
|
|
@@ -1756,8 +1783,59 @@ def GetHeaderGuardCPPVariable(filename):
|
|
|
|
|
|
fileinfo = FileInfo(filename)
|
|
fileinfo = FileInfo(filename)
|
|
file_path_from_root = fileinfo.RepositoryName()
|
|
file_path_from_root = fileinfo.RepositoryName()
|
|
- if _root:
|
|
|
|
- file_path_from_root = re.sub('^' + _root + os.sep, '', file_path_from_root)
|
|
|
|
|
|
+
|
|
|
|
+ def FixupPathFromRoot():
|
|
|
|
+ if _root_debug:
|
|
|
|
+ sys.stderr.write("\n_root fixup, _root = '%s', repository name = '%s'\n"
|
|
|
|
+ % (_root, fileinfo.RepositoryName()))
|
|
|
|
+
|
|
|
|
+ # Process the file path with the --root flag if it was set.
|
|
|
|
+ if not _root:
|
|
|
|
+ if _root_debug:
|
|
|
|
+ sys.stderr.write("_root unspecified\n")
|
|
|
|
+ return file_path_from_root
|
|
|
|
+
|
|
|
|
+ def StripListPrefix(lst, prefix):
|
|
|
|
+ # f(['x', 'y'], ['w, z']) -> None (not a valid prefix)
|
|
|
|
+ if lst[:len(prefix)] != prefix:
|
|
|
|
+ return None
|
|
|
|
+ # f(['a, 'b', 'c', 'd'], ['a', 'b']) -> ['c', 'd']
|
|
|
|
+ return lst[(len(prefix)):]
|
|
|
|
+
|
|
|
|
+ # root behavior:
|
|
|
|
+ # --root=subdir , lstrips subdir from the header guard
|
|
|
|
+ maybe_path = StripListPrefix(PathSplitToList(file_path_from_root),
|
|
|
|
+ PathSplitToList(_root))
|
|
|
|
+
|
|
|
|
+ if _root_debug:
|
|
|
|
+ sys.stderr.write(("_root lstrip (maybe_path=%s, file_path_from_root=%s," +
|
|
|
|
+ " _root=%s)\n") % (maybe_path, file_path_from_root, _root))
|
|
|
|
+
|
|
|
|
+ if maybe_path:
|
|
|
|
+ return os.path.join(*maybe_path)
|
|
|
|
+
|
|
|
|
+ # --root=.. , will prepend the outer directory to the header guard
|
|
|
|
+ full_path = fileinfo.FullName()
|
|
|
|
+ # adapt slashes for windows
|
|
|
|
+ root_abspath = os.path.abspath(_root).replace('\\', '/')
|
|
|
|
+
|
|
|
|
+ maybe_path = StripListPrefix(PathSplitToList(full_path),
|
|
|
|
+ PathSplitToList(root_abspath))
|
|
|
|
+
|
|
|
|
+ if _root_debug:
|
|
|
|
+ sys.stderr.write(("_root prepend (maybe_path=%s, full_path=%s, " +
|
|
|
|
+ "root_abspath=%s)\n") % (maybe_path, full_path, root_abspath))
|
|
|
|
+
|
|
|
|
+ if maybe_path:
|
|
|
|
+ return os.path.join(*maybe_path)
|
|
|
|
+
|
|
|
|
+ if _root_debug:
|
|
|
|
+ sys.stderr.write("_root ignore, returning %s\n" % (file_path_from_root))
|
|
|
|
+
|
|
|
|
+ # --root=FAKE_DIR is ignored
|
|
|
|
+ return file_path_from_root
|
|
|
|
+
|
|
|
|
+ file_path_from_root = FixupPathFromRoot()
|
|
return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
|
|
return re.sub(r'[^a-zA-Z0-9]', '_', file_path_from_root).upper() + '_'
|
|
|
|
|
|
|
|
|