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);
42 Finder->addMatcher(namespaceDecl().bind(
"namespace"),
this);
46 SourceLocation Loc1, SourceLocation Loc2) {
47 return Loc1.isFileID() && Loc2.isFileID() &&
48 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
51 static llvm::Optional<std::string>
53 const LangOptions &LangOpts) {
62 Loc, Sources, LangOpts)) {
63 Loc = T->getLocation();
64 if (T->is(tok::l_brace))
67 if (T->isOneOf(tok::l_square, tok::l_paren)) {
69 }
else if (T->isOneOf(tok::r_square, tok::r_paren)) {
71 }
else if (Nesting == 0) {
72 if (T->is(tok::raw_identifier)) {
73 StringRef ID = T->getRawIdentifier();
74 if (ID !=
"namespace" && ID !=
"inline")
76 }
else if (T->is(tok::coloncolon)) {
87 const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>(
"namespace");
88 const SourceManager &Sources = *Result.SourceManager;
91 if (ND->getBeginLoc().isMacroID() ||
97 unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
98 unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
99 if (EndLine - StartLine + 1 <= ShortNamespaceLines)
103 SourceLocation AfterRBrace = Lexer::getLocForEndOfToken(
105 SourceLocation
Loc = AfterRBrace;
106 SourceLocation LBraceLoc = ND->getBeginLoc();
111 for (
const auto &EndOfNameLocation : Ends) {
112 if (Sources.isBeforeInTranslationUnit(ND->getLocation(), EndOfNameLocation))
116 llvm::Optional<std::string> NamespaceNameAsWritten =
118 if (!NamespaceNameAsWritten)
121 if (NamespaceNameAsWritten->empty() != ND->isAnonymousNamespace()) {
126 Ends.push_back(LBraceLoc);
130 while (Lexer::getRawToken(Loc, Tok, Sources,
getLangOpts()) ||
132 Loc = Loc.getLocWithOffset(1);
138 bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
141 bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
143 SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
144 std::string
Message =
"%0 not terminated with a closing comment";
147 if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
148 StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
149 SmallVector<StringRef, 7> Groups;
150 if (NamespaceCommentPattern.match(Comment, &Groups)) {
151 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] :
"";
152 StringRef Anonymous = Groups.size() > 3 ? Groups[3] :
"";
154 if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
155 (*NamespaceNameAsWritten == NamespaceNameInComment &&
156 Anonymous.empty())) {
164 NeedLineBreak = Comment.startswith(
"/*");
166 SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
169 "%0 ends with a comment that refers to a wrong namespace '") +
170 NamespaceNameInComment +
"'")
172 }
else if (Comment.startswith(
"//")) {
175 NeedLineBreak =
false;
177 SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
178 Message =
"%0 ends with an unrecognized comment";
184 std::string NamespaceNameForDiag =
185 ND->isAnonymousNamespace() ?
"anonymous namespace" 186 : (
"namespace '" + *NamespaceNameAsWritten +
"'");
188 std::string
Fix(SpacesBeforeComments,
' ');
189 Fix.append(
"// namespace");
190 if (!ND->isAnonymousNamespace())
191 Fix.append(
" ").append(*NamespaceNameAsWritten);
196 SourceLocation DiagLoc =
197 OldCommentRange.getBegin() != OldCommentRange.getEnd()
198 ? OldCommentRange.getBegin()
199 : ND->getRBraceLoc();
201 diag(DiagLoc, Message) << NamespaceNameForDiag
202 << FixItHint::CreateReplacement(
203 CharSourceRange::getCharRange(OldCommentRange),
205 diag(ND->getLocation(),
"%0 starts here", DiagnosticIDs::Note)
206 << NamespaceNameForDiag;
SourceLocation Loc
'#' location in the include directive
Some operations such as code completion produce a set of candidates.
constexpr llvm::StringLiteral Message
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static bool locationsInSameFile(const SourceManager &Sources, SourceLocation Loc1, SourceLocation Loc2)
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
Optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
static llvm::Optional< std::string > getNamespaceNameAsWritten(SourceLocation &Loc, const SourceManager &Sources, const LangOptions &LangOpts)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
static cl::opt< bool > Fix("fix", cl::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))
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.