Skip to content

Commit

Permalink
[clang-format] Fix a regression in dumping the config (llvm#80628)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
owenca authored and tstellar committed Feb 14, 2024
1 parent aa74e4c commit 3cd0789
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
3 changes: 3 additions & 0 deletions clang/test/Format/dump-config-objc-stdin.m
Original file line number Diff line number Diff line change
@@ -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
10 changes: 2 additions & 8 deletions clang/test/Format/verbose.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down
49 changes: 25 additions & 24 deletions clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -545,24 +546,25 @@ static void PrintVersion(raw_ostream &OS) {
}

// Dump the configuration.
static int dumpConfig(bool IsSTDIN) {
static int dumpConfig() {
std::unique_ptr<llvm::MemoryBuffer> 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<std::unique_ptr<MemoryBuffer>> 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<std::unique_ptr<MemoryBuffer>> 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<clang::format::FormatStyle> 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";
Expand Down Expand Up @@ -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)};
Expand All @@ -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";
Expand All @@ -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 (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;
}

0 comments on commit 3cd0789

Please sign in to comment.