10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Basic/TokenKinds.h"
15#include "clang/Lex/Lexer.h"
16#include "llvm/ADT/StringExtras.h"
26 NamespaceCommentPattern(
27 "^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
28 "namespace( +(((inline )|([a-zA-Z0-9_:]))+))?\\.? *(\\*/)?$",
29 llvm::Regex::IgnoreCase),
30 ShortNamespaceLines(Options.get(
"ShortNamespaceLines", 1U)),
31 SpacesBeforeComments(Options.get(
"SpacesBeforeComments", 1U)) {}
34 Options.
store(Opts,
"ShortNamespaceLines", ShortNamespaceLines);
35 Options.
store(Opts,
"SpacesBeforeComments", SpacesBeforeComments);
39 Finder->addMatcher(namespaceDecl().bind(
"namespace"),
this);
43 SourceLocation Loc1, SourceLocation Loc2) {
44 return Loc1.isFileID() && Loc2.isFileID() &&
45 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
48static std::optional<std::string>
50 const LangOptions &LangOpts) {
59 Loc, Sources, LangOpts)) {
60 Loc = T->getLocation();
61 if (T->is(tok::l_brace))
64 if (T->isOneOf(tok::l_square, tok::l_paren)) {
66 }
else if (T->isOneOf(tok::r_square, tok::r_paren)) {
69 if (T->is(tok::raw_identifier)) {
70 StringRef
ID = T->getRawIdentifier();
71 if (
ID !=
"namespace")
72 Result.append(std::string(
ID));
75 }
else if (T->is(tok::coloncolon)) {
86 const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>(
"namespace");
87 const SourceManager &Sources = *Result.SourceManager;
90 if (ND->getBeginLoc().isMacroID() ||
96 unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
97 unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
98 if (EndLine - StartLine + 1 <= ShortNamespaceLines)
102 SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
104 SourceLocation
Loc = AfterRBrace;
105 SourceLocation LBraceLoc = ND->getBeginLoc();
110 for (
const auto &EndOfNameLocation : Ends) {
111 if (Sources.isBeforeInTranslationUnit(ND->getLocation(), EndOfNameLocation))
115 std::optional<std::string> NamespaceNameAsWritten =
117 if (!NamespaceNameAsWritten)
120 if (NamespaceNameAsWritten->empty() != ND->isAnonymousNamespace()) {
125 Ends.push_back(LBraceLoc);
131 Loc =
Loc.getLocWithOffset(1);
137 bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(
Loc) == EndLine;
140 bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
142 SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
143 std::string Message =
"%0 not terminated with a closing comment";
146 if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
147 StringRef Comment(Sources.getCharacterData(
Loc), Tok.getLength());
148 SmallVector<StringRef, 7> Groups;
149 if (NamespaceCommentPattern.match(Comment, &Groups)) {
150 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] :
"";
151 StringRef Anonymous = Groups.size() > 3 ? Groups[3] :
"";
153 if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
154 (*NamespaceNameAsWritten == NamespaceNameInComment &&
155 Anonymous.empty())) {
163 NeedLineBreak = Comment.starts_with(
"/*");
165 SourceRange(AfterRBrace,
Loc.getLocWithOffset(Tok.getLength()));
168 "%0 ends with a comment that refers to a wrong namespace '") +
169 NamespaceNameInComment +
"'")
171 }
else if (Comment.starts_with(
"//")) {
174 NeedLineBreak =
false;
176 SourceRange(AfterRBrace,
Loc.getLocWithOffset(Tok.getLength()));
177 Message =
"%0 ends with an unrecognized comment";
183 std::string NamespaceNameForDiag =
184 ND->isAnonymousNamespace() ?
"anonymous namespace"
185 : (
"namespace '" + *NamespaceNameAsWritten +
"'");
187 std::string
Fix(SpacesBeforeComments,
' ');
188 Fix.append(
"// namespace");
189 if (!ND->isAnonymousNamespace())
190 Fix.append(
" ").append(*NamespaceNameAsWritten);
195 SourceLocation DiagLoc =
196 OldCommentRange.getBegin() != OldCommentRange.getEnd()
197 ? OldCommentRange.getBegin()
198 : ND->getRBraceLoc();
200 diag(DiagLoc, Message) << NamespaceNameForDiag
201 << FixItHint::CreateReplacement(
202 CharSourceRange::getCharRange(OldCommentRange),
204 diag(ND->getLocation(),
"%0 starts here", DiagnosticIDs::Note)
205 << NamespaceNameForDiag;
llvm::SmallString< 256U > Name
static cl::opt< bool > Fix("fix", desc(R"(
Apply suggested fixes. Without -fix-errors
clang-tidy will bail out if any compilation
errors were found.
)"), cl::init(false), cl::cat(ClangTidyCategory))
const unsigned short Nesting
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
static std::optional< std::string > getNamespaceNameAsWritten(SourceLocation &Loc, const SourceManager &Sources, const LangOptions &LangOpts)
static bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, SourceLocation Loc2)
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Some operations such as code completion produce a set of candidates.
llvm::StringMap< ClangTidyValue > OptionMap