| 138 | |
| 139 | |
| 140 | def run_clang_format_diff(args: Any, file: str) -> tuple[list[str], list[str]]: |
| 141 | try: |
| 142 | with io.open(file, "r", encoding="utf-8") as f: |
| 143 | original = f.readlines() |
| 144 | except IOError as exc: |
| 145 | raise DiffError(str(exc)) |
| 146 | |
| 147 | if args.in_place: |
| 148 | invocation = [args.clang_format_executable, "-i", file] |
| 149 | else: |
| 150 | invocation = [args.clang_format_executable, file] |
| 151 | |
| 152 | if args.style: |
| 153 | invocation.extend(["--style", args.style]) |
| 154 | |
| 155 | if args.dry_run: |
| 156 | print(" ".join(invocation)) |
| 157 | return [], [] |
| 158 | |
| 159 | # Use of utf-8 to decode the process output. |
| 160 | # |
| 161 | # Hopefully, this is the correct thing to do. |
| 162 | # |
| 163 | # It's done due to the following assumptions (which may be incorrect): |
| 164 | # - clang-format will returns the bytes read from the files as-is, |
| 165 | # without conversion, and it is already assumed that the files use utf-8. |
| 166 | # - if the diagnostics were internationalized, they would use utf-8: |
| 167 | # > Adding Translations to Clang |
| 168 | # > |
| 169 | # > Not possible yet! |
| 170 | # > Diagnostic strings should be written in UTF-8, |
| 171 | # > the client can translate to the relevant code page if needed. |
| 172 | # > Each translation completely replaces the format string |
| 173 | # > for the diagnostic. |
| 174 | # > -- http://clang.llvm.org/docs/InternalsManual.html#internals-diag-translation |
| 175 | # |
| 176 | # It's not pretty, due to Python 2 & 3 compatibility. |
| 177 | encoding_py3: dict[str, str] = {} |
| 178 | if sys.version_info[0] >= 3: |
| 179 | encoding_py3["encoding"] = "utf-8" |
| 180 | |
| 181 | try: |
| 182 | if sys.version_info[0] >= 3: |
| 183 | proc: subprocess.Popen[str] = subprocess.Popen( |
| 184 | invocation, |
| 185 | stdout=subprocess.PIPE, |
| 186 | stderr=subprocess.PIPE, |
| 187 | universal_newlines=True, |
| 188 | encoding="utf-8", |
| 189 | ) |
| 190 | else: |
| 191 | proc: subprocess.Popen[str] = subprocess.Popen( |
| 192 | invocation, |
| 193 | stdout=subprocess.PIPE, |
| 194 | stderr=subprocess.PIPE, |
| 195 | universal_newlines=True, |
| 196 | ) |
| 197 | except OSError as exc: |