-
Notifications
You must be signed in to change notification settings - Fork 13.4k
PR for llvm/llvm-project#80628 #81096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@owenca What do you think about merging this PR to the release branch? |
@llvm/pr-subscribers-clang-format Author: None (llvmbot) Changesresolves llvm/llvm-project#80628 Full diff: https://github.com/llvm/llvm-project/pull/81096.diff 3 Files Affected:
diff --git a/clang/test/Format/dump-config-objc-stdin.m b/clang/test/Format/dump-config-objc-stdin.m
index b22ff7b3328caa..d81711a84d79bf 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 dd625e3f67e55d..4ab03d8f62aefc 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 5ee6092bb9bb7f..e122cea50f7268 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<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";
@@ -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 (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;
}
|
owenca
approved these changes
Feb 9, 2024
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)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
resolves #80628