r326205 - [clang-format] Tidy up new API guessLanguage()

Ben Hamilton via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 27 07:56:40 PST 2018


Author: benhamilton
Date: Tue Feb 27 07:56:40 2018
New Revision: 326205

URL: http://llvm.org/viewvc/llvm-project?rev=326205&view=rev
Log:
[clang-format] Tidy up new API guessLanguage()

Summary:
This fixes a few issues djasper@ brought up in his review of D43522.

Test Plan:
  make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D43598

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=326205&r1=326204&r2=326205&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Feb 27 07:56:40 2018
@@ -2295,8 +2295,8 @@ static FormatStyle::LanguageKind getLang
 }
 
 FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) {
-  FormatStyle::LanguageKind result = getLanguageByFileName(FileName);
-  if (result == FormatStyle::LK_Cpp) {
+  const auto GuessedLanguage = getLanguageByFileName(FileName);
+  if (GuessedLanguage == 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.
@@ -2306,12 +2306,11 @@ FormatStyle::LanguageKind guessLanguage(
           Environment::CreateVirtualEnvironment(Code, NonEmptyFileName, /*Ranges=*/{});
       ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle());
       Guesser.process();
-      if (Guesser.isObjC()) {
-        result = FormatStyle::LK_ObjC;
-      }
+      if (Guesser.isObjC())
+        return FormatStyle::LK_ObjC;
     }
   }
-  return result;
+  return GuessedLanguage;
 }
 
 llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=326205&r1=326204&r2=326205&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Feb 27 07:56:40 2018
@@ -11959,34 +11959,16 @@ 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<GuessLanguageTestCase> {};
-
-TEST_P(GuessLanguageTest, FileAndCode) {
-  auto TestCase = GetParam();
-  EXPECT_EQ(TestCase.ExpectedResult,
-            guessLanguage(TestCase.FileName, TestCase.Code));
+TEST_F(FormatTest, FileAndCode) {
+  EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
+  EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n at end\n"));
+  EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n at end\n"));
 }
 
-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 at end\n", FormatStyle::LK_ObjC},
-    {"foo", "", FormatStyle::LK_Cpp},
-    {"foo", "@interface Foo\n at end\n", FormatStyle::LK_ObjC},
-};
-INSTANTIATE_TEST_CASE_P(ValidLanguages, GuessLanguageTest,
-                        ::testing::ValuesIn(TestCases));
-
 } // end namespace
 } // end namespace format
 } // end namespace clang




More information about the cfe-commits mailing list