Commit faa7e15b authored by Nico Weber's avatar Nico Weber

Enable -Wmisleading-indentation for chromium_code on Chrome OS and Chromecast builds.

This requires changing wayland/fuzzer/wayland_templater.py a bit.
-Wmisleading-indentation used to fire on harness.cc (see try jobs on
https://chromium-review.googlesource.com/c/chromium/src/+/2063736/1),
because harness.cc.tmpl is indentede according to jinja directive
level, not indented according to generated output code level.

That makes the .tmpl files readable, but the generated code looks weird.

As a fix, pipe the jinja output through clang-format before writing
it to disk, so that the output looks readable too (to both compilers
and humans). This is the approach we use in other code generators in
Chromium as well.

While here, also change the script to only write its output if
the contents change. This allows ninja to not run the compile step
for the generated cc files if the file contents don't change (due
to a no-op change to the generator, or due to a whitespace-only change
to the .tmpl file that clang-format now normalizes away).

The wayland_templater.py change is meant to be behavior-preserving.

Bug: 031169
Change-Id: I045070be3c1649e8752b6e09bb6655b1f3f60fb4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2066108Reviewed-by: default avatarNic Hollingum <hollingum@google.com>
Commit-Queue: Nico Weber <thakis@chromium.org>
Auto-Submit: Nico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743412}
parent 6d111ad6
......@@ -1533,13 +1533,6 @@ config("default_warnings") {
]
}
if (is_chromeos || is_chromecast) {
cflags += [
# TODO(https://crbug.com/1031169): Clean up and enable.
"-Wno-misleading-indentation",
]
}
if (llvm_force_head_revision) {
cflags += [
# TODO(https://crbug.com/1050281): Clean up, enable.
......
......@@ -12,6 +12,7 @@ from __future__ import absolute_import
from __future__ import print_function
import os
import subprocess
import sys
import jinja2
......@@ -38,6 +39,53 @@ cpp_type_conversions = {
}
def GetClangFormatPath():
"""Returns the path to clang-format, for formatting the output."""
if sys.platform.startswith('linux'):
platform, exe_suffix = 'linux64', ''
exe_suffix = ""
elif sys.platform == 'darwin':
platform, exe_suffix = 'mac', ''
elif sys.platform == 'win32':
platform, exe_suffix = 'win', '.exe'
else:
assert False, 'Unknown platform: ' + sys.platform
this_dir = os.path.abspath(os.path.dirname(__file__))
root_src_dir = os.path.abspath(
os.path.join(this_dir, '..', '..', '..', '..'))
buildtools_platform_dir = os.path.join(root_src_dir, 'buildtools', platform)
return os.path.join(buildtools_platform_dir, 'clang-format' + exe_suffix)
def ClangFormat(source, filename):
"""Runs clang-format on source and returns the result."""
# clang-format the output, for better readability and for
# -Wmisleading-indentation.
clang_format_cmd = [GetClangFormatPath(), '--assume-filename=' + filename]
proc = subprocess.Popen(
clang_format_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout_output, stderr_output = proc.communicate(input=source)
retcode = proc.wait()
if retcode != 0:
raise CalledProcessError(retcode, 'clang-format error: ' + stderr_output)
return stdout_output
def WriteIfChanged(contents, filename):
"""Writes contents to filename.
If filename already has the right contents, nothing is written so that
the mtime on filename doesn't change.
"""
if os.path.exists(filename):
with open(filename) as in_fi:
if in_fi.read() == contents:
return
with open(filename, 'w') as out_fi:
out_fi.write(contents)
def GetCppPtrType(interface_name):
"""Returns the c++ type associated with interfaces of the given name.
......@@ -176,8 +224,15 @@ def InstantiateTemplate(in_tmpl, in_ctx, output, in_directory):
lstrip_blocks=True,
trim_blocks=True) # so don't need {%- -%} everywhere
template = env.get_template(in_tmpl)
with open(output, 'w') as out_fi:
out_fi.write(template.render(in_ctx))
raw_output = template.render(in_ctx)
# For readability, and for -Wmisleading-indentation.
if output.endswith(('.h', '.cc', '.proto')):
formatted_output = ClangFormat(raw_output, filename=output)
else:
formatted_output = raw_output
WriteIfChanged(formatted_output, filename=output)
def main(argv):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment