blob: 6428f8cdc98932693d5dbc5a546f1c372779ffcc [file] [log] [blame]
Alexander Kornienkobef51cd2014-05-19 16:39:08 +00001//===--- NamespaceCommentCheck.cpp - clang-tidy ---------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexander Kornienkobef51cd2014-05-19 16:39:08 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "NamespaceCommentCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include "clang/Lex/Lexer.h"
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000013#include "llvm/ADT/StringExtras.h"
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000014
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
Alexander Kornienko33fc3db2014-09-22 10:41:39 +000019namespace readability {
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000020
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000021NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
22 ClangTidyContext *Context)
23 : ClangTidyCheck(Name, Context),
24 NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
Alexander Kornienko5a65e672018-01-11 13:00:28 +000025 "namespace( +([a-zA-Z0-9_:]+))?\\.? *(\\*/)?$",
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000026 llvm::Regex::IgnoreCase),
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000027 ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)),
Alexander Kornienko1efc4252014-10-16 11:27:57 +000028 SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {}
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000029
30void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
31 Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
32 Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
33}
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000034
35void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman1f1b0672015-09-02 16:05:21 +000036 // Only register the matchers for C++; the functionality currently does not
37 // provide any benefit to other languages, despite being benign.
38 if (getLangOpts().CPlusPlus)
39 Finder->addMatcher(namespaceDecl().bind("namespace"), this);
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000040}
41
Benjamin Kramere7103712015-03-23 12:49:15 +000042static bool locationsInSameFile(const SourceManager &Sources,
43 SourceLocation Loc1, SourceLocation Loc2) {
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000044 return Loc1.isFileID() && Loc2.isFileID() &&
45 Sources.getFileID(Loc1) == Sources.getFileID(Loc2);
46}
47
Benjamin Kramere7103712015-03-23 12:49:15 +000048static std::string getNamespaceComment(const NamespaceDecl *ND,
49 bool InsertLineBreak) {
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000050 std::string Fix = "// namespace";
51 if (!ND->isAnonymousNamespace())
Alexander Kornienko33fc3db2014-09-22 10:41:39 +000052 Fix.append(" ").append(ND->getNameAsString());
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000053 if (InsertLineBreak)
54 Fix.append("\n");
55 return Fix;
56}
57
Alexander Kornienko5a65e672018-01-11 13:00:28 +000058static std::string getNamespaceComment(const std::string &NameSpaceName,
59 bool InsertLineBreak) {
60 std::string Fix = "// namespace ";
61 Fix.append(NameSpaceName);
62 if (InsertLineBreak)
63 Fix.append("\n");
64 return Fix;
65}
66
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000067void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
Piotr Padlewski08124b12016-12-14 15:29:23 +000068 const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace");
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000069 const SourceManager &Sources = *Result.SourceManager;
70
Stephen Kelly43465bf2018-08-09 22:42:26 +000071 if (!locationsInSameFile(Sources, ND->getBeginLoc(), ND->getRBraceLoc()))
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000072 return;
73
74 // Don't require closing comments for namespaces spanning less than certain
75 // number of lines.
Stephen Kelly43465bf2018-08-09 22:42:26 +000076 unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc());
Alexander Kornienkobef51cd2014-05-19 16:39:08 +000077 unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc());
78 if (EndLine - StartLine + 1 <= ShortNamespaceLines)
79 return;
80
81 // Find next token after the namespace closing brace.
82 SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1);
83 SourceLocation Loc = AfterRBrace;
84 Token Tok;
Alexander Kornienko5a65e672018-01-11 13:00:28 +000085 SourceLocation LBracketLocation = ND->getLocation();
86 SourceLocation NestedNamespaceBegin = LBracketLocation;
87
88 // Currently for nested namepsace (n1::n2::...) the AST matcher will match foo
89 // then bar instead of a single match. So if we got a nested namespace we have
90 // to skip the next ones.
91 for (const auto &EndOfNameLocation : Ends) {
92 if (Sources.isBeforeInTranslationUnit(NestedNamespaceBegin,
93 EndOfNameLocation))
94 return;
95 }
96
97 // Ignore macros
98 if (!ND->getLocation().isMacroID()) {
99 while (Lexer::getRawToken(LBracketLocation, Tok, Sources, getLangOpts()) ||
100 !Tok.is(tok::l_brace)) {
101 LBracketLocation = LBracketLocation.getLocWithOffset(1);
102 }
103 }
104
Artem Dergachev8c099ce2019-04-23 21:15:26 +0000105 // FIXME: This probably breaks on comments between the namespace and its '{'.
Alexander Kornienko5a65e672018-01-11 13:00:28 +0000106 auto TextRange =
107 Lexer::getAsCharRange(SourceRange(NestedNamespaceBegin, LBracketLocation),
108 Sources, getLangOpts());
109 StringRef NestedNamespaceName =
Artem Dergachev8c099ce2019-04-23 21:15:26 +0000110 Lexer::getSourceText(TextRange, Sources, getLangOpts())
111 .rtrim('{') // Drop the { itself.
112 .rtrim(); // Drop any whitespace before it.
Alexander Kornienko5a65e672018-01-11 13:00:28 +0000113 bool IsNested = NestedNamespaceName.contains(':');
114
115 if (IsNested)
116 Ends.push_back(LBracketLocation);
117 else
118 NestedNamespaceName = ND->getName();
119
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000120 // Skip whitespace until we find the next token.
Gabor Horvathafad84c2016-09-24 02:13:45 +0000121 while (Lexer::getRawToken(Loc, Tok, Sources, getLangOpts()) ||
Alexander Kornienko2a538302015-12-16 15:44:42 +0000122 Tok.is(tok::semi)) {
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000123 Loc = Loc.getLocWithOffset(1);
124 }
Alexander Kornienko5a65e672018-01-11 13:00:28 +0000125
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000126 if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc))
127 return;
128
129 bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine;
Alexander Kornienko16ff5c72014-05-19 17:46:28 +0000130 // If we insert a line comment before the token in the same line, we need
131 // to insert a line break.
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000132 bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
133
Alexander Kornienkoda4ebb22015-03-05 14:56:11 +0000134 SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
Daniel Jasper08201e32015-03-05 23:17:32 +0000135 std::string Message = "%0 not terminated with a closing comment";
Alexander Kornienkoda4ebb22015-03-05 14:56:11 +0000136
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000137 // Try to find existing namespace closing comment on the same line.
138 if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
139 StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
Alexander Kornienko27f126b2014-10-16 15:11:54 +0000140 SmallVector<StringRef, 7> Groups;
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000141 if (NamespaceCommentPattern.match(Comment, &Groups)) {
Alexander Kornienko27f126b2014-10-16 15:11:54 +0000142 StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
143 StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000144
Alexander Kornienko5a65e672018-01-11 13:00:28 +0000145 if (IsNested && NestedNamespaceName == NamespaceNameInComment) {
146 // C++17 nested namespace.
147 return;
148 } else if ((ND->isAnonymousNamespace() &&
149 NamespaceNameInComment.empty()) ||
150 (ND->getNameAsString() == NamespaceNameInComment &&
151 Anonymous.empty())) {
152 // Check if the namespace in the comment is the same.
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000153 // FIXME: Maybe we need a strict mode, where we always fix namespace
154 // comments with different format.
155 return;
156 }
157
158 // Otherwise we need to fix the comment.
159 NeedLineBreak = Comment.startswith("/*");
Alexander Kornienkoda4ebb22015-03-05 14:56:11 +0000160 OldCommentRange =
161 SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
162 Message =
163 (llvm::Twine(
164 "%0 ends with a comment that refers to a wrong namespace '") +
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +0000165 NamespaceNameInComment + "'")
166 .str();
Alexander Kornienkoda4ebb22015-03-05 14:56:11 +0000167 } else if (Comment.startswith("//")) {
168 // Assume that this is an unrecognized form of a namespace closing line
169 // comment. Replace it.
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000170 NeedLineBreak = false;
Alexander Kornienkoda4ebb22015-03-05 14:56:11 +0000171 OldCommentRange =
172 SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
173 Message = "%0 ends with an unrecognized comment";
174 }
175 // If it's a block comment, just move it to the next line, as it can be
176 // multi-line or there may be other tokens behind it.
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000177 }
178
Alexander Kornienkob23eb5e2014-11-17 17:32:32 +0000179 std::string NamespaceName =
180 ND->isAnonymousNamespace()
181 ? "anonymous namespace"
Alexander Kornienko5a65e672018-01-11 13:00:28 +0000182 : ("namespace '" + NestedNamespaceName.str() + "'");
Alexander Kornienkob23eb5e2014-11-17 17:32:32 +0000183
Alexander Kornienkoda4ebb22015-03-05 14:56:11 +0000184 diag(AfterRBrace, Message)
Alexander Kornienko5a65e672018-01-11 13:00:28 +0000185 << NamespaceName
186 << FixItHint::CreateReplacement(
187 CharSourceRange::getCharRange(OldCommentRange),
188 std::string(SpacesBeforeComments, ' ') +
189 (IsNested
190 ? getNamespaceComment(NestedNamespaceName, NeedLineBreak)
191 : getNamespaceComment(ND, NeedLineBreak)));
Alexander Kornienkob23eb5e2014-11-17 17:32:32 +0000192 diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note)
193 << NamespaceName;
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000194}
195
Alexander Kornienko33fc3db2014-09-22 10:41:39 +0000196} // namespace readability
Alexander Kornienkobef51cd2014-05-19 16:39:08 +0000197} // namespace tidy
198} // namespace clang