From d49f94ca7de184fb7859a2d6d3f85e9b9adcc041 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 4 Feb 2024 18:29:42 -0800 Subject: [PATCH 1/4] [clang-format] Fix a regression in dumping the config Commit d813af73f70f addressed a regression introduced by commit 3791b3fca6ea but caused `clang-format -dump-config` to "hang". This patch reverts changes to ClangFormat.cpp by both 3791b3fca6ea and d813af73f70f and reworks the cleanup. Fixes #80621. --- clang/tools/clang-format/ClangFormat.cpp | 49 ++++++++++++------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index 5ee6092bb9bb7..a854a97a2d189 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -399,7 +399,8 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer { }; // Returns true on error. -static bool format(StringRef FileName, bool IsSTDIN) { +static bool format(StringRef FileName) { + const bool IsSTDIN = FileName == "-"; if (!OutputXML && Inplace && IsSTDIN) { errs() << "error: cannot use -i when reading from stdin.\n"; return false; @@ -545,24 +546,25 @@ static void PrintVersion(raw_ostream &OS) { } // Dump the configuration. -static int dumpConfig(bool IsSTDIN) { +static int dumpConfig() { std::unique_ptr Code; - - // `FileNames` must have at least "-" in it even if no file was specified. - assert(!FileNames.empty()); - - // Read in the code in case the filename alone isn't enough to detect the - // language. - ErrorOr> CodeOrErr = - MemoryBuffer::getFileOrSTDIN(FileNames[0]); - if (std::error_code EC = CodeOrErr.getError()) { - llvm::errs() << EC.message() << "\n"; - return 1; + // We can't read the code to detect the language if there's no file name. + if (!FileNames.empty()) { + // Read in the code in case the filename alone isn't enough to detect the + // language. + ErrorOr> CodeOrErr = + MemoryBuffer::getFileOrSTDIN(FileNames[0]); + if (std::error_code EC = CodeOrErr.getError()) { + llvm::errs() << EC.message() << "\n"; + return 1; + } + Code = std::move(CodeOrErr.get()); } - Code = std::move(CodeOrErr.get()); - llvm::Expected FormatStyle = - clang::format::getStyle(Style, IsSTDIN ? AssumeFileName : FileNames[0], + clang::format::getStyle(Style, + FileNames.empty() || FileNames[0] == "-" + ? AssumeFileName + : FileNames[0], FallbackStyle, Code ? Code->getBuffer() : ""); if (!FormatStyle) { llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n"; @@ -682,11 +684,8 @@ int main(int argc, const char **argv) { return 0; } - if (FileNames.empty()) - FileNames.push_back("-"); - if (DumpConfig) - return dumpConfig(FileNames[0] == "-"); + return dumpConfig(); if (!Files.empty()) { std::ifstream ExternalFileOfFiles{std::string(Files)}; @@ -699,7 +698,10 @@ int main(int argc, const char **argv) { errs() << "Clang-formating " << LineNo << " files\n"; } - if (FileNames.size() != 1 && + if (FileNames.empty()) + return clang::format::format("-"); + + if (FileNames.size() > 1 && (!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) { errs() << "error: -offset, -length and -lines can only be used for " "single file.\n"; @@ -709,14 +711,13 @@ int main(int argc, const char **argv) { unsigned FileNo = 1; bool Error = false; for (const auto &FileName : FileNames) { - const bool IsSTDIN = FileName == "-"; - if (!IsSTDIN && isIgnored(FileName)) + if (FileName != "-" && isIgnored(FileName)) continue; if (Verbose) { errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] " << FileName << "\n"; } - Error |= clang::format::format(FileName, IsSTDIN); + Error |= clang::format::format(FileName); } return Error ? 1 : 0; } From 272442889a7e78eb1bd64f78136eb8edd86a6573 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 5 Feb 2024 00:52:44 -0800 Subject: [PATCH 2/4] Add a lit test. --- clang/test/Format/dump-config.cpp | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 clang/test/Format/dump-config.cpp diff --git a/clang/test/Format/dump-config.cpp b/clang/test/Format/dump-config.cpp new file mode 100644 index 0000000000000..fad517c44642f --- /dev/null +++ b/clang/test/Format/dump-config.cpp @@ -0,0 +1,3 @@ +// RUN: clang-format -dump-config 2>&1 | FileCheck %s + +// CHECK: Language: Cpp From 680b50a71b606225ca945bc6793adb9c81f3fe54 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 5 Feb 2024 19:33:12 -0800 Subject: [PATCH 3/4] Revert "Add a lit test." This reverts commit 272442889a7e78eb1bd64f78136eb8edd86a6573. --- clang/test/Format/dump-config.cpp | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 clang/test/Format/dump-config.cpp diff --git a/clang/test/Format/dump-config.cpp b/clang/test/Format/dump-config.cpp deleted file mode 100644 index fad517c44642f..0000000000000 --- a/clang/test/Format/dump-config.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// RUN: clang-format -dump-config 2>&1 | FileCheck %s - -// CHECK: Language: Cpp From 2e1caa208fda86fd5ac3b929fa70100a5a6baee4 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Mon, 5 Feb 2024 20:50:34 -0800 Subject: [PATCH 4/4] Add test cases for -dump-confif and -verbose options. --- clang/test/Format/dump-config-objc-stdin.m | 3 +++ clang/test/Format/verbose.cpp | 10 ++-------- clang/tools/clang-format/ClangFormat.cpp | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/clang/test/Format/dump-config-objc-stdin.m b/clang/test/Format/dump-config-objc-stdin.m index b22ff7b3328ca..d81711a84d79b 100644 --- a/clang/test/Format/dump-config-objc-stdin.m +++ b/clang/test/Format/dump-config-objc-stdin.m @@ -1,5 +1,8 @@ +// RUN: clang-format -assume-filename=foo.m -dump-config | FileCheck %s + // RUN: clang-format -dump-config - < %s | FileCheck %s // CHECK: Language: ObjC + @interface Foo @end diff --git a/clang/test/Format/verbose.cpp b/clang/test/Format/verbose.cpp index dd625e3f67e55..4ab03d8f62aef 100644 --- a/clang/test/Format/verbose.cpp +++ b/clang/test/Format/verbose.cpp @@ -1,12 +1,6 @@ -// RUN: clang-format %s 2> %t.stderr +// RUN: clang-format -verbose 2> %t.stderr // RUN: not grep "Formatting" %t.stderr -// RUN: clang-format %s -verbose 2> %t.stderr -// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr -// RUN: clang-format %s -verbose=false 2> %t.stderr -// RUN: not grep "Formatting" %t.stderr - -int a; -// RUN: clang-format %s 2> %t.stderr +// RUN: clang-format %s 2> %t.stderr // RUN: not grep "Formatting" %t.stderr // RUN: clang-format %s -verbose 2> %t.stderr // RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp index a854a97a2d189..e122cea50f726 100644 --- a/clang/tools/clang-format/ClangFormat.cpp +++ b/clang/tools/clang-format/ClangFormat.cpp @@ -711,7 +711,7 @@ int main(int argc, const char **argv) { unsigned FileNo = 1; bool Error = false; for (const auto &FileName : FileNames) { - if (FileName != "-" && isIgnored(FileName)) + if (isIgnored(FileName)) continue; if (Verbose) { errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "