Skip to content

Commit 5340574

Browse files
committed
[GitHub][CI] Add running of dump_ast_matchers.py to premerge workflow
1 parent 22f860a commit 5340574

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,70 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
466466
return report
467467

468468

469-
ALL_FORMATTERS = (DarkerFormatHelper(), ClangFormatHelper(), UndefGetFormatHelper())
469+
class DumpASTMatchersHelper(FormatHelper):
470+
name = "dump_ast_matchers"
471+
friendly_name = "AST matchers documentation"
472+
473+
output_html = "clang/docs/LibASTMatchersReference.html"
474+
script_dir = "clang/docs/tools"
475+
script_name = "dump_ast_matchers.py"
476+
477+
@property
478+
def instructions(self) -> str:
479+
return f"cd {self.script_dir} && python {self.script_name}"
480+
481+
def should_run(self, changed_files: List[str]) -> List[str]:
482+
for file in changed_files:
483+
if file == "clang/include/clang/ASTMatchers/ASTMatchers.h":
484+
return True
485+
return False
486+
487+
def has_tool(self) -> bool:
488+
if not os.path.exists(os.path.join(self.script_dir, self.script_name)):
489+
return False
490+
return True
491+
492+
def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str]:
493+
if not self.should_run(changed_files):
494+
return None
495+
496+
if args.verbose:
497+
print(f"Running: {self.instructions}")
498+
499+
# Run the 'dump_ast_matchers.py' from its directory as specified in the script doc
500+
proc = subprocess.run(
501+
["python", self.script_name],
502+
stdout=subprocess.PIPE,
503+
stderr=subprocess.PIPE,
504+
encoding="utf-8",
505+
cwd=self.script_dir,
506+
)
507+
508+
if proc.returncode != 0:
509+
return f"Execution of 'dump_ast_matchers.py' failed:\n{proc.stderr}\n{proc.stdout}"
510+
511+
# Check if 'LibASTMatchersReference.html' file was modified
512+
cmd = ["git", "diff", "--exit-code", self.output_html]
513+
proc = subprocess.run(
514+
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8"
515+
)
516+
517+
# 'LibASTMatchersReference.html' was modified - count as failure
518+
if proc.returncode != 0:
519+
if args.verbose:
520+
print(f"error: {self.name} exited with code {proc.returncode}")
521+
print(proc.stdout.decode("utf-8"))
522+
return proc.stdout.decode("utf-8")
523+
else:
524+
return None
525+
526+
527+
ALL_FORMATTERS = (
528+
DarkerFormatHelper(),
529+
ClangFormatHelper(),
530+
UndefGetFormatHelper(),
531+
DumpASTMatchersHelper(),
532+
)
470533

471534

472535
def hook_main():

0 commit comments

Comments
 (0)