2014-05-20 00:39:08 +08:00
|
|
|
//===--- NamespaceCommentCheck.cpp - clang-tidy ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NamespaceCommentCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#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)? *"
|
2014-10-16 23:11:54 +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) {
|
|
|
|
Finder->addMatcher(namespaceDecl().bind("namespace"), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1,
|
|
|
|
SourceLocation Loc2) {
|
|
|
|
return Loc1.isFileID() && Loc2.isFileID() &&
|
|
|
|
Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
|
|
|
|
}
|
|
|
|
|
2014-09-22 18:41:39 +08:00
|
|
|
std::string getNamespaceComment(const NamespaceDecl *ND, bool InsertLineBreak) {
|
2014-05-20 00:39:08 +08:00
|
|
|
std::string Fix = "// namespace";
|
|
|
|
if (!ND->isAnonymousNamespace())
|
2014-09-22 18:41:39 +08:00
|
|
|
Fix.append(" ").append(ND->getNameAsString());
|
2014-05-20 00:39:08 +08:00
|
|
|
if (InsertLineBreak)
|
|
|
|
Fix.append("\n");
|
|
|
|
return Fix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const NamespaceDecl *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace");
|
|
|
|
const SourceManager &Sources = *Result.SourceManager;
|
|
|
|
|
|
|
|
if (!locationsInSameFile(Sources, ND->getLocStart(), ND->getRBraceLoc()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't require closing comments for namespaces spanning less than certain
|
|
|
|
// number of lines.
|
|
|
|
unsigned StartLine = Sources.getSpellingLineNumber(ND->getLocStart());
|
|
|
|
unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
|
|
|
|
if (EndLine - StartLine + 1 <= ShortNamespaceLines)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find next token after the namespace closing brace.
|
|
|
|
SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1);
|
|
|
|
SourceLocation Loc = AfterRBrace;
|
|
|
|
Token Tok;
|
|
|
|
// Skip whitespace until we find the next token.
|
|
|
|
while (Lexer::getRawToken(Loc, Tok, Sources, Result.Context->getLangOpts())) {
|
|
|
|
Loc = Loc.getLocWithOffset(1);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Check if the namespace in the comment is the same.
|
|
|
|
if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
|
2014-10-16 23:11:54 +08:00
|
|
|
(ND->getNameAsString() == NamespaceNameInComment &&
|
|
|
|
Anonymous.empty())) {
|
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("/*");
|
|
|
|
CharSourceRange OldCommentRange = CharSourceRange::getCharRange(
|
|
|
|
SourceRange(Loc, Loc.getLocWithOffset(Tok.getLength())));
|
|
|
|
diag(Loc, "namespace closing comment refers to a wrong namespace '%0'")
|
|
|
|
<< NamespaceNameInComment
|
|
|
|
<< FixItHint::CreateReplacement(
|
2014-09-22 18:41:39 +08:00
|
|
|
OldCommentRange, getNamespaceComment(ND, NeedLineBreak));
|
2014-05-20 00:39:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not a recognized form of a namespace closing comment.
|
|
|
|
// Leave line comment on the same line. Move block comment to the next line,
|
|
|
|
// as it can be multi-line or there may be other tokens behind it.
|
|
|
|
if (Comment.startswith("//"))
|
|
|
|
NeedLineBreak = false;
|
|
|
|
}
|
|
|
|
|
2014-11-18 01:32:32 +08:00
|
|
|
std::string NamespaceName =
|
|
|
|
ND->isAnonymousNamespace()
|
|
|
|
? "anonymous namespace"
|
|
|
|
: ("namespace '" + ND->getNameAsString() + "'");
|
|
|
|
|
|
|
|
diag(AfterRBrace, "%0 not terminated with a closing comment")
|
|
|
|
<< NamespaceName
|
2014-09-22 18:41:39 +08:00
|
|
|
<< FixItHint::CreateInsertion(AfterRBrace,
|
|
|
|
std::string(SpacesBeforeComments, ' ') +
|
|
|
|
getNamespaceComment(ND, NeedLineBreak));
|
2014-11-18 01:32:32 +08:00
|
|
|
diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note)
|
|
|
|
<< NamespaceName;
|
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
|