Skip to content

Instantly share code, notes, and snippets.

@jlisee
Created February 14, 2015 20:15
Show Gist options
  • Save jlisee/a255c8c7a6f65d3b31fa to your computer and use it in GitHub Desktop.
Save jlisee/a255c8c7a6f65d3b31fa to your computer and use it in GitHub Desktop.
Diffs a C++ file, or directory of C++ files with the results of running those files through clang-format.
#! /usr/bin/env python
# Author: Joseph Lisee <[email protected]>
# Purpose: Test out various clang formatting options
# Usage: compare-format.py <diff-tool> <input-path> [<clang-options]
# Example:
# compare-format.py meld file.cpp "-style={BasedOnStyle: Google, IndentWidth: 4}"
import os
import subprocess
import sys
import tempfile
# Generate a temporary file
diff_tool = sys.argv[1].split(' ')
input_path = sys.argv[2]
clang_opts = sys.argv[3:]
# Get the list of files to process
valid_extension = set(['hh', 'h', 'hpp', 'c++', 'c', 'cpp', 'cc', 'C', 'H'])
if os.path.isdir(input_path):
to_process = []
for root, dirs, files in os.walk(input_path, topdown=True):
for name in files:
_, ext = os.path.splitext(name)
if ext[1:] in valid_extension:
to_process.append(os.path.join(root, name))
else:
to_process = [input_path]
# Process our files:
for file_path in to_process:
#print file_path
with tempfile.NamedTemporaryFile() as ofile:
# Run clang format sending output to temp
subprocess.check_call(['clang-format', file_path] + clang_opts,
stdout=ofile)
# Run meld to compare the results
subprocess.call(diff_tool + [file_path, ofile.name])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment