[clang] f2d8a0a - [clang][NFC] Refactor `ParamCommandComment::PassDirection`

Vlad Serebrennikov via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 11:55:58 PST 2023


Author: Vlad Serebrennikov
Date: 2023-11-06T22:55:51+03:00
New Revision: f2d8a0ac1dd1fe55b2c3b358c525fbc01b0121ed

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

LOG: [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`.

Added: 
    

Modified: 
    clang/include/clang/AST/Comment.h
    clang/lib/AST/Comment.cpp
    clang/lib/AST/CommentSema.cpp
    clang/lib/AST/JSONNodeDumper.cpp
    clang/lib/Index/CommentToXML.cpp
    clang/tools/libclang/CXComment.cpp
    clang/unittests/AST/CommentParser.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/Comment.h b/clang/include/clang/AST/Comment.h
index 06effad42db45e6..d501ce1822862d9 100644
--- a/clang/include/clang/AST/Comment.h
+++ b/clang/include/clang/AST/Comment.h
@@ -675,6 +675,8 @@ class BlockCommandComment : public BlockContentComment {
   }
 };
 
+enum class ParamCommandPassDirection { In, Out, InOut };
+
 /// Doxygen \\param command.
 class ParamCommandComment : public BlockCommandComment {
 private:
@@ -692,7 +694,8 @@ class ParamCommandComment : public BlockCommandComment {
       : BlockCommandComment(CommentKind::ParamCommandComment, LocBegin, LocEnd,
                             CommandID, CommandMarker),
         ParamIndex(InvalidParamIndex) {
-    ParamCommandCommentBits.Direction = In;
+    ParamCommandCommentBits.Direction =
+        llvm::to_underlying(ParamCommandPassDirection::In);
     ParamCommandCommentBits.IsDirectionExplicit = false;
   }
 
@@ -700,24 +703,19 @@ class ParamCommandComment : public BlockCommandComment {
     return C->getCommentKind() == CommentKind::ParamCommandComment;
   }
 
-  enum PassDirection {
-    In,
-    Out,
-    InOut
-  };
-
-  static const char *getDirectionAsString(PassDirection D);
+  static const char *getDirectionAsString(ParamCommandPassDirection D);
 
-  PassDirection getDirection() const LLVM_READONLY {
-    return static_cast<PassDirection>(ParamCommandCommentBits.Direction);
+  ParamCommandPassDirection getDirection() const LLVM_READONLY {
+    return static_cast<ParamCommandPassDirection>(
+        ParamCommandCommentBits.Direction);
   }
 
   bool isDirectionExplicit() const LLVM_READONLY {
     return ParamCommandCommentBits.IsDirectionExplicit;
   }
 
-  void setDirection(PassDirection Direction, bool Explicit) {
-    ParamCommandCommentBits.Direction = Direction;
+  void setDirection(ParamCommandPassDirection Direction, bool Explicit) {
+    ParamCommandCommentBits.Direction = llvm::to_underlying(Direction);
     ParamCommandCommentBits.IsDirectionExplicit = Explicit;
   }
 

diff  --git a/clang/lib/AST/Comment.cpp b/clang/lib/AST/Comment.cpp
index 0a2d310e5b48911..cce8b12170f21fd 100644
--- a/clang/lib/AST/Comment.cpp
+++ b/clang/lib/AST/Comment.cpp
@@ -187,13 +187,14 @@ static bool getFunctionTypeLoc(TypeLoc TL, FunctionTypeLoc &ResFTL) {
   return false;
 }
 
-const char *ParamCommandComment::getDirectionAsString(PassDirection D) {
+const char *
+ParamCommandComment::getDirectionAsString(ParamCommandPassDirection D) {
   switch (D) {
-  case ParamCommandComment::In:
+  case ParamCommandPassDirection::In:
     return "[in]";
-  case ParamCommandComment::Out:
+  case ParamCommandPassDirection::Out:
     return "[out]";
-  case ParamCommandComment::InOut:
+  case ParamCommandPassDirection::InOut:
     return "[in,out]";
   }
   llvm_unreachable("unknown PassDirection");

diff  --git a/clang/lib/AST/CommentSema.cpp b/clang/lib/AST/CommentSema.cpp
index 6f68577954137f3..bc01baa1d917b57 100644
--- a/clang/lib/AST/CommentSema.cpp
+++ b/clang/lib/AST/CommentSema.cpp
@@ -219,12 +219,12 @@ void Sema::checkContainerDecl(const BlockCommandComment *Comment) {
 
 /// Turn a string into the corresponding PassDirection or -1 if it's not
 /// valid.
-static int getParamPassDirection(StringRef Arg) {
-  return llvm::StringSwitch<int>(Arg)
-      .Case("[in]", ParamCommandComment::In)
-      .Case("[out]", ParamCommandComment::Out)
-      .Cases("[in,out]", "[out,in]", ParamCommandComment::InOut)
-      .Default(-1);
+static ParamCommandPassDirection getParamPassDirection(StringRef Arg) {
+  return llvm::StringSwitch<ParamCommandPassDirection>(Arg)
+      .Case("[in]", ParamCommandPassDirection::In)
+      .Case("[out]", ParamCommandPassDirection::Out)
+      .Cases("[in,out]", "[out,in]", ParamCommandPassDirection::InOut)
+      .Default(static_cast<ParamCommandPassDirection>(-1));
 }
 
 void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
@@ -232,25 +232,25 @@ void Sema::actOnParamCommandDirectionArg(ParamCommandComment *Command,
                                          SourceLocation ArgLocEnd,
                                          StringRef Arg) {
   std::string ArgLower = Arg.lower();
-  int Direction = getParamPassDirection(ArgLower);
+  ParamCommandPassDirection Direction = getParamPassDirection(ArgLower);
 
-  if (Direction == -1) {
+  if (Direction == static_cast<ParamCommandPassDirection>(-1)) {
     // Try again with whitespace removed.
     llvm::erase_if(ArgLower, clang::isWhitespace);
     Direction = getParamPassDirection(ArgLower);
 
     SourceRange ArgRange(ArgLocBegin, ArgLocEnd);
-    if (Direction != -1) {
-      const char *FixedName = ParamCommandComment::getDirectionAsString(
-          (ParamCommandComment::PassDirection)Direction);
+    if (Direction != static_cast<ParamCommandPassDirection>(-1)) {
+      const char *FixedName =
+          ParamCommandComment::getDirectionAsString(Direction);
       Diag(ArgLocBegin, diag::warn_doc_param_spaces_in_direction)
           << ArgRange << FixItHint::CreateReplacement(ArgRange, FixedName);
     } else {
       Diag(ArgLocBegin, diag::warn_doc_param_invalid_direction) << ArgRange;
-      Direction = ParamCommandComment::In; // Sane fall back.
+      Direction = ParamCommandPassDirection::In; // Sane fall back.
     }
   }
-  Command->setDirection((ParamCommandComment::PassDirection)Direction,
+  Command->setDirection(Direction,
                         /*Explicit=*/true);
 }
 
@@ -263,7 +263,8 @@ void Sema::actOnParamCommandParamNameArg(ParamCommandComment *Command,
 
   if (!Command->isDirectionExplicit()) {
     // User didn't provide a direction argument.
-    Command->setDirection(ParamCommandComment::In, /* Explicit = */ false);
+    Command->setDirection(ParamCommandPassDirection::In,
+                          /* Explicit = */ false);
   }
   auto *A = new (Allocator)
       Comment::Argument{SourceRange(ArgLocBegin, ArgLocEnd), Arg};

diff  --git a/clang/lib/AST/JSONNodeDumper.cpp b/clang/lib/AST/JSONNodeDumper.cpp
index cfcf27ecddebbc4..ace5178bf625828 100644
--- a/clang/lib/AST/JSONNodeDumper.cpp
+++ b/clang/lib/AST/JSONNodeDumper.cpp
@@ -1740,13 +1740,13 @@ void JSONNodeDumper::visitBlockCommandComment(
 void JSONNodeDumper::visitParamCommandComment(
     const comments::ParamCommandComment *C, const comments::FullComment *FC) {
   switch (C->getDirection()) {
-  case comments::ParamCommandComment::In:
+  case comments::ParamCommandPassDirection::In:
     JOS.attribute("direction", "in");
     break;
-  case comments::ParamCommandComment::Out:
+  case comments::ParamCommandPassDirection::Out:
     JOS.attribute("direction", "out");
     break;
-  case comments::ParamCommandComment::InOut:
+  case comments::ParamCommandPassDirection::InOut:
     JOS.attribute("direction", "in,out");
     break;
   }

diff  --git a/clang/lib/Index/CommentToXML.cpp b/clang/lib/Index/CommentToXML.cpp
index e7f5bfebec2334c..295f3f228ff79bd 100644
--- a/clang/lib/Index/CommentToXML.cpp
+++ b/clang/lib/Index/CommentToXML.cpp
@@ -751,13 +751,13 @@ void CommentASTToXMLConverter::visitParamCommandComment(
 
   Result << "<Direction isExplicit=\"" << C->isDirectionExplicit() << "\">";
   switch (C->getDirection()) {
-  case ParamCommandComment::In:
+  case ParamCommandPassDirection::In:
     Result << "in";
     break;
-  case ParamCommandComment::Out:
+  case ParamCommandPassDirection::Out:
     Result << "out";
     break;
-  case ParamCommandComment::InOut:
+  case ParamCommandPassDirection::InOut:
     Result << "in,out";
     break;
   }

diff  --git a/clang/tools/libclang/CXComment.cpp b/clang/tools/libclang/CXComment.cpp
index 5abbf9c1161a1b7..e87efd23578f022 100644
--- a/clang/tools/libclang/CXComment.cpp
+++ b/clang/tools/libclang/CXComment.cpp
@@ -296,13 +296,13 @@ enum CXCommentParamPassDirection clang_ParamCommandComment_getDirection(
     return CXCommentParamPassDirection_In;
 
   switch (PCC->getDirection()) {
-  case ParamCommandComment::In:
+  case ParamCommandPassDirection::In:
     return CXCommentParamPassDirection_In;
 
-  case ParamCommandComment::Out:
+  case ParamCommandPassDirection::Out:
     return CXCommentParamPassDirection_Out;
 
-  case ParamCommandComment::InOut:
+  case ParamCommandPassDirection::InOut:
     return CXCommentParamPassDirection_InOut;
   }
   llvm_unreachable("unknown ParamCommandComment::PassDirection");

diff  --git a/clang/unittests/AST/CommentParser.cpp b/clang/unittests/AST/CommentParser.cpp
index ab2157dcb71b097..1368f56d0f8eddc 100644
--- a/clang/unittests/AST/CommentParser.cpp
+++ b/clang/unittests/AST/CommentParser.cpp
@@ -176,16 +176,11 @@ ::testing::AssertionResult HasBlockCommandAt(const Comment *C,
   return ::testing::AssertionSuccess();
 }
 
-::testing::AssertionResult HasParamCommandAt(
-                              const Comment *C,
-                              const CommandTraits &Traits,
-                              size_t Idx,
-                              ParamCommandComment *&PCC,
-                              StringRef CommandName,
-                              ParamCommandComment::PassDirection Direction,
-                              bool IsDirectionExplicit,
-                              StringRef ParamName,
-                              ParagraphComment *&Paragraph) {
+::testing::AssertionResult
+HasParamCommandAt(const Comment *C, const CommandTraits &Traits, size_t Idx,
+                  ParamCommandComment *&PCC, StringRef CommandName,
+                  ParamCommandPassDirection Direction, bool IsDirectionExplicit,
+                  StringRef ParamName, ParagraphComment *&Paragraph) {
   ::testing::AssertionResult AR = GetChildAt(C, Idx, PCC);
   if (!AR)
     return AR;
@@ -756,10 +751,9 @@ TEST_F(CommentParserTest, ParamCommand1) {
   {
     ParamCommandComment *PCC;
     ParagraphComment *PC;
-    ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                  ParamCommandComment::In,
-                                  /* IsDirectionExplicit = */ false,
-                                  "aaa", PC));
+    ASSERT_TRUE(HasParamCommandAt(
+        FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
+        /* IsDirectionExplicit = */ false, "aaa", PC));
     ASSERT_TRUE(HasChildCount(PCC, 1));
     ASSERT_TRUE(HasChildCount(PC, 0));
   }
@@ -776,9 +770,8 @@ TEST_F(CommentParserTest, ParamCommand2) {
     ParamCommandComment *PCC;
     ParagraphComment *PC;
     ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                  ParamCommandComment::In,
-                                  /* IsDirectionExplicit = */ false,
-                                  "", PC));
+                                  ParamCommandPassDirection::In,
+                                  /* IsDirectionExplicit = */ false, "", PC));
     ASSERT_TRUE(HasChildCount(PCC, 1));
     ASSERT_TRUE(HasChildCount(PC, 0));
   }
@@ -809,10 +802,9 @@ TEST_F(CommentParserTest, ParamCommand3) {
     {
       ParamCommandComment *PCC;
       ParagraphComment *PC;
-      ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                    ParamCommandComment::In,
-                                    /* IsDirectionExplicit = */ false,
-                                    "aaa", PC));
+      ASSERT_TRUE(HasParamCommandAt(
+          FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
+          /* IsDirectionExplicit = */ false, "aaa", PC));
       ASSERT_TRUE(HasChildCount(PCC, 1));
       ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
     }
@@ -839,10 +831,9 @@ TEST_F(CommentParserTest, ParamCommand4) {
     {
       ParamCommandComment *PCC;
       ParagraphComment *PC;
-      ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                    ParamCommandComment::In,
-                                    /* IsDirectionExplicit = */ true,
-                                    "aaa", PC));
+      ASSERT_TRUE(HasParamCommandAt(
+          FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
+          /* IsDirectionExplicit = */ true, "aaa", PC));
       ASSERT_TRUE(HasChildCount(PCC, 1));
       ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
     }
@@ -869,10 +860,9 @@ TEST_F(CommentParserTest, ParamCommand5) {
     {
       ParamCommandComment *PCC;
       ParagraphComment *PC;
-      ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                    ParamCommandComment::Out,
-                                    /* IsDirectionExplicit = */ true,
-                                    "aaa", PC));
+      ASSERT_TRUE(HasParamCommandAt(
+          FC, Traits, 1, PCC, "param", ParamCommandPassDirection::Out,
+          /* IsDirectionExplicit = */ true, "aaa", PC));
       ASSERT_TRUE(HasChildCount(PCC, 1));
       ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
     }
@@ -900,10 +890,9 @@ TEST_F(CommentParserTest, ParamCommand6) {
     {
       ParamCommandComment *PCC;
       ParagraphComment *PC;
-      ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                    ParamCommandComment::InOut,
-                                    /* IsDirectionExplicit = */ true,
-                                    "aaa", PC));
+      ASSERT_TRUE(HasParamCommandAt(
+          FC, Traits, 1, PCC, "param", ParamCommandPassDirection::InOut,
+          /* IsDirectionExplicit = */ true, "aaa", PC));
       ASSERT_TRUE(HasChildCount(PCC, 1));
       ASSERT_TRUE(HasParagraphCommentAt(PCC, 0, " Bbb"));
     }
@@ -921,10 +910,9 @@ TEST_F(CommentParserTest, ParamCommand7) {
   {
     ParamCommandComment *PCC;
     ParagraphComment *PC;
-    ASSERT_TRUE(HasParamCommandAt(FC, Traits, 1, PCC, "param",
-                                  ParamCommandComment::In,
-                                  /* IsDirectionExplicit = */ false,
-                                  "aaa", PC));
+    ASSERT_TRUE(HasParamCommandAt(
+        FC, Traits, 1, PCC, "param", ParamCommandPassDirection::In,
+        /* IsDirectionExplicit = */ false, "aaa", PC));
     ASSERT_TRUE(HasChildCount(PCC, 1));
 
     ASSERT_TRUE(HasChildCount(PC, 5));


        


More information about the cfe-commits mailing list