Commit f2d8a0ac authored by Vlad Serebrennikov's avatar Vlad Serebrennikov
Browse files

[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`.
parent 3de645ef
Loading
Loading
Loading
Loading
+10 −12
Original line number Diff line number Diff line
@@ -675,6 +675,8 @@ public:
  }
};

enum class ParamCommandPassDirection { In, Out, InOut };

/// Doxygen \\param command.
class ParamCommandComment : public BlockCommandComment {
private:
@@ -692,7 +694,8 @@ public:
      : 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 @@ public:
    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;
  }

+5 −4
Original line number Diff line number Diff line
@@ -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");
+15 −14
Original line number Diff line number Diff line
@@ -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};
+3 −3
Original line number Diff line number Diff line
@@ -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;
  }
+3 −3
Original line number Diff line number Diff line
@@ -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;
  }
Loading