[clang] [clang-format] Add -r option for recursing into directories (PR #160299)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 23 12:04:49 PDT 2025


================
@@ -700,6 +705,31 @@ int main(int argc, const char **argv) {
     errs() << "Clang-formatting " << LineNo << " files\n";
   }
 
+  if (Recursive) {
+    SmallVector<std::string> ExpandedNames;
+    for (const std::string &Path : FileNames) {
+      if (sys::fs::is_directory(Path)) {
+        std::error_code ErrorCode;
+        for (sys::fs::recursive_directory_iterator I(Path, ErrorCode), E;
+             I != E && !ErrorCode; I.increment(ErrorCode)) {
+          bool Result = false;
+          ErrorCode = sys::fs::is_regular_file(I->path(), Result);
+          // Conservatively assume that any unopenable entries are also regular
+          // files. Later code will emit an error when trying to format them, if
+          // they aren't valid by then.
+          if (ErrorCode || Result)
+            ExpandedNames.push_back(I->path());
----------------
HazardyKnusperkeks wrote:

If `ErrorCode` evaluates to `true` within the loop, the loop directly ends. I don't think you want to re use the variable here.

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


More information about the cfe-commits mailing list