[clang] 00ea679 - [Format/ObjC] Support NS_ERROR_ENUM in ObjC language guesser
Ben Hamilton via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 7 15:11:14 PDT 2023
Author: Ben Hamilton
Date: 2023-04-07T16:10:52-06:00
New Revision: 00ea6798959d358aff9d7ef0907bbe241be7f7a4
URL: https://github.com/llvm/llvm-project/commit/00ea6798959d358aff9d7ef0907bbe241be7f7a4
DIFF: https://github.com/llvm/llvm-project/commit/00ea6798959d358aff9d7ef0907bbe241be7f7a4.diff
LOG: [Format/ObjC] Support NS_ERROR_ENUM in ObjC language guesser
Apple added a new NS_ERROR_ENUM macro to help define enums for
NSError codes.
This updates libformat's Objective-C language-guessing heuristic
to detect the new macro as well as related NSError types.
Tested: New tests added.
Reviewed By: MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D147577
Added:
Modified:
clang/lib/Format/Format.cpp
clang/lib/Format/FormatToken.h
clang/unittests/Format/FormatTestObjC.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 248d3684d01f2..6f377bb28e520 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2699,6 +2699,8 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
"NSDecimalNumber",
"NSDictionary",
"NSEdgeInsets",
+ "NSError",
+ "NSErrorDomain",
"NSHashTable",
"NSIndexPath",
"NSIndexSet",
@@ -2760,6 +2762,7 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
FormatTok->TokenText)) ||
FormatTok->is(TT_ObjCStringLiteral) ||
FormatTok->isOneOf(Keywords.kw_NS_CLOSED_ENUM, Keywords.kw_NS_ENUM,
+ Keywords.kw_NS_ERROR_ENUM,
Keywords.kw_NS_OPTIONS, TT_ObjCBlockLBrace,
TT_ObjCBlockLParen, TT_ObjCDecl, TT_ObjCForIn,
TT_ObjCMethodExpr, TT_ObjCMethodSpecifier,
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index dc136428082e0..834ebf6f8b204 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -949,6 +949,7 @@ struct AdditionalKeywords {
kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM");
kw_NS_ENUM = &IdentTable.get("NS_ENUM");
+ kw_NS_ERROR_ENUM = &IdentTable.get("NS_ERROR_ENUM");
kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
kw_as = &IdentTable.get("as");
@@ -1334,6 +1335,7 @@ struct AdditionalKeywords {
IdentifierInfo *kw_CF_OPTIONS;
IdentifierInfo *kw_NS_CLOSED_ENUM;
IdentifierInfo *kw_NS_ENUM;
+ IdentifierInfo *kw_NS_ERROR_ENUM;
IdentifierInfo *kw_NS_OPTIONS;
IdentifierInfo *kw___except;
IdentifierInfo *kw___has_include;
diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp
index e32b09239ae45..55969ff6fd4a9 100644
--- a/clang/unittests/Format/FormatTestObjC.cpp
+++ b/clang/unittests/Format/FormatTestObjC.cpp
@@ -89,6 +89,11 @@ TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
ASSERT_TRUE((bool)Style);
EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+ Style =
+ getStyle("{}", "a.h", "none", "typedef NS_ERROR_ENUM(int, Foo) {};\n");
+ 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