diff options
| author | Ben Hamilton <benhamilton@google.com> | 2018-02-21 15:54:31 +0000 |
|---|---|---|
| committer | Ben Hamilton <benhamilton@google.com> | 2018-02-21 15:54:31 +0000 |
| commit | 3b345c3677b361433caf292de01b31cfc0835ba1 (patch) | |
| tree | fec3849a6d67189c843e86865654c5aeff15d68a /clang/lib/Format | |
| parent | f3a9ab07aa0e1a12bb523d795d18ca4dac2c438b (diff) | |
| download | bcm5719-llvm-3b345c3677b361433caf292de01b31cfc0835ba1.tar.gz bcm5719-llvm-3b345c3677b361433caf292de01b31cfc0835ba1.zip | |
[clang-format] New API guessLanguage()
Summary:
For clients which don't have a filesystem, calling getStyle() doesn't
make much sense (there's no .clang-format files to search for).
In this diff, I hoist out the language-guessing logic from getStyle()
and move it into a new API guessLanguage().
I also added support for guessing the language of files which have no
extension (they could be C++ or ObjC).
Test Plan: New tests added. Ran tests with:
% make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests
Reviewers: jolesiak, krasimir
Reviewed By: jolesiak, krasimir
Subscribers: klimek, cfe-commits, sammccall
Differential Revision: https://reviews.llvm.org/D43522
llvm-svn: 325691
Diffstat (limited to 'clang/lib/Format')
| -rw-r--r-- | clang/lib/Format/Format.cpp | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e77314d255d..54b10f474c0 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2294,6 +2294,25 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { return FormatStyle::LK_Cpp; } +FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { + FormatStyle::LanguageKind result = getLanguageByFileName(FileName); + if (result == FormatStyle::LK_Cpp) { + auto extension = llvm::sys::path::extension(FileName); + // If there's no file extension (or it's .h), we need to check the contents + // of the code to see if it contains Objective-C. + if (extension.empty() || extension == ".h") { + std::unique_ptr<Environment> Env = + Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); + ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle()); + Guesser.process(); + if (Guesser.isObjC()) { + result = FormatStyle::LK_ObjC; + } + } + } + return result; +} + llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, StringRef Code, vfs::FileSystem *FS) { @@ -2301,17 +2320,7 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName, FS = vfs::getRealFileSystem().get(); } FormatStyle Style = getLLVMStyle(); - Style.Language = getLanguageByFileName(FileName); - - if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h")) { - std::unique_ptr<Environment> Env = - Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); - ObjCHeaderStyleGuesser Guesser(*Env, Style); - Guesser.process(); - if (Guesser.isObjC()) { - Style.Language = FormatStyle::LK_ObjC; - } - } + Style.Language = guessLanguage(FileName, Code); FormatStyle FallbackStyle = getNoStyle(); if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle)) |

