Qijiang Fan | 4add22e | 2020-11-14 03:00:42 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """ |
| 7 | Usage: ag DISALLOW_COPY_AND_ASSIGN -l | xargs -n 1 python[23] ./disallow-copy-and-assign.py |
| 8 | |
| 9 | Please note, this script doesn't guanrantee all usages are correctly replaced. |
| 10 | Result must be checked by humans. |
| 11 | Known issues: |
| 12 | 1. It may put in the middle of inlined complex constructor function body. |
| 13 | 2. It may failed to locate constructor and put the line after public: if |
| 14 | constructor is complex (especially member initialization). |
| 15 | 3. It may failed to locate public: and not altering the file. |
| 16 | 4. It may put the code into protected or private if the original code has a |
| 17 | protected or private constructor. |
| 18 | |
| 19 | This script doesn't format code, please run: |
| 20 | src/repohooks/clang-format.py --fix . --working |
| 21 | to format code with clang-foramt. |
| 22 | |
| 23 | Also the script doesn't delete empty lines or empty priavte: section. |
| 24 | Scripts like: |
| 25 | git diff-tree -r HEAD | awk '{print $6;}' | xargs perl -0 -i -pe 's/ *private:\n*}/}/g' |
| 26 | git diff-tree -r HEAD | awk '{print $6;}' | xargs perl -0 -i -pe 's/\n\n};/\n};/g' |
| 27 | are recommend AFTER git commit is made (and you can amend commit later). |
| 28 | |
| 29 | You can use the following validity check script to help locate known issue (1): |
| 30 | git diff m/master | egrep -o '^[+-] +([A-Za-z0-9]+\(|DISALLOW_COPY_AND_ASSIGN).*' |
| 31 | The script helps you quickly filter only diffs that are not with exact 2-space |
| 32 | indent. And you can look at if the result is in pairs. |
| 33 | """ |
| 34 | |
| 35 | import re |
| 36 | import sys |
| 37 | |
| 38 | |
| 39 | def main(): |
| 40 | content = open(sys.argv[1], 'r').read() |
| 41 | |
| 42 | while True: |
| 43 | found = re.search(r'^ *DISALLOW_COPY_AND_ASSIGN\(([^)]*)\);', content, |
| 44 | re.MULTILINE) |
| 45 | if not found: |
| 46 | break |
| 47 | classname = found.group(1) |
| 48 | found_constructors = list( |
| 49 | re.compile( |
| 50 | '^ *(explicit )?' + classname + '\(([^;{]*;|[^;{]*{[^}]*})$', |
| 51 | re.MULTILINE).finditer(content, 0, found.start())) |
| 52 | if found_constructors: |
| 53 | # Pick the last constructor. |
| 54 | insertion_point = found_constructors[-1].end() |
| 55 | else: |
| 56 | # If no constructors are found, look for public:. |
| 57 | insertion_point = list( |
| 58 | re.compile('class ' + classname + r' .*{[ \n]*public:\n', |
| 59 | re.MULTILINE).finditer(content, 0, |
| 60 | found.start()))[-1].end() |
| 61 | content = ('%(prefix)s' + '%(name)s(const %(name)s&) = delete;\n' + |
| 62 | '%(name)s& operator=(const %(name)s&) = delete;\n' |
| 63 | '%(middle)s%(suffix)s') % { |
| 64 | 'prefix': content[0:insertion_point + 1], |
| 65 | 'name': classname, |
| 66 | 'middle': content[insertion_point:found.start()], |
| 67 | 'suffix': content[found.end() + 1:], |
| 68 | } |
| 69 | open(sys.argv[1], 'w').write(content) |
| 70 | |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | main() |