diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:50:49 +0000 | 
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:50:49 +0000 | 
| commit | 2298981669bf3bd63335a4be179bc0f96823a8f4 (patch) | |
| tree | 1cbe2eb27f030d2d70b80ee5ca3c86bee7326a9f /lib/Format/NamespaceEndCommentsFixer.cpp | |
| parent | 9a83721404652cea39e9f02ae3e3b5c964602a5c (diff) | |
Notes
Diffstat (limited to 'lib/Format/NamespaceEndCommentsFixer.cpp')
| -rw-r--r-- | lib/Format/NamespaceEndCommentsFixer.cpp | 98 | 
1 files changed, 70 insertions, 28 deletions
| diff --git a/lib/Format/NamespaceEndCommentsFixer.cpp b/lib/Format/NamespaceEndCommentsFixer.cpp index dd364866d1ce..d04fc8f115fb 100644 --- a/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/lib/Format/NamespaceEndCommentsFixer.cpp @@ -1,9 +1,8 @@  //===--- NamespaceEndCommentsFixer.cpp --------------------------*- C++ -*-===//  // -//                     The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception  //  //===----------------------------------------------------------------------===//  /// @@ -30,24 +29,41 @@ static const int kShortNamespaceMaxLines = 1;  // Computes the name of a namespace given the namespace token.  // Returns "" for anonymous namespace.  std::string computeName(const FormatToken *NamespaceTok) { -  assert(NamespaceTok && NamespaceTok->is(tok::kw_namespace) && +  assert(NamespaceTok && +         NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&           "expecting a namespace token");    std::string name = ""; -  // Collects all the non-comment tokens between 'namespace' and '{'.    const FormatToken *Tok = NamespaceTok->getNextNonComment(); -  while (Tok && !Tok->is(tok::l_brace)) { -    name += Tok->TokenText; +  if (NamespaceTok->is(TT_NamespaceMacro)) { +    // Collects all the non-comment tokens between opening parenthesis +    // and closing parenthesis or comma +    assert(Tok && Tok->is(tok::l_paren) && "expected an opening parenthesis");      Tok = Tok->getNextNonComment(); +    while (Tok && !Tok->isOneOf(tok::r_paren, tok::comma)) { +      name += Tok->TokenText; +      Tok = Tok->getNextNonComment(); +    } +  } else { +    // Collects all the non-comment tokens between 'namespace' and '{'. +    while (Tok && !Tok->is(tok::l_brace)) { +      name += Tok->TokenText; +      Tok = Tok->getNextNonComment(); +    }    }    return name;  } -std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline) { -  std::string text = "// namespace"; -  if (!NamespaceName.empty()) { +std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline, +                                  const FormatToken *NamespaceTok) { +  std::string text = "// "; +  text += NamespaceTok->TokenText; +  if (NamespaceTok->is(TT_NamespaceMacro)) +    text += "("; +  else if (!NamespaceName.empty())      text += ' '; -    text += NamespaceName; -  } +  text += NamespaceName; +  if (NamespaceTok->is(TT_NamespaceMacro)) +    text += ")";    if (AddNewline)      text += '\n';    return text; @@ -57,7 +73,8 @@ bool hasEndComment(const FormatToken *RBraceTok) {    return RBraceTok->Next && RBraceTok->Next->is(tok::comment);  } -bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) { +bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName, +                     const FormatToken *NamespaceTok) {    assert(hasEndComment(RBraceTok));    const FormatToken *Comment = RBraceTok->Next; @@ -67,19 +84,32 @@ bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) {        new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"                        "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",                        llvm::Regex::IgnoreCase); -  SmallVector<StringRef, 7> Groups; -  if (NamespaceCommentPattern->match(Comment->TokenText, &Groups)) { -    StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : ""; -    // Anonymous namespace comments must not mention a namespace name. -    if (NamespaceName.empty() && !NamespaceNameInComment.empty()) -      return false; -    StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : ""; -    // Named namespace comments must not mention anonymous namespace. -    if (!NamespaceName.empty() && !AnonymousInComment.empty()) +  static llvm::Regex *const NamespaceMacroCommentPattern = +      new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" +                      "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$", +                      llvm::Regex::IgnoreCase); + +  SmallVector<StringRef, 8> Groups; +  if (NamespaceTok->is(TT_NamespaceMacro) && +      NamespaceMacroCommentPattern->match(Comment->TokenText, &Groups)) { +    StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] : ""; +    // The name of the macro must be used. +    if (NamespaceTokenText != NamespaceTok->TokenText)        return false; -    return NamespaceNameInComment == NamespaceName; +  } else if (NamespaceTok->isNot(tok::kw_namespace) || +             !NamespaceCommentPattern->match(Comment->TokenText, &Groups)) { +    // Comment does not match regex. +    return false;    } -  return false; +  StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : ""; +  // Anonymous namespace comments must not mention a namespace name. +  if (NamespaceName.empty() && !NamespaceNameInComment.empty()) +    return false; +  StringRef AnonymousInComment = Groups.size() > 3 ? Groups[3] : ""; +  // Named namespace comments must not mention anonymous namespace. +  if (!NamespaceName.empty() && !AnonymousInComment.empty()) +    return false; +  return NamespaceNameInComment == NamespaceName;  }  void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText, @@ -128,6 +158,13 @@ getNamespaceToken(const AnnotatedLine *Line,    return NamespaceTok->getNamespaceToken();  } +StringRef +getNamespaceTokenText(const AnnotatedLine *Line, +                      const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { +  const FormatToken *NamespaceTok = getNamespaceToken(Line, AnnotatedLines); +  return NamespaceTok ? NamespaceTok->TokenText : StringRef(); +} +  NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,                                                       const FormatStyle &Style)      : TokenAnalyzer(Env, Style) {} @@ -140,6 +177,7 @@ std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(    tooling::Replacements Fixes;    std::string AllNamespaceNames = "";    size_t StartLineIndex = SIZE_MAX; +  StringRef NamespaceTokenText;    unsigned int CompactedNamespacesCount = 0;    for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {      const AnnotatedLine *EndLine = AnnotatedLines[I]; @@ -161,8 +199,11 @@ std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(        StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;      std::string NamespaceName = computeName(NamespaceTok);      if (Style.CompactNamespaces) { +      if (CompactedNamespacesCount == 0) +        NamespaceTokenText = NamespaceTok->TokenText;        if ((I + 1 < E) && -          getNamespaceToken(AnnotatedLines[I + 1], AnnotatedLines) && +          NamespaceTokenText == +              getNamespaceTokenText(AnnotatedLines[I + 1], AnnotatedLines) &&            StartLineIndex - CompactedNamespacesCount - 1 ==                AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&            !AnnotatedLines[I + 1]->First->Finalized) { @@ -190,12 +231,13 @@ std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(                        EndCommentNextTok->NewlinesBefore == 0 &&                        EndCommentNextTok->isNot(tok::eof);      const std::string EndCommentText = -        computeEndCommentText(NamespaceName, AddNewline); +        computeEndCommentText(NamespaceName, AddNewline, NamespaceTok);      if (!hasEndComment(EndCommentPrevTok)) {        bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;        if (!isShort)          addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes); -    } else if (!validEndComment(EndCommentPrevTok, NamespaceName)) { +    } else if (!validEndComment(EndCommentPrevTok, NamespaceName, +                                NamespaceTok)) {        updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);      }      StartLineIndex = SIZE_MAX; | 
