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" |
| 12 | #include "clang/Lex/Lexer.h" |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringExtras.h" |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 19 | namespace readability { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 20 | |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 21 | NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name, |
| 22 | ClangTidyContext *Context) |
| 23 | : ClangTidyCheck(Name, Context), |
| 24 | NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *" |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 25 | "namespace( +([a-zA-Z0-9_:]+))?\\.? *(\\*/)?$", |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 26 | llvm::Regex::IgnoreCase), |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 27 | ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)), |
Alexander Kornienko | 1efc425 | 2014-10-16 11:27:57 +0000 | [diff] [blame] | 28 | SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {} |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 29 | |
| 30 | void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 31 | Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines); |
| 32 | Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments); |
| 33 | } |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 34 | |
| 35 | void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 1f1b067 | 2015-09-02 16:05:21 +0000 | [diff] [blame] | 36 | // 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 Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 42 | static bool locationsInSameFile(const SourceManager &Sources, |
| 43 | SourceLocation Loc1, SourceLocation Loc2) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 44 | return Loc1.isFileID() && Loc2.isFileID() && |
| 45 | Sources.getFileID(Loc1) == Sources.getFileID(Loc2); |
| 46 | } |
| 47 | |
Benjamin Kramer | e710371 | 2015-03-23 12:49:15 +0000 | [diff] [blame] | 48 | static std::string getNamespaceComment(const NamespaceDecl *ND, |
| 49 | bool InsertLineBreak) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 50 | std::string Fix = "// namespace"; |
| 51 | if (!ND->isAnonymousNamespace()) |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 52 | Fix.append(" ").append(ND->getNameAsString()); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 53 | if (InsertLineBreak) |
| 54 | Fix.append("\n"); |
| 55 | return Fix; |
| 56 | } |
| 57 | |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 58 | static 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 Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 67 | void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 68 | const auto *ND = Result.Nodes.getNodeAs<NamespaceDecl>("namespace"); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 69 | const SourceManager &Sources = *Result.SourceManager; |
| 70 | |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 71 | if (!locationsInSameFile(Sources, ND->getBeginLoc(), ND->getRBraceLoc())) |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 72 | return; |
| 73 | |
| 74 | // Don't require closing comments for namespaces spanning less than certain |
| 75 | // number of lines. |
Stephen Kelly | 43465bf | 2018-08-09 22:42:26 +0000 | [diff] [blame] | 76 | unsigned StartLine = Sources.getSpellingLineNumber(ND->getBeginLoc()); |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 77 | 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 Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 85 | 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 Dergachev | 8c099ce | 2019-04-23 21:15:26 +0000 | [diff] [blame^] | 105 | // FIXME: This probably breaks on comments between the namespace and its '{'. |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 106 | auto TextRange = |
| 107 | Lexer::getAsCharRange(SourceRange(NestedNamespaceBegin, LBracketLocation), |
| 108 | Sources, getLangOpts()); |
| 109 | StringRef NestedNamespaceName = |
Artem Dergachev | 8c099ce | 2019-04-23 21:15:26 +0000 | [diff] [blame^] | 110 | Lexer::getSourceText(TextRange, Sources, getLangOpts()) |
| 111 | .rtrim('{') // Drop the { itself. |
| 112 | .rtrim(); // Drop any whitespace before it. |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 113 | bool IsNested = NestedNamespaceName.contains(':'); |
| 114 | |
| 115 | if (IsNested) |
| 116 | Ends.push_back(LBracketLocation); |
| 117 | else |
| 118 | NestedNamespaceName = ND->getName(); |
| 119 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 120 | // Skip whitespace until we find the next token. |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 121 | while (Lexer::getRawToken(Loc, Tok, Sources, getLangOpts()) || |
Alexander Kornienko | 2a53830 | 2015-12-16 15:44:42 +0000 | [diff] [blame] | 122 | Tok.is(tok::semi)) { |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 123 | Loc = Loc.getLocWithOffset(1); |
| 124 | } |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 125 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 126 | if (!locationsInSameFile(Sources, ND->getRBraceLoc(), Loc)) |
| 127 | return; |
| 128 | |
| 129 | bool NextTokenIsOnSameLine = Sources.getSpellingLineNumber(Loc) == EndLine; |
Alexander Kornienko | 16ff5c7 | 2014-05-19 17:46:28 +0000 | [diff] [blame] | 130 | // If we insert a line comment before the token in the same line, we need |
| 131 | // to insert a line break. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 132 | bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof); |
| 133 | |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 134 | SourceRange OldCommentRange(AfterRBrace, AfterRBrace); |
Daniel Jasper | 08201e3 | 2015-03-05 23:17:32 +0000 | [diff] [blame] | 135 | std::string Message = "%0 not terminated with a closing comment"; |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 136 | |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 137 | // 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 Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 140 | SmallVector<StringRef, 7> Groups; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 141 | if (NamespaceCommentPattern.match(Comment, &Groups)) { |
Alexander Kornienko | 27f126b | 2014-10-16 15:11:54 +0000 | [diff] [blame] | 142 | StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : ""; |
| 143 | StringRef Anonymous = Groups.size() > 3 ? Groups[3] : ""; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 144 | |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 145 | 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 Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 153 | // 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 Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 160 | 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 Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 165 | NamespaceNameInComment + "'") |
| 166 | .str(); |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 167 | } else if (Comment.startswith("//")) { |
| 168 | // Assume that this is an unrecognized form of a namespace closing line |
| 169 | // comment. Replace it. |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 170 | NeedLineBreak = false; |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 171 | 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 Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 179 | std::string NamespaceName = |
| 180 | ND->isAnonymousNamespace() |
| 181 | ? "anonymous namespace" |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 182 | : ("namespace '" + NestedNamespaceName.str() + "'"); |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 183 | |
Alexander Kornienko | da4ebb2 | 2015-03-05 14:56:11 +0000 | [diff] [blame] | 184 | diag(AfterRBrace, Message) |
Alexander Kornienko | 5a65e67 | 2018-01-11 13:00:28 +0000 | [diff] [blame] | 185 | << NamespaceName |
| 186 | << FixItHint::CreateReplacement( |
| 187 | CharSourceRange::getCharRange(OldCommentRange), |
| 188 | std::string(SpacesBeforeComments, ' ') + |
| 189 | (IsNested |
| 190 | ? getNamespaceComment(NestedNamespaceName, NeedLineBreak) |
| 191 | : getNamespaceComment(ND, NeedLineBreak))); |
Alexander Kornienko | b23eb5e | 2014-11-17 17:32:32 +0000 | [diff] [blame] | 192 | diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note) |
| 193 | << NamespaceName; |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Alexander Kornienko | 33fc3db | 2014-09-22 10:41:39 +0000 | [diff] [blame] | 196 | } // namespace readability |
Alexander Kornienko | bef51cd | 2014-05-19 16:39:08 +0000 | [diff] [blame] | 197 | } // namespace tidy |
| 198 | } // namespace clang |