Skip to content

Commit

Permalink
Have run_tool.py automatically invoke clang-format-diff.py if available.
Browse files Browse the repository at this point in the history
This makes for nicer code and removes the extra post-processing step.
It also provides more granular warnings; simply returning the formatter
across the entire diff will provide cryptic errors like missing { at
line 540.

BUG=

Review URL: https://codereview.chromium.org/13605003

git-svn-id: http://src.chromium.org/svn/trunk/src/tools/clang@192359 4ff67af0-8c30-449e-8e8b-ad334ec8d88c
  • Loading branch information
dcheng@chromium.org committed Apr 4, 2013
1 parent f9bbb6b commit d8b56a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
13 changes: 6 additions & 7 deletions empty_string/tests/test-expected.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ void TestDeclarations() { std::string a, b("abc"), c; }
// Tests for std::string allocated with new.
void TestNew() {
std::string* a = new std::string,
*b = new std::string("abc"),
*c = new std::string,
*d = new std::string();
*b = new std::string("abc"),
*c = new std::string,
*d = new std::string();
}

// Tests for std::string construction in initializer lists.
class TestInitializers {
public:
TestInitializers() {}
TestInitializers(bool) {}
TestInitializers(double) : b("cat"), c() {}
TestInitializers() {}
TestInitializers(bool) {}
TestInitializers(double) : b("cat"), c() {}

private:
std::string a;
Expand All @@ -43,4 +43,3 @@ void TestWideTemporaries(const std::wstring& reference_argument,
TestWideTemporaries(std::wstring(), std::wstring());
TestWideTemporaries(std::wstring(), std::wstring());
}

22 changes: 18 additions & 4 deletions scripts/run_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,14 @@ def __ProcessResult(self, result):
sys.stdout.flush()


def _ApplyEdits(edits):
def _ApplyEdits(edits, clang_format_diff_path):
"""Apply the generated edits.
Args:
edits: A dict mapping filenames to Edit instances that apply to that file.
clang_format_diff_path: Path to the clang-format-diff.py helper to help
automatically reformat diffs to avoid style violations. Pass None if the
clang-format step should be skipped.
"""
edit_count = 0
for k, v in edits.iteritems():
Expand All @@ -210,6 +213,10 @@ def _ApplyEdits(edits):
f.seek(0)
f.truncate()
f.write(contents)
if clang_format_diff_path:
if subprocess.call('git diff -U0 %s | python %s -style=Chromium' % (
k, clang_format_diff_path), shell=True) != 0:
print 'clang-format failed for %s' % k
print 'Applied %d edits to %d files' % (edit_count, len(edits))


Expand Down Expand Up @@ -266,6 +273,14 @@ def main(argv):
print ' <path 1> <path2> ... can be used to filter what files are edited'
return 1

clang_format_diff_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'../../../third_party/llvm/tools/clang/tools/clang-format',
'clang-format-diff.py')
# TODO(dcheng): Allow this to be controlled with a flag as well.
if not os.path.isfile(clang_format_diff_path):
clang_format_diff_path = None

filenames = frozenset(_GetFilesFromGit(argv[2:]))
# Filter out files that aren't C/C++/Obj-C/Obj-C++.
extensions = frozenset(('.c', '.cc', '.m', '.mm'))
Expand All @@ -277,9 +292,8 @@ def main(argv):
# useful to modify files that aren't under source control--typically, these
# are generated files or files in a git submodule that's not part of Chromium.
_ApplyEdits({k : v for k, v in dispatcher.edits.iteritems()
if k in filenames})
# TODO(dcheng): Consider clang-formatting the result to avoid egregious style
# violations.
if k in filenames},
clang_format_diff_path)
if dispatcher.failed_count != 0:
return 2
return 0
Expand Down

0 comments on commit d8b56a6

Please sign in to comment.