Skip to content

Commit

Permalink
[Format/ObjC] Support NS_ERROR_ENUM in ObjC language guesser
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bhamiltoncx committed Apr 7, 2023
1 parent 0f9bfe0 commit 00ea679
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,8 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
"NSDecimalNumber",
"NSDictionary",
"NSEdgeInsets",
"NSError",
"NSErrorDomain",
"NSHashTable",
"NSIndexPath",
"NSIndexSet",
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/FormatTestObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 00ea679

Please sign in to comment.