Skip to content

Commit

Permalink
[include-fixer] Python 3 support for clang-include-fixer.py
Browse files Browse the repository at this point in the history
Patch by Yannick Brehon!
  • Loading branch information
d0k committed Nov 28, 2019
1 parent 42c857a commit 6623788
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py
Expand Up @@ -17,6 +17,7 @@
# It operates on the current, potentially unsaved buffer and does not create
# or save any files. To revert a fix, just undo.

from __future__ import print_function
import argparse
import difflib
import json
Expand Down Expand Up @@ -79,7 +80,7 @@ def GetUserSelection(message, headers, maximum_suggested_headers):
except Exception:
# Show a new prompt on invalid option instead of aborting so that users
# don't need to wait for another clang-include-fixer run.
print >> sys.stderr, "Invalid option:", res
print("Invalid option: {}".format(res), file=sys.stderr)
return GetUserSelection(message, headers, maximum_suggested_headers)
return headers[idx - 1]

Expand All @@ -95,7 +96,7 @@ def execute(command, text):
p = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
stdin=subprocess.PIPE, startupinfo=startupinfo)
return p.communicate(input=text)
return p.communicate(input=text.encode('utf-8'))


def InsertHeaderToVimBuffer(header, text):
Expand Down Expand Up @@ -159,7 +160,7 @@ def main():
if query_mode:
symbol = get_symbol_under_cursor()
if len(symbol) == 0:
print "Skip querying empty symbol."
print("Skip querying empty symbol.")
return
command = [binary, "-stdin", "-query-symbol="+get_symbol_under_cursor(),
"-db=" + args.db, "-input=" + args.input,
Expand All @@ -170,13 +171,14 @@ def main():
"-input=" + args.input, vim.current.buffer.name]
stdout, stderr = execute(command, text)
if stderr:
print >> sys.stderr, "Error while running clang-include-fixer: " + stderr
print("Error while running clang-include-fixer: {}".format(stderr),
file=sys.stderr)
return

include_fixer_context = json.loads(stdout)
query_symbol_infos = include_fixer_context["QuerySymbolInfos"]
if not query_symbol_infos:
print "The file is fine, no need to add a header."
print("The file is fine, no need to add a header.")
return
symbol = query_symbol_infos[0]["RawIdentifier"]
# The header_infos is already sorted by clang-include-fixer.
Expand All @@ -192,7 +194,7 @@ def main():
unique_headers.append(header)

if not unique_headers:
print "Couldn't find a header for {0}.".format(symbol)
print("Couldn't find a header for {0}.".format(symbol))
return

try:
Expand All @@ -207,9 +209,9 @@ def main():
include_fixer_context["HeaderInfos"] = inserted_header_infos

InsertHeaderToVimBuffer(include_fixer_context, text)
print "Added #include {0} for {1}.".format(selected, symbol)
print("Added #include {0} for {1}.".format(selected, symbol))
except Exception as error:
print >> sys.stderr, error.message
print(error.message, file=sys.stderr)
return


Expand Down

0 comments on commit 6623788

Please sign in to comment.