Skip to content

Commit f2d8a0a

Browse files
committed
[clang][NFC] Refactor ParamCommandComment::PassDirection
This patch converts `ParamCommandComment::PassDirection` to a scoped enum at namespace scope, making it eligible for forward declaring. This is useful for e.g. annotating bit-fields with `preferred_type`.
1 parent 3de645e commit f2d8a0a

File tree

7 files changed

+64
-76
lines changed

7 files changed

+64
-76
lines changed

clang/include/clang/AST/Comment.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ class BlockCommandComment : public BlockContentComment {
675675
}
676676
};
677677

678+
enum class ParamCommandPassDirection { In, Out, InOut };
679+
678680
/// Doxygen \\param command.
679681
class ParamCommandComment : public BlockCommandComment {
680682
private:
@@ -692,32 +694,28 @@ class ParamCommandComment : public BlockCommandComment {
692694
: BlockCommandComment(CommentKind::ParamCommandComment, LocBegin, LocEnd,
693695
CommandID, CommandMarker),
694696
ParamIndex(InvalidParamIndex) {
695-
ParamCommandCommentBits.Direction = In;
697+
ParamCommandCommentBits.Direction =
698+
llvm::to_underlying(ParamCommandPassDirection::In);
696699
ParamCommandCommentBits.IsDirectionExplicit = false;
697700
}
698701

699702
static bool classof(const Comment *C) {
700703
return C->getCommentKind() == CommentKind::ParamCommandComment;
701704
}
702705

703-
enum PassDirection {
704-
In,
705-
Out,
706-
InOut
707-
};
708-
709-
static const char *getDirectionAsString(PassDirection D);
706+
static const char *getDirectionAsString(ParamCommandPassDirection D);
710707

711-
PassDirection getDirection() const LLVM_READONLY {
712-
return static_cast<PassDirection>(ParamCommandCommentBits.Direction);
708+
ParamCommandPassDirection getDirection() const LLVM_READONLY {
709+
return static_cast<ParamCommandPassDirection>(
710+
ParamCommandCommentBits.Direction);
713711
}
714712

715713
bool isDirectionExplicit() const LLVM_READONLY {
716714
return ParamCommandCommentBits.IsDirectionExplicit;
717715
}
718716

719-
void setDirection(PassDirection Direction, bool Explicit) {
720-
ParamCommandCommentBits.Direction = Direction;
717+
void setDirection(ParamCommandPassDirection Direction, bool Explicit) {
718+
ParamCommandCommentBits.Direction = llvm::to_underlying(Direction);
721719
ParamCommandCommentBits.IsDirectionExplicit = Explicit;
722720
}
723721

clang/lib/AST/Comment.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,14 @@ static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
187187
return false;
188188
}
189189

190-
const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
190+
const char *
191+
ParamCommandComment::getDirectionAsString(ParamCommandPassDirection D) {
191192
switch (D) {
192-
case ParamCommandComment::In:
193+
case ParamCommandPassDirection::In:
193194
return "[in]";
194-
case ParamCommandComment::Out:
195+
case ParamCommandPassDirection::Out:
195196
return "[out]";
196-
case ParamCommandComment::InOut:
197+
case ParamCommandPassDirection::InOut:
197198
return "[in,out]";
198199
}
199200
llvm_unreachable("unknown PassDirection");

clang/lib/AST/CommentSema.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,38 +219,38 @@ void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
219219

220220
/// Turn a string into the corresponding PassDirection or -1 if it's not
221221
/// valid.
222-
static int getParamPassDirection(StringRef Arg) {
223-
return llvm::StringSwitch<int>(Arg)
224-
.Case("[in]", ParamCommandComment::In)
225-
.Case("[out]", ParamCommandComment::Out)
226-
.Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
227-
.Default(-1);
222+
static ParamCommandPassDirection getParamPassDirection(StringRef Arg) {
223+
return llvm::StringSwitch<ParamCommandPassDirection>(Arg)
224+
.Case("[in]", ParamCommandPassDirection::In)
225+
.Case("[out]", ParamCommandPassDirection::Out)
226+
.Cases("[in,out]", "[out,in]", ParamCommandPassDirection::InOut)
227+
.Default(static_cast<ParamCommandPassDirection>(-1));
228228
}
229229

230230
void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
231231
SourceLocation ArgLocBegin,
232232
SourceLocation ArgLocEnd,
233233
StringRef Arg) {
234234
std::string ArgLower = Arg.lower();
235-
int Direction = getParamPassDirection(ArgLower);
235+
ParamCommandPassDirection Direction = getParamPassDirection(ArgLower);
236236

237-
if (Direction == -1) {
237+
if (Direction == static_cast<ParamCommandPassDirection>(-1)) {
238238
// Try again with whitespace removed.
239239
llvm::erase_if(ArgLower, clang::isWhitespace);
240240
Direction = getParamPassDirection(ArgLower);
241241

242242
SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
243-
if (Direction != -1) {
244-
const char *FixedName = ParamCommandComment::getDirectionAsString(
245-
(ParamCommandComment::PassDirection)Direction);
243+
if (Direction != static_cast<ParamCommandPassDirection>(-1)) {
244+
const char *FixedName =
245+
ParamCommandComment::getDirectionAsString(Direction);
246246
Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
247247
<< ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
248248
} else {
249249
Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
250-
Direction = ParamCommandComment::In; // Sane fall back.
250+
Direction = ParamCommandPassDirection::In; // Sane fall back.
251251
}
252252
}
253-
Command->setDirection((ParamCommandComment::PassDirection)Direction,
253+
Command->setDirection(Direction,
254254
/*Explicit=*/true);
255255
}
256256

@@ -263,7 +263,8 @@ void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
263263

264264
if (!Command->isDirectionExplicit()) {
265265
// User didn't provide a direction argument.
266-
Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
266+
Command->setDirection(ParamCommandPassDirection::In,
267+
/* Explicit = */ false);
267268
}
268269
auto *A = new (Allocator)
269270
Comment::Argument{SourceRange(ArgLocBegin, ArgLocEnd), Arg};

clang/lib/AST/JSONNodeDumper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,13 +1740,13 @@ void JSONNodeDumper::visitBlockCommandComment(
17401740
void JSONNodeDumper::visitParamCommandComment(
17411741
const comments::ParamCommandComment *C, const comments::FullComment *FC) {
17421742
switch (C->getDirection()) {
1743-
case comments::ParamCommandComment::In:
1743+
case comments::ParamCommandPassDirection::In:
17441744
JOS.attribute("direction", "in");
17451745
break;
1746-
case comments::ParamCommandComment::Out:
1746+
case comments::ParamCommandPassDirection::Out:
17471747
JOS.attribute("direction", "out");
17481748
break;
1749-
case comments::ParamCommandComment::InOut:
1749+
case comments::ParamCommandPassDirection::InOut:
17501750
JOS.attribute("direction", "in,out");
17511751
break;
17521752
}

clang/lib/Index/CommentToXML.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,13 +751,13 @@ void CommentASTToXMLConverter::visitParamCommandComment(
751751

752752
Result << "<Direction isExplicit=\"" << C->isDirectionExplicit() << "\">";
753753
switch (C->getDirection()) {
754-
case ParamCommandComment::In:
754+
case ParamCommandPassDirection::In:
755755
Result << "in";
756756
break;
757-
case ParamCommandComment::Out:
757+
case ParamCommandPassDirection::Out:
758758
Result << "out";
759759
break;
760-
case ParamCommandComment::InOut:
760+
case ParamCommandPassDirection::InOut:
761761
Result << "in,out";
762762
break;
763763
}

clang/tools/libclang/CXComment.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,13 @@ enum CXCommentParamPassDirection clang_ParamCommandComment_getDirection(
296296
return CXCommentParamPassDirection_In;
297297

298298
switch (PCC->getDirection()) {
299-
case ParamCommandComment::In:
299+
case ParamCommandPassDirection::In:
300300
return CXCommentParamPassDirection_In;
301301

302-
case ParamCommandComment::Out:
302+
case ParamCommandPassDirection::Out:
303303
return CXCommentParamPassDirection_Out;
304304

305-
case ParamCommandComment::InOut:
305+
case ParamCommandPassDirection::InOut:
306306
return CXCommentParamPassDirection_InOut;
307307
}
308308
llvm_unreachable("unknown ParamCommandComment::PassDirection");

clang/unittests/AST/CommentParser.cpp

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,11 @@ ::testing::AssertionResult HasBlockCommandAt(const Comment *C,
176176
return ::testing::AssertionSuccess();
177177
}
178178

179-
::testing::AssertionResult HasParamCommandAt(
180-
const Comment *C,
181-
const CommandTraits &Traits,
182-
size_t Idx,
183-
ParamCommandComment *&PCC,
184-
StringRef CommandName,
185-
ParamCommandComment::PassDirection Direction,
186-
bool IsDirectionExplicit,
187-
StringRef ParamName,
188-
ParagraphComment *&Paragraph) {
179+
::testing::AssertionResult
180+
HasParamCommandAt(const Comment *C, const CommandTraits &Traits, size_t Idx,
181+
ParamCommandComment *&PCC, StringRef CommandName,
182+
ParamCommandPassDirection Direction, bool IsDirectionExplicit,
183+
StringRef ParamName, ParagraphComment *&Paragraph) {
189184
::testing::AssertionResult AR = GetChildAt(C, Idx, PCC);
190185
if (!AR)
191186
return AR;
@@ -756,10 +751,9 @@ TEST_F(CommentParserTest, ParamCommand1) {
756751
{
757752
ParamCommandComment *PCC;
758753
ParagraphComment *PC;
759-
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
760-
ParamCommandComment::In,
761-
/* IsDirectionExplicit = */ false,
762-
"aaa", PC));
754+
ASSERT_TRUE(HasParamCommandAt(
755+
FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
756+
/* IsDirectionExplicit = */ false, "aaa", PC));
763757
ASSERT_TRUE(HasChildCount(PCC, 1));
764758
ASSERT_TRUE(HasChildCount(PC, 0));
765759
}
@@ -776,9 +770,8 @@ TEST_F(CommentParserTest, ParamCommand2) {
776770
ParamCommandComment *PCC;
777771
ParagraphComment *PC;
778772
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
779-
ParamCommandComment::In,
780-
/* IsDirectionExplicit = */ false,
781-
"", PC));
773+
ParamCommandPassDirection::In,
774+
/* IsDirectionExplicit = */ false, "", PC));
782775
ASSERT_TRUE(HasChildCount(PCC, 1));
783776
ASSERT_TRUE(HasChildCount(PC, 0));
784777
}
@@ -809,10 +802,9 @@ TEST_F(CommentParserTest, ParamCommand3) {
809802
{
810803
ParamCommandComment *PCC;
811804
ParagraphComment *PC;
812-
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
813-
ParamCommandComment::In,
814-
/* IsDirectionExplicit = */ false,
815-
"aaa", PC));
805+
ASSERT_TRUE(HasParamCommandAt(
806+
FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
807+
/* IsDirectionExplicit = */ false, "aaa", PC));
816808
ASSERT_TRUE(HasChildCount(PCC, 1));
817809
ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
818810
}
@@ -839,10 +831,9 @@ TEST_F(CommentParserTest, ParamCommand4) {
839831
{
840832
ParamCommandComment *PCC;
841833
ParagraphComment *PC;
842-
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
843-
ParamCommandComment::In,
844-
/* IsDirectionExplicit = */ true,
845-
"aaa", PC));
834+
ASSERT_TRUE(HasParamCommandAt(
835+
FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
836+
/* IsDirectionExplicit = */ true, "aaa", PC));
846837
ASSERT_TRUE(HasChildCount(PCC, 1));
847838
ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
848839
}
@@ -869,10 +860,9 @@ TEST_F(CommentParserTest, ParamCommand5) {
869860
{
870861
ParamCommandComment *PCC;
871862
ParagraphComment *PC;
872-
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
873-
ParamCommandComment::Out,
874-
/* IsDirectionExplicit = */ true,
875-
"aaa", PC));
863+
ASSERT_TRUE(HasParamCommandAt(
864+
FC, Traits, 1, PCC, "param", ParamCommandPassDirection::Out,
865+
/* IsDirectionExplicit = */ true, "aaa", PC));
876866
ASSERT_TRUE(HasChildCount(PCC, 1));
877867
ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
878868
}
@@ -900,10 +890,9 @@ TEST_F(CommentParserTest, ParamCommand6) {
900890
{
901891
ParamCommandComment *PCC;
902892
ParagraphComment *PC;
903-
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
904-
ParamCommandComment::InOut,
905-
/* IsDirectionExplicit = */ true,
906-
"aaa", PC));
893+
ASSERT_TRUE(HasParamCommandAt(
894+
FC, Traits, 1, PCC, "param", ParamCommandPassDirection::InOut,
895+
/* IsDirectionExplicit = */ true, "aaa", PC));
907896
ASSERT_TRUE(HasChildCount(PCC, 1));
908897
ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
909898
}
@@ -921,10 +910,9 @@ TEST_F(CommentParserTest, ParamCommand7) {
921910
{
922911
ParamCommandComment *PCC;
923912
ParagraphComment *PC;
924-
ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
925-
ParamCommandComment::In,
926-
/* IsDirectionExplicit = */ false,
927-
"aaa", PC));
913+
ASSERT_TRUE(HasParamCommandAt(
914+
FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
915+
/* IsDirectionExplicit = */ false, "aaa", PC));
928916
ASSERT_TRUE(HasChildCount(PCC, 1));
929917

930918
ASSERT_TRUE(HasChildCount(PC, 5));

0 commit comments

Comments
 (0)