[clang-tools-extra] 6623788 - [include-fixer] Python 3 support for clang-include-fixer.py

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 28 05:22:43 PST 2019


Author: Benjamin Kramer
Date: 2019-11-28T14:22:21+01:00
New Revision: 66237889a79f728fffc96394740b975774de26bf

URL: https://github.com/llvm/llvm-project/commit/66237889a79f728fffc96394740b975774de26bf
DIFF: https://github.com/llvm/llvm-project/commit/66237889a79f728fffc96394740b975774de26bf.diff

LOG: [include-fixer] Python 3 support for clang-include-fixer.py

Patch by Yannick Brehon!

Added: 
    

Modified: 
    clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py b/clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py
index df05101e4fd8..fcdd5a0b60ee 100644
--- a/clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py
+++ b/clang-tools-extra/clang-include-fixer/tool/clang-include-fixer.py
@@ -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 
diff lib
 import json
@@ -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]
 
@@ -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):
@@ -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,
@@ -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.
@@ -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:
@@ -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
 
 


        


More information about the cfe-commits mailing list