Skip to content

Commit 86825db

Browse files
committed
[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
1 parent 0019684 commit 86825db

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

clang/lib/Format/Format.cpp

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,6 +2600,10 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
26002600
if (std::error_code EC = FS->makeAbsolute(Path))
26012601
return make_string_error(EC.message());
26022602

2603+
llvm::SmallVector<std::string, 2> FilesToLookFor;
2604+
FilesToLookFor.push_back(".clang-format");
2605+
FilesToLookFor.push_back("_clang-format");
2606+
26032607
for (StringRef Directory = Path; !Directory.empty();
26042608
Directory = llvm::sys::path::parent_path(Directory)) {
26052609

@@ -2609,43 +2613,35 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
26092613
continue;
26102614
}
26112615

2612-
SmallString<128> ConfigFile(Directory);
2613-
2614-
llvm::sys::path::append(ConfigFile, ".clang-format");
2615-
LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
2616+
for (const auto &F : FilesToLookFor) {
2617+
SmallString<128> ConfigFile(Directory);
26162618

2617-
Status = FS->status(ConfigFile.str());
2618-
bool FoundConfigFile =
2619-
Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);
2620-
if (!FoundConfigFile) {
2621-
// Try _clang-format too, since dotfiles are not commonly used on Windows.
2622-
ConfigFile = Directory;
2623-
llvm::sys::path::append(ConfigFile, "_clang-format");
2619+
llvm::sys::path::append(ConfigFile, F);
26242620
LLVM_DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");
2621+
26252622
Status = FS->status(ConfigFile.str());
2626-
FoundConfigFile = Status && (Status->getType() ==
2627-
llvm::sys::fs::file_type::regular_file);
2628-
}
26292623

2630-
if (FoundConfigFile) {
2631-
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
2632-
FS->getBufferForFile(ConfigFile.str());
2633-
if (std::error_code EC = Text.getError())
2634-
return make_string_error(EC.message());
2635-
if (std::error_code ec =
2636-
parseConfiguration(Text.get()->getBuffer(), &Style)) {
2637-
if (ec == ParseError::Unsuitable) {
2638-
if (!UnsuitableConfigFiles.empty())
2639-
UnsuitableConfigFiles.append(", ");
2640-
UnsuitableConfigFiles.append(ConfigFile);
2641-
continue;
2624+
if (Status &&
2625+
(Status->getType() == llvm::sys::fs::file_type::regular_file)) {
2626+
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
2627+
FS->getBufferForFile(ConfigFile.str());
2628+
if (std::error_code EC = Text.getError())
2629+
return make_string_error(EC.message());
2630+
if (std::error_code ec =
2631+
parseConfiguration(Text.get()->getBuffer(), &Style)) {
2632+
if (ec == ParseError::Unsuitable) {
2633+
if (!UnsuitableConfigFiles.empty())
2634+
UnsuitableConfigFiles.append(", ");
2635+
UnsuitableConfigFiles.append(ConfigFile);
2636+
continue;
2637+
}
2638+
return make_string_error("Error reading " + ConfigFile + ": " +
2639+
ec.message());
26422640
}
2643-
return make_string_error("Error reading " + ConfigFile + ": " +
2644-
ec.message());
2641+
LLVM_DEBUG(llvm::dbgs()
2642+
<< "Using configuration file " << ConfigFile << "\n");
2643+
return Style;
26452644
}
2646-
LLVM_DEBUG(llvm::dbgs()
2647-
<< "Using configuration file " << ConfigFile << "\n");
2648-
return Style;
26492645
}
26502646
}
26512647
if (!UnsuitableConfigFiles.empty())

0 commit comments

Comments
 (0)