2014-05-20 00:39:08 +08:00
|
|
|
//===--- NamespaceCommentCheck.cpp - clang-tidy ---------------------------===//
|
|
|
|
//
|
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
|
2014-05-20 00:39:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NamespaceCommentCheck.h"
|
2019-12-05 19:11:06 +08:00
|
|
|
#include "../utils/LexerUtils.h"
|
2014-05-20 00:39:08 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
2019-07-17 23:22:14 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2019-12-05 19:11:06 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2014-05-20 00:39:08 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-09-12 16:53:36 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-05-20 00:39:08 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2014-09-22 18:41:39 +08:00
|
|
|
namespace readability {
|
2014-05-20 00:39:08 +08:00
|
|
|
|
2014-09-12 16:53:36 +08:00
|
|
|
NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
|
|
|
|
ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
|
|
|
NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
|
2018-01-11 21:00:28 +08:00
|
|
|
"namespace( +([a-zA-Z0-9_:]+))?\\.? *(\\*/)?$",
|
2014-05-20 00:39:08 +08:00
|
|
|
llvm::Regex::IgnoreCase),
|
2014-09-12 16:53:36 +08:00
|
|
|
ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)),
|
2014-10-16 19:27:57 +08:00
|
|
|
SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {}
|
2014-09-12 16:53:36 +08:00
|
|
|
|
|
|
|
void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
|
|
|
|
Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
|
|
|
|
}
|
2014-05-20 00:39:08 +08:00
|
|
|
|
|
|
|
void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
|
[clang-tidy] Change checks to use new isLanguageVersionSupported restriction
Summary: Modifies all checks that are language version dependent to use `isLanguageVersionSupported`
Reviewers: jdoerfert, lebedev.ri, aaron.ballman, gribozavr2, Eugene.Zelenko
Reviewed By: gribozavr2
Subscribers: wuzish, nemanjai, xazax.hun, hiraditya, kbarton, steven_wu, dexonsmith, arphaman, lebedev.ri, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75340
2020-03-04 00:39:32 +08:00
|
|
|
Finder->addMatcher(namespaceDecl().bind("namespace"), this);
|
2014-05-20 00:39:08 +08:00
|
|
|
}
|
|
|
|
|
2015-03-23 20:49:15 +08:00
|
|
|
static bool locationsInSameFile(const SourceManager &Sources,
|
|
|
|
SourceLocation Loc1, SourceLocation Loc2) {
|
2014-05-20 00:39:08 +08:00
|
|
|
return Loc1.isFileID() && Loc2.isFileID() &&
|
|
|
|
Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
static llvm::Optional<std::string>
|
|
|
|
getNamespaceNameAsWritten(SourceLocation &Loc, const SourceManager &Sources,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
// Loc should be at the begin of the namespace decl (usually, `namespace`
|
|
|
|
// token). We skip the first token right away, but in case of `inline
|
|
|
|
// namespace` or `namespace a::inline b` we can see both `inline` and
|
|
|
|
// `namespace` keywords, which we just ignore. Nested parens/squares before
|
|
|
|
// the opening brace can result from attributes.
|
|
|
|
std::string Result;
|
|
|
|
int Nesting = 0;
|
|
|
|
while (llvm::Optional<Token> T = utils::lexer::findNextTokenSkippingComments(
|
|
|
|
Loc, Sources, LangOpts)) {
|
|
|
|
Loc = T->getLocation();
|
|
|
|
if (T->is(tok::l_brace))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (T->isOneOf(tok::l_square, tok::l_paren)) {
|
|
|
|
++Nesting;
|
|
|
|
} else if (T->isOneOf(tok::r_square, tok::r_paren)) {
|
|
|
|
--Nesting;
|
|
|
|
} else if (Nesting == 0) {
|
|
|
|
if (T->is(tok::raw_identifier)) {
|
|
|
|
StringRef ID = T->getRawIdentifier();
|
|
|
|
if (ID != "namespace" && ID != "inline")
|
2020-01-29 03:23:46 +08:00
|
|
|
Result.append(std::string(ID));
|
2019-12-05 19:11:06 +08:00
|
|
|
} else if (T->is(tok::coloncolon)) {
|
|
|
|
Result.append("::");
|
|
|
|
} else { // Any other kind of token is unexpected here.
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
2018-01-11 21:00:28 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 00:39:08 +08:00
|
|
|
void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-12-14 23:29:23 +08:00
|
|
|
const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace");
|
2014-05-20 00:39:08 +08:00
|
|
|
const SourceManager &Sources = *Result.SourceManager;
|
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
// Ignore namespaces inside macros and namespaces split across files.
|
|
|
|
if (ND->getBeginLoc().isMacroID() ||
|
|
|
|
!locationsInSameFile(Sources, ND->getBeginLoc(), ND->getRBraceLoc()))
|
2014-05-20 00:39:08 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't require closing comments for namespaces spanning less than certain
|
|
|
|
// number of lines.
|
2018-08-10 06:42:26 +08:00
|
|
|
unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
|
2014-05-20 00:39:08 +08:00
|
|
|
unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
|
|
|
|
if (EndLine - StartLine + 1 <= ShortNamespaceLines)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find next token after the namespace closing brace.
|
2019-12-05 19:11:06 +08:00
|
|
|
SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
|
|
|
|
ND->getRBraceLoc(), /*Offset=*/0, Sources, getLangOpts());
|
2014-05-20 00:39:08 +08:00
|
|
|
SourceLocation Loc = AfterRBrace;
|
2019-12-05 19:11:06 +08:00
|
|
|
SourceLocation LBraceLoc = ND->getBeginLoc();
|
2018-01-11 21:00:28 +08:00
|
|
|
|
2020-04-05 14:28:11 +08:00
|
|
|
// Currently for nested namespace (n1::n2::...) the AST matcher will match foo
|
2018-01-11 21:00:28 +08:00
|
|
|
// then bar instead of a single match. So if we got a nested namespace we have
|
|
|
|
// to skip the next ones.
|
|
|
|
for (const auto &EndOfNameLocation : Ends) {
|
2019-12-05 19:11:06 +08:00
|
|
|
if (Sources.isBeforeInTranslationUnit(ND->getLocation(), EndOfNameLocation))
|
2018-01-11 21:00:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
llvm::Optional<std::string> NamespaceNameAsWritten =
|
|
|
|
getNamespaceNameAsWritten(LBraceLoc, Sources, getLangOpts());
|
|
|
|
if (!NamespaceNameAsWritten)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (NamespaceNameAsWritten->empty() != ND->isAnonymousNamespace()) {
|
|
|
|
// Apparently, we didn't find the correct namespace name. Give up.
|
|
|
|
return;
|
2018-01-11 21:00:28 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
Ends.push_back(LBraceLoc);
|
2018-01-11 21:00:28 +08:00
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
Token Tok;
|
2014-05-20 00:39:08 +08:00
|
|
|
// Skip whitespace until we find the next token.
|
2016-09-24 10:13:45 +08:00
|
|
|
while (Lexer::getRawToken(Loc, Tok, Sources, getLangOpts()) ||
|
2015-12-16 23:44:42 +08:00
|
|
|
Tok.is(tok::semi)) {
|
2014-05-20 00:39:08 +08:00
|
|
|
Loc = Loc.getLocWithOffset(1);
|
|
|
|
}
|
2018-01-11 21:00:28 +08:00
|
|
|
|
2014-05-20 00:39:08 +08:00
|
|
|
if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
|
2014-05-20 01:46:28 +08:00
|
|
|
// If we insert a line comment before the token in the same line, we need
|
|
|
|
// to insert a line break.
|
2014-05-20 00:39:08 +08:00
|
|
|
bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
|
|
|
|
|
2015-03-05 22:56:11 +08:00
|
|
|
SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
|
2015-03-06 07:17:32 +08:00
|
|
|
std::string Message = "%0 not terminated with a closing comment";
|
2015-03-05 22:56:11 +08:00
|
|
|
|
2014-05-20 00:39:08 +08:00
|
|
|
// Try to find existing namespace closing comment on the same line.
|
|
|
|
if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
|
|
|
|
StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
|
2014-10-16 23:11:54 +08:00
|
|
|
SmallVector<StringRef, 7> Groups;
|
2014-05-20 00:39:08 +08:00
|
|
|
if (NamespaceCommentPattern.match(Comment, &Groups)) {
|
2014-10-16 23:11:54 +08:00
|
|
|
StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
|
|
|
|
StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
|
2014-05-20 00:39:08 +08:00
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
|
|
|
|
(*NamespaceNameAsWritten == NamespaceNameInComment &&
|
|
|
|
Anonymous.empty())) {
|
2018-01-11 21:00:28 +08:00
|
|
|
// Check if the namespace in the comment is the same.
|
2014-05-20 00:39:08 +08:00
|
|
|
// FIXME: Maybe we need a strict mode, where we always fix namespace
|
|
|
|
// comments with different format.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to fix the comment.
|
|
|
|
NeedLineBreak = Comment.startswith("/*");
|
2015-03-05 22:56:11 +08:00
|
|
|
OldCommentRange =
|
|
|
|
SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
|
2019-12-03 23:27:57 +08:00
|
|
|
Message =
|
|
|
|
(llvm::Twine(
|
|
|
|
"%0 ends with a comment that refers to a wrong namespace '") +
|
|
|
|
NamespaceNameInComment + "'")
|
|
|
|
.str();
|
2015-03-05 22:56:11 +08:00
|
|
|
} else if (Comment.startswith("//")) {
|
|
|
|
// Assume that this is an unrecognized form of a namespace closing line
|
|
|
|
// comment. Replace it.
|
2014-05-20 00:39:08 +08:00
|
|
|
NeedLineBreak = false;
|
2015-03-05 22:56:11 +08:00
|
|
|
OldCommentRange =
|
|
|
|
SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
|
|
|
|
Message = "%0 ends with an unrecognized comment";
|
|
|
|
}
|
|
|
|
// If it's a block comment, just move it to the next line, as it can be
|
|
|
|
// multi-line or there may be other tokens behind it.
|
2014-05-20 00:39:08 +08:00
|
|
|
}
|
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
std::string NamespaceNameForDiag =
|
|
|
|
ND->isAnonymousNamespace() ? "anonymous namespace"
|
|
|
|
: ("namespace '" + *NamespaceNameAsWritten + "'");
|
|
|
|
|
|
|
|
std::string Fix(SpacesBeforeComments, ' ');
|
|
|
|
Fix.append("// namespace");
|
|
|
|
if (!ND->isAnonymousNamespace())
|
|
|
|
Fix.append(" ").append(*NamespaceNameAsWritten);
|
|
|
|
if (NeedLineBreak)
|
|
|
|
Fix.append("\n");
|
2014-11-18 01:32:32 +08:00
|
|
|
|
2019-07-17 23:22:14 +08:00
|
|
|
// Place diagnostic at an old comment, or closing brace if we did not have it.
|
|
|
|
SourceLocation DiagLoc =
|
|
|
|
OldCommentRange.getBegin() != OldCommentRange.getEnd()
|
|
|
|
? OldCommentRange.getBegin()
|
|
|
|
: ND->getRBraceLoc();
|
|
|
|
|
2019-12-05 19:11:06 +08:00
|
|
|
diag(DiagLoc, Message) << NamespaceNameForDiag
|
|
|
|
<< FixItHint::CreateReplacement(
|
|
|
|
CharSourceRange::getCharRange(OldCommentRange),
|
|
|
|
Fix);
|
2014-11-18 01:32:32 +08:00
|
|
|
diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note)
|
2019-12-05 19:11:06 +08:00
|
|
|
<< NamespaceNameForDiag;
|
2014-05-20 00:39:08 +08:00
|
|
|
}
|
|
|
|
|
2014-09-22 18:41:39 +08:00
|
|
|
} // namespace readability
|
2014-05-20 00:39:08 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|