Skip to content

Commit 3cd0789

Browse files
owencatstellar
authored andcommitted
[clang-format] Fix a regression in dumping the config (llvm#80628)
Commit d813af7 addressed a regression introduced by commit 3791b3f but caused `clang-format -dump-config` to "hang". This patch reverts changes to ClangFormat.cpp by both commits and reworks the cleanup. Fixes llvm#80621. (cherry picked from commit 8f6e13e)
1 parent aa74e4c commit 3cd0789

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
// RUN: clang-format -assume-filename=foo.m -dump-config | FileCheck %s
2+
13
// RUN: clang-format -dump-config - < %s | FileCheck %s
24

35
// CHECK: Language: ObjC
6+
47
@interface Foo
58
@end

clang/test/Format/verbose.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
// RUN: clang-format %s 2> %t.stderr
1+
// RUN: clang-format -verbose 2> %t.stderr
22
// RUN: not grep "Formatting" %t.stderr
3-
// RUN: clang-format %s -verbose 2> %t.stderr
4-
// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr
5-
// RUN: clang-format %s -verbose=false 2> %t.stderr
6-
// RUN: not grep "Formatting" %t.stderr
7-
8-
int a;
9-
// RUN: clang-format %s 2> %t.stderr
3+
// RUN: clang-format %s 2> %t.stderr
104
// RUN: not grep "Formatting" %t.stderr
115
// RUN: clang-format %s -verbose 2> %t.stderr
126
// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr

clang/tools/clang-format/ClangFormat.cpp

+25-24
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
399399
};
400400

401401
// Returns true on error.
402-
static bool format(StringRef FileName, bool IsSTDIN) {
402+
static bool format(StringRef FileName) {
403+
const bool IsSTDIN = FileName == "-";
403404
if (!OutputXML && Inplace && IsSTDIN) {
404405
errs() << "error: cannot use -i when reading from stdin.\n";
405406
return false;
@@ -545,24 +546,25 @@ static void PrintVersion(raw_ostream &OS) {
545546
}
546547

547548
// Dump the configuration.
548-
static int dumpConfig(bool IsSTDIN) {
549+
static int dumpConfig() {
549550
std::unique_ptr<llvm::MemoryBuffer> Code;
550-
551-
// `FileNames` must have at least "-" in it even if no file was specified.
552-
assert(!FileNames.empty());
553-
554-
// Read in the code in case the filename alone isn't enough to detect the
555-
// language.
556-
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
557-
MemoryBuffer::getFileOrSTDIN(FileNames[0]);
558-
if (std::error_code EC = CodeOrErr.getError()) {
559-
llvm::errs() << EC.message() << "\n";
560-
return 1;
551+
// We can't read the code to detect the language if there's no file name.
552+
if (!FileNames.empty()) {
553+
// Read in the code in case the filename alone isn't enough to detect the
554+
// language.
555+
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
556+
MemoryBuffer::getFileOrSTDIN(FileNames[0]);
557+
if (std::error_code EC = CodeOrErr.getError()) {
558+
llvm::errs() << EC.message() << "\n";
559+
return 1;
560+
}
561+
Code = std::move(CodeOrErr.get());
561562
}
562-
Code = std::move(CodeOrErr.get());
563-
564563
llvm::Expected<clang::format::FormatStyle> FormatStyle =
565-
clang::format::getStyle(Style, IsSTDIN ? AssumeFileName : FileNames[0],
564+
clang::format::getStyle(Style,
565+
FileNames.empty() || FileNames[0] == "-"
566+
? AssumeFileName
567+
: FileNames[0],
566568
FallbackStyle, Code ? Code->getBuffer() : "");
567569
if (!FormatStyle) {
568570
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
@@ -682,11 +684,8 @@ int main(int argc, const char **argv) {
682684
return 0;
683685
}
684686

685-
if (FileNames.empty())
686-
FileNames.push_back("-");
687-
688687
if (DumpConfig)
689-
return dumpConfig(FileNames[0] == "-");
688+
return dumpConfig();
690689

691690
if (!Files.empty()) {
692691
std::ifstream ExternalFileOfFiles{std::string(Files)};
@@ -699,7 +698,10 @@ int main(int argc, const char **argv) {
699698
errs() << "Clang-formating " << LineNo << " files\n";
700699
}
701700

702-
if (FileNames.size() != 1 &&
701+
if (FileNames.empty())
702+
return clang::format::format("-");
703+
704+
if (FileNames.size() > 1 &&
703705
(!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
704706
errs() << "error: -offset, -length and -lines can only be used for "
705707
"single file.\n";
@@ -709,14 +711,13 @@ int main(int argc, const char **argv) {
709711
unsigned FileNo = 1;
710712
bool Error = false;
711713
for (const auto &FileName : FileNames) {
712-
const bool IsSTDIN = FileName == "-";
713-
if (!IsSTDIN && isIgnored(FileName))
714+
if (isIgnored(FileName))
714715
continue;
715716
if (Verbose) {
716717
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
717718
<< FileName << "\n";
718719
}
719-
Error |= clang::format::format(FileName, IsSTDIN);
720+
Error |= clang::format::format(FileName);
720721
}
721722
return Error ? 1 : 0;
722723
}

0 commit comments

Comments
 (0)