[clang] 85670ac - [Format/ObjC] Support NS_ASSUME_NONNULL_BEGIN and FOUNDATION_EXPORT in ObjC language guesser

Ben Hamilton via cfe-commits cfe-commits at lists.llvm.org
Tue May 30 10:12:28 PDT 2023


Author: Ben Hamilton
Date: 2023-05-30T11:12:03-06:00
New Revision: 85670ac86813b170c9301aa477421c56a71a7e1e

URL: https://github.com/llvm/llvm-project/commit/85670ac86813b170c9301aa477421c56a71a7e1e
DIFF: https://github.com/llvm/llvm-project/commit/85670ac86813b170c9301aa477421c56a71a7e1e.diff

LOG: [Format/ObjC] Support NS_ASSUME_NONNULL_BEGIN and FOUNDATION_EXPORT in ObjC language guesser

This adds to the ObjC language guesser a few more common macros used
in ObjC headers. These can help distinguish ObjC headers which
otherwise lack ObjC types from C++ headers.

Contributed by danblakemore.

Tested: New tests included. Ran unit tests with:
  ```
  % cmake -S llvm -B build -G Ninja && \
    ninja -C build FormatTests && \
    ./build/tools/clang/unittests/Format/FormatTests --gtest_filter="*FormatTestObjC*"

  (snip)
  [----------] 24 tests from FormatTestObjC (265 ms total)

  [----------] Global test environment tear-down
  [==========] 26 tests from 2 test suites ran. (270 ms total)
  [  PASSED  ] 26 tests.
  ```

Reviewed By: MyDeveloperDay

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

Added: 
    clang/test/Format/dump-config-objc-macros.h

Modified: 
    clang/lib/Format/Format.cpp
    clang/unittests/Format/FormatTestObjC.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 154e6a21981e8..d7128ed558dc5 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2687,6 +2687,8 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
         "CGSizeMake",
         "CGVector",
         "CGVectorMake",
+        "FOUNDATION_EXPORT", // This is an alias for FOUNDATION_EXTERN.
+        "FOUNDATION_EXTERN",
         "NSAffineTransform",
         "NSArray",
         "NSAttributedString",
@@ -2743,6 +2745,7 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
         "NSURLQueryItem",
         "NSUUID",
         "NSValue",
+        "NS_ASSUME_NONNULL_BEGIN",
         "UIImage",
         "UIView",
     };

diff  --git a/clang/test/Format/dump-config-objc-macros.h b/clang/test/Format/dump-config-objc-macros.h
new file mode 100644
index 0000000000000..c90aa6067b1f4
--- /dev/null
+++ b/clang/test/Format/dump-config-objc-macros.h
@@ -0,0 +1,8 @@
+// RUN: clang-format -dump-config %s | FileCheck %s
+
+// CHECK: Language: ObjC
+NS_ASSUME_NONNULL_BEGIN
+
+FOUNDATION_EXTERN int kConstant;
+
+NS_ASSUME_NONNULL_END

diff  --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp
index 55969ff6fd4a9..0cae6e2950fe6 100644
--- a/clang/unittests/Format/FormatTestObjC.cpp
+++ b/clang/unittests/Format/FormatTestObjC.cpp
@@ -94,6 +94,26 @@ TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
   ASSERT_TRUE((bool)Style);
   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
 
+  Style = getStyle("{}", "a.h", "none", R"objc(
+NS_ASSUME_NONNULL_BEGIN
+extern int i;
+NS_ASSUME_NONNULL_END
+)objc");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+  Style = getStyle("{}", "a.h", "none", R"objc(
+FOUNDATION_EXTERN void DoStuff(void);
+)objc");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+  Style = getStyle("{}", "a.h", "none", R"objc(
+FOUNDATION_EXPORT void DoStuff(void);
+)objc");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
   Style = getStyle("{}", "a.h", "none", "enum Foo {};");
   ASSERT_TRUE((bool)Style);
   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);


        


More information about the cfe-commits mailing list