[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2024-01-22 Thread Ben Hamilton via cfe-commits


@@ -544,28 +544,23 @@ static void PrintVersion(raw_ostream ) {
 }
 
 // Dump the configuration.
-static int dumpConfig() {
-  StringRef FileName;
+static int dumpConfig(bool IsSTDIN) {
   std::unique_ptr Code;
-  if (FileNames.empty()) {
-// We can't read the code to detect the language if there's no
-// file name, so leave Code empty here.
-FileName = AssumeFileName;
-  } else {
-// Read in the code in case the filename alone isn't enough to
-// detect the language.
+  // We can't read the code to detect the language if there's no file name.
+  if (!IsSTDIN) {
+// Read in the code in case the filename alone isn't enough to detect the
+// language.
 ErrorOr> CodeOrErr =
 MemoryBuffer::getFileOrSTDIN(FileNames[0]);

bhamiltoncx wrote:

Sent #79051 with a fix.

https://github.com/llvm/llvm-project/pull/74794
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2024-01-22 Thread Ben Hamilton via cfe-commits


@@ -544,28 +544,23 @@ static void PrintVersion(raw_ostream ) {
 }
 
 // Dump the configuration.
-static int dumpConfig() {
-  StringRef FileName;
+static int dumpConfig(bool IsSTDIN) {
   std::unique_ptr Code;
-  if (FileNames.empty()) {
-// We can't read the code to detect the language if there's no
-// file name, so leave Code empty here.
-FileName = AssumeFileName;
-  } else {
-// Read in the code in case the filename alone isn't enough to
-// detect the language.
+  // We can't read the code to detect the language if there's no file name.
+  if (!IsSTDIN) {
+// Read in the code in case the filename alone isn't enough to detect the
+// language.
 ErrorOr> CodeOrErr =
 MemoryBuffer::getFileOrSTDIN(FileNames[0]);

bhamiltoncx wrote:

Looks like this is the regression that caused #79023.

Previously, this would try to read in the code to detect the language whether 
or not this was reading from stdin.

Now, this fails to read in the code to detect the language when reading from 
stdin.


https://github.com/llvm/llvm-project/pull/74794
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2024-01-22 Thread Ben Hamilton via cfe-commits

bhamiltoncx wrote:

Seems like this regressed `clang-format - -dump_config < path/to/objc_file.m`. 
I filed https://github.com/llvm/llvm-project/issues/79023.


https://github.com/llvm/llvm-project/pull/74794
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2023-12-08 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/74794
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2023-12-08 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks approved this pull request.


https://github.com/llvm/llvm-project/pull/74794
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2023-12-07 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)


Changes



---
Full diff: https://github.com/llvm/llvm-project/pull/74794.diff


2 Files Affected:

- (modified) clang/lib/Format/Format.cpp (+48-47) 
- (modified) clang/tools/clang-format/ClangFormat.cpp (+17-23) 


``diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b09487435adb2d..8feee7457fc31b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3955,10 +3955,7 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
  StringRef FallbackStyleName,
  StringRef Code, llvm::vfs::FileSystem *FS,
  bool AllowUnknownOptions) {
-  if (!FS)
-FS = llvm::vfs::getRealFileSystem().get();
   FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
-
   FormatStyle FallbackStyle = getNoStyle();
   if (!getPredefinedStyle(FallbackStyleName, Style.Language, ))
 return make_string_error("Invalid fallback style: " + FallbackStyleName);
@@ -3974,14 +3971,18 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
AllowUnknownOptions)) {
   return make_string_error("Error parsing -style: " + ec.message());
 }
-if (Style.InheritsParentConfig) {
-  ChildFormatTextToApply.emplace_back(
-  llvm::MemoryBuffer::getMemBuffer(StyleName, Source, false));
-} else {
+
+if (!Style.InheritsParentConfig)
   return Style;
-}
+
+ChildFormatTextToApply.emplace_back(
+llvm::MemoryBuffer::getMemBuffer(StyleName, Source, false));
   }
 
+  if (!FS)
+FS = llvm::vfs::getRealFileSystem().get();
+  assert(FS);
+
   // User provided clang-format file using -style=file:path/to/format/file.
   if (!Style.InheritsParentConfig &&
   StyleName.starts_with_insensitive("file:")) {
@@ -4015,18 +4016,12 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
   return Style;
   }
 
-  // Reset possible inheritance
-  Style.InheritsParentConfig = false;
-
-  // Look for .clang-format/_clang-format file in the file's parent 
directories.
-  SmallString<128> UnsuitableConfigFiles;
   SmallString<128> Path(FileName);
   if (std::error_code EC = FS->makeAbsolute(Path))
 return make_string_error(EC.message());
 
-  llvm::SmallVector FilesToLookFor;
-  FilesToLookFor.push_back(".clang-format");
-  FilesToLookFor.push_back("_clang-format");
+  // Reset possible inheritance
+  Style.InheritsParentConfig = false;
 
   auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
 
@@ -4040,9 +4035,14 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
 }
   };
 
+  // Look for .clang-format/_clang-format file in the file's parent 
directories.
+  llvm::SmallVector FilesToLookFor;
+  FilesToLookFor.push_back(".clang-format");
+  FilesToLookFor.push_back("_clang-format");
+
+  SmallString<128> UnsuitableConfigFiles;
   for (StringRef Directory = Path; !Directory.empty();
Directory = llvm::sys::path::parent_path(Directory)) {
-
 auto Status = FS->status(Directory);
 if (!Status ||
 Status->getType() != llvm::sys::fs::file_type::directory_file) {
@@ -4055,50 +4055,51 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
   llvm::sys::path::append(ConfigFile, F);
   LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
 
-  Status = FS->status(ConfigFile.str());
-
-  if (Status &&
-  (Status->getType() == llvm::sys::fs::file_type::regular_file)) {
-llvm::ErrorOr> Text =
-loadAndParseConfigFile(ConfigFile, FS, , 
AllowUnknownOptions);
-if (auto EC = Text.getError()) {
-  if (EC == ParseError::Unsuitable) {
-if (!UnsuitableConfigFiles.empty())
-  UnsuitableConfigFiles.append(", ");
-UnsuitableConfigFiles.append(ConfigFile);
-continue;
-  }
+  Status = FS->status(ConfigFile);
+  if (!Status ||
+  Status->getType() != llvm::sys::fs::file_type::regular_file) {
+continue;
+  }
+
+  llvm::ErrorOr> Text =
+  loadAndParseConfigFile(ConfigFile, FS, , AllowUnknownOptions);
+  if (auto EC = Text.getError()) {
+if (EC != ParseError::Unsuitable) {
   return make_string_error("Error reading " + ConfigFile + ": " +
EC.message());
 }
-LLVM_DEBUG(llvm::dbgs()
-   << "Using configuration file " << ConfigFile << "\n");
+if (!UnsuitableConfigFiles.empty())
+  UnsuitableConfigFiles.append(", ");
+UnsuitableConfigFiles.append(ConfigFile);
+continue;
+  }
 
-if (!Style.InheritsParentConfig) {
-  if (ChildFormatTextToApply.empty())
-return Style;
+  LLVM_DEBUG(llvm::dbgs()
+ << "Using 

[clang] [clang-format][NFC] Clean up the driver and getStyle() in Format.cpp (PR #74794)

2023-12-07 Thread Owen Pan via cfe-commits

https://github.com/owenca created 
https://github.com/llvm/llvm-project/pull/74794

None

>From fb239719267ec97905ebb339176b5f7015de04eb Mon Sep 17 00:00:00 2001
From: Owen Pan 
Date: Thu, 7 Dec 2023 17:42:45 -0800
Subject: [PATCH] [clang-format][NFC] Clean up ClangFormat.cpp and getStyle()
 in Format.cpp

---
 clang/lib/Format/Format.cpp  | 95 
 clang/tools/clang-format/ClangFormat.cpp | 40 +-
 2 files changed, 65 insertions(+), 70 deletions(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index b09487435adb2..8feee7457fc31 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -3955,10 +3955,7 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
  StringRef FallbackStyleName,
  StringRef Code, llvm::vfs::FileSystem *FS,
  bool AllowUnknownOptions) {
-  if (!FS)
-FS = llvm::vfs::getRealFileSystem().get();
   FormatStyle Style = getLLVMStyle(guessLanguage(FileName, Code));
-
   FormatStyle FallbackStyle = getNoStyle();
   if (!getPredefinedStyle(FallbackStyleName, Style.Language, ))
 return make_string_error("Invalid fallback style: " + FallbackStyleName);
@@ -3974,14 +3971,18 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
AllowUnknownOptions)) {
   return make_string_error("Error parsing -style: " + ec.message());
 }
-if (Style.InheritsParentConfig) {
-  ChildFormatTextToApply.emplace_back(
-  llvm::MemoryBuffer::getMemBuffer(StyleName, Source, false));
-} else {
+
+if (!Style.InheritsParentConfig)
   return Style;
-}
+
+ChildFormatTextToApply.emplace_back(
+llvm::MemoryBuffer::getMemBuffer(StyleName, Source, false));
   }
 
+  if (!FS)
+FS = llvm::vfs::getRealFileSystem().get();
+  assert(FS);
+
   // User provided clang-format file using -style=file:path/to/format/file.
   if (!Style.InheritsParentConfig &&
   StyleName.starts_with_insensitive("file:")) {
@@ -4015,18 +4016,12 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
   return Style;
   }
 
-  // Reset possible inheritance
-  Style.InheritsParentConfig = false;
-
-  // Look for .clang-format/_clang-format file in the file's parent 
directories.
-  SmallString<128> UnsuitableConfigFiles;
   SmallString<128> Path(FileName);
   if (std::error_code EC = FS->makeAbsolute(Path))
 return make_string_error(EC.message());
 
-  llvm::SmallVector FilesToLookFor;
-  FilesToLookFor.push_back(".clang-format");
-  FilesToLookFor.push_back("_clang-format");
+  // Reset possible inheritance
+  Style.InheritsParentConfig = false;
 
   auto dropDiagnosticHandler = [](const llvm::SMDiagnostic &, void *) {};
 
@@ -4040,9 +4035,14 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
 }
   };
 
+  // Look for .clang-format/_clang-format file in the file's parent 
directories.
+  llvm::SmallVector FilesToLookFor;
+  FilesToLookFor.push_back(".clang-format");
+  FilesToLookFor.push_back("_clang-format");
+
+  SmallString<128> UnsuitableConfigFiles;
   for (StringRef Directory = Path; !Directory.empty();
Directory = llvm::sys::path::parent_path(Directory)) {
-
 auto Status = FS->status(Directory);
 if (!Status ||
 Status->getType() != llvm::sys::fs::file_type::directory_file) {
@@ -4055,50 +4055,51 @@ llvm::Expected getStyle(StringRef 
StyleName, StringRef FileName,
   llvm::sys::path::append(ConfigFile, F);
   LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
 
-  Status = FS->status(ConfigFile.str());
-
-  if (Status &&
-  (Status->getType() == llvm::sys::fs::file_type::regular_file)) {
-llvm::ErrorOr> Text =
-loadAndParseConfigFile(ConfigFile, FS, , 
AllowUnknownOptions);
-if (auto EC = Text.getError()) {
-  if (EC == ParseError::Unsuitable) {
-if (!UnsuitableConfigFiles.empty())
-  UnsuitableConfigFiles.append(", ");
-UnsuitableConfigFiles.append(ConfigFile);
-continue;
-  }
+  Status = FS->status(ConfigFile);
+  if (!Status ||
+  Status->getType() != llvm::sys::fs::file_type::regular_file) {
+continue;
+  }
+
+  llvm::ErrorOr> Text =
+  loadAndParseConfigFile(ConfigFile, FS, , AllowUnknownOptions);
+  if (auto EC = Text.getError()) {
+if (EC != ParseError::Unsuitable) {
   return make_string_error("Error reading " + ConfigFile + ": " +
EC.message());
 }
-LLVM_DEBUG(llvm::dbgs()
-   << "Using configuration file " << ConfigFile << "\n");
+if (!UnsuitableConfigFiles.empty())
+  UnsuitableConfigFiles.append(", ");
+UnsuitableConfigFiles.append(ConfigFile);
+continue;
+  }
 
-