[llvm] Add PR check to suggest alternatives to using undef (PR #118506)

David Spickett via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 4 03:03:39 PST 2024


================
@@ -312,7 +313,84 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
             return None
 
 
-ALL_FORMATTERS = (DarkerFormatHelper(), ClangFormatHelper())
+class UndefGetFormatHelper(FormatHelper):
+    name = "undef deprecator"
+    friendly_name = "undef deprecator"
+
+    @property
+    def instructions(self) -> str:
+        return " ".join(f"'{c}'" for c in self.cmd)
+
+    def filter_changed_files(self, changed_files: List[str]) -> List[str]:
+        filtered_files = []
+        for path in changed_files:
+            _, ext = os.path.splitext(path)
+            if ext in (".cpp", ".c", ".h", ".hpp", ".hxx", ".cxx", ".inc", ".cppm", ".ll"):
+                filtered_files.append(path)
+        return filtered_files
+
+    def has_tool(self) -> bool:
+        return True
+
+    def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str]:
+        files = self.filter_changed_files(changed_files)
+
+        regex = '([^a-zA-Z0-9#_-]undef[^a-zA-Z0-9_-]|UndefValue::get)'
+        cmd = ['git', 'diff', '-U0', '--pickaxe-regex', '-S', regex]
+
+        if args.start_rev and args.end_rev:
+            cmd.append(args.start_rev)
+            cmd.append(args.end_rev)
+
+        cmd += files
+        self.cmd = cmd
+
+        if args.verbose:
+            print(f"Running: {self.instructions}")
+        proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        sys.stdout.write(proc.stderr.decode("utf-8"))
+        stdout = proc.stdout.decode("utf-8")
+
+        files = []
+        for file in re.split('^diff --git ', stdout, 0, re.MULTILINE):
+            if re.search('^[+].*'+regex, file, re.MULTILINE):
+                files.append(re.match('a/([^ ]+)', file.splitlines()[0])[1])
+
+        if files:
----------------
DavidSpickett wrote:

I would early exit like:
```
if not files:
  return None

# Now the rest doesn't need to be indented.
```


https://github.com/llvm/llvm-project/pull/118506


More information about the llvm-commits mailing list