aboutsummaryrefslogtreecommitdiff
path: root/examples/Tooling/replace.py
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2011-05-31 23:49:32 +0000
committerManuel Klimek <klimek@google.com>2011-05-31 23:49:32 +0000
commit64cbdf370984783911bb6d3bc25ec35a8b59e998 (patch)
treed9f1e5c11584852365aeef877c1f1a36db5548e3 /examples/Tooling/replace.py
parentd65e091631cc521fcb95a16d6587c0fed8b7164b (diff)
This patch implements an AST matching framework that allows to write
tools that match on the C++ ASTs. The main interface is in ASTMatchers.h, an example implementation of a tool that removes redundant .c_str() calls is in the example RemoveCStrCalls.cpp. Various contributions: Zhanyong Wan, Chandler Carruth, Marcin Kowalczyk, Wei Xu, James Dennett. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132374 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'examples/Tooling/replace.py')
-rwxr-xr-xexamples/Tooling/replace.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/Tooling/replace.py b/examples/Tooling/replace.py
new file mode 100755
index 0000000000..a738dea70c
--- /dev/null
+++ b/examples/Tooling/replace.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+#===- replace.py - Applying code rewrites --------------------*- python -*--===#
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+#===------------------------------------------------------------------------===#
+#
+# This script applies the rewrites generated by replace-cstr-calls on a source
+# tree.
+#
+# Usage:
+# ./replace.py < /path/to/replace-cstr-calls-output
+#
+#===------------------------------------------------------------------------===#
+
+import fileinput
+import re
+import sys
+
+for line in sys.stdin.readlines():
+ # The format is:
+ # <file>:<start_line>:<start_column>:<end_line>:<end_column>:<replacement>
+ # FIXME: This currently does not support files with colons, we'll need to
+ # figure out a format when we implement more refactoring support.
+ match = re.match(r'(.*):(\d+):(\d+):(\d+):(\d+):(.*)$', line)
+ if match is not None:
+ file_name = match.group(1)
+ start_line, start_column = int(match.group(2)), int(match.group(3))
+ end_line, end_column = int(match.group(4)), int(match.group(5))
+ replacement = match.group(6)
+ if start_line != end_line:
+ print ('Skipping match "%s": only single line ' +
+ 'replacements are supported') % line.strip()
+ continue
+ try:
+ replace_file = fileinput.input(file_name, inplace=1)
+ for replace_line in replace_file:
+ # FIXME: Looping over the file for each replacement is both inefficient
+ # and incorrect if replacements add or remove lines.
+ if replace_file.lineno() == start_line:
+ sys.stdout.write(replace_line[:start_column-1] + replacement +
+ replace_line[end_column:])
+ else:
+ sys.stdout.write(replace_line)
+ except OSError, e:
+ print 'Cannot open %s for editing' % file_name