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"
22 namespace readability {
24 NamespaceCommentCheck::NamespaceCommentCheck(StringRef
Name,
27 NamespaceCommentPattern(
"^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
28 "namespace( +([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);
48 static llvm::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)) {
68 }
else if (Nesting == 0) {
69 if (T->is(tok::raw_identifier)) {
70 StringRef ID = T->getRawIdentifier();
71 if (ID !=
"namespace" && ID !=
"inline")
72 Result.append(std::string(ID));
73 }
else if (T->is(tok::coloncolon)) {
84 const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>(
"namespace");
85 const SourceManager &Sources = *Result.SourceManager;
88 if (ND->getBeginLoc().isMacroID() ||
94 unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
95 unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
96 if (EndLine - StartLine + 1 <= ShortNamespaceLines)
100 SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
102 SourceLocation
Loc = AfterRBrace;
103 SourceLocation LBraceLoc = ND->getBeginLoc();
108 for (
const auto &EndOfNameLocation : Ends) {
109 if (Sources.isBeforeInTranslationUnit(ND->getLocation(), EndOfNameLocation))
113 llvm::Optional<std::string> NamespaceNameAsWritten =
115 if (!NamespaceNameAsWritten)
118 if (NamespaceNameAsWritten->empty() != ND->isAnonymousNamespace()) {
123 Ends.push_back(LBraceLoc);
129 Loc =
Loc.getLocWithOffset(1);
135 bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(
Loc) == EndLine;
138 bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
140 SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
141 std::string
Message =
"%0 not terminated with a closing comment";
144 if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
145 StringRef Comment(Sources.getCharacterData(
Loc), Tok.getLength());
146 SmallVector<StringRef, 7> Groups;
147 if (NamespaceCommentPattern.match(Comment, &Groups)) {
148 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] :
"";
149 StringRef Anonymous = Groups.size() > 3 ? Groups[3] :
"";
151 if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
152 (*NamespaceNameAsWritten == NamespaceNameInComment &&
153 Anonymous.empty())) {
161 NeedLineBreak = Comment.startswith(
"/*");
163 SourceRange(AfterRBrace,
Loc.getLocWithOffset(Tok.getLength()));
166 "%0 ends with a comment that refers to a wrong namespace '") +
167 NamespaceNameInComment +
"'")
169 }
else if (Comment.startswith(
"//")) {
172 NeedLineBreak =
false;
174 SourceRange(AfterRBrace,
Loc.getLocWithOffset(Tok.getLength()));
175 Message =
"%0 ends with an unrecognized comment";
181 std::string NamespaceNameForDiag =
182 ND->isAnonymousNamespace() ?
"anonymous namespace"
183 : (
"namespace '" + *NamespaceNameAsWritten +
"'");
185 std::string
Fix(SpacesBeforeComments,
' ');
186 Fix.append(
"// namespace");
187 if (!ND->isAnonymousNamespace())
188 Fix.append(
" ").append(*NamespaceNameAsWritten);
193 SourceLocation DiagLoc =
194 OldCommentRange.getBegin() != OldCommentRange.getEnd()
195 ? OldCommentRange.getBegin()
196 : ND->getRBraceLoc();
199 << FixItHint::CreateReplacement(
200 CharSourceRange::getCharRange(OldCommentRange),
202 diag(ND->getLocation(),
"%0 starts here", DiagnosticIDs::Note)
203 << NamespaceNameForDiag;