diff options
author | Anders Waldenborg <anders@0x63.nu> | 2019-11-07 09:59:34 +0100 |
---|---|---|
committer | Anders Waldenborg <anders@0x63.nu> | 2019-11-07 10:00:04 +0100 |
commit | 86825dbe3306d296094432feb4a7af7d385d6b1d (patch) | |
tree | fa83f90e70b2f2de7888351e76fc94fde35b26c6 /clang/lib/Format/Format.cpp | |
parent | 0019684900491f517f3b08b4fa92740b69a8cc0f (diff) | |
download | bcm5719-llvm-86825dbe3306d296094432feb4a7af7d385d6b1d.tar.gz bcm5719-llvm-86825dbe3306d296094432feb4a7af7d385d6b1d.zip |
[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
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index cd44c0be85f..50a68773093 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()) |