Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 1 | //===--- NamespaceCommentCheck.cpp - clang-tidy ---------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "NamespaceCommentCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchers.h" |
Ilya Biryukov | f81ee43 | 2019-07-17 15:22:14 +0000 | [diff] [blame] | 12 | #include "clang/Basic/SourceLocation.h" |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace clang::ast_matchers; |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 20 | namespace readability { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 21 | |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 22 | NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name, |
| 23 | ClangTidyContext *Context) |
| 24 | : ClangTidyCheck(Name, Context), |
| 25 | NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 26 | "namespace( +([a-zA-Z0-9_:]+))?\\.? *(\\*/)?$", |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 27 | llvm::Regex::IgnoreCase), |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 28 | ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), |
Alexander Kornienko | 1efc425 | 2014-10-16 11:27:57 +0000 | [diff] [blame] | 29 | SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 30 | |
| 31 | void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 32 | Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines); |
| 33 | Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments); |
| 34 | } |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 35 | |
| 36 | void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame] | 37 | // Only register the matchers for C++; the functionality currently does not |
| 38 | // provide any benefit to other languages, despite being benign. |
| 39 | if (getLangOpts().CPlusPlus) |
| 40 | Finder->addMatcher(namespaceDecl().bind("namespace"), this); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 43 | static bool locationsInSameFile(const SourceManager &Sources, |
| 44 | SourceLocation Loc1, SourceLocation Loc2) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 45 | return Loc1.isFileID() && Loc2.isFileID() && |
| 46 | Sources.getFileID(Loc1) == Sources.getFileID(Loc2); |
| 47 | } |
| 48 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 49 | static std::string getNamespaceComment(const NamespaceDecl *ND, |
| 50 | bool InsertLineBreak) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 51 | std::string Fix = "// namespace"; |
| 52 | if (!ND->isAnonymousNamespace()) |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 53 | Fix.append(" ").append(ND->getNameAsString()); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 54 | if (InsertLineBreak) |
| 55 | Fix.append("\n"); |
| 56 | return Fix; |
| 57 | } |
| 58 | |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 59 | static std::string getNamespaceComment(const std::string &NameSpaceName, |
| 60 | bool InsertLineBreak) { |
| 61 | std::string Fix = "// namespace "; |
| 62 | Fix.append(NameSpaceName); |
| 63 | if (InsertLineBreak) |
| 64 | Fix.append("\n"); |
| 65 | return Fix; |
| 66 | } |
| 67 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 68 | void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 69 | const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace"); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 70 | const SourceManager &Sources = *Result.SourceManager; |
| 71 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 72 | if (!locationsInSameFile(Sources, ND->getBeginLoc(), ND->getRBraceLoc())) |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 73 | return; |
| 74 | |
| 75 | // Don't require closing comments for namespaces spanning less than certain |
| 76 | // number of lines. |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 77 | unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc()); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 78 | unsigned EndLine = Sources.getSpellingLineNumber(ND->getRBraceLoc()); |
| 79 | if (EndLine - StartLine + 1 <= ShortNamespaceLines) |
| 80 | return; |
| 81 | |
| 82 | // Find next token after the namespace closing brace. |
| 83 | SourceLocation AfterRBrace = ND->getRBraceLoc().getLocWithOffset(1); |
| 84 | SourceLocation Loc = AfterRBrace; |
| 85 | Token Tok; |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 86 | SourceLocation LBracketLocation = ND->getLocation(); |
| 87 | SourceLocation NestedNamespaceBegin = LBracketLocation; |
| 88 | |
| 89 | // Currently for nested namepsace (n1::n2::...) the AST matcher will match foo |
| 90 | // then bar instead of a single match. So if we got a nested namespace we have |
| 91 | // to skip the next ones. |
| 92 | for (const auto &EndOfNameLocation : Ends) { |
| 93 | if (Sources.isBeforeInTranslationUnit(NestedNamespaceBegin, |
| 94 | EndOfNameLocation)) |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // Ignore macros |
| 99 | if (!ND->getLocation().isMacroID()) { |
| 100 | while (Lexer::getRawToken(LBracketLocation, Tok, Sources, getLangOpts()) || |
| 101 | !Tok.is(tok::l_brace)) { |
| 102 | LBracketLocation = LBracketLocation.getLocWithOffset(1); |
| 103 | } |
| 104 | } |
| 105 | |
Artem Dergachev | 8c099ce | 2019-04-23 21:15:26 +0000 | [diff] [blame] | 106 | // FIXME: This probably breaks on comments between the namespace and its '{'. |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 107 | auto TextRange = |
| 108 | Lexer::getAsCharRange(SourceRange(NestedNamespaceBegin, LBracketLocation), |
| 109 | Sources, getLangOpts()); |
| 110 | StringRef NestedNamespaceName = |
Artem Dergachev | 8c099ce | 2019-04-23 21:15:26 +0000 | [diff] [blame] | 111 | Lexer::getSourceText(TextRange, Sources, getLangOpts()) |
| 112 | .rtrim('{') // Drop the { itself. |
| 113 | .rtrim(); // Drop any whitespace before it. |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 114 | bool IsNested = NestedNamespaceName.contains(':'); |
| 115 | |
| 116 | if (IsNested) |
| 117 | Ends.push_back(LBracketLocation); |
| 118 | else |
| 119 | NestedNamespaceName = ND->getName(); |
| 120 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 121 | // Skip whitespace until we find the next token. |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 122 | while (Lexer::getRawToken(Loc, Tok, Sources, getLangOpts()) || |
Alexander Kornienko | 2a53830 | 2015-12-16 15:44:42 +0000 | [diff] [blame] | 123 | Tok.is(tok::semi)) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 124 | Loc = Loc.getLocWithOffset(1); |
| 125 | } |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 126 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 127 | if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc)) |
| 128 | return; |
| 129 | |
| 130 | bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine; |
Alexander Kornienko | 16ff5c7 | 2014-05-19 17:46:28 +0000 | [diff] [blame] | 131 | // If we insert a line comment before the token in the same line, we need |
| 132 | // to insert a line break. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 133 | bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof); |
| 134 | |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 135 | SourceRange OldCommentRange(AfterRBrace, AfterRBrace); |
Daniel Jasper | 08201e3 | 2015-03-05 23:17:32 +0000 | [diff] [blame] | 136 | std::string Message = "%0 not terminated with a closing comment"; |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 137 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 138 | // Try to find existing namespace closing comment on the same line. |
| 139 | if (Tok.is(tok::comment) && NextTokenIsOnSameLine) { |
| 140 | StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength()); |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 141 | SmallVector<StringRef, 7> Groups; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 142 | if (NamespaceCommentPattern.match(Comment, &Groups)) { |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 143 | StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : ""; |
| 144 | StringRef Anonymous = Groups.size() > 3 ? Groups[3] : ""; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 145 | |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 146 | if (IsNested && NestedNamespaceName == NamespaceNameInComment) { |
| 147 | // C++17 nested namespace. |
| 148 | return; |
| 149 | } else if ((ND->isAnonymousNamespace() && |
| 150 | NamespaceNameInComment.empty()) || |
| 151 | (ND->getNameAsString() == NamespaceNameInComment && |
| 152 | Anonymous.empty())) { |
| 153 | // Check if the namespace in the comment is the same. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 154 | // FIXME: Maybe we need a strict mode, where we always fix namespace |
| 155 | // comments with different format. |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Otherwise we need to fix the comment. |
| 160 | NeedLineBreak = Comment.startswith("/*"); |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 161 | OldCommentRange = |
| 162 | SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength())); |
| 163 | Message = |
| 164 | (llvm::Twine( |
| 165 | "%0 ends with a comment that refers to a wrong namespace '") + |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 166 | NamespaceNameInComment + "'") |
| 167 | .str(); |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 168 | } else if (Comment.startswith("//")) { |
| 169 | // Assume that this is an unrecognized form of a namespace closing line |
| 170 | // comment. Replace it. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 171 | NeedLineBreak = false; |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 172 | OldCommentRange = |
| 173 | SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength())); |
| 174 | Message = "%0 ends with an unrecognized comment"; |
| 175 | } |
| 176 | // If it's a block comment, just move it to the next line, as it can be |
| 177 | // multi-line or there may be other tokens behind it. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 180 | std::string NamespaceName = |
| 181 | ND->isAnonymousNamespace() |
| 182 | ? "anonymous namespace" |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 183 | : ("namespace '" + NestedNamespaceName.str() + "'"); |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 184 | |
Ilya Biryukov | f81ee43 | 2019-07-17 15:22:14 +0000 | [diff] [blame] | 185 | // Place diagnostic at an old comment, or closing brace if we did not have it. |
| 186 | SourceLocation DiagLoc = |
| 187 | OldCommentRange.getBegin() != OldCommentRange.getEnd() |
| 188 | ? OldCommentRange.getBegin() |
| 189 | : ND->getRBraceLoc(); |
| 190 | |
| 191 | diag(DiagLoc, Message) |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 192 | << NamespaceName |
| 193 | << FixItHint::CreateReplacement( |
| 194 | CharSourceRange::getCharRange(OldCommentRange), |
| 195 | std::string(SpacesBeforeComments, ' ') + |
| 196 | (IsNested |
| 197 | ? getNamespaceComment(NestedNamespaceName, NeedLineBreak) |
| 198 | : getNamespaceComment(ND, NeedLineBreak))); |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 199 | diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note) |
| 200 | << NamespaceName; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 203 | } // namespace readability |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 204 | } // namespace tidy |
| 205 | } // namespace clang |