[clang] 86825db - [clang-format] Make '.clang-format' variants finding a loop (NFC)
Anders Waldenborg via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 7 01:08:59 PST 2019
Author: Anders Waldenborg
Date: 2019-11-07T10:00:04+01:00
New Revision: 86825dbe3306d296094432feb4a7af7d385d6b1d
URL: https://github.com/llvm/llvm-project/commit/86825dbe3306d296094432feb4a7af7d385d6b1d
DIFF: https://github.com/llvm/llvm-project/commit/86825dbe3306d296094432feb4a7af7d385d6b1d.diff
LOG: [clang-format] Make '.clang-format' variants finding a loop (NFC)
This simplifies logic making it trivial to add searching for other
files later.
Differential revision: https://reviews.llvm.org/D68568
Added:
Modified:
clang/lib/Format/Format.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index cd44c0be85f0..50a687730932 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2600,6 +2600,10 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
if (std::error_code EC = FS->makeAbsolute(Path))
return make_string_error(EC.message());
+ llvm::SmallVector<std::string, 2> FilesToLookFor;
+ FilesToLookFor.push_back(".clang-format");
+ FilesToLookFor.push_back("_clang-format");
+
for (StringRef Directory = Path; !Directory.empty();
Directory = llvm::sys::path::parent_path(Directory)) {
@@ -2609,43 +2613,35 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
continue;
}
- SmallString<128> ConfigFile(Directory);
-
- llvm::sys::path::append(ConfigFile, ".clang-format");
- LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
+ for (const auto &F : FilesToLookFor) {
+ SmallString<128> ConfigFile(Directory);
- Status = FS->status(ConfigFile.str());
- bool FoundConfigFile =
- Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
- if (!FoundConfigFile) {
- // Try _clang-format too, since dotfiles are not commonly used on Windows.
- ConfigFile = Directory;
- llvm::sys::path::append(ConfigFile, "_clang-format");
+ llvm::sys::path::append(ConfigFile, F);
LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
+
Status = FS->status(ConfigFile.str());
- FoundConfigFile = Status && (Status->getType() ==
- llvm::sys::fs::file_type::regular_file);
- }
- if (FoundConfigFile) {
- llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
- FS->getBufferForFile(ConfigFile.str());
- if (std::error_code EC = Text.getError())
- return make_string_error(EC.message());
- if (std::error_code ec =
- parseConfiguration(Text.get()->getBuffer(), &Style)) {
- if (ec == ParseError::Unsuitable) {
- if (!UnsuitableConfigFiles.empty())
- UnsuitableConfigFiles.append(", ");
- UnsuitableConfigFiles.append(ConfigFile);
- continue;
+ if (Status &&
+ (Status->getType() == llvm::sys::fs::file_type::regular_file)) {
+ llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
+ FS->getBufferForFile(ConfigFile.str());
+ if (std::error_code EC = Text.getError())
+ return make_string_error(EC.message());
+ if (std::error_code ec =
+ parseConfiguration(Text.get()->getBuffer(), &Style)) {
+ if (ec == ParseError::Unsuitable) {
+ if (!UnsuitableConfigFiles.empty())
+ UnsuitableConfigFiles.append(", ");
+ UnsuitableConfigFiles.append(ConfigFile);
+ continue;
+ }
+ return make_string_error("Error reading " + ConfigFile + ": " +
+ ec.message());
}
- return make_string_error("Error reading " + ConfigFile + ": " +
- ec.message());
+ LLVM_DEBUG(llvm::dbgs()
+ << "Using configuration file " << ConfigFile << "\n");
+ return Style;
}
- LLVM_DEBUG(llvm::dbgs()
- << "Using configuration file " << ConfigFile << "\n");
- return Style;
}
}
if (!UnsuitableConfigFiles.empty())
More information about the cfe-commits
mailing list