mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-26 12:36:07 +00:00
[clang-format] Fix a regression in dumping the config (#80628)
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 commits and reworks the cleanup. Fixes #80621.
This commit is contained in:
parent
351f94d981
commit
8f6e13e6da
@ -1,5 +1,8 @@
|
|||||||
|
// RUN: clang-format -assume-filename=foo.m -dump-config | FileCheck %s
|
||||||
|
|
||||||
// RUN: clang-format -dump-config - < %s | FileCheck %s
|
// RUN: clang-format -dump-config - < %s | FileCheck %s
|
||||||
|
|
||||||
// CHECK: Language: ObjC
|
// CHECK: Language: ObjC
|
||||||
|
|
||||||
@interface Foo
|
@interface Foo
|
||||||
@end
|
@end
|
||||||
|
@ -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: not grep "Formatting" %t.stderr
|
||||||
// RUN: clang-format %s -verbose 2> %t.stderr
|
// RUN: clang-format %s 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: not grep "Formatting" %t.stderr
|
// RUN: not grep "Formatting" %t.stderr
|
||||||
// RUN: clang-format %s -verbose 2> %t.stderr
|
// RUN: clang-format %s -verbose 2> %t.stderr
|
||||||
// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr
|
// RUN: grep -E "Formatting (.*)verbose.cpp(.*)" %t.stderr
|
||||||
|
@ -399,7 +399,8 @@ class ClangFormatDiagConsumer : public DiagnosticConsumer {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Returns true on error.
|
// Returns true on error.
|
||||||
static bool format(StringRef FileName, bool IsSTDIN) {
|
static bool format(StringRef FileName) {
|
||||||
|
const bool IsSTDIN = FileName == "-";
|
||||||
if (!OutputXML && Inplace && IsSTDIN) {
|
if (!OutputXML && Inplace && IsSTDIN) {
|
||||||
errs() << "error: cannot use -i when reading from stdin.\n";
|
errs() << "error: cannot use -i when reading from stdin.\n";
|
||||||
return false;
|
return false;
|
||||||
@ -545,24 +546,25 @@ static void PrintVersion(raw_ostream &OS) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Dump the configuration.
|
// Dump the configuration.
|
||||||
static int dumpConfig(bool IsSTDIN) {
|
static int dumpConfig() {
|
||||||
std::unique_ptr<llvm::MemoryBuffer> Code;
|
std::unique_ptr<llvm::MemoryBuffer> Code;
|
||||||
|
// We can't read the code to detect the language if there's no file name.
|
||||||
// `FileNames` must have at least "-" in it even if no file was specified.
|
if (!FileNames.empty()) {
|
||||||
assert(!FileNames.empty());
|
// Read in the code in case the filename alone isn't enough to detect the
|
||||||
|
// language.
|
||||||
// Read in the code in case the filename alone isn't enough to detect the
|
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
|
||||||
// language.
|
MemoryBuffer::getFileOrSTDIN(FileNames[0]);
|
||||||
ErrorOr<std::unique_ptr<MemoryBuffer>> CodeOrErr =
|
if (std::error_code EC = CodeOrErr.getError()) {
|
||||||
MemoryBuffer::getFileOrSTDIN(FileNames[0]);
|
llvm::errs() << EC.message() << "\n";
|
||||||
if (std::error_code EC = CodeOrErr.getError()) {
|
return 1;
|
||||||
llvm::errs() << EC.message() << "\n";
|
}
|
||||||
return 1;
|
Code = std::move(CodeOrErr.get());
|
||||||
}
|
}
|
||||||
Code = std::move(CodeOrErr.get());
|
|
||||||
|
|
||||||
llvm::Expected<clang::format::FormatStyle> FormatStyle =
|
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() : "");
|
FallbackStyle, Code ? Code->getBuffer() : "");
|
||||||
if (!FormatStyle) {
|
if (!FormatStyle) {
|
||||||
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
|
llvm::errs() << llvm::toString(FormatStyle.takeError()) << "\n";
|
||||||
@ -682,11 +684,8 @@ int main(int argc, const char **argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FileNames.empty())
|
|
||||||
FileNames.push_back("-");
|
|
||||||
|
|
||||||
if (DumpConfig)
|
if (DumpConfig)
|
||||||
return dumpConfig(FileNames[0] == "-");
|
return dumpConfig();
|
||||||
|
|
||||||
if (!Files.empty()) {
|
if (!Files.empty()) {
|
||||||
std::ifstream ExternalFileOfFiles{std::string(Files)};
|
std::ifstream ExternalFileOfFiles{std::string(Files)};
|
||||||
@ -699,7 +698,10 @@ int main(int argc, const char **argv) {
|
|||||||
errs() << "Clang-formating " << LineNo << " files\n";
|
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())) {
|
(!Offsets.empty() || !Lengths.empty() || !LineRanges.empty())) {
|
||||||
errs() << "error: -offset, -length and -lines can only be used for "
|
errs() << "error: -offset, -length and -lines can only be used for "
|
||||||
"single file.\n";
|
"single file.\n";
|
||||||
@ -709,14 +711,13 @@ int main(int argc, const char **argv) {
|
|||||||
unsigned FileNo = 1;
|
unsigned FileNo = 1;
|
||||||
bool Error = false;
|
bool Error = false;
|
||||||
for (const auto &FileName : FileNames) {
|
for (const auto &FileName : FileNames) {
|
||||||
const bool IsSTDIN = FileName == "-";
|
if (isIgnored(FileName))
|
||||||
if (!IsSTDIN && isIgnored(FileName))
|
|
||||||
continue;
|
continue;
|
||||||
if (Verbose) {
|
if (Verbose) {
|
||||||
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
|
errs() << "Formatting [" << FileNo++ << "/" << FileNames.size() << "] "
|
||||||
<< FileName << "\n";
|
<< FileName << "\n";
|
||||||
}
|
}
|
||||||
Error |= clang::format::format(FileName, IsSTDIN);
|
Error |= clang::format::format(FileName);
|
||||||
}
|
}
|
||||||
return Error ? 1 : 0;
|
return Error ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user