Skip to content

Commit 75acf3b

Browse files
committed
Update scripts
1 parent 27c30ab commit 75acf3b

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

llvm/utils/git/code-lint-helper.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ def filter_changed_files(changed_files: List[str]) -> List[str]:
111111
return filtered_files
112112

113113

114+
def filter_doc_files(changed_files: List[str]) -> List[str]:
115+
filtered_files = []
116+
for filepath in changed_files:
117+
_, ext = os.path.splitext(filepath)
118+
if ext not in (".rst"):
119+
continue
120+
if not filepath.startswith("clang-tools-extra/docs/clang-tidy/checks/"):
121+
continue
122+
if os.path.exists(filepath):
123+
filtered_files.append(filepath)
124+
return filtered_files
125+
126+
114127
def create_comment_text(warning: str, cpp_files: List[str]) -> str:
115128
instructions = get_instructions(cpp_files)
116129
return f"""
@@ -235,37 +248,13 @@ def run_clang_tidy(changed_files: List[str], args: LintArgs) -> Optional[str]:
235248
return clean_clang_tidy_output(proc.stdout.strip())
236249

237250

238-
def clean_doc8_output(output: str) -> Optional[str]:
239-
if not output:
240-
return None
251+
def get_doc8_instructions(doc_files: List[str]) -> str:
252+
files_str = " ".join(doc_files)
253+
return f"doc8 -q {files_str}"
241254

242-
lines = output.split("\n")
243-
cleaned_lines = []
244-
in_summary = False
245255

246-
for line in lines:
247-
if line.startswith("Scanning...") or line.startswith("Validating..."):
248-
continue
249-
if line.startswith("========"):
250-
in_summary = True
251-
continue
252-
if in_summary:
253-
continue
254-
if line.strip():
255-
cleaned_lines.append(line)
256-
257-
if cleaned_lines:
258-
return "\n".join(cleaned_lines)
259-
return None
260-
261-
262-
def get_doc8_instructions() -> str:
263-
# TODO: use git diff
264-
return "doc8 ./clang-tools-extra/docs/clang-tidy/checks/"
265-
266-
267-
def create_doc8_comment_text(doc8_output: str) -> str:
268-
instructions = get_doc8_instructions()
256+
def create_doc8_comment_text(doc8_output: str, doc_files: List[str]) -> str:
257+
instructions = get_doc8_instructions(doc_files)
269258
return f"""
270259
:warning: Documentation linter doc8 found issues in your code. :warning:
271260
@@ -293,8 +282,11 @@ def create_doc8_comment_text(doc8_output: str) -> str:
293282
"""
294283

295284

296-
def run_doc8(args: LintArgs) -> tuple[int, Optional[str]]:
297-
doc8_cmd = [args.doc8_binary, "./clang-tools-extra/docs/clang-tidy/checks/"]
285+
def run_doc8(doc_files: List[str], args: LintArgs) -> tuple[int, Optional[str]]:
286+
if not doc_files:
287+
return 0, None
288+
289+
doc8_cmd = [args.doc8_binary, "-q"] + doc_files
298290

299291
if args.verbose:
300292
print(f"Running doc8: {' '.join(doc8_cmd)}")
@@ -307,20 +299,32 @@ def run_doc8(args: LintArgs) -> tuple[int, Optional[str]]:
307299
check=False,
308300
)
309301

310-
cleaned_output = clean_doc8_output(proc.stdout.strip())
311-
if proc.returncode != 0 and cleaned_output is None:
302+
output = proc.stdout.strip()
303+
if proc.returncode != 0 and not output:
312304
# Infrastructure failure
313305
return proc.returncode, proc.stderr.strip()
314306

315-
return proc.returncode, cleaned_output
307+
return proc.returncode, output if output else None
316308

317309

318310
def run_doc8_linter(args: LintArgs) -> tuple[bool, Optional[dict]]:
319-
returncode, result = run_doc8(args)
311+
changed_files = []
312+
if args.changed_files:
313+
changed_files = args.changed_files.split(',')
314+
doc_files = filter_doc_files(changed_files)
315+
316+
is_success = True
317+
result = None
318+
319+
if doc_files:
320+
returncode, result = run_doc8(doc_files, args)
321+
if returncode != 0:
322+
is_success = False
323+
320324
should_update_gh = args.token is not None and args.repo is not None
321325
comment = None
322326

323-
if returncode == 0:
327+
if is_success:
324328
if should_update_gh:
325329
comment_text = (
326330
":white_check_mark: With the latest revision "
@@ -331,7 +335,7 @@ def run_doc8_linter(args: LintArgs) -> tuple[bool, Optional[dict]]:
331335
else:
332336
if should_update_gh:
333337
if result:
334-
comment_text = create_doc8_comment_text(result)
338+
comment_text = create_doc8_comment_text(result, doc_files)
335339
comment = create_comment(comment_text, args, create_new=True)
336340
else:
337341
comment_text = (

0 commit comments

Comments
 (0)