[PATCH] D43598: [clang-format] Tidy up new API guessLanguage()

2018-02-27 Thread Ben Hamilton via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL326205: [clang-format] Tidy up new API guessLanguage() 
(authored by benhamilton, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D43598?vs=135341=136080#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D43598

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


Index: cfe/trunk/lib/Format/Format.cpp
===
--- cfe/trunk/lib/Format/Format.cpp
+++ cfe/trunk/lib/Format/Format.cpp
@@ -2295,8 +2295,8 @@
 }
 
 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 @@
   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 getStyle(StringRef StyleName, StringRef FileName,
Index: cfe/trunk/unittests/Format/FormatTest.cpp
===
--- cfe/trunk/unittests/Format/FormatTest.cpp
+++ cfe/trunk/unittests/Format/FormatTest.cpp
@@ -11959,33 +11959,15 @@
   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));
+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@end\n"));
+  EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface 
Foo\n@end\n"));
+}
 
 } // end namespace
 } // end namespace format


Index: cfe/trunk/lib/Format/Format.cpp
===
--- cfe/trunk/lib/Format/Format.cpp
+++ cfe/trunk/lib/Format/Format.cpp
@@ -2295,8 +2295,8 @@
 }
 
 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 @@
   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 getStyle(StringRef StyleName, StringRef FileName,
Index: cfe/trunk/unittests/Format/FormatTest.cpp
===
--- cfe/trunk/unittests/Format/FormatTest.cpp
+++ cfe/trunk/unittests/Format/FormatTest.cpp
@@ -11959,33 +11959,15 @@
   verifyFormat("auto const &[ a, b ] = f();", Spaces);
 }
 
-struct GuessLanguageTestCase {
-  const char *const FileName;
-  const 

[PATCH] D43598: [clang-format] Tidy up new API guessLanguage()

2018-02-21 Thread Daniel Jasper via Phabricator via cfe-commits
djasper accepted this revision.
djasper added a comment.
This revision is now accepted and ready to land.

Thank you for doing these follow up changes!


Repository:
  rC Clang

https://reviews.llvm.org/D43598



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D43598: [clang-format] Tidy up new API guessLanguage()

2018-02-21 Thread Ben Hamilton via Phabricator via cfe-commits
benhamilton created this revision.
benhamilton added a reviewer: djasper.
Herald added subscribers: cfe-commits, klimek.

This fixes a few issues djasper@ brought up in his review of 
https://reviews.llvm.org/D43522.

Test Plan:

  make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests


Repository:
  rC Clang

https://reviews.llvm.org/D43598

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp


Index: unittests/Format/FormatTest.cpp
===
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -11958,34 +11958,16 @@
   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));
+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@end\n"));
+  EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface 
Foo\n@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@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
Index: lib/Format/Format.cpp
===
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -2295,8 +2295,8 @@
 }
 
 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 @@
   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 getStyle(StringRef StyleName, StringRef FileName,


Index: unittests/Format/FormatTest.cpp
===
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -11958,34 +11958,16 @@
   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));
+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@end\n"));
+  EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@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@end\n", FormatStyle::LK_ObjC},
-{"foo", "", FormatStyle::LK_Cpp},
-{"foo", "@interface Foo\n@end\n", FormatStyle::LK_ObjC},
-};