2017-02-27 21:28:36 +08:00
|
|
|
//===--- NamespaceEndCommentsFixer.cpp --------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-02-27 21:28:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This file implements NamespaceEndCommentsFixer, a TokenAnalyzer that
|
2017-02-27 21:28:36 +08:00
|
|
|
/// fixes namespace end comments.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NamespaceEndCommentsFixer.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Regex.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "namespace-end-comments-fixer"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
namespace {
|
2017-03-02 17:54:44 +08:00
|
|
|
// The maximal number of unwrapped lines that a short namespace spans.
|
2017-02-27 21:28:36 +08:00
|
|
|
// Short namespaces don't need an end comment.
|
|
|
|
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) &&
|
|
|
|
"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;
|
|
|
|
Tok = Tok->getNextNonComment();
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string computeEndCommentText(StringRef NamespaceName, bool AddNewline) {
|
|
|
|
std::string text = "// namespace";
|
|
|
|
if (!NamespaceName.empty()) {
|
|
|
|
text += ' ';
|
|
|
|
text += NamespaceName;
|
|
|
|
}
|
|
|
|
if (AddNewline)
|
|
|
|
text += '\n';
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasEndComment(const FormatToken *RBraceTok) {
|
|
|
|
return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName) {
|
|
|
|
assert(hasEndComment(RBraceTok));
|
|
|
|
const FormatToken *Comment = RBraceTok->Next;
|
2018-03-21 04:43:12 +08:00
|
|
|
|
|
|
|
// Matches a valid namespace end comment.
|
|
|
|
// Valid namespace end comments don't need to be edited.
|
|
|
|
static llvm::Regex *const NamespaceCommentPattern =
|
|
|
|
new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
|
|
|
|
"namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
|
|
|
|
llvm::Regex::IgnoreCase);
|
2017-02-27 21:28:36 +08:00
|
|
|
SmallVector<StringRef, 7> Groups;
|
2018-03-21 04:43:12 +08:00
|
|
|
if (NamespaceCommentPattern->match(Comment->TokenText, &Groups)) {
|
2017-02-27 21:28:36 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
|
|
|
|
const SourceManager &SourceMgr,
|
|
|
|
tooling::Replacements *Fixes) {
|
|
|
|
auto EndLoc = RBraceTok->Tok.getEndLoc();
|
|
|
|
auto Range = CharSourceRange::getCharRange(EndLoc, EndLoc);
|
|
|
|
auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
|
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << "Error while adding namespace end comment: "
|
|
|
|
<< llvm::toString(std::move(Err)) << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateEndComment(const FormatToken *RBraceTok, StringRef EndCommentText,
|
|
|
|
const SourceManager &SourceMgr,
|
|
|
|
tooling::Replacements *Fixes) {
|
|
|
|
assert(hasEndComment(RBraceTok));
|
|
|
|
const FormatToken *Comment = RBraceTok->Next;
|
|
|
|
auto Range = CharSourceRange::getCharRange(Comment->getStartOfNonWhitespace(),
|
|
|
|
Comment->Tok.getEndLoc());
|
|
|
|
auto Err = Fixes->add(tooling::Replacement(SourceMgr, Range, EndCommentText));
|
|
|
|
if (Err) {
|
|
|
|
llvm::errs() << "Error while updating namespace end comment: "
|
|
|
|
<< llvm::toString(std::move(Err)) << "\n";
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 21:02:15 +08:00
|
|
|
} // namespace
|
2017-06-14 20:29:47 +08:00
|
|
|
|
|
|
|
const FormatToken *
|
2018-04-23 18:02:59 +08:00
|
|
|
getNamespaceToken(const AnnotatedLine *Line,
|
2017-06-14 20:29:47 +08:00
|
|
|
const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
|
2018-04-23 18:02:59 +08:00
|
|
|
if (!Line->Affected || Line->InPPDirective || !Line->startsWith(tok::r_brace))
|
2017-06-14 20:29:47 +08:00
|
|
|
return nullptr;
|
2018-04-23 18:02:59 +08:00
|
|
|
size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
|
2017-06-14 20:29:47 +08:00
|
|
|
if (StartLineIndex == UnwrappedLine::kInvalidIndex)
|
|
|
|
return nullptr;
|
|
|
|
assert(StartLineIndex < AnnotatedLines.size());
|
|
|
|
const FormatToken *NamespaceTok = AnnotatedLines[StartLineIndex]->First;
|
2017-09-27 15:51:51 +08:00
|
|
|
if (NamespaceTok->is(tok::l_brace)) {
|
|
|
|
// "namespace" keyword can be on the line preceding '{', e.g. in styles
|
|
|
|
// where BraceWrapping.AfterNamespace is true.
|
|
|
|
if (StartLineIndex > 0)
|
|
|
|
NamespaceTok = AnnotatedLines[StartLineIndex - 1]->First;
|
|
|
|
}
|
2018-09-05 15:44:02 +08:00
|
|
|
return NamespaceTok->getNamespaceToken();
|
2017-06-14 20:29:47 +08:00
|
|
|
}
|
2017-02-27 21:28:36 +08:00
|
|
|
|
|
|
|
NamespaceEndCommentsFixer::NamespaceEndCommentsFixer(const Environment &Env,
|
|
|
|
const FormatStyle &Style)
|
|
|
|
: TokenAnalyzer(Env, Style) {}
|
|
|
|
|
2017-10-30 22:01:50 +08:00
|
|
|
std::pair<tooling::Replacements, unsigned> NamespaceEndCommentsFixer::analyze(
|
2017-02-27 21:28:36 +08:00
|
|
|
TokenAnnotator &Annotator, SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
|
|
|
|
FormatTokenLexer &Tokens) {
|
|
|
|
const SourceManager &SourceMgr = Env.getSourceManager();
|
2018-04-23 17:34:26 +08:00
|
|
|
AffectedRangeMgr.computeAffectedLines(AnnotatedLines);
|
2017-02-27 21:28:36 +08:00
|
|
|
tooling::Replacements Fixes;
|
2017-06-14 20:29:47 +08:00
|
|
|
std::string AllNamespaceNames = "";
|
|
|
|
size_t StartLineIndex = SIZE_MAX;
|
|
|
|
unsigned int CompactedNamespacesCount = 0;
|
2017-02-27 21:28:36 +08:00
|
|
|
for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
|
|
|
|
const AnnotatedLine *EndLine = AnnotatedLines[I];
|
2017-06-14 20:29:47 +08:00
|
|
|
const FormatToken *NamespaceTok =
|
|
|
|
getNamespaceToken(EndLine, AnnotatedLines);
|
|
|
|
if (!NamespaceTok)
|
2017-02-27 21:28:36 +08:00
|
|
|
continue;
|
2017-03-07 00:44:45 +08:00
|
|
|
FormatToken *RBraceTok = EndLine->First;
|
|
|
|
if (RBraceTok->Finalized)
|
|
|
|
continue;
|
|
|
|
RBraceTok->Finalized = true;
|
2017-03-07 22:07:43 +08:00
|
|
|
const FormatToken *EndCommentPrevTok = RBraceTok;
|
|
|
|
// Namespaces often end with '};'. In that case, attach namespace end
|
|
|
|
// comments to the semicolon tokens.
|
|
|
|
if (RBraceTok->Next && RBraceTok->Next->is(tok::semi)) {
|
|
|
|
EndCommentPrevTok = RBraceTok->Next;
|
|
|
|
}
|
2017-06-14 20:29:47 +08:00
|
|
|
if (StartLineIndex == SIZE_MAX)
|
|
|
|
StartLineIndex = EndLine->MatchingOpeningBlockLineIndex;
|
|
|
|
std::string NamespaceName = computeName(NamespaceTok);
|
|
|
|
if (Style.CompactNamespaces) {
|
|
|
|
if ((I + 1 < E) &&
|
|
|
|
getNamespaceToken(AnnotatedLines[I + 1], AnnotatedLines) &&
|
|
|
|
StartLineIndex - CompactedNamespacesCount - 1 ==
|
|
|
|
AnnotatedLines[I + 1]->MatchingOpeningBlockLineIndex &&
|
|
|
|
!AnnotatedLines[I + 1]->First->Finalized) {
|
|
|
|
if (hasEndComment(EndCommentPrevTok)) {
|
|
|
|
// remove end comment, it will be merged in next one
|
|
|
|
updateEndComment(EndCommentPrevTok, std::string(), SourceMgr, &Fixes);
|
|
|
|
}
|
|
|
|
CompactedNamespacesCount++;
|
|
|
|
AllNamespaceNames = "::" + NamespaceName + AllNamespaceNames;
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-27 22:07:45 +08:00
|
|
|
NamespaceName += AllNamespaceNames;
|
2017-06-14 20:29:47 +08:00
|
|
|
CompactedNamespacesCount = 0;
|
|
|
|
AllNamespaceNames = std::string();
|
|
|
|
}
|
2017-03-07 22:07:43 +08:00
|
|
|
// The next token in the token stream after the place where the end comment
|
|
|
|
// token must be. This is either the next token on the current line or the
|
|
|
|
// first token on the next line.
|
|
|
|
const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;
|
|
|
|
if (EndCommentNextTok && EndCommentNextTok->is(tok::comment))
|
|
|
|
EndCommentNextTok = EndCommentNextTok->Next;
|
|
|
|
if (!EndCommentNextTok && I + 1 < E)
|
|
|
|
EndCommentNextTok = AnnotatedLines[I + 1]->First;
|
|
|
|
bool AddNewline = EndCommentNextTok &&
|
|
|
|
EndCommentNextTok->NewlinesBefore == 0 &&
|
|
|
|
EndCommentNextTok->isNot(tok::eof);
|
2017-02-27 21:28:36 +08:00
|
|
|
const std::string EndCommentText =
|
|
|
|
computeEndCommentText(NamespaceName, AddNewline);
|
2017-03-07 22:07:43 +08:00
|
|
|
if (!hasEndComment(EndCommentPrevTok)) {
|
2017-03-02 17:54:44 +08:00
|
|
|
bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
|
|
|
|
if (!isShort)
|
2017-03-07 22:07:43 +08:00
|
|
|
addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
|
2017-06-14 20:29:47 +08:00
|
|
|
} else if (!validEndComment(EndCommentPrevTok, NamespaceName)) {
|
2017-03-07 22:07:43 +08:00
|
|
|
updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
|
2017-06-14 20:29:47 +08:00
|
|
|
}
|
|
|
|
StartLineIndex = SIZE_MAX;
|
2017-02-27 21:28:36 +08:00
|
|
|
}
|
2017-10-30 22:01:50 +08:00
|
|
|
return {Fixes, 0};
|
2017-02-27 21:28:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|