From 3b345c3677b361433caf292de01b31cfc0835ba1 Mon Sep 17 00:00:00 2001 From: Ben Hamilton Date: Wed, 21 Feb 2018 15:54:31 +0000 Subject: [PATCH] [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 --- clang/include/clang/Format/Format.h | 4 ++++ clang/lib/Format/Format.cpp | 31 ++++++++++++++++++++----------- clang/unittests/Format/FormatTest.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6a37137..94df954 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1981,6 +1981,10 @@ llvm::Expected getStyle(StringRef StyleName, StringRef FileName, StringRef Code = "", vfs::FileSystem *FS = nullptr); +// \brief Guesses the language from the ``FileName`` and ``Code`` to be formatted. +// Defaults to FormatStyle::LK_Cpp. +FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code); + // \brief Returns a string representation of ``Language``. inline StringRef getLanguageName(FormatStyle::LanguageKind Language) { switch (Language) { diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e77314d..54b10f4 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 Env = + Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); + ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle()); + Guesser.process(); + if (Guesser.isObjC()) { + result = FormatStyle::LK_ObjC; + } + } + } + return result; +} + llvm::Expected getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, StringRef Code, vfs::FileSystem *FS) { @@ -2301,17 +2320,7 @@ llvm::Expected 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 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)) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e5e8fde..c029a74 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -11952,6 +11952,34 @@ TEST_F(FormatTest, StructuredBindings) { verifyFormat("auto const &[ a, b ] = f();", Spaces); } +struct GuessLanguageTestCase { + const char *const FileName; + const char *const Code; + const FormatStyle::LanguageKind ExpectedResult; +}; + +class GuessLanguageTest + : public FormatTest, + public ::testing::WithParamInterface {}; + +TEST_P(GuessLanguageTest, FileAndCode) { + auto TestCase = GetParam(); + EXPECT_EQ(TestCase.ExpectedResult, + guessLanguage(TestCase.FileName, TestCase.Code)); +} + +static const GuessLanguageTestCase TestCases[] = { + {"foo.cc", "", FormatStyle::LK_Cpp}, + {"foo.m", "", FormatStyle::LK_ObjC}, + {"foo.mm", "", FormatStyle::LK_ObjC}, + {"foo.h", "", FormatStyle::LK_Cpp}, + {"foo.h", "@interface Foo\n@end\n", FormatStyle::LK_ObjC}, + {"foo", "", FormatStyle::LK_Cpp}, + {"foo", "@interface Foo\n@end\n", FormatStyle::LK_ObjC}, +}; +INSTANTIATE_TEST_CASE_P(ValidLanguages, GuessLanguageTest, + ::testing::ValuesIn(TestCases)); + } // end namespace } // end namespace format } // end namespace clang -- 2.7.4