Skip to content

Commit 7cebfa4

Browse files
[analyzer] SATestUtils.py: Refactor and add type annotations
Differential Revision: https://reviews.llvm.org/D80424
1 parent 4902ca6 commit 7cebfa4

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

clang/utils/analyzer/SATestBuild.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060

6161
from queue import Queue
6262
from subprocess import CalledProcessError, check_call
63-
from typing import (cast, Dict, Iterable, IO, List, NamedTuple, Tuple,
64-
TYPE_CHECKING)
63+
from typing import (cast, Dict, Iterable, IO, List, NamedTuple, Optional,
64+
Tuple, TYPE_CHECKING)
6565

6666

6767
###############################################################################
@@ -93,13 +93,15 @@ def stdout(message: str):
9393

9494
# Find Clang for static analysis.
9595
if 'CC' in os.environ:
96-
CLANG = os.environ['CC']
96+
cc_candidate: Optional[str] = os.environ['CC']
9797
else:
98-
CLANG = SATestUtils.which("clang", os.environ['PATH'])
99-
if not CLANG:
98+
cc_candidate = SATestUtils.which("clang", os.environ['PATH'])
99+
if not cc_candidate:
100100
stderr("Error: cannot find 'clang' in PATH")
101101
sys.exit(1)
102102

103+
CLANG = cc_candidate
104+
103105
# Number of jobs.
104106
MAX_JOBS = int(math.ceil(multiprocessing.cpu_count() * 0.75))
105107

@@ -204,8 +206,9 @@ def run_cleanup_script(directory: str, build_log_file: IO):
204206
cwd = os.path.join(directory, PATCHED_SOURCE_DIR_NAME)
205207
script_path = os.path.join(directory, CLEANUP_SCRIPT)
206208

207-
SATestUtils.runScript(script_path, build_log_file, cwd,
208-
Stdout=LOCAL.stdout, Stderr=LOCAL.stderr)
209+
SATestUtils.run_script(script_path, build_log_file, cwd,
210+
out=LOCAL.stdout, err=LOCAL.stderr,
211+
verbose=VERBOSE)
209212

210213

211214
def download_and_patch(directory: str, build_log_file: IO):
@@ -238,8 +241,9 @@ def download(directory: str, build_log_file: IO):
238241
Run the script to download the project, if it exists.
239242
"""
240243
script_path = os.path.join(directory, DOWNLOAD_SCRIPT)
241-
SATestUtils.runScript(script_path, build_log_file, directory,
242-
Stdout=LOCAL.stdout, Stderr=LOCAL.stderr)
244+
SATestUtils.run_script(script_path, build_log_file, directory,
245+
out=LOCAL.stdout, err=LOCAL.stderr,
246+
verbose=VERBOSE)
243247

244248

245249
def apply_patch(directory: str, build_log_file: IO):
@@ -557,9 +561,9 @@ def analyze_preprocessed(self, directory: str, output_dir: str):
557561
failed = False
558562

559563
# Only run the analyzes on supported files.
560-
if SATestUtils.hasNoExtension(file_name):
564+
if SATestUtils.has_no_extension(file_name):
561565
continue
562-
if not SATestUtils.isValidSingleInputFile(file_name):
566+
if not SATestUtils.is_valid_single_input_file(file_name):
563567
stderr(f"Error: Invalid single input file {full_file_name}.\n")
564568
raise Exception()
565569

@@ -859,7 +863,7 @@ def get_projects(map_file: IO) -> Iterable[Tuple[str, str]]:
859863
map_file.seek(0)
860864
# TODO: csv format is not very readable, change it to JSON
861865
for project_info in csv.reader(map_file):
862-
if (SATestUtils.isCommentCSVLine(project_info)):
866+
if SATestUtils.is_comment_csv_line(project_info):
863867
continue
864868
# suppress mypy error
865869
yield cast(Tuple[str, str], project_info)

clang/utils/analyzer/SATestUtils.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import os
2-
from subprocess import check_call
32
import sys
43

4+
from subprocess import CalledProcessError, check_call
5+
from typing import List, IO, Optional
56

6-
Verbose = 1
77

8-
9-
def which(command, paths=None):
8+
def which(command: str, paths: Optional[str] = None) -> Optional[str]:
109
"""which(command, [paths]) - Look up the given command in the paths string
1110
(or the PATH environment variable, if unspecified)."""
1211

@@ -38,41 +37,44 @@ def which(command, paths=None):
3837
return None
3938

4039

41-
def hasNoExtension(FileName):
42-
(Root, Ext) = os.path.splitext(FileName)
43-
return (Ext == "")
40+
def has_no_extension(file_name: str) -> bool:
41+
root, ext = os.path.splitext(file_name)
42+
return ext == ""
4443

4544

46-
def isValidSingleInputFile(FileName):
47-
(Root, Ext) = os.path.splitext(FileName)
48-
return Ext in (".i", ".ii", ".c", ".cpp", ".m", "")
45+
def is_valid_single_input_file(file_name: str) -> bool:
46+
root, ext = os.path.splitext(file_name)
47+
return ext in (".i", ".ii", ".c", ".cpp", ".m", "")
4948

5049

51-
def runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout,
52-
Stderr=sys.stderr):
50+
def run_script(script_path: str, build_log_file: IO, cwd: str,
51+
out=sys.stdout, err=sys.stderr, verbose: int = 0):
5352
"""
5453
Run the provided script if it exists.
5554
"""
56-
if os.path.exists(ScriptPath):
55+
if os.path.exists(script_path):
5756
try:
58-
if Verbose == 1:
59-
Stdout.write(" Executing: %s\n" % (ScriptPath,))
60-
check_call("chmod +x '%s'" % ScriptPath, cwd=Cwd,
61-
stderr=PBuildLogFile,
62-
stdout=PBuildLogFile,
57+
if verbose == 1:
58+
out.write(f" Executing: {script_path}\n")
59+
60+
check_call(f"chmod +x '{script_path}'", cwd=cwd,
61+
stderr=build_log_file,
62+
stdout=build_log_file,
6363
shell=True)
64-
check_call("'%s'" % ScriptPath, cwd=Cwd,
65-
stderr=PBuildLogFile,
66-
stdout=PBuildLogFile,
64+
65+
check_call(f"'{script_path}'", cwd=cwd,
66+
stderr=build_log_file,
67+
stdout=build_log_file,
6768
shell=True)
68-
except:
69-
Stderr.write("Error: Running %s failed. See %s for details.\n" % (
70-
ScriptPath, PBuildLogFile.name))
69+
70+
except CalledProcessError:
71+
err.write(f"Error: Running {script_path} failed. "
72+
f"See {build_log_file.name} for details.\n")
7173
sys.exit(-1)
7274

7375

74-
def isCommentCSVLine(Entries):
76+
def is_comment_csv_line(entries: List[str]) -> bool:
7577
"""
7678
Treat CSV lines starting with a '#' as a comment.
7779
"""
78-
return len(Entries) > 0 and Entries[0].startswith("#")
80+
return len(entries) > 0 and entries[0].startswith("#")

0 commit comments

Comments
 (0)